2016-03-09 22:49:54 +00:00
|
|
|
"""Helpers for components that manage entities."""
|
2016-10-16 16:35:46 +00:00
|
|
|
import asyncio
|
2015-11-28 23:55:01 +00:00
|
|
|
|
2016-09-07 13:59:16 +00:00
|
|
|
from homeassistant import config as conf_util
|
|
|
|
from homeassistant.bootstrap import (prepare_setup_platform,
|
|
|
|
prepare_setup_component)
|
2016-04-23 04:34:49 +00:00
|
|
|
from homeassistant.const import (
|
|
|
|
ATTR_ENTITY_ID, CONF_SCAN_INTERVAL, CONF_ENTITY_NAMESPACE,
|
|
|
|
DEVICE_DEFAULT_NAME)
|
2016-10-16 16:35:46 +00:00
|
|
|
from homeassistant.core import callback
|
2016-09-07 13:59:16 +00:00
|
|
|
from homeassistant.exceptions import HomeAssistantError
|
|
|
|
from homeassistant.loader import get_component
|
2016-06-12 00:43:13 +00:00
|
|
|
from homeassistant.helpers import config_per_platform, discovery
|
2016-10-16 16:35:46 +00:00
|
|
|
from homeassistant.helpers.entity import async_generate_entity_id
|
|
|
|
from homeassistant.helpers.event import async_track_utc_time_change
|
2016-01-24 06:57:14 +00:00
|
|
|
from homeassistant.helpers.service import extract_entity_ids
|
2016-10-16 16:35:46 +00:00
|
|
|
from homeassistant.util.async import (
|
|
|
|
run_callback_threadsafe, run_coroutine_threadsafe)
|
2015-03-22 01:49:30 +00:00
|
|
|
|
|
|
|
DEFAULT_SCAN_INTERVAL = 15
|
|
|
|
|
|
|
|
|
|
|
|
class EntityComponent(object):
|
2016-01-31 02:55:52 +00:00
|
|
|
"""Helper class that will help a component manage its entities."""
|
|
|
|
|
2015-03-22 01:49:30 +00:00
|
|
|
# pylint: disable=too-many-instance-attributes
|
|
|
|
# pylint: disable=too-many-arguments
|
|
|
|
def __init__(self, logger, domain, hass,
|
2016-06-12 00:43:13 +00:00
|
|
|
scan_interval=DEFAULT_SCAN_INTERVAL, group_name=None):
|
2016-01-31 02:55:52 +00:00
|
|
|
"""Initialize an entity component."""
|
2015-03-22 01:49:30 +00:00
|
|
|
self.logger = logger
|
|
|
|
self.hass = hass
|
|
|
|
|
|
|
|
self.domain = domain
|
|
|
|
self.entity_id_format = domain + '.{}'
|
|
|
|
self.scan_interval = scan_interval
|
|
|
|
self.group_name = group_name
|
|
|
|
|
|
|
|
self.entities = {}
|
|
|
|
self.group = None
|
|
|
|
|
2015-05-15 04:36:12 +00:00
|
|
|
self.config = None
|
|
|
|
|
2016-09-04 15:15:52 +00:00
|
|
|
self._platforms = {
|
|
|
|
'core': EntityPlatform(self, self.scan_interval, None),
|
|
|
|
}
|
2016-10-16 16:35:46 +00:00
|
|
|
self.async_add_entities = self._platforms['core'].async_add_entities
|
2016-09-04 15:15:52 +00:00
|
|
|
self.add_entities = self._platforms['core'].add_entities
|
2016-01-31 08:55:46 +00:00
|
|
|
|
2015-03-22 01:49:30 +00:00
|
|
|
def setup(self, config):
|
2016-03-07 22:39:52 +00:00
|
|
|
"""Set up a full entity component.
|
2016-01-31 02:55:52 +00:00
|
|
|
|
|
|
|
Loads the platforms from the config and will listen for supported
|
|
|
|
discovered platforms.
|
2015-03-22 01:49:30 +00:00
|
|
|
"""
|
2016-10-16 16:35:46 +00:00
|
|
|
run_coroutine_threadsafe(
|
|
|
|
self.async_setup(config), self.hass.loop
|
|
|
|
).result()
|
|
|
|
|
|
|
|
@asyncio.coroutine
|
|
|
|
def async_setup(self, config):
|
|
|
|
"""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-15 04:36:12 +00:00
|
|
|
self.config = config
|
|
|
|
|
2015-03-22 01:49:30 +00:00
|
|
|
# Look in config for Domain, Domain 2, Domain 3 etc and load them
|
2016-10-16 16:35:46 +00:00
|
|
|
tasks = []
|
2016-03-28 01:48:51 +00:00
|
|
|
for p_type, p_config in config_per_platform(config, self.domain):
|
2016-10-16 16:35:46 +00:00
|
|
|
tasks.append(self._async_setup_platform(p_type, p_config))
|
|
|
|
|
|
|
|
yield from asyncio.gather(*tasks, loop=self.hass.loop)
|
2015-03-22 01:49:30 +00:00
|
|
|
|
2016-05-12 04:58:22 +00:00
|
|
|
# Generic discovery listener for loading platform dynamically
|
|
|
|
# Refer to: homeassistant.components.discovery.load_platform()
|
2016-10-16 16:35:46 +00:00
|
|
|
@callback
|
2016-06-12 00:43:13 +00:00
|
|
|
def component_platform_discovered(platform, info):
|
2016-05-12 04:58:22 +00:00
|
|
|
"""Callback to load a platform."""
|
2016-10-16 16:35:46 +00:00
|
|
|
self.hass.loop.create_task(
|
|
|
|
self._async_setup_platform(platform, {}, info))
|
2016-06-12 00:43:13 +00:00
|
|
|
|
2016-10-16 16:35:46 +00:00
|
|
|
discovery.async_listen_platform(
|
|
|
|
self.hass, self.domain, component_platform_discovered)
|
2016-05-10 05:48:03 +00:00
|
|
|
|
2015-03-22 01:49:30 +00:00
|
|
|
def extract_from_service(self, service):
|
2016-03-07 22:39:52 +00:00
|
|
|
"""Extract all known entities from a service call.
|
2016-01-31 02:55:52 +00:00
|
|
|
|
|
|
|
Will return all entities if no entities specified in call.
|
|
|
|
Will return an empty list if entities specified but unknown.
|
2015-03-22 01:49:30 +00:00
|
|
|
"""
|
2016-10-16 16:35:46 +00:00
|
|
|
return run_callback_threadsafe(
|
|
|
|
self.hass.loop, self.async_extract_from_service, service
|
|
|
|
).result()
|
|
|
|
|
|
|
|
def async_extract_from_service(self, service):
|
|
|
|
"""Extract all known entities from a service call.
|
|
|
|
|
|
|
|
Will return all entities if no entities specified in call.
|
|
|
|
Will return an empty list if entities specified but unknown.
|
|
|
|
|
|
|
|
This method must be run in the event loop.
|
|
|
|
"""
|
|
|
|
if ATTR_ENTITY_ID not in service.data:
|
|
|
|
return list(self.entities.values())
|
2015-11-28 23:55:01 +00:00
|
|
|
|
2016-10-16 16:35:46 +00:00
|
|
|
return [self.entities[entity_id] for entity_id
|
|
|
|
in extract_entity_ids(self.hass, service)
|
|
|
|
if entity_id in self.entities]
|
2015-03-22 01:49:30 +00:00
|
|
|
|
2016-10-16 16:35:46 +00:00
|
|
|
@asyncio.coroutine
|
|
|
|
def _async_setup_platform(self, platform_type, platform_config,
|
|
|
|
discovery_info=None):
|
|
|
|
"""Setup a platform for this component.
|
|
|
|
|
|
|
|
This method must be run in the event loop.
|
|
|
|
"""
|
|
|
|
platform = yield from self.hass.loop.run_in_executor(
|
|
|
|
None, prepare_setup_platform, self.hass, self.config, self.domain,
|
|
|
|
platform_type
|
|
|
|
)
|
2015-03-22 01:49:30 +00:00
|
|
|
|
2016-04-18 05:07:53 +00:00
|
|
|
if platform is None:
|
|
|
|
return
|
|
|
|
|
2016-04-03 03:10:57 +00:00
|
|
|
# Config > Platform > Component
|
2016-09-04 15:15:52 +00:00
|
|
|
scan_interval = (platform_config.get(CONF_SCAN_INTERVAL) or
|
|
|
|
getattr(platform, 'SCAN_INTERVAL', None) or
|
|
|
|
self.scan_interval)
|
2016-04-23 04:34:49 +00:00
|
|
|
entity_namespace = platform_config.get(CONF_ENTITY_NAMESPACE)
|
2015-03-22 01:49:30 +00:00
|
|
|
|
2016-09-04 15:15:52 +00:00
|
|
|
key = (platform_type, scan_interval, entity_namespace)
|
|
|
|
|
|
|
|
if key not in self._platforms:
|
|
|
|
self._platforms[key] = EntityPlatform(self, scan_interval,
|
|
|
|
entity_namespace)
|
|
|
|
entity_platform = self._platforms[key]
|
|
|
|
|
2015-03-22 01:49:30 +00:00
|
|
|
try:
|
2016-10-16 16:35:46 +00:00
|
|
|
if getattr(platform, 'async_setup_platform', None):
|
|
|
|
yield from platform.async_setup_platform(
|
|
|
|
self.hass, platform_config,
|
|
|
|
entity_platform.async_add_entities, discovery_info
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
yield from self.hass.loop.run_in_executor(
|
|
|
|
None, platform.setup_platform, self.hass, platform_config,
|
|
|
|
entity_platform.add_entities, discovery_info
|
|
|
|
)
|
2016-04-03 03:10:57 +00:00
|
|
|
|
|
|
|
self.hass.config.components.append(
|
|
|
|
'{}.{}'.format(self.domain, platform_type))
|
2015-03-22 05:21:57 +00:00
|
|
|
except Exception: # pylint: disable=broad-except
|
|
|
|
self.logger.exception(
|
2015-05-12 05:23:38 +00:00
|
|
|
'Error while setting up platform %s', platform_type)
|
2015-09-10 06:37:15 +00:00
|
|
|
|
2016-04-23 04:34:49 +00:00
|
|
|
def add_entity(self, entity, platform=None):
|
2016-01-31 08:55:46 +00:00
|
|
|
"""Add entity to component."""
|
2016-10-16 16:35:46 +00:00
|
|
|
return run_coroutine_threadsafe(
|
|
|
|
self.async_add_entity(entity, platform), self.hass.loop
|
|
|
|
).result()
|
|
|
|
|
|
|
|
@asyncio.coroutine
|
|
|
|
def async_add_entity(self, entity, platform=None):
|
|
|
|
"""Add entity to component.
|
|
|
|
|
|
|
|
This method must be run in the event loop.
|
|
|
|
"""
|
2016-01-31 08:55:46 +00:00
|
|
|
if entity is None or entity in self.entities.values():
|
|
|
|
return False
|
|
|
|
|
|
|
|
entity.hass = self.hass
|
|
|
|
|
|
|
|
if getattr(entity, 'entity_id', None) is None:
|
2016-04-23 04:34:49 +00:00
|
|
|
object_id = entity.name or DEVICE_DEFAULT_NAME
|
|
|
|
|
|
|
|
if platform is not None and platform.entity_namespace is not None:
|
|
|
|
object_id = '{} {}'.format(platform.entity_namespace,
|
|
|
|
object_id)
|
|
|
|
|
2016-10-16 16:35:46 +00:00
|
|
|
entity.entity_id = async_generate_entity_id(
|
2016-04-23 04:34:49 +00:00
|
|
|
self.entity_id_format, object_id,
|
2016-01-31 08:55:46 +00:00
|
|
|
self.entities.keys())
|
|
|
|
|
|
|
|
self.entities[entity.entity_id] = entity
|
2016-10-16 16:35:46 +00:00
|
|
|
yield from entity.async_update_ha_state()
|
2016-01-31 08:55:46 +00:00
|
|
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
def update_group(self):
|
|
|
|
"""Set up and/or update component group."""
|
2016-10-16 16:35:46 +00:00
|
|
|
run_callback_threadsafe(
|
|
|
|
self.hass.loop, self.async_update_group).result()
|
|
|
|
|
|
|
|
@asyncio.coroutine
|
|
|
|
def async_update_group(self):
|
|
|
|
"""Set up and/or update component group.
|
|
|
|
|
|
|
|
This method must be run in the event loop.
|
|
|
|
"""
|
2016-01-31 08:55:46 +00:00
|
|
|
if self.group is None and self.group_name is not None:
|
2016-09-07 13:59:16 +00:00
|
|
|
group = get_component('group')
|
2016-10-16 16:35:46 +00:00
|
|
|
self.group = yield from group.Group.async_create_group(
|
|
|
|
self.hass, self.group_name, self.entities.keys(),
|
|
|
|
user_defined=False
|
|
|
|
)
|
|
|
|
elif self.group is not None:
|
|
|
|
yield from self.group.async_update_tracked_entity_ids(
|
|
|
|
self.entities.keys())
|
2016-01-31 08:55:46 +00:00
|
|
|
|
2016-09-04 15:15:52 +00:00
|
|
|
def reset(self):
|
|
|
|
"""Remove entities and reset the entity component to initial values."""
|
2016-10-16 16:35:46 +00:00
|
|
|
run_coroutine_threadsafe(self.async_reset(), self.hass.loop).result()
|
2016-09-04 15:15:52 +00:00
|
|
|
|
2016-10-16 16:35:46 +00:00
|
|
|
@asyncio.coroutine
|
|
|
|
def async_reset(self):
|
|
|
|
"""Remove entities and reset the entity component to initial values.
|
2016-09-04 15:15:52 +00:00
|
|
|
|
2016-10-16 16:35:46 +00:00
|
|
|
This method must be run in the event loop.
|
|
|
|
"""
|
|
|
|
tasks = [platform.async_reset() for platform
|
|
|
|
in self._platforms.values()]
|
|
|
|
|
|
|
|
yield from asyncio.gather(*tasks, loop=self.hass.loop)
|
|
|
|
|
|
|
|
self._platforms = {
|
|
|
|
'core': self._platforms['core']
|
|
|
|
}
|
|
|
|
self.entities = {}
|
|
|
|
self.config = None
|
|
|
|
|
|
|
|
if self.group is not None:
|
|
|
|
yield from self.group.async_stop()
|
|
|
|
self.group = None
|
2016-09-04 15:15:52 +00:00
|
|
|
|
2016-09-07 13:59:16 +00:00
|
|
|
def prepare_reload(self):
|
|
|
|
"""Prepare reloading this entity component."""
|
|
|
|
try:
|
|
|
|
path = conf_util.find_config_file(self.hass.config.config_dir)
|
|
|
|
conf = conf_util.load_yaml_config_file(path)
|
|
|
|
except HomeAssistantError as err:
|
|
|
|
self.logger.error(err)
|
|
|
|
return None
|
|
|
|
|
|
|
|
conf = prepare_setup_component(self.hass, conf, self.domain)
|
|
|
|
|
|
|
|
if conf is None:
|
|
|
|
return None
|
|
|
|
|
|
|
|
self.reset()
|
|
|
|
return conf
|
|
|
|
|
2016-10-16 16:35:46 +00:00
|
|
|
@asyncio.coroutine
|
|
|
|
def async_prepare_reload(self):
|
|
|
|
"""Prepare reloading this entity component.
|
|
|
|
|
|
|
|
This method must be run in the event loop.
|
|
|
|
"""
|
|
|
|
conf = yield from self.hass.loop.run_in_executor(
|
|
|
|
None, self.prepare_reload
|
|
|
|
)
|
|
|
|
return conf
|
|
|
|
|
2016-01-31 08:55:46 +00:00
|
|
|
|
|
|
|
class EntityPlatform(object):
|
2016-10-16 16:35:46 +00:00
|
|
|
"""Keep track of entities for a single platform and stay in loop."""
|
2016-01-31 08:55:46 +00:00
|
|
|
|
|
|
|
# pylint: disable=too-few-public-methods
|
2016-04-23 04:34:49 +00:00
|
|
|
def __init__(self, component, scan_interval, entity_namespace):
|
2016-03-07 22:39:52 +00:00
|
|
|
"""Initalize the entity platform."""
|
2016-01-31 08:55:46 +00:00
|
|
|
self.component = component
|
|
|
|
self.scan_interval = scan_interval
|
2016-04-23 04:34:49 +00:00
|
|
|
self.entity_namespace = entity_namespace
|
2016-01-31 08:55:46 +00:00
|
|
|
self.platform_entities = []
|
2016-10-16 16:35:46 +00:00
|
|
|
self._async_unsub_polling = None
|
2016-01-31 08:55:46 +00:00
|
|
|
|
|
|
|
def add_entities(self, new_entities):
|
|
|
|
"""Add entities for a single platform."""
|
2016-10-16 16:35:46 +00:00
|
|
|
run_coroutine_threadsafe(
|
|
|
|
self.async_add_entities(new_entities), self.component.hass.loop
|
|
|
|
).result()
|
2016-01-31 08:55:46 +00:00
|
|
|
|
2016-10-16 16:35:46 +00:00
|
|
|
@asyncio.coroutine
|
|
|
|
def async_add_entities(self, new_entities):
|
|
|
|
"""Add entities for a single platform async.
|
2016-01-31 08:55:46 +00:00
|
|
|
|
2016-10-16 16:35:46 +00:00
|
|
|
This method must be run in the event loop.
|
|
|
|
"""
|
2016-10-16 22:35:57 +00:00
|
|
|
tasks = [self._async_process_entity(entity) for entity in new_entities]
|
2016-01-31 08:55:46 +00:00
|
|
|
|
2016-10-16 22:35:57 +00:00
|
|
|
yield from asyncio.gather(*tasks, loop=self.component.hass.loop)
|
2016-10-16 16:35:46 +00:00
|
|
|
yield from self.component.async_update_group()
|
2016-01-31 08:55:46 +00:00
|
|
|
|
2016-10-16 16:35:46 +00:00
|
|
|
if self._async_unsub_polling is not None or \
|
|
|
|
not any(entity.should_poll for entity
|
|
|
|
in self.platform_entities):
|
|
|
|
return
|
2016-09-04 15:15:52 +00:00
|
|
|
|
2016-10-16 16:35:46 +00:00
|
|
|
self._async_unsub_polling = async_track_utc_time_change(
|
|
|
|
self.component.hass, self._update_entity_states,
|
|
|
|
second=range(0, 60, self.scan_interval))
|
|
|
|
|
2016-10-16 22:35:57 +00:00
|
|
|
@asyncio.coroutine
|
|
|
|
def _async_process_entity(self, new_entity):
|
|
|
|
"""Add entities to StateMachine."""
|
|
|
|
ret = yield from self.component.async_add_entity(new_entity, self)
|
|
|
|
if ret:
|
|
|
|
self.platform_entities.append(new_entity)
|
|
|
|
|
2016-10-16 16:35:46 +00:00
|
|
|
@asyncio.coroutine
|
|
|
|
def async_reset(self):
|
|
|
|
"""Remove all entities and reset data.
|
|
|
|
|
|
|
|
This method must be run in the event loop.
|
|
|
|
"""
|
|
|
|
tasks = [entity.async_remove() for entity in self.platform_entities]
|
|
|
|
|
|
|
|
yield from asyncio.gather(*tasks, loop=self.component.hass.loop)
|
|
|
|
|
|
|
|
if self._async_unsub_polling is not None:
|
|
|
|
self._async_unsub_polling()
|
|
|
|
self._async_unsub_polling = None
|
|
|
|
|
|
|
|
@callback
|
2016-01-31 08:55:46 +00:00
|
|
|
def _update_entity_states(self, now):
|
2016-10-16 16:35:46 +00:00
|
|
|
"""Update the states of all the polling entities.
|
|
|
|
|
|
|
|
This method must be run in the event loop.
|
|
|
|
"""
|
|
|
|
for entity in self.platform_entities:
|
|
|
|
if entity.should_poll:
|
|
|
|
self.component.hass.loop.create_task(
|
|
|
|
entity.async_update_ha_state(True)
|
|
|
|
)
|