2018-01-30 09:39:39 +00:00
|
|
|
"""Provide a registry to track entity IDs.
|
|
|
|
|
|
|
|
The Entity Registry keeps a registry of entities. Entities are uniquely
|
|
|
|
identified by their domain, platform and a unique id provided by that platform.
|
|
|
|
|
|
|
|
The Entity Registry will persist itself 10 seconds after a new entity is
|
|
|
|
registered. Registering a new entity while a timer is in progress resets the
|
|
|
|
timer.
|
|
|
|
"""
|
2018-02-11 17:16:01 +00:00
|
|
|
from collections import OrderedDict
|
2018-01-30 09:39:39 +00:00
|
|
|
import logging
|
2020-04-22 00:43:49 +00:00
|
|
|
from typing import (
|
|
|
|
TYPE_CHECKING,
|
|
|
|
Any,
|
|
|
|
Callable,
|
|
|
|
Dict,
|
|
|
|
Iterable,
|
|
|
|
List,
|
|
|
|
Optional,
|
|
|
|
Tuple,
|
2021-01-05 01:03:16 +00:00
|
|
|
Union,
|
2021-02-11 16:36:19 +00:00
|
|
|
cast,
|
2020-04-22 00:43:49 +00:00
|
|
|
)
|
2018-01-30 09:39:39 +00:00
|
|
|
|
2018-02-11 17:16:01 +00:00
|
|
|
import attr
|
|
|
|
|
2019-12-31 13:29:43 +00:00
|
|
|
from homeassistant.const import (
|
|
|
|
ATTR_DEVICE_CLASS,
|
2020-02-11 17:40:50 +00:00
|
|
|
ATTR_FRIENDLY_NAME,
|
|
|
|
ATTR_ICON,
|
2020-10-21 15:01:51 +00:00
|
|
|
ATTR_RESTORED,
|
2019-12-31 13:29:43 +00:00
|
|
|
ATTR_SUPPORTED_FEATURES,
|
2020-01-15 16:09:05 +00:00
|
|
|
ATTR_UNIT_OF_MEASUREMENT,
|
2019-12-31 13:29:43 +00:00
|
|
|
EVENT_HOMEASSISTANT_START,
|
|
|
|
STATE_UNAVAILABLE,
|
|
|
|
)
|
2019-10-28 20:36:26 +00:00
|
|
|
from homeassistant.core import Event, callback, split_entity_id, valid_entity_id
|
2021-02-12 16:00:35 +00:00
|
|
|
from homeassistant.helpers import device_registry as dr
|
2019-06-24 18:26:45 +00:00
|
|
|
from homeassistant.helpers.device_registry import EVENT_DEVICE_REGISTRY_UPDATED
|
2021-02-11 16:36:19 +00:00
|
|
|
from homeassistant.loader import bind_hass
|
2020-04-19 23:19:11 +00:00
|
|
|
from homeassistant.util import slugify
|
2018-08-18 11:34:33 +00:00
|
|
|
from homeassistant.util.yaml import load_yaml
|
2018-01-30 09:39:39 +00:00
|
|
|
|
2021-01-05 01:03:16 +00:00
|
|
|
from .typing import UNDEFINED, HomeAssistantType, UndefinedType
|
2019-03-27 14:06:20 +00:00
|
|
|
|
2019-12-22 18:51:39 +00:00
|
|
|
if TYPE_CHECKING:
|
|
|
|
from homeassistant.config_entries import ConfigEntry # noqa: F401
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
PATH_REGISTRY = "entity_registry.yaml"
|
|
|
|
DATA_REGISTRY = "entity_registry"
|
|
|
|
EVENT_ENTITY_REGISTRY_UPDATED = "entity_registry_updated"
|
2018-01-30 09:39:39 +00:00
|
|
|
SAVE_DELAY = 10
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
2019-08-18 04:34:11 +00:00
|
|
|
DISABLED_CONFIG_ENTRY = "config_entry"
|
2020-11-26 15:45:02 +00:00
|
|
|
DISABLED_DEVICE = "device"
|
2019-07-31 19:25:30 +00:00
|
|
|
DISABLED_HASS = "hass"
|
2019-08-16 23:17:16 +00:00
|
|
|
DISABLED_INTEGRATION = "integration"
|
2020-11-26 15:45:02 +00:00
|
|
|
DISABLED_USER = "user"
|
2018-02-13 12:33:15 +00:00
|
|
|
|
2018-08-18 11:34:33 +00:00
|
|
|
STORAGE_VERSION = 1
|
2019-07-31 19:25:30 +00:00
|
|
|
STORAGE_KEY = "core.entity_registry"
|
2018-08-18 11:34:33 +00:00
|
|
|
|
2020-04-24 08:49:11 +00:00
|
|
|
# Attributes relevant to describing entity
|
|
|
|
# to external services.
|
|
|
|
ENTITY_DESCRIBING_ATTRIBUTES = {
|
|
|
|
"entity_id",
|
|
|
|
"name",
|
|
|
|
"original_name",
|
|
|
|
"capabilities",
|
|
|
|
"supported_features",
|
|
|
|
"device_class",
|
|
|
|
"unit_of_measurement",
|
|
|
|
}
|
|
|
|
|
2018-01-30 09:39:39 +00:00
|
|
|
|
2018-09-17 11:39:30 +00:00
|
|
|
@attr.s(slots=True, frozen=True)
|
2018-02-11 17:16:01 +00:00
|
|
|
class RegistryEntry:
|
|
|
|
"""Entity Registry Entry."""
|
|
|
|
|
2020-07-14 17:30:30 +00:00
|
|
|
entity_id: str = attr.ib()
|
|
|
|
unique_id: str = attr.ib()
|
|
|
|
platform: str = attr.ib()
|
|
|
|
name: Optional[str] = attr.ib(default=None)
|
|
|
|
icon: Optional[str] = attr.ib(default=None)
|
2019-12-22 18:51:39 +00:00
|
|
|
device_id: Optional[str] = attr.ib(default=None)
|
2020-10-24 19:25:28 +00:00
|
|
|
area_id: Optional[str] = attr.ib(default=None)
|
2019-10-28 20:36:26 +00:00
|
|
|
config_entry_id: Optional[str] = attr.ib(default=None)
|
2020-07-14 17:30:30 +00:00
|
|
|
disabled_by: Optional[str] = attr.ib(
|
2019-07-31 19:25:30 +00:00
|
|
|
default=None,
|
2019-08-16 23:17:16 +00:00
|
|
|
validator=attr.validators.in_(
|
2019-08-18 04:34:11 +00:00
|
|
|
(
|
2020-11-26 15:45:02 +00:00
|
|
|
DISABLED_CONFIG_ENTRY,
|
|
|
|
DISABLED_DEVICE,
|
2019-08-18 04:34:11 +00:00
|
|
|
DISABLED_HASS,
|
|
|
|
DISABLED_INTEGRATION,
|
2020-11-26 15:45:02 +00:00
|
|
|
DISABLED_USER,
|
2019-08-18 04:34:11 +00:00
|
|
|
None,
|
|
|
|
)
|
2019-08-16 23:17:16 +00:00
|
|
|
),
|
2019-09-07 06:48:58 +00:00
|
|
|
)
|
2019-12-31 13:29:43 +00:00
|
|
|
capabilities: Optional[Dict[str, Any]] = attr.ib(default=None)
|
|
|
|
supported_features: int = attr.ib(default=0)
|
|
|
|
device_class: Optional[str] = attr.ib(default=None)
|
2020-01-15 16:09:05 +00:00
|
|
|
unit_of_measurement: Optional[str] = attr.ib(default=None)
|
2020-02-11 17:40:50 +00:00
|
|
|
# As set by integration
|
|
|
|
original_name: Optional[str] = attr.ib(default=None)
|
|
|
|
original_icon: Optional[str] = attr.ib(default=None)
|
2020-07-14 17:30:30 +00:00
|
|
|
domain: str = attr.ib(init=False, repr=False)
|
2018-02-11 17:16:01 +00:00
|
|
|
|
2018-02-24 18:53:59 +00:00
|
|
|
@domain.default
|
2019-10-28 20:36:26 +00:00
|
|
|
def _domain_default(self) -> str:
|
2018-02-24 18:53:59 +00:00
|
|
|
"""Compute domain value."""
|
|
|
|
return split_entity_id(self.entity_id)[0]
|
2018-02-11 17:16:01 +00:00
|
|
|
|
2018-02-13 12:33:15 +00:00
|
|
|
@property
|
2019-10-28 20:36:26 +00:00
|
|
|
def disabled(self) -> bool:
|
2018-02-13 12:33:15 +00:00
|
|
|
"""Return if entry is disabled."""
|
|
|
|
return self.disabled_by is not None
|
|
|
|
|
2021-02-08 09:45:46 +00:00
|
|
|
@callback
|
|
|
|
def write_unavailable_state(self, hass: HomeAssistantType) -> None:
|
|
|
|
"""Write the unavailable state to the state machine."""
|
|
|
|
attrs: Dict[str, Any] = {ATTR_RESTORED: True}
|
|
|
|
|
|
|
|
if self.capabilities is not None:
|
|
|
|
attrs.update(self.capabilities)
|
|
|
|
|
|
|
|
if self.supported_features is not None:
|
|
|
|
attrs[ATTR_SUPPORTED_FEATURES] = self.supported_features
|
|
|
|
|
|
|
|
if self.device_class is not None:
|
|
|
|
attrs[ATTR_DEVICE_CLASS] = self.device_class
|
|
|
|
|
|
|
|
if self.unit_of_measurement is not None:
|
|
|
|
attrs[ATTR_UNIT_OF_MEASUREMENT] = self.unit_of_measurement
|
|
|
|
|
|
|
|
name = self.name or self.original_name
|
|
|
|
if name is not None:
|
|
|
|
attrs[ATTR_FRIENDLY_NAME] = name
|
|
|
|
|
|
|
|
icon = self.icon or self.original_icon
|
|
|
|
if icon is not None:
|
|
|
|
attrs[ATTR_ICON] = icon
|
|
|
|
|
|
|
|
hass.states.async_set(self.entity_id, STATE_UNAVAILABLE, attrs)
|
|
|
|
|
2018-02-11 17:16:01 +00:00
|
|
|
|
2018-01-30 09:39:39 +00:00
|
|
|
class EntityRegistry:
|
|
|
|
"""Class to hold a registry of entities."""
|
|
|
|
|
2019-10-28 20:36:26 +00:00
|
|
|
def __init__(self, hass: HomeAssistantType):
|
2018-01-30 09:39:39 +00:00
|
|
|
"""Initialize the registry."""
|
|
|
|
self.hass = hass
|
2019-10-28 20:36:26 +00:00
|
|
|
self.entities: Dict[str, RegistryEntry]
|
2020-07-20 05:52:41 +00:00
|
|
|
self._index: Dict[Tuple[str, str, str], str] = {}
|
2018-08-18 11:34:33 +00:00
|
|
|
self._store = hass.helpers.storage.Store(STORAGE_VERSION, STORAGE_KEY)
|
2019-06-24 18:26:45 +00:00
|
|
|
self.hass.bus.async_listen(
|
2020-11-26 15:45:02 +00:00
|
|
|
EVENT_DEVICE_REGISTRY_UPDATED, self.async_device_modified
|
2019-06-24 18:26:45 +00:00
|
|
|
)
|
2018-01-30 09:39:39 +00:00
|
|
|
|
2020-04-22 00:43:49 +00:00
|
|
|
@callback
|
|
|
|
def async_get_device_class_lookup(self, domain_device_classes: set) -> dict:
|
|
|
|
"""Return a lookup for the device class by domain."""
|
|
|
|
lookup: Dict[str, Dict[Tuple[Any, Any], str]] = {}
|
|
|
|
for entity in self.entities.values():
|
|
|
|
if not entity.device_id:
|
|
|
|
continue
|
|
|
|
domain_device_class = (entity.domain, entity.device_class)
|
|
|
|
if domain_device_class not in domain_device_classes:
|
|
|
|
continue
|
|
|
|
if entity.device_id not in lookup:
|
|
|
|
lookup[entity.device_id] = {domain_device_class: entity.entity_id}
|
|
|
|
else:
|
|
|
|
lookup[entity.device_id][domain_device_class] = entity.entity_id
|
|
|
|
return lookup
|
|
|
|
|
2018-01-30 09:39:39 +00:00
|
|
|
@callback
|
2019-10-28 20:36:26 +00:00
|
|
|
def async_is_registered(self, entity_id: str) -> bool:
|
2018-01-30 09:39:39 +00:00
|
|
|
"""Check if an entity_id is currently registered."""
|
|
|
|
return entity_id in self.entities
|
|
|
|
|
2018-12-05 10:41:00 +00:00
|
|
|
@callback
|
|
|
|
def async_get(self, entity_id: str) -> Optional[RegistryEntry]:
|
|
|
|
"""Get EntityEntry for an entity_id."""
|
|
|
|
return self.entities.get(entity_id)
|
|
|
|
|
2018-05-12 21:45:36 +00:00
|
|
|
@callback
|
2019-08-15 15:53:25 +00:00
|
|
|
def async_get_entity_id(
|
|
|
|
self, domain: str, platform: str, unique_id: str
|
|
|
|
) -> Optional[str]:
|
2018-05-12 21:45:36 +00:00
|
|
|
"""Check if an entity_id is currently registered."""
|
2020-07-20 05:52:41 +00:00
|
|
|
return self._index.get((domain, platform, unique_id))
|
2018-05-12 21:45:36 +00:00
|
|
|
|
2018-01-30 09:39:39 +00:00
|
|
|
@callback
|
2019-07-31 19:25:30 +00:00
|
|
|
def async_generate_entity_id(
|
2019-10-28 20:36:26 +00:00
|
|
|
self,
|
|
|
|
domain: str,
|
|
|
|
suggested_object_id: str,
|
|
|
|
known_object_ids: Optional[Iterable[str]] = None,
|
|
|
|
) -> str:
|
2018-01-30 09:39:39 +00:00
|
|
|
"""Generate an entity ID that does not conflict.
|
|
|
|
|
|
|
|
Conflicts checked against registered and currently existing entities.
|
|
|
|
"""
|
2020-04-19 23:19:11 +00:00
|
|
|
preferred_string = f"{domain}.{slugify(suggested_object_id)}"
|
|
|
|
test_string = preferred_string
|
|
|
|
if not known_object_ids:
|
|
|
|
known_object_ids = {}
|
|
|
|
|
|
|
|
tries = 1
|
|
|
|
while (
|
|
|
|
test_string in self.entities
|
|
|
|
or test_string in known_object_ids
|
2020-10-21 15:01:51 +00:00
|
|
|
or not self.hass.states.async_available(test_string)
|
2020-04-19 23:19:11 +00:00
|
|
|
):
|
|
|
|
tries += 1
|
|
|
|
test_string = f"{preferred_string}_{tries}"
|
|
|
|
|
|
|
|
return test_string
|
2018-01-30 09:39:39 +00:00
|
|
|
|
|
|
|
@callback
|
2019-07-31 19:25:30 +00:00
|
|
|
def async_get_or_create(
|
|
|
|
self,
|
2019-12-22 18:51:39 +00:00
|
|
|
domain: str,
|
|
|
|
platform: str,
|
|
|
|
unique_id: str,
|
2019-07-31 19:25:30 +00:00
|
|
|
*,
|
2019-12-31 13:29:43 +00:00
|
|
|
# To influence entity ID generation
|
2019-12-22 18:51:39 +00:00
|
|
|
suggested_object_id: Optional[str] = None,
|
|
|
|
known_object_ids: Optional[Iterable[str]] = None,
|
2019-12-31 13:29:43 +00:00
|
|
|
# To disable an entity if it gets created
|
2019-12-22 18:51:39 +00:00
|
|
|
disabled_by: Optional[str] = None,
|
2019-12-31 13:29:43 +00:00
|
|
|
# Data that we want entry to have
|
|
|
|
config_entry: Optional["ConfigEntry"] = None,
|
|
|
|
device_id: Optional[str] = None,
|
2020-10-24 19:25:28 +00:00
|
|
|
area_id: Optional[str] = None,
|
2019-12-31 13:29:43 +00:00
|
|
|
capabilities: Optional[Dict[str, Any]] = None,
|
|
|
|
supported_features: Optional[int] = None,
|
|
|
|
device_class: Optional[str] = None,
|
2020-01-15 16:09:05 +00:00
|
|
|
unit_of_measurement: Optional[str] = None,
|
2020-02-11 17:40:50 +00:00
|
|
|
original_name: Optional[str] = None,
|
|
|
|
original_icon: Optional[str] = None,
|
2019-12-22 18:51:39 +00:00
|
|
|
) -> RegistryEntry:
|
2018-01-30 09:39:39 +00:00
|
|
|
"""Get entity. Create if it doesn't exist."""
|
2019-08-18 04:34:11 +00:00
|
|
|
config_entry_id = None
|
|
|
|
if config_entry:
|
|
|
|
config_entry_id = config_entry.entry_id
|
|
|
|
|
2018-05-12 21:45:36 +00:00
|
|
|
entity_id = self.async_get_entity_id(domain, platform, unique_id)
|
2019-08-18 04:34:11 +00:00
|
|
|
|
2018-05-12 21:45:36 +00:00
|
|
|
if entity_id:
|
2021-01-05 01:03:16 +00:00
|
|
|
return self._async_update_entity(
|
2019-01-22 22:07:17 +00:00
|
|
|
entity_id,
|
2020-12-19 11:46:27 +00:00
|
|
|
config_entry_id=config_entry_id or UNDEFINED,
|
|
|
|
device_id=device_id or UNDEFINED,
|
|
|
|
area_id=area_id or UNDEFINED,
|
|
|
|
capabilities=capabilities or UNDEFINED,
|
|
|
|
supported_features=supported_features or UNDEFINED,
|
|
|
|
device_class=device_class or UNDEFINED,
|
|
|
|
unit_of_measurement=unit_of_measurement or UNDEFINED,
|
|
|
|
original_name=original_name or UNDEFINED,
|
|
|
|
original_icon=original_icon or UNDEFINED,
|
2019-01-22 22:07:17 +00:00
|
|
|
# When we changed our slugify algorithm, we invalidated some
|
|
|
|
# stored entity IDs with either a __ or ending in _.
|
2019-01-24 00:33:21 +00:00
|
|
|
# Fix introduced in 0.86 (Jan 23, 2019). Next line can be
|
|
|
|
# removed when we release 1.0 or in 2020.
|
2019-07-31 19:25:30 +00:00
|
|
|
new_entity_id=".".join(
|
|
|
|
slugify(part) for part in entity_id.split(".", 1)
|
|
|
|
),
|
|
|
|
)
|
2018-01-30 09:39:39 +00:00
|
|
|
|
|
|
|
entity_id = self.async_generate_entity_id(
|
2019-08-23 16:53:33 +00:00
|
|
|
domain, suggested_object_id or f"{platform}_{unique_id}", known_object_ids
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2018-06-07 18:23:09 +00:00
|
|
|
|
2019-08-18 04:34:11 +00:00
|
|
|
if (
|
|
|
|
disabled_by is None
|
|
|
|
and config_entry
|
|
|
|
and config_entry.system_options.disable_new_entities
|
|
|
|
):
|
|
|
|
disabled_by = DISABLED_INTEGRATION
|
|
|
|
|
2018-02-11 17:16:01 +00:00
|
|
|
entity = RegistryEntry(
|
2018-01-30 09:39:39 +00:00
|
|
|
entity_id=entity_id,
|
2018-06-07 18:23:09 +00:00
|
|
|
config_entry_id=config_entry_id,
|
2018-08-22 08:46:37 +00:00
|
|
|
device_id=device_id,
|
2020-10-24 19:25:28 +00:00
|
|
|
area_id=area_id,
|
2018-01-30 09:39:39 +00:00
|
|
|
unique_id=unique_id,
|
|
|
|
platform=platform,
|
2019-08-16 23:17:16 +00:00
|
|
|
disabled_by=disabled_by,
|
2019-12-31 13:29:43 +00:00
|
|
|
capabilities=capabilities,
|
|
|
|
supported_features=supported_features or 0,
|
|
|
|
device_class=device_class,
|
2020-01-15 16:09:05 +00:00
|
|
|
unit_of_measurement=unit_of_measurement,
|
2020-02-11 17:40:50 +00:00
|
|
|
original_name=original_name,
|
|
|
|
original_icon=original_icon,
|
2018-01-30 09:39:39 +00:00
|
|
|
)
|
2020-07-20 05:52:41 +00:00
|
|
|
self._register_entry(entity)
|
2019-07-31 19:25:30 +00:00
|
|
|
_LOGGER.info("Registered new %s.%s entity: %s", domain, platform, entity_id)
|
2018-01-30 09:39:39 +00:00
|
|
|
self.async_schedule_save()
|
2019-05-08 03:04:57 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
self.hass.bus.async_fire(
|
|
|
|
EVENT_ENTITY_REGISTRY_UPDATED, {"action": "create", "entity_id": entity_id}
|
|
|
|
)
|
2019-05-08 03:04:57 +00:00
|
|
|
|
2018-01-30 09:39:39 +00:00
|
|
|
return entity
|
|
|
|
|
2019-01-30 17:50:32 +00:00
|
|
|
@callback
|
2019-10-28 20:36:26 +00:00
|
|
|
def async_remove(self, entity_id: str) -> None:
|
2019-01-30 17:50:32 +00:00
|
|
|
"""Remove an entity from registry."""
|
2020-07-20 05:52:41 +00:00
|
|
|
self._unregister_entry(self.entities[entity_id])
|
2019-07-31 19:25:30 +00:00
|
|
|
self.hass.bus.async_fire(
|
|
|
|
EVENT_ENTITY_REGISTRY_UPDATED, {"action": "remove", "entity_id": entity_id}
|
|
|
|
)
|
2019-01-30 17:50:32 +00:00
|
|
|
self.async_schedule_save()
|
|
|
|
|
2021-02-12 16:00:35 +00:00
|
|
|
@callback
|
|
|
|
def async_device_modified(self, event: Event) -> None:
|
2020-11-26 15:45:02 +00:00
|
|
|
"""Handle the removal or update of a device.
|
2019-06-24 18:26:45 +00:00
|
|
|
|
|
|
|
Remove entities from the registry that are associated to a device when
|
|
|
|
the device is removed.
|
2020-11-26 15:45:02 +00:00
|
|
|
|
|
|
|
Disable entities in the registry that are associated to a device when
|
|
|
|
the device is disabled.
|
2019-06-24 18:26:45 +00:00
|
|
|
"""
|
2020-11-26 15:45:02 +00:00
|
|
|
if event.data["action"] == "remove":
|
2020-11-27 08:03:44 +00:00
|
|
|
entities = async_entries_for_device(
|
|
|
|
self, event.data["device_id"], include_disabled_entities=True
|
|
|
|
)
|
2020-11-26 15:45:02 +00:00
|
|
|
for entity in entities:
|
|
|
|
self.async_remove(entity.entity_id)
|
|
|
|
return
|
|
|
|
|
|
|
|
if event.data["action"] != "update":
|
2019-06-24 18:26:45 +00:00
|
|
|
return
|
2020-11-26 15:45:02 +00:00
|
|
|
|
2021-02-12 16:00:35 +00:00
|
|
|
device_registry = dr.async_get(self.hass)
|
2020-11-26 15:45:02 +00:00
|
|
|
device = device_registry.async_get(event.data["device_id"])
|
2021-02-10 09:50:44 +00:00
|
|
|
|
|
|
|
# The device may be deleted already if the event handling is late
|
|
|
|
if not device or not device.disabled:
|
2020-12-02 20:20:14 +00:00
|
|
|
entities = async_entries_for_device(
|
|
|
|
self, event.data["device_id"], include_disabled_entities=True
|
|
|
|
)
|
|
|
|
for entity in entities:
|
|
|
|
if entity.disabled_by != DISABLED_DEVICE:
|
|
|
|
continue
|
2021-01-05 01:03:16 +00:00
|
|
|
self.async_update_entity(entity.entity_id, disabled_by=None)
|
2020-11-26 15:45:02 +00:00
|
|
|
return
|
|
|
|
|
2020-12-02 20:20:14 +00:00
|
|
|
entities = async_entries_for_device(self, event.data["device_id"])
|
2019-06-24 18:26:45 +00:00
|
|
|
for entity in entities:
|
2021-01-05 01:03:16 +00:00
|
|
|
self.async_update_entity(entity.entity_id, disabled_by=DISABLED_DEVICE)
|
2019-06-24 18:26:45 +00:00
|
|
|
|
2018-02-24 18:53:59 +00:00
|
|
|
@callback
|
2019-07-31 19:25:30 +00:00
|
|
|
def async_update_entity(
|
2019-08-16 23:22:45 +00:00
|
|
|
self,
|
2021-01-05 01:03:16 +00:00
|
|
|
entity_id: str,
|
2019-08-16 23:22:45 +00:00
|
|
|
*,
|
2021-01-05 01:03:16 +00:00
|
|
|
name: Union[str, None, UndefinedType] = UNDEFINED,
|
|
|
|
icon: Union[str, None, UndefinedType] = UNDEFINED,
|
|
|
|
area_id: Union[str, None, UndefinedType] = UNDEFINED,
|
|
|
|
new_entity_id: Union[str, UndefinedType] = UNDEFINED,
|
|
|
|
new_unique_id: Union[str, UndefinedType] = UNDEFINED,
|
|
|
|
disabled_by: Union[str, None, UndefinedType] = UNDEFINED,
|
|
|
|
) -> RegistryEntry:
|
2018-02-24 18:53:59 +00:00
|
|
|
"""Update properties of an entity."""
|
2021-01-05 01:03:16 +00:00
|
|
|
return self._async_update_entity(
|
|
|
|
entity_id,
|
|
|
|
name=name,
|
|
|
|
icon=icon,
|
|
|
|
area_id=area_id,
|
|
|
|
new_entity_id=new_entity_id,
|
|
|
|
new_unique_id=new_unique_id,
|
|
|
|
disabled_by=disabled_by,
|
2018-07-24 12:12:53 +00:00
|
|
|
)
|
2018-07-19 06:37:13 +00:00
|
|
|
|
|
|
|
@callback
|
2019-07-31 19:25:30 +00:00
|
|
|
def _async_update_entity(
|
|
|
|
self,
|
2021-01-05 01:03:16 +00:00
|
|
|
entity_id: str,
|
2019-07-31 19:25:30 +00:00
|
|
|
*,
|
2021-01-05 01:03:16 +00:00
|
|
|
name: Union[str, None, UndefinedType] = UNDEFINED,
|
|
|
|
icon: Union[str, None, UndefinedType] = UNDEFINED,
|
|
|
|
config_entry_id: Union[str, None, UndefinedType] = UNDEFINED,
|
|
|
|
new_entity_id: Union[str, UndefinedType] = UNDEFINED,
|
|
|
|
device_id: Union[str, None, UndefinedType] = UNDEFINED,
|
|
|
|
area_id: Union[str, None, UndefinedType] = UNDEFINED,
|
|
|
|
new_unique_id: Union[str, UndefinedType] = UNDEFINED,
|
|
|
|
disabled_by: Union[str, None, UndefinedType] = UNDEFINED,
|
|
|
|
capabilities: Union[Dict[str, Any], None, UndefinedType] = UNDEFINED,
|
|
|
|
supported_features: Union[int, UndefinedType] = UNDEFINED,
|
|
|
|
device_class: Union[str, None, UndefinedType] = UNDEFINED,
|
|
|
|
unit_of_measurement: Union[str, None, UndefinedType] = UNDEFINED,
|
|
|
|
original_name: Union[str, None, UndefinedType] = UNDEFINED,
|
|
|
|
original_icon: Union[str, None, UndefinedType] = UNDEFINED,
|
|
|
|
) -> RegistryEntry:
|
2018-07-19 06:37:13 +00:00
|
|
|
"""Private facing update properties method."""
|
2018-02-24 18:53:59 +00:00
|
|
|
old = self.entities[entity_id]
|
|
|
|
|
|
|
|
changes = {}
|
|
|
|
|
2019-08-16 23:22:45 +00:00
|
|
|
for attr_name, value in (
|
|
|
|
("name", name),
|
2020-02-11 17:40:50 +00:00
|
|
|
("icon", icon),
|
2019-08-16 23:22:45 +00:00
|
|
|
("config_entry_id", config_entry_id),
|
|
|
|
("device_id", device_id),
|
2020-10-24 19:25:28 +00:00
|
|
|
("area_id", area_id),
|
2019-08-16 23:22:45 +00:00
|
|
|
("disabled_by", disabled_by),
|
2019-12-31 13:29:43 +00:00
|
|
|
("capabilities", capabilities),
|
|
|
|
("supported_features", supported_features),
|
|
|
|
("device_class", device_class),
|
2020-01-15 16:09:05 +00:00
|
|
|
("unit_of_measurement", unit_of_measurement),
|
2020-02-11 17:40:50 +00:00
|
|
|
("original_name", original_name),
|
|
|
|
("original_icon", original_icon),
|
2019-08-16 23:22:45 +00:00
|
|
|
):
|
2020-12-19 11:46:27 +00:00
|
|
|
if value is not UNDEFINED and value != getattr(old, attr_name):
|
2019-08-16 23:22:45 +00:00
|
|
|
changes[attr_name] = value
|
2018-08-22 08:46:37 +00:00
|
|
|
|
2020-12-19 11:46:27 +00:00
|
|
|
if new_entity_id is not UNDEFINED and new_entity_id != old.entity_id:
|
2018-07-24 12:12:53 +00:00
|
|
|
if self.async_is_registered(new_entity_id):
|
2021-01-22 14:16:13 +00:00
|
|
|
raise ValueError("Entity with this ID is already registered")
|
2018-07-24 12:12:53 +00:00
|
|
|
|
|
|
|
if not valid_entity_id(new_entity_id):
|
2019-07-31 19:25:30 +00:00
|
|
|
raise ValueError("Invalid entity ID")
|
2018-07-24 12:12:53 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
if split_entity_id(new_entity_id)[0] != split_entity_id(entity_id)[0]:
|
|
|
|
raise ValueError("New entity ID should be same domain")
|
2018-07-24 12:12:53 +00:00
|
|
|
|
|
|
|
self.entities.pop(entity_id)
|
2019-07-31 19:25:30 +00:00
|
|
|
entity_id = changes["entity_id"] = new_entity_id
|
2018-07-24 12:12:53 +00:00
|
|
|
|
2020-12-19 11:46:27 +00:00
|
|
|
if new_unique_id is not UNDEFINED:
|
2020-07-20 05:52:41 +00:00
|
|
|
conflict_entity_id = self.async_get_entity_id(
|
|
|
|
old.domain, old.platform, new_unique_id
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2020-07-20 05:52:41 +00:00
|
|
|
if conflict_entity_id:
|
2019-04-30 17:04:37 +00:00
|
|
|
raise ValueError(
|
2020-01-03 13:47:06 +00:00
|
|
|
f"Unique id '{new_unique_id}' is already in use by "
|
2020-07-20 05:52:41 +00:00
|
|
|
f"'{conflict_entity_id}'"
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
|
|
|
changes["unique_id"] = new_unique_id
|
2019-04-30 17:04:37 +00:00
|
|
|
|
2018-02-24 18:53:59 +00:00
|
|
|
if not changes:
|
|
|
|
return old
|
|
|
|
|
2020-07-20 05:52:41 +00:00
|
|
|
self._remove_index(old)
|
|
|
|
new = attr.evolve(old, **changes)
|
|
|
|
self._register_entry(new)
|
2018-02-24 18:53:59 +00:00
|
|
|
|
|
|
|
self.async_schedule_save()
|
|
|
|
|
2019-08-23 00:32:43 +00:00
|
|
|
data = {"action": "update", "entity_id": entity_id, "changes": list(changes)}
|
2019-06-26 16:22:51 +00:00
|
|
|
|
|
|
|
if old.entity_id != entity_id:
|
2019-07-31 19:25:30 +00:00
|
|
|
data["old_entity_id"] = old.entity_id
|
2019-06-26 16:22:51 +00:00
|
|
|
|
|
|
|
self.hass.bus.async_fire(EVENT_ENTITY_REGISTRY_UPDATED, data)
|
2019-05-08 03:04:57 +00:00
|
|
|
|
2018-02-24 18:53:59 +00:00
|
|
|
return new
|
|
|
|
|
2019-10-28 20:36:26 +00:00
|
|
|
async def async_load(self) -> None:
|
2018-01-30 09:39:39 +00:00
|
|
|
"""Load the entity registry."""
|
2019-12-31 13:29:43 +00:00
|
|
|
async_setup_entity_restore(self.hass, self)
|
|
|
|
|
2018-08-18 11:34:33 +00:00
|
|
|
data = await self.hass.helpers.storage.async_migrator(
|
2019-07-31 19:25:30 +00:00
|
|
|
self.hass.config.path(PATH_REGISTRY),
|
|
|
|
self._store,
|
2018-08-18 11:34:33 +00:00
|
|
|
old_conf_load_func=load_yaml,
|
2019-07-31 19:25:30 +00:00
|
|
|
old_conf_migrate_func=_async_migrate,
|
2018-08-18 11:34:33 +00:00
|
|
|
)
|
2019-10-28 20:36:26 +00:00
|
|
|
entities: Dict[str, RegistryEntry] = OrderedDict()
|
2018-01-30 09:39:39 +00:00
|
|
|
|
2018-08-18 11:34:33 +00:00
|
|
|
if data is not None:
|
2019-07-31 19:25:30 +00:00
|
|
|
for entity in data["entities"]:
|
2020-05-05 22:07:54 +00:00
|
|
|
# Some old installations can have some bad entities.
|
|
|
|
# Filter them out as they cause errors down the line.
|
|
|
|
# Can be removed in Jan 2021
|
|
|
|
if not valid_entity_id(entity["entity_id"]):
|
|
|
|
continue
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
entities[entity["entity_id"]] = RegistryEntry(
|
|
|
|
entity_id=entity["entity_id"],
|
|
|
|
config_entry_id=entity.get("config_entry_id"),
|
|
|
|
device_id=entity.get("device_id"),
|
2020-10-24 19:25:28 +00:00
|
|
|
area_id=entity.get("area_id"),
|
2019-07-31 19:25:30 +00:00
|
|
|
unique_id=entity["unique_id"],
|
|
|
|
platform=entity["platform"],
|
|
|
|
name=entity.get("name"),
|
2020-02-18 16:32:34 +00:00
|
|
|
icon=entity.get("icon"),
|
2019-07-31 19:25:30 +00:00
|
|
|
disabled_by=entity.get("disabled_by"),
|
2019-12-31 13:29:43 +00:00
|
|
|
capabilities=entity.get("capabilities") or {},
|
|
|
|
supported_features=entity.get("supported_features", 0),
|
|
|
|
device_class=entity.get("device_class"),
|
2020-01-15 16:09:05 +00:00
|
|
|
unit_of_measurement=entity.get("unit_of_measurement"),
|
2020-02-18 16:32:34 +00:00
|
|
|
original_name=entity.get("original_name"),
|
|
|
|
original_icon=entity.get("original_icon"),
|
2018-01-30 09:39:39 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
self.entities = entities
|
2020-07-20 05:52:41 +00:00
|
|
|
self._rebuild_index()
|
2018-01-30 09:39:39 +00:00
|
|
|
|
|
|
|
@callback
|
2019-10-28 20:36:26 +00:00
|
|
|
def async_schedule_save(self) -> None:
|
2018-01-30 09:39:39 +00:00
|
|
|
"""Schedule saving the entity registry."""
|
2018-08-18 11:34:33 +00:00
|
|
|
self._store.async_delay_save(self._data_to_save, SAVE_DELAY)
|
2018-01-30 09:39:39 +00:00
|
|
|
|
2018-08-18 11:34:33 +00:00
|
|
|
@callback
|
2019-10-28 20:36:26 +00:00
|
|
|
def _data_to_save(self) -> Dict[str, Any]:
|
2018-08-24 08:28:43 +00:00
|
|
|
"""Return data of entity registry to store in a file."""
|
2018-08-18 11:34:33 +00:00
|
|
|
data = {}
|
2018-01-30 09:39:39 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
data["entities"] = [
|
2018-08-18 11:34:33 +00:00
|
|
|
{
|
2019-07-31 19:25:30 +00:00
|
|
|
"entity_id": entry.entity_id,
|
|
|
|
"config_entry_id": entry.config_entry_id,
|
|
|
|
"device_id": entry.device_id,
|
2020-10-24 19:25:28 +00:00
|
|
|
"area_id": entry.area_id,
|
2019-07-31 19:25:30 +00:00
|
|
|
"unique_id": entry.unique_id,
|
|
|
|
"platform": entry.platform,
|
|
|
|
"name": entry.name,
|
2020-02-18 16:32:34 +00:00
|
|
|
"icon": entry.icon,
|
2019-07-31 19:25:30 +00:00
|
|
|
"disabled_by": entry.disabled_by,
|
2019-12-31 13:29:43 +00:00
|
|
|
"capabilities": entry.capabilities,
|
|
|
|
"supported_features": entry.supported_features,
|
|
|
|
"device_class": entry.device_class,
|
2020-01-15 16:09:05 +00:00
|
|
|
"unit_of_measurement": entry.unit_of_measurement,
|
2020-02-18 16:32:34 +00:00
|
|
|
"original_name": entry.original_name,
|
|
|
|
"original_icon": entry.original_icon,
|
2019-07-31 19:25:30 +00:00
|
|
|
}
|
|
|
|
for entry in self.entities.values()
|
2018-08-18 11:34:33 +00:00
|
|
|
]
|
2018-01-30 09:39:39 +00:00
|
|
|
|
2018-08-18 11:34:33 +00:00
|
|
|
return data
|
2018-02-24 18:53:59 +00:00
|
|
|
|
2018-09-04 07:00:14 +00:00
|
|
|
@callback
|
2019-10-28 20:36:26 +00:00
|
|
|
def async_clear_config_entry(self, config_entry: str) -> None:
|
2018-09-04 07:00:14 +00:00
|
|
|
"""Clear config entry from registry entries."""
|
2019-05-19 09:41:39 +00:00
|
|
|
for entity_id in [
|
2019-07-31 19:25:30 +00:00
|
|
|
entity_id
|
|
|
|
for entity_id, entry in self.entities.items()
|
|
|
|
if config_entry == entry.config_entry_id
|
|
|
|
]:
|
2019-05-19 09:41:39 +00:00
|
|
|
self.async_remove(entity_id)
|
2018-09-04 07:00:14 +00:00
|
|
|
|
2020-10-24 19:25:28 +00:00
|
|
|
@callback
|
|
|
|
def async_clear_area_id(self, area_id: str) -> None:
|
|
|
|
"""Clear area id from registry entries."""
|
|
|
|
for entity_id, entry in self.entities.items():
|
|
|
|
if area_id == entry.area_id:
|
2021-01-05 01:03:16 +00:00
|
|
|
self._async_update_entity(entity_id, area_id=None)
|
2020-10-24 19:25:28 +00:00
|
|
|
|
2020-07-20 05:52:41 +00:00
|
|
|
def _register_entry(self, entry: RegistryEntry) -> None:
|
|
|
|
self.entities[entry.entity_id] = entry
|
|
|
|
self._add_index(entry)
|
|
|
|
|
|
|
|
def _add_index(self, entry: RegistryEntry) -> None:
|
|
|
|
self._index[(entry.domain, entry.platform, entry.unique_id)] = entry.entity_id
|
|
|
|
|
|
|
|
def _unregister_entry(self, entry: RegistryEntry) -> None:
|
|
|
|
self._remove_index(entry)
|
|
|
|
del self.entities[entry.entity_id]
|
|
|
|
|
|
|
|
def _remove_index(self, entry: RegistryEntry) -> None:
|
|
|
|
del self._index[(entry.domain, entry.platform, entry.unique_id)]
|
|
|
|
|
|
|
|
def _rebuild_index(self) -> None:
|
|
|
|
self._index = {}
|
|
|
|
for entry in self.entities.values():
|
|
|
|
self._add_index(entry)
|
|
|
|
|
2018-02-24 18:53:59 +00:00
|
|
|
|
2021-02-11 16:36:19 +00:00
|
|
|
@callback
|
|
|
|
def async_get(hass: HomeAssistantType) -> EntityRegistry:
|
|
|
|
"""Get entity registry."""
|
|
|
|
return cast(EntityRegistry, hass.data[DATA_REGISTRY])
|
|
|
|
|
|
|
|
|
|
|
|
async def async_load(hass: HomeAssistantType) -> None:
|
|
|
|
"""Load entity registry."""
|
|
|
|
assert DATA_REGISTRY not in hass.data
|
|
|
|
hass.data[DATA_REGISTRY] = EntityRegistry(hass)
|
|
|
|
await hass.data[DATA_REGISTRY].async_load()
|
|
|
|
|
|
|
|
|
|
|
|
@bind_hass
|
2019-03-27 14:06:20 +00:00
|
|
|
async def async_get_registry(hass: HomeAssistantType) -> EntityRegistry:
|
2021-02-11 16:36:19 +00:00
|
|
|
"""Get entity registry.
|
|
|
|
|
|
|
|
This is deprecated and will be removed in the future. Use async_get instead.
|
|
|
|
"""
|
|
|
|
return async_get(hass)
|
2018-02-24 18:53:59 +00:00
|
|
|
|
|
|
|
|
2019-03-04 17:51:12 +00:00
|
|
|
@callback
|
2019-07-31 19:25:30 +00:00
|
|
|
def async_entries_for_device(
|
2020-11-27 08:03:44 +00:00
|
|
|
registry: EntityRegistry, device_id: str, include_disabled_entities: bool = False
|
2019-07-31 19:25:30 +00:00
|
|
|
) -> List[RegistryEntry]:
|
2019-03-04 17:51:12 +00:00
|
|
|
"""Return entries that match a device."""
|
2019-07-31 19:25:30 +00:00
|
|
|
return [
|
2020-11-27 08:03:44 +00:00
|
|
|
entry
|
|
|
|
for entry in registry.entities.values()
|
|
|
|
if entry.device_id == device_id
|
|
|
|
and (not entry.disabled_by or include_disabled_entities)
|
2019-07-31 19:25:30 +00:00
|
|
|
]
|
2019-03-04 17:51:12 +00:00
|
|
|
|
|
|
|
|
2020-10-24 19:25:28 +00:00
|
|
|
@callback
|
|
|
|
def async_entries_for_area(
|
|
|
|
registry: EntityRegistry, area_id: str
|
|
|
|
) -> List[RegistryEntry]:
|
|
|
|
"""Return entries that match an area."""
|
|
|
|
return [entry for entry in registry.entities.values() if entry.area_id == area_id]
|
|
|
|
|
|
|
|
|
2020-01-10 18:57:37 +00:00
|
|
|
@callback
|
|
|
|
def async_entries_for_config_entry(
|
|
|
|
registry: EntityRegistry, config_entry_id: str
|
|
|
|
) -> List[RegistryEntry]:
|
|
|
|
"""Return entries that match a config entry."""
|
|
|
|
return [
|
|
|
|
entry
|
|
|
|
for entry in registry.entities.values()
|
|
|
|
if entry.config_entry_id == config_entry_id
|
|
|
|
]
|
|
|
|
|
|
|
|
|
2019-10-28 20:36:26 +00:00
|
|
|
async def _async_migrate(entities: Dict[str, Any]) -> Dict[str, List[Dict[str, Any]]]:
|
2018-08-18 11:34:33 +00:00
|
|
|
"""Migrate the YAML config file to storage helper format."""
|
|
|
|
return {
|
2019-07-31 19:25:30 +00:00
|
|
|
"entities": [
|
|
|
|
{"entity_id": entity_id, **info} for entity_id, info in entities.items()
|
2018-08-18 11:34:33 +00:00
|
|
|
]
|
|
|
|
}
|
2019-12-31 13:29:43 +00:00
|
|
|
|
|
|
|
|
|
|
|
@callback
|
|
|
|
def async_setup_entity_restore(
|
|
|
|
hass: HomeAssistantType, registry: EntityRegistry
|
|
|
|
) -> None:
|
|
|
|
"""Set up the entity restore mechanism."""
|
|
|
|
|
2021-02-14 19:42:55 +00:00
|
|
|
@callback
|
|
|
|
def cleanup_restored_states_filter(event: Event) -> bool:
|
|
|
|
"""Clean up restored states filter."""
|
|
|
|
return bool(event.data["action"] == "remove")
|
|
|
|
|
2019-12-31 13:29:43 +00:00
|
|
|
@callback
|
|
|
|
def cleanup_restored_states(event: Event) -> None:
|
|
|
|
"""Clean up restored states."""
|
|
|
|
state = hass.states.get(event.data["entity_id"])
|
|
|
|
|
|
|
|
if state is None or not state.attributes.get(ATTR_RESTORED):
|
|
|
|
return
|
|
|
|
|
2020-03-24 16:59:17 +00:00
|
|
|
hass.states.async_remove(event.data["entity_id"], context=event.context)
|
2019-12-31 13:29:43 +00:00
|
|
|
|
2021-02-14 19:42:55 +00:00
|
|
|
hass.bus.async_listen(
|
|
|
|
EVENT_ENTITY_REGISTRY_UPDATED,
|
|
|
|
cleanup_restored_states,
|
|
|
|
event_filter=cleanup_restored_states_filter,
|
|
|
|
)
|
2019-12-31 13:29:43 +00:00
|
|
|
|
|
|
|
if hass.is_running:
|
|
|
|
return
|
|
|
|
|
|
|
|
@callback
|
|
|
|
def _write_unavailable_states(_: Event) -> None:
|
|
|
|
"""Make sure state machine contains entry for each registered entity."""
|
2021-02-08 09:45:46 +00:00
|
|
|
existing = set(hass.states.async_entity_ids())
|
2019-12-31 13:29:43 +00:00
|
|
|
|
|
|
|
for entry in registry.entities.values():
|
|
|
|
if entry.entity_id in existing or entry.disabled:
|
|
|
|
continue
|
|
|
|
|
2021-02-08 09:45:46 +00:00
|
|
|
entry.write_unavailable_state(hass)
|
2019-12-31 13:29:43 +00:00
|
|
|
|
|
|
|
hass.bus.async_listen(EVENT_HOMEASSISTANT_START, _write_unavailable_states)
|
2020-03-03 01:59:32 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def async_migrate_entries(
|
|
|
|
hass: HomeAssistantType,
|
|
|
|
config_entry_id: str,
|
|
|
|
entry_callback: Callable[[RegistryEntry], Optional[dict]],
|
|
|
|
) -> None:
|
|
|
|
"""Migrator of unique IDs."""
|
|
|
|
ent_reg = await async_get_registry(hass)
|
|
|
|
|
|
|
|
for entry in ent_reg.entities.values():
|
|
|
|
if entry.config_entry_id != config_entry_id:
|
|
|
|
continue
|
|
|
|
|
|
|
|
updates = entry_callback(entry)
|
|
|
|
|
|
|
|
if updates is not None:
|
2020-04-30 23:47:14 +00:00
|
|
|
ent_reg.async_update_entity(entry.entity_id, **updates)
|