Add scene component
This commit is contained in:
1 parent
36feb7f50a
commit
19a43cea26
13 files changed
+382
-138
No files matched your search
@@ -7,6 +7,8 @@ from homeassistant.helpers import (
|
||||
from homeassistant.components import group, discovery
|
||||
from homeassistant.const import ATTR_ENTITY_ID
|
||||
|
||||
DEFAULT_SCAN_INTERVAL = 15
|
||||
|
||||
|
||||
class DeviceComponent(object):
|
||||
# pylint: disable=too-many-instance-attributes
|
||||
@@ -14,7 +16,8 @@ class DeviceComponent(object):
|
||||
"""
|
||||
Helper class that will help a device component manage its devices.
|
||||
"""
|
||||
def __init__(self, logger, domain, hass, scan_interval,
|
||||
def __init__(self, logger, domain, hass,
|
||||
scan_interval=DEFAULT_SCAN_INTERVAL,
|
||||
discovery_platforms=None, group_name=None):
|
||||
self.logger = logger
|
||||
self.hass = hass
|
||||
@@ -33,17 +36,8 @@ class DeviceComponent(object):
|
||||
"""
|
||||
Sets up a full device component:
|
||||
- Loads the platforms from the config
|
||||
- Will update devices on an interval
|
||||
- Will listen for supported discovered platforms
|
||||
"""
|
||||
|
||||
# only setup group if name is given
|
||||
if self.group_name is None:
|
||||
self.group = None
|
||||
else:
|
||||
self.group = group.Group(self.hass, self.group_name,
|
||||
user_defined=False)
|
||||
|
||||
# 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):
|
||||
@@ -54,6 +48,31 @@ class DeviceComponent(object):
|
||||
discovery.listen(self.hass, self.discovery_platforms.keys(),
|
||||
self._device_discovered)
|
||||
|
||||
def add_devices(self, new_devices):
|
||||
"""
|
||||
Takes in a list of new devices. For each device will see if it already
|
||||
exists. If not, will add it, set it up and push the first state.
|
||||
"""
|
||||
for device in new_devices:
|
||||
if device is not None and device not in self.devices.values():
|
||||
device.hass = self.hass
|
||||
|
||||
device.entity_id = generate_entity_id(
|
||||
self.entity_id_format, device.name, self.devices.keys())
|
||||
|
||||
self.devices[device.entity_id] = device
|
||||
|
||||
device.update_ha_state()
|
||||
|
||||
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.devices.keys())
|
||||
|
||||
self._start_polling()
|
||||
|
||||
def extract_from_service(self, service):
|
||||
"""
|
||||
Takes a service and extracts all known devices.
|
||||
@@ -81,27 +100,6 @@ class DeviceComponent(object):
|
||||
|
||||
self._setup_platform(self.discovery_platforms[service], {}, info)
|
||||
|
||||
def _add_devices(self, new_devices):
|
||||
"""
|
||||
Takes in a list of new devices. For each device will see if it already
|
||||
exists. If not, will add it, set it up and push the first state.
|
||||
"""
|
||||
for device in new_devices:
|
||||
if device is not None and device not in self.devices.values():
|
||||
device.hass = self.hass
|
||||
|
||||
device.entity_id = generate_entity_id(
|
||||
self.entity_id_format, device.name, self.devices.keys())
|
||||
|
||||
self.devices[device.entity_id] = device
|
||||
|
||||
device.update_ha_state()
|
||||
|
||||
if self.group is not None:
|
||||
self.group.update_tracked_entity_ids(self.devices.keys())
|
||||
|
||||
self._start_polling()
|
||||
|
||||
def _start_polling(self):
|
||||
""" Start polling device states if necessary. """
|
||||
if self.is_polling or \
|
||||
@@ -125,7 +123,7 @@ class DeviceComponent(object):
|
||||
|
||||
try:
|
||||
platform.setup_platform(
|
||||
self.hass, config, self._add_devices, discovery_info)
|
||||
self.hass, config, self.add_devices, discovery_info)
|
||||
except AttributeError:
|
||||
# Support old deprecated method for now - 3/1/2015
|
||||
if hasattr(platform, 'get_devices'):
|
||||
@@ -133,7 +131,7 @@ class DeviceComponent(object):
|
||||
"Please upgrade %s to return new devices using "
|
||||
"setup_platform. See %s/demo.py for an example.",
|
||||
platform_name, self.domain)
|
||||
self._add_devices(platform.get_devices(self.hass, config))
|
||||
self.add_devices(platform.get_devices(self.hass, config))
|
||||
|
||||
else:
|
||||
# AttributeError if setup_platform does not exist
|
||||
|
||||
Reference in new issue
Block a user