Merge branch 'dev' into component-thermostat

* dev:
  Extracted a base HA device from ToggleDevice
This commit is contained in:
Paulus Schoutsen 2015-01-05 23:07:17 -08:00
commit 68b712adfd
3 changed files with 73 additions and 75 deletions

View File

@ -163,40 +163,23 @@ def setup(hass, config):
return False return False
lights = platform_devices_from_config(config, DOMAIN, hass, _LOGGER) lights = platform_devices_from_config(
config, DOMAIN, hass, ENTITY_ID_FORMAT, _LOGGER)
if not lights: if not lights:
return False return False
ent_to_light = {}
no_name_count = 1
for light in lights:
name = light.get_name()
if name is None:
name = "Light #{}".format(no_name_count)
no_name_count += 1
entity_id = util.ensure_unique_string(
ENTITY_ID_FORMAT.format(util.slugify(name)),
ent_to_light.keys())
light.entity_id = entity_id
ent_to_light[entity_id] = light
# pylint: disable=unused-argument # pylint: disable=unused-argument
def update_lights_state(now): def update_lights_state(now):
""" Update the states of all the lights. """ """ Update the states of all the lights. """
for light in lights: for light in lights.values():
light.update_ha_state(hass) light.update_ha_state(hass)
update_lights_state(None) update_lights_state(None)
# Track all lights in a group # Track all lights in a group
group.setup_group( group.setup_group(
hass, GROUP_NAME_ALL_LIGHTS, ent_to_light.keys(), False) hass, GROUP_NAME_ALL_LIGHTS, lights.keys(), False)
def handle_light_service(service): def handle_light_service(service):
""" Hande a turn light on or off service call. """ """ Hande a turn light on or off service call. """
@ -204,12 +187,12 @@ def setup(hass, config):
dat = service.data dat = service.data
# Convert the entity ids to valid light ids # Convert the entity ids to valid light ids
lights = [ent_to_light[entity_id] for entity_id target_lights = [lights[entity_id] for entity_id
in extract_entity_ids(hass, service) in extract_entity_ids(hass, service)
if entity_id in ent_to_light] if entity_id in lights]
if not lights: if not target_lights:
lights = list(ent_to_light.values()) target_lights = lights.values()
params = {} params = {}
@ -219,7 +202,7 @@ def setup(hass, config):
params[ATTR_TRANSITION] = transition params[ATTR_TRANSITION] = transition
if service.service == SERVICE_TURN_OFF: if service.service == SERVICE_TURN_OFF:
for light in lights: for light in target_lights:
# pylint: disable=star-args # pylint: disable=star-args
light.turn_off(**params) light.turn_off(**params)
@ -277,11 +260,11 @@ def setup(hass, config):
elif dat[ATTR_FLASH] == FLASH_LONG: elif dat[ATTR_FLASH] == FLASH_LONG:
params[ATTR_FLASH] = FLASH_LONG params[ATTR_FLASH] = FLASH_LONG
for light in lights: for light in target_lights:
# pylint: disable=star-args # pylint: disable=star-args
light.turn_on(**params) light.turn_on(**params)
for light in lights: for light in target_lights:
light.update_ha_state(hass, True) light.update_ha_state(hass, True)
# Update light state every 30 seconds # Update light state every 30 seconds

View File

@ -55,30 +55,12 @@ def setup(hass, config):
""" Track states and offer events for switches. """ """ Track states and offer events for switches. """
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
switches = platform_devices_from_config(config, DOMAIN, hass, logger) switches = platform_devices_from_config(
config, DOMAIN, hass, ENTITY_ID_FORMAT, logger)
if not switches: if not switches:
return False return False
# Setup a dict mapping entity IDs to devices
ent_to_switch = {}
no_name_count = 1
for switch in switches:
name = switch.get_name()
if name is None:
name = "Switch #{}".format(no_name_count)
no_name_count += 1
entity_id = util.ensure_unique_string(
ENTITY_ID_FORMAT.format(util.slugify(name)),
ent_to_switch.keys())
switch.entity_id = entity_id
ent_to_switch[entity_id] = switch
# pylint: disable=unused-argument # pylint: disable=unused-argument
@util.Throttle(MIN_TIME_BETWEEN_SCANS) @util.Throttle(MIN_TIME_BETWEEN_SCANS)
def update_states(now): def update_states(now):
@ -86,21 +68,21 @@ def setup(hass, config):
logger.info("Updating switch states") logger.info("Updating switch states")
for switch in switches: for switch in switches.values():
switch.update_ha_state(hass) switch.update_ha_state(hass)
update_states(None) update_states(None)
def handle_switch_service(service): def handle_switch_service(service):
""" Handles calls to the switch services. """ """ Handles calls to the switch services. """
devices = [ent_to_switch[entity_id] for entity_id target_switches = [switches[entity_id] for entity_id
in extract_entity_ids(hass, service) in extract_entity_ids(hass, service)
if entity_id in ent_to_switch] if entity_id in switches]
if not devices: if not target_switches:
devices = switches target_switches = switches.values()
for switch in devices: for switch in target_switches:
if service.service == SERVICE_TURN_ON: if service.service == SERVICE_TURN_ON:
switch.turn_on() switch.turn_on()
else: else:
@ -110,7 +92,7 @@ def setup(hass, config):
# Track all switches in a group # Track all switches in a group
group.setup_group(hass, GROUP_NAME_ALL_SWITCHES, group.setup_group(hass, GROUP_NAME_ALL_SWITCHES,
ent_to_switch.keys(), False) switches.keys(), False)
# Update state every 30 seconds # Update state every 30 seconds
hass.track_time_change(update_states, second=[0, 30]) hass.track_time_change(update_states, second=[0, 30])

View File

@ -8,6 +8,7 @@ from homeassistant import NoEntitySpecifiedError
from homeassistant.loader import get_component from homeassistant.loader import get_component
from homeassistant.const import ( from homeassistant.const import (
ATTR_ENTITY_ID, STATE_ON, STATE_OFF, CONF_PLATFORM, CONF_TYPE) ATTR_ENTITY_ID, STATE_ON, STATE_OFF, CONF_PLATFORM, CONF_TYPE)
from homeassistant.util import ensure_unique_string, slugify
def extract_entity_ids(hass, service): def extract_entity_ids(hass, service):
@ -112,7 +113,9 @@ def config_per_platform(config, domain, logger):
config_key = "{} {}".format(domain, found) config_key = "{} {}".format(domain, found)
def platform_devices_from_config(config, domain, hass, logger): def platform_devices_from_config(config, domain, hass,
entity_id_format, logger):
""" Parses the config for specified domain. """ Parses the config for specified domain.
Loads different platforms and retrieve domains. """ Loads different platforms and retrieve domains. """
devices = [] devices = []
@ -146,11 +149,30 @@ def platform_devices_from_config(config, domain, hass, logger):
if len(devices) == 0: if len(devices) == 0:
logger.error("No devices found for %s", domain) logger.error("No devices found for %s", domain)
return devices # Setup entity IDs for each device
no_name_count = 1
device_dict = {}
for device in devices:
name = device.get_name()
if name is None:
name = "{} #{}".format(domain, no_name_count)
no_name_count += 1
entity_id = ensure_unique_string(
entity_id_format.format(slugify(name)),
device_dict.keys())
device.entity_id = entity_id
device_dict[entity_id] = device
return device_dict
class ToggleDevice(object): class Device(object):
""" ABC for devices that can be turned on and off. """ """ ABC for Home Assistant devices. """
# pylint: disable=no-self-use # pylint: disable=no-self-use
entity_id = None entity_id = None
@ -159,17 +181,9 @@ class ToggleDevice(object):
""" Returns the name of the device if any. """ """ Returns the name of the device if any. """
return None return None
def turn_on(self, **kwargs): def get_state(self):
""" Turn the device on. """ """ Returns state of the device. """
pass return "Unknown"
def turn_off(self, **kwargs):
""" Turn the device off. """
pass
def is_on(self):
""" True if device is on. """
return False
def get_state_attributes(self): def get_state_attributes(self):
""" Returns optional state attributes. """ """ Returns optional state attributes. """
@ -191,7 +205,26 @@ class ToggleDevice(object):
if force_refresh: if force_refresh:
self.update() self.update()
state = STATE_ON if self.is_on() else STATE_OFF return hass.states.set(self.entity_id, self.get_state(),
return hass.states.set(self.entity_id, state,
self.get_state_attributes()) self.get_state_attributes())
class ToggleDevice(Device):
""" ABC for devices that can be turned on and off. """
# pylint: disable=no-self-use
def get_state(self):
""" Returns the state. """
return STATE_ON if self.is_on() else STATE_OFF
def turn_on(self, **kwargs):
""" Turn the device on. """
pass
def turn_off(self, **kwargs):
""" Turn the device off. """
pass
def is_on(self):
""" True if device is on. """
return False