Skip to content

Commit

Permalink
Merge pull request #138 from Snuffy2/Add-Device-Selector
Browse files Browse the repository at this point in the history
Add Device Selector
  • Loading branch information
enkama authored Nov 4, 2024
2 parents 7c76e42 + a19524b commit 9ccf917
Show file tree
Hide file tree
Showing 11 changed files with 553 additions and 34 deletions.
5 changes: 4 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,8 @@
"editor.formatOnPaste": true,
"editor.formatOnSave": true,
"files.trimTrailingWhitespace": true,
"git.ignoreLimitWarning": true
"git.ignoreLimitWarning": true,
"files.associations": {
"*.yaml": "home-assistant"
}
}
1 change: 0 additions & 1 deletion custom_components/__init__.py

This file was deleted.

20 changes: 18 additions & 2 deletions custom_components/variable/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@
import json
import logging

import voluptuous as vol
from homeassistant.config_entries import SOURCE_IMPORT, ConfigEntry
from homeassistant.const import (
CONF_DEVICE,
CONF_DEVICE_ID,
CONF_ENTITY_ID,
CONF_FRIENDLY_NAME,
CONF_ICON,
Expand All @@ -17,9 +20,11 @@
from homeassistant.core import HomeAssistant, ServiceCall
from homeassistant.exceptions import HomeAssistantError
from homeassistant.helpers import config_validation as cv
from homeassistant.helpers.device import (
async_remove_stale_devices_links_keep_current_device,
)
from homeassistant.helpers.reload import async_integration_yaml_config
from homeassistant.helpers.typing import ConfigType
import voluptuous as vol

from .const import (
ATTR_ATTRIBUTES,
Expand All @@ -40,6 +45,7 @@
PLATFORMS,
SERVICE_UPDATE_SENSOR,
)
from .device import create_device, remove_device

_LOGGER = logging.getLogger(__name__)

Expand Down Expand Up @@ -116,6 +122,7 @@ async def _async_reload_service_handler(service: ServiceCall) -> None:
return
_LOGGER.debug(f" reload_config: {reload_config}")
await _async_process_yaml(hass, reload_config)

hass.services.async_register(
DOMAIN,
SERVICE_SET_VARIABLE_LEGACY,
Expand Down Expand Up @@ -219,27 +226,36 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
yaml_data = copy.deepcopy(dict(entry.data))
yaml_data.pop(CONF_YAML_PRESENT, None)
hass.config_entries.async_update_entry(entry, data=yaml_data, options={})

async_remove_stale_devices_links_keep_current_device(
hass,
entry.entry_id,
entry.data.get(CONF_DEVICE_ID),
)
hass.data.setdefault(DOMAIN, {})
hass_data = dict(entry.data)
hass.data[DOMAIN][entry.entry_id] = hass_data
if hass_data.get(CONF_ENTITY_PLATFORM) in PLATFORMS:
await hass.config_entries.async_forward_entry_setups(
entry, [hass_data.get(CONF_ENTITY_PLATFORM)]
)
elif hass_data.get(CONF_ENTITY_PLATFORM) == CONF_DEVICE:
await create_device(hass, entry)
return True


async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""Unload a config entry."""

_LOGGER.info(f"Unloading: {entry.data}")
# _LOGGER.debug(f"[init async_unload_entry] entry: {entry}")
hass_data = dict(entry.data)
unload_ok = False
if hass_data.get(CONF_ENTITY_PLATFORM) in PLATFORMS:
unload_ok = await hass.config_entries.async_unload_platforms(
entry, [hass_data.get(CONF_ENTITY_PLATFORM)]
)
elif hass_data.get(CONF_ENTITY_PLATFORM) == CONF_DEVICE:
unload_ok = await remove_device(hass, entry)
if unload_ok:
hass.data[DOMAIN].pop(entry.entry_id)

Expand Down
15 changes: 11 additions & 4 deletions custom_components/variable/binary_sensor.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
from collections.abc import MutableMapping
import copy
import logging
from collections.abc import MutableMapping

import homeassistant.helpers.entity_registry as er
import voluptuous as vol
from homeassistant.components.binary_sensor import PLATFORM_SCHEMA, BinarySensorEntity
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import (
ATTR_FRIENDLY_NAME,
ATTR_ICON,
CONF_DEVICE_CLASS,
CONF_DEVICE_ID,
CONF_ICON,
CONF_NAME,
MATCH_ALL,
Expand All @@ -16,12 +19,12 @@
Platform,
)
from homeassistant.core import HomeAssistant
from homeassistant.helpers import config_validation as cv, entity_platform, selector
from homeassistant.helpers import config_validation as cv
from homeassistant.helpers import entity_platform, selector
from homeassistant.helpers.device import async_device_info_to_link_from_device_id
from homeassistant.helpers.entity import generate_entity_id
import homeassistant.helpers.entity_registry as er
from homeassistant.helpers.restore_state import RestoreEntity
from homeassistant.util import slugify
import voluptuous as vol

from .const import (
ATTR_ATTRIBUTES,
Expand Down Expand Up @@ -152,6 +155,10 @@ def __init__(
self._force_update = config.get(CONF_FORCE_UPDATE)
self._yaml_variable = config.get(CONF_YAML_VARIABLE)
self._exclude_from_recorder = config.get(CONF_EXCLUDE_FROM_RECORDER)
self._attr_device_info = async_device_info_to_link_from_device_id(
hass,
config.get(CONF_DEVICE_ID),
)
if (
config.get(CONF_ATTRIBUTES) is not None
and config.get(CONF_ATTRIBUTES)
Expand Down
Loading

0 comments on commit 9ccf917

Please sign in to comment.