2016-03-09 22:49:54 +00:00
|
|
|
"""Helpers for components that manage entities."""
|
2015-11-28 23:55:01 +00:00
|
|
|
from threading import Lock
|
|
|
|
|
2015-05-12 05:23:20 +00:00
|
|
|
from homeassistant.bootstrap import prepare_setup_platform
|
2016-02-19 05:27:50 +00:00
|
|
|
from homeassistant.components import discovery, group
|
|
|
|
from homeassistant.const import ATTR_ENTITY_ID, CONF_SCAN_INTERVAL
|
2016-01-24 07:00:46 +00:00
|
|
|
from homeassistant.helpers import config_per_platform
|
|
|
|
from homeassistant.helpers.entity import generate_entity_id
|
2015-08-03 15:08:13 +00:00
|
|
|
from homeassistant.helpers.event import track_utc_time_change
|
2016-01-24 06:57:14 +00:00
|
|
|
from homeassistant.helpers.service import extract_entity_ids
|
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,
|
|
|
|
scan_interval=DEFAULT_SCAN_INTERVAL,
|
|
|
|
discovery_platforms=None, 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.discovery_platforms = discovery_platforms
|
|
|
|
self.group_name = group_name
|
|
|
|
|
|
|
|
self.entities = {}
|
|
|
|
self.group = None
|
|
|
|
self.is_polling = False
|
|
|
|
|
2015-05-15 04:36:12 +00:00
|
|
|
self.config = None
|
2015-11-28 23:55:01 +00:00
|
|
|
self.lock = Lock()
|
2015-05-15 04:36:12 +00:00
|
|
|
|
2016-01-31 08:55:46 +00:00
|
|
|
self.add_entities = EntityPlatform(self,
|
|
|
|
self.scan_interval).add_entities
|
|
|
|
|
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
|
|
|
"""
|
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
|
|
|
|
for p_type, p_config in \
|
|
|
|
config_per_platform(config, self.domain, self.logger):
|
|
|
|
|
2015-05-15 04:36:12 +00:00
|
|
|
self._setup_platform(p_type, p_config)
|
2015-03-22 01:49:30 +00:00
|
|
|
|
|
|
|
if self.discovery_platforms:
|
2016-01-31 02:55:52 +00:00
|
|
|
discovery.listen(
|
|
|
|
self.hass, self.discovery_platforms.keys(),
|
|
|
|
lambda service, info:
|
|
|
|
self._setup_platform(self.discovery_platforms[service], {},
|
|
|
|
info))
|
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
|
|
|
"""
|
2015-11-28 23:55:01 +00:00
|
|
|
with self.lock:
|
|
|
|
if ATTR_ENTITY_ID not in service.data:
|
|
|
|
return list(self.entities.values())
|
|
|
|
|
2015-03-22 01:49:30 +00:00
|
|
|
return [self.entities[entity_id] for entity_id
|
|
|
|
in extract_entity_ids(self.hass, service)
|
|
|
|
if entity_id in self.entities]
|
|
|
|
|
2015-05-15 04:36:12 +00:00
|
|
|
def _setup_platform(self, platform_type, platform_config,
|
2015-05-12 05:23:20 +00:00
|
|
|
discovery_info=None):
|
2016-01-31 02:55:52 +00:00
|
|
|
"""Setup a platform for this component."""
|
2015-05-12 05:23:20 +00:00
|
|
|
platform = prepare_setup_platform(
|
2015-05-15 04:36:12 +00:00
|
|
|
self.hass, self.config, self.domain, platform_type)
|
2015-03-22 01:49:30 +00:00
|
|
|
|
|
|
|
if platform is None:
|
|
|
|
return
|
|
|
|
|
|
|
|
try:
|
2016-01-31 08:55:46 +00:00
|
|
|
# Config > Platform > Component
|
|
|
|
scan_interval = platform_config.get(
|
|
|
|
CONF_SCAN_INTERVAL,
|
|
|
|
getattr(platform, 'SCAN_INTERVAL', self.scan_interval))
|
2015-03-22 01:49:30 +00:00
|
|
|
platform.setup_platform(
|
2016-01-31 08:55:46 +00:00
|
|
|
self.hass, platform_config,
|
|
|
|
EntityPlatform(self, scan_interval).add_entities,
|
|
|
|
discovery_info)
|
|
|
|
platform_name = '{}.{}'.format(self.domain, platform_type)
|
|
|
|
self.hass.config.components.append(platform_name)
|
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
|
|
|
return
|
|
|
|
|
2016-01-31 08:55:46 +00:00
|
|
|
def add_entity(self, entity):
|
|
|
|
"""Add entity to component."""
|
|
|
|
if entity is None or entity in self.entities.values():
|
|
|
|
return False
|
|
|
|
|
|
|
|
entity.hass = self.hass
|
|
|
|
|
|
|
|
if getattr(entity, 'entity_id', None) is None:
|
|
|
|
entity.entity_id = generate_entity_id(
|
|
|
|
self.entity_id_format, entity.name,
|
|
|
|
self.entities.keys())
|
|
|
|
|
|
|
|
self.entities[entity.entity_id] = entity
|
|
|
|
entity.update_ha_state()
|
|
|
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
def update_group(self):
|
|
|
|
"""Set up and/or update component group."""
|
|
|
|
if self.group is None and self.group_name is not None:
|
|
|
|
self.group = group.Group(self.hass, self.group_name,
|
|
|
|
user_defined=False)
|
|
|
|
|
|
|
|
if self.group is not None:
|
|
|
|
self.group.update_tracked_entity_ids(self.entities.keys())
|
|
|
|
|
|
|
|
|
|
|
|
class EntityPlatform(object):
|
|
|
|
"""Keep track of entities for a single platform."""
|
|
|
|
|
|
|
|
# pylint: disable=too-few-public-methods
|
|
|
|
def __init__(self, component, scan_interval):
|
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
|
|
|
|
self.platform_entities = []
|
|
|
|
self.is_polling = False
|
|
|
|
|
|
|
|
def add_entities(self, new_entities):
|
|
|
|
"""Add entities for a single platform."""
|
|
|
|
with self.component.lock:
|
|
|
|
for entity in new_entities:
|
|
|
|
if self.component.add_entity(entity):
|
|
|
|
self.platform_entities.append(entity)
|
|
|
|
|
|
|
|
self.component.update_group()
|
|
|
|
|
|
|
|
if self.is_polling or \
|
|
|
|
not any(entity.should_poll for entity
|
|
|
|
in self.platform_entities):
|
|
|
|
return
|
|
|
|
|
|
|
|
self.is_polling = True
|
|
|
|
|
|
|
|
track_utc_time_change(
|
|
|
|
self.component.hass, self._update_entity_states,
|
|
|
|
second=range(0, 60, self.scan_interval))
|
|
|
|
|
|
|
|
def _update_entity_states(self, now):
|
|
|
|
"""Update the states of all the polling entities."""
|
|
|
|
with self.component.lock:
|
|
|
|
# We copy the entities because new entities might be detected
|
|
|
|
# during state update causing deadlocks.
|
|
|
|
entities = list(entity for entity in self.platform_entities
|
|
|
|
if entity.should_poll)
|
|
|
|
|
|
|
|
for entity in entities:
|
|
|
|
entity.update_ha_state(True)
|