2016-03-09 23:49:54 +01:00
|
|
|
"""Helpers for components that manage entities."""
|
2016-10-16 18:35:46 +02:00
|
|
|
import asyncio
|
2017-01-05 14:05:16 -08:00
|
|
|
from datetime import timedelta
|
2018-01-22 22:54:41 -08:00
|
|
|
from itertools import chain
|
2018-10-09 16:54:38 +02:00
|
|
|
import logging
|
2015-11-28 15:55:01 -08:00
|
|
|
|
2016-09-07 06:59:16 -07:00
|
|
|
from homeassistant import config as conf_util
|
2019-12-21 09:23:48 +02:00
|
|
|
from homeassistant.config_entries import ConfigEntry
|
2020-01-19 17:55:18 -08:00
|
|
|
from homeassistant.const import CONF_ENTITY_NAMESPACE, CONF_SCAN_INTERVAL
|
2019-12-21 09:23:48 +02:00
|
|
|
from homeassistant.core import HomeAssistant, callback
|
2018-02-08 03:16:51 -08:00
|
|
|
from homeassistant.exceptions import HomeAssistantError
|
2020-01-19 17:55:18 -08:00
|
|
|
from homeassistant.helpers import (
|
|
|
|
config_per_platform,
|
|
|
|
config_validation as cv,
|
|
|
|
discovery,
|
|
|
|
service,
|
|
|
|
)
|
2019-12-09 16:42:10 +01:00
|
|
|
from homeassistant.loader import async_get_integration, bind_hass
|
|
|
|
from homeassistant.setup import async_prepare_setup_platform
|
2015-03-21 18:49:30 -07:00
|
|
|
|
2019-12-09 16:42:10 +01:00
|
|
|
from .entity_platform import EntityPlatform
|
2019-07-21 19:59:02 +03:00
|
|
|
|
|
|
|
# mypy: allow-untyped-defs, no-check-untyped-defs
|
|
|
|
|
2017-01-06 00:16:12 +01:00
|
|
|
DEFAULT_SCAN_INTERVAL = timedelta(seconds=15)
|
2019-07-31 12:25:30 -07:00
|
|
|
DATA_INSTANCES = "entity_components"
|
2018-10-09 16:54:38 +02:00
|
|
|
|
|
|
|
|
|
|
|
@bind_hass
|
2019-12-21 09:23:48 +02:00
|
|
|
async def async_update_entity(hass: HomeAssistant, entity_id: str) -> None:
|
2018-10-09 16:54:38 +02:00
|
|
|
"""Trigger an update for an entity."""
|
2019-07-31 12:25:30 -07:00
|
|
|
domain = entity_id.split(".", 1)[0]
|
2018-10-09 16:54:38 +02:00
|
|
|
entity_comp = hass.data.get(DATA_INSTANCES, {}).get(domain)
|
|
|
|
|
|
|
|
if entity_comp is None:
|
|
|
|
logging.getLogger(__name__).warning(
|
2019-07-31 12:25:30 -07:00
|
|
|
"Forced update failed. Component for %s not loaded.", entity_id
|
|
|
|
)
|
2018-10-09 16:54:38 +02:00
|
|
|
return
|
|
|
|
|
|
|
|
entity = entity_comp.get_entity(entity_id)
|
|
|
|
|
|
|
|
if entity is None:
|
|
|
|
logging.getLogger(__name__).warning(
|
2019-07-31 12:25:30 -07:00
|
|
|
"Forced update failed. Entity %s not found.", entity_id
|
|
|
|
)
|
2018-10-09 16:54:38 +02:00
|
|
|
return
|
|
|
|
|
|
|
|
await entity.async_update_ha_state(True)
|
2015-03-21 18:49:30 -07:00
|
|
|
|
|
|
|
|
2018-07-20 11:45:20 +03:00
|
|
|
class EntityComponent:
|
2018-01-22 22:54:41 -08:00
|
|
|
"""The EntityComponent manages platforms that manages entities.
|
|
|
|
|
|
|
|
This class has the following responsibilities:
|
|
|
|
- Process the configuration and set up a platform based component.
|
|
|
|
- Manage the platforms and their entities.
|
|
|
|
- Help extract the entities from a service call.
|
|
|
|
- Listen for discovery events for platforms related to the domain.
|
|
|
|
"""
|
2016-01-30 18:55:52 -08:00
|
|
|
|
2020-01-07 17:30:53 +01:00
|
|
|
def __init__(self, logger, domain, hass, scan_interval=DEFAULT_SCAN_INTERVAL):
|
2016-01-30 18:55:52 -08:00
|
|
|
"""Initialize an entity component."""
|
2015-03-21 18:49:30 -07:00
|
|
|
self.logger = logger
|
|
|
|
self.hass = hass
|
|
|
|
self.domain = domain
|
|
|
|
self.scan_interval = scan_interval
|
|
|
|
|
2015-05-14 21:36:12 -07:00
|
|
|
self.config = None
|
|
|
|
|
2019-07-31 12:25:30 -07:00
|
|
|
self._platforms = {domain: self._async_init_entity_platform(domain, None)}
|
2018-02-11 09:16:01 -08:00
|
|
|
self.async_add_entities = self._platforms[domain].async_add_entities
|
|
|
|
self.add_entities = self._platforms[domain].add_entities
|
2016-01-31 00:55:46 -08:00
|
|
|
|
2018-10-09 16:54:38 +02:00
|
|
|
hass.data.setdefault(DATA_INSTANCES, {})[domain] = self
|
|
|
|
|
2018-01-22 22:54:41 -08:00
|
|
|
@property
|
|
|
|
def entities(self):
|
|
|
|
"""Return an iterable that returns all entities."""
|
2019-07-31 12:25:30 -07:00
|
|
|
return chain.from_iterable(
|
|
|
|
platform.entities.values() for platform in self._platforms.values()
|
|
|
|
)
|
2018-01-22 22:54:41 -08:00
|
|
|
|
|
|
|
def get_entity(self, entity_id):
|
2018-08-24 11:28:43 +03:00
|
|
|
"""Get an entity."""
|
2018-01-22 22:54:41 -08:00
|
|
|
for platform in self._platforms.values():
|
|
|
|
entity = platform.entities.get(entity_id)
|
|
|
|
if entity is not None:
|
|
|
|
return entity
|
|
|
|
return None
|
|
|
|
|
2015-03-21 18:49:30 -07:00
|
|
|
def setup(self, config):
|
2016-03-07 23:39:52 +01:00
|
|
|
"""Set up a full entity component.
|
2016-01-30 18:55:52 -08:00
|
|
|
|
2017-03-01 05:33:19 +01:00
|
|
|
This doesn't block the executor to protect from deadlocks.
|
2015-03-21 18:49:30 -07:00
|
|
|
"""
|
2017-03-01 05:33:19 +01:00
|
|
|
self.hass.add_job(self.async_setup(config))
|
2016-10-16 18:35:46 +02:00
|
|
|
|
2018-02-25 12:38:46 +01:00
|
|
|
async def async_setup(self, config):
|
2016-10-16 18:35:46 +02:00
|
|
|
"""Set up a full entity component.
|
|
|
|
|
|
|
|
Loads the platforms from the config and will listen for supported
|
|
|
|
discovered platforms.
|
|
|
|
|
|
|
|
This method must be run in the event loop.
|
|
|
|
"""
|
2015-05-14 21:36:12 -07:00
|
|
|
self.config = config
|
|
|
|
|
2015-03-21 18:49:30 -07:00
|
|
|
# Look in config for Domain, Domain 2, Domain 3 etc and load them
|
2016-10-16 18:35:46 +02:00
|
|
|
tasks = []
|
2016-03-27 18:48:51 -07:00
|
|
|
for p_type, p_config in config_per_platform(config, self.domain):
|
2019-08-05 14:04:20 -07:00
|
|
|
tasks.append(self.async_setup_platform(p_type, p_config))
|
2016-10-16 18:35:46 +02:00
|
|
|
|
2016-11-06 09:26:40 -08:00
|
|
|
if tasks:
|
2019-05-22 21:09:59 -07:00
|
|
|
await asyncio.wait(tasks)
|
2015-03-21 18:49:30 -07:00
|
|
|
|
2016-05-12 06:58:22 +02:00
|
|
|
# Generic discovery listener for loading platform dynamically
|
|
|
|
# Refer to: homeassistant.components.discovery.load_platform()
|
2018-02-25 12:38:46 +01:00
|
|
|
async def component_platform_discovered(platform, info):
|
2017-05-02 18:18:47 +02:00
|
|
|
"""Handle the loading of a platform."""
|
2019-08-05 14:04:20 -07:00
|
|
|
await self.async_setup_platform(platform, {}, info)
|
2016-06-11 17:43:13 -07:00
|
|
|
|
2016-10-16 18:35:46 +02:00
|
|
|
discovery.async_listen_platform(
|
2019-07-31 12:25:30 -07:00
|
|
|
self.hass, self.domain, component_platform_discovered
|
|
|
|
)
|
2016-05-10 07:48:03 +02:00
|
|
|
|
2018-04-09 10:09:08 -04:00
|
|
|
async def async_setup_entry(self, config_entry):
|
2018-08-19 22:29:08 +02:00
|
|
|
"""Set up a config entry."""
|
2018-04-09 10:09:08 -04:00
|
|
|
platform_type = config_entry.domain
|
|
|
|
platform = await async_prepare_setup_platform(
|
2019-04-14 16:59:06 -07:00
|
|
|
self.hass,
|
|
|
|
# In future PR we should make hass_config part of the constructor
|
|
|
|
# params.
|
|
|
|
self.config or {},
|
2019-07-31 12:25:30 -07:00
|
|
|
self.domain,
|
|
|
|
platform_type,
|
|
|
|
)
|
2018-04-09 10:09:08 -04:00
|
|
|
|
|
|
|
if platform is None:
|
|
|
|
return False
|
|
|
|
|
|
|
|
key = config_entry.entry_id
|
|
|
|
|
|
|
|
if key in self._platforms:
|
2019-07-31 12:25:30 -07:00
|
|
|
raise ValueError("Config entry has already been setup!")
|
2018-04-09 10:09:08 -04:00
|
|
|
|
|
|
|
self._platforms[key] = self._async_init_entity_platform(
|
2019-07-31 12:25:30 -07:00
|
|
|
platform_type,
|
|
|
|
platform,
|
|
|
|
scan_interval=getattr(platform, "SCAN_INTERVAL", None),
|
2018-04-09 10:09:08 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
return await self._platforms[key].async_setup_entry(config_entry)
|
|
|
|
|
2019-12-21 09:23:48 +02:00
|
|
|
async def async_unload_entry(self, config_entry: ConfigEntry) -> bool:
|
2018-04-12 08:28:54 -04:00
|
|
|
"""Unload a config entry."""
|
|
|
|
key = config_entry.entry_id
|
|
|
|
|
|
|
|
platform = self._platforms.pop(key, None)
|
|
|
|
|
|
|
|
if platform is None:
|
2019-07-31 12:25:30 -07:00
|
|
|
raise ValueError("Config entry was never loaded!")
|
2018-04-12 08:28:54 -04:00
|
|
|
|
|
|
|
await platform.async_reset()
|
|
|
|
return True
|
|
|
|
|
2020-01-19 17:55:18 -08:00
|
|
|
async def async_extract_from_service(self, service_call, expand_group=True):
|
2017-04-11 17:59:46 +02:00
|
|
|
"""Extract all known and available entities from a service call.
|
2016-10-16 18:35:46 +02:00
|
|
|
|
|
|
|
Will return an empty list if entities specified but unknown.
|
|
|
|
|
|
|
|
This method must be run in the event loop.
|
|
|
|
"""
|
2020-01-19 17:55:18 -08:00
|
|
|
return await service.async_extract_entities(
|
|
|
|
self.hass, self.entities, service_call, expand_group
|
|
|
|
)
|
2015-03-21 18:49:30 -07:00
|
|
|
|
2018-08-16 09:50:11 +02:00
|
|
|
@callback
|
2019-07-31 12:25:30 -07:00
|
|
|
def async_register_entity_service(self, name, schema, func, required_features=None):
|
2018-08-16 09:50:11 +02:00
|
|
|
"""Register an entity service."""
|
2019-09-03 00:50:24 -07:00
|
|
|
if isinstance(schema, dict):
|
2020-01-19 17:55:18 -08:00
|
|
|
schema = cv.make_entity_service_schema(schema)
|
2019-07-31 12:25:30 -07:00
|
|
|
|
2018-08-16 09:50:11 +02:00
|
|
|
async def handle_service(call):
|
|
|
|
"""Handle the service."""
|
|
|
|
await self.hass.helpers.service.entity_service_call(
|
2020-01-19 17:55:18 -08:00
|
|
|
self._platforms.values(), func, call, required_features
|
2018-08-16 09:50:11 +02:00
|
|
|
)
|
|
|
|
|
2019-07-31 12:25:30 -07:00
|
|
|
self.hass.services.async_register(self.domain, name, handle_service, schema)
|
2018-08-16 09:50:11 +02:00
|
|
|
|
2019-08-05 14:04:20 -07:00
|
|
|
async def async_setup_platform(
|
2019-07-31 12:25:30 -07:00
|
|
|
self, platform_type, platform_config, discovery_info=None
|
|
|
|
):
|
2018-02-08 03:16:51 -08:00
|
|
|
"""Set up a platform for this component."""
|
2019-08-05 14:04:20 -07:00
|
|
|
if self.config is None:
|
|
|
|
raise RuntimeError("async_setup needs to be called first")
|
|
|
|
|
2018-02-25 12:38:46 +01:00
|
|
|
platform = await async_prepare_setup_platform(
|
2019-07-31 12:25:30 -07:00
|
|
|
self.hass, self.config, self.domain, platform_type
|
|
|
|
)
|
2015-03-21 18:49:30 -07:00
|
|
|
|
2016-04-17 22:07:53 -07:00
|
|
|
if platform is None:
|
|
|
|
return
|
|
|
|
|
2018-04-07 23:04:50 -04:00
|
|
|
# Use config scan interval, fallback to platform if none set
|
|
|
|
scan_interval = platform_config.get(
|
2019-07-31 12:25:30 -07:00
|
|
|
CONF_SCAN_INTERVAL, getattr(platform, "SCAN_INTERVAL", None)
|
|
|
|
)
|
2016-04-23 06:34:49 +02:00
|
|
|
entity_namespace = platform_config.get(CONF_ENTITY_NAMESPACE)
|
2015-03-21 18:49:30 -07:00
|
|
|
|
2016-09-04 17:15:52 +02:00
|
|
|
key = (platform_type, scan_interval, entity_namespace)
|
|
|
|
|
|
|
|
if key not in self._platforms:
|
2018-04-07 23:04:50 -04:00
|
|
|
self._platforms[key] = self._async_init_entity_platform(
|
|
|
|
platform_type, platform, scan_interval, entity_namespace
|
2018-02-08 03:16:51 -08:00
|
|
|
)
|
2016-09-04 17:15:52 +02:00
|
|
|
|
2018-04-07 23:04:50 -04:00
|
|
|
await self._platforms[key].async_setup(platform_config, discovery_info)
|
2015-09-09 23:37:15 -07:00
|
|
|
|
2019-12-21 09:23:48 +02:00
|
|
|
async def _async_reset(self) -> None:
|
2016-10-16 18:35:46 +02:00
|
|
|
"""Remove entities and reset the entity component to initial values.
|
2016-09-04 17:15:52 +02:00
|
|
|
|
2016-10-16 18:35:46 +02:00
|
|
|
This method must be run in the event loop.
|
|
|
|
"""
|
2019-07-31 12:25:30 -07:00
|
|
|
tasks = [platform.async_reset() for platform in self._platforms.values()]
|
2016-10-16 18:35:46 +02:00
|
|
|
|
2016-11-06 09:26:40 -08:00
|
|
|
if tasks:
|
2019-05-22 21:09:59 -07:00
|
|
|
await asyncio.wait(tasks)
|
2016-10-16 18:35:46 +02:00
|
|
|
|
2019-07-31 12:25:30 -07:00
|
|
|
self._platforms = {self.domain: self._platforms[self.domain]}
|
2016-10-16 18:35:46 +02:00
|
|
|
self.config = None
|
|
|
|
|
2019-12-21 09:23:48 +02:00
|
|
|
async def async_remove_entity(self, entity_id: str) -> None:
|
2018-01-22 22:54:41 -08:00
|
|
|
"""Remove an entity managed by one of the platforms."""
|
|
|
|
for platform in self._platforms.values():
|
|
|
|
if entity_id in platform.entities:
|
2018-02-25 12:38:46 +01:00
|
|
|
await platform.async_remove_entity(entity_id)
|
2016-09-04 17:15:52 +02:00
|
|
|
|
2020-01-05 11:16:37 +01:00
|
|
|
async def async_prepare_reload(self, *, skip_reset=False):
|
2016-10-27 09:16:23 +02:00
|
|
|
"""Prepare reloading this entity component.
|
|
|
|
|
|
|
|
This method must be run in the event loop.
|
|
|
|
"""
|
2016-09-07 06:59:16 -07:00
|
|
|
try:
|
2019-07-31 12:25:30 -07:00
|
|
|
conf = await conf_util.async_hass_config_yaml(self.hass)
|
2016-09-07 06:59:16 -07:00
|
|
|
except HomeAssistantError as err:
|
|
|
|
self.logger.error(err)
|
|
|
|
return None
|
|
|
|
|
2019-04-14 07:23:01 -07:00
|
|
|
integration = await async_get_integration(self.hass, self.domain)
|
|
|
|
|
|
|
|
conf = await conf_util.async_process_component_config(
|
2019-07-31 12:25:30 -07:00
|
|
|
self.hass, conf, integration
|
|
|
|
)
|
2016-09-07 06:59:16 -07:00
|
|
|
|
|
|
|
if conf is None:
|
|
|
|
return None
|
|
|
|
|
2020-01-05 11:16:37 +01:00
|
|
|
if not skip_reset:
|
|
|
|
await self._async_reset()
|
2016-10-16 18:35:46 +02:00
|
|
|
return conf
|
2018-04-07 23:04:50 -04:00
|
|
|
|
2019-07-31 12:25:30 -07:00
|
|
|
def _async_init_entity_platform(
|
|
|
|
self, platform_type, platform, scan_interval=None, entity_namespace=None
|
|
|
|
):
|
2018-08-24 11:28:43 +03:00
|
|
|
"""Initialize an entity platform."""
|
2018-04-07 23:04:50 -04:00
|
|
|
if scan_interval is None:
|
|
|
|
scan_interval = self.scan_interval
|
|
|
|
|
|
|
|
return EntityPlatform(
|
|
|
|
hass=self.hass,
|
|
|
|
logger=self.logger,
|
|
|
|
domain=self.domain,
|
|
|
|
platform_name=platform_type,
|
|
|
|
platform=platform,
|
|
|
|
scan_interval=scan_interval,
|
|
|
|
entity_namespace=entity_namespace,
|
|
|
|
)
|