diff --git a/homeassistant/components/light/__init__.py b/homeassistant/components/light/__init__.py index 64f54c3d66a..352cc500a9b 100644 --- a/homeassistant/components/light/__init__.py +++ b/homeassistant/components/light/__init__.py @@ -163,40 +163,23 @@ def setup(hass, config): 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: 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 def update_lights_state(now): """ Update the states of all the lights. """ - for light in lights: + for light in lights.values(): light.update_ha_state(hass) update_lights_state(None) # Track all lights in a 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): """ Hande a turn light on or off service call. """ @@ -204,12 +187,12 @@ def setup(hass, config): dat = service.data # Convert the entity ids to valid light ids - lights = [ent_to_light[entity_id] for entity_id - in extract_entity_ids(hass, service) - if entity_id in ent_to_light] + target_lights = [lights[entity_id] for entity_id + in extract_entity_ids(hass, service) + if entity_id in lights] - if not lights: - lights = list(ent_to_light.values()) + if not target_lights: + target_lights = lights.values() params = {} @@ -219,7 +202,7 @@ def setup(hass, config): params[ATTR_TRANSITION] = transition if service.service == SERVICE_TURN_OFF: - for light in lights: + for light in target_lights: # pylint: disable=star-args light.turn_off(**params) @@ -277,11 +260,11 @@ def setup(hass, config): elif dat[ATTR_FLASH] == FLASH_LONG: params[ATTR_FLASH] = FLASH_LONG - for light in lights: + for light in target_lights: # pylint: disable=star-args light.turn_on(**params) - for light in lights: + for light in target_lights: light.update_ha_state(hass, True) # Update light state every 30 seconds diff --git a/homeassistant/components/switch/__init__.py b/homeassistant/components/switch/__init__.py index c2d110393f1..4c687b31ef6 100644 --- a/homeassistant/components/switch/__init__.py +++ b/homeassistant/components/switch/__init__.py @@ -55,30 +55,12 @@ def setup(hass, config): """ Track states and offer events for switches. """ 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: 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 @util.Throttle(MIN_TIME_BETWEEN_SCANS) def update_states(now): @@ -86,21 +68,21 @@ def setup(hass, config): logger.info("Updating switch states") - for switch in switches: + for switch in switches.values(): switch.update_ha_state(hass) update_states(None) def handle_switch_service(service): """ Handles calls to the switch services. """ - devices = [ent_to_switch[entity_id] for entity_id - in extract_entity_ids(hass, service) - if entity_id in ent_to_switch] + target_switches = [switches[entity_id] for entity_id + in extract_entity_ids(hass, service) + if entity_id in switches] - if not devices: - devices = switches + if not target_switches: + target_switches = switches.values() - for switch in devices: + for switch in target_switches: if service.service == SERVICE_TURN_ON: switch.turn_on() else: @@ -110,7 +92,7 @@ def setup(hass, config): # Track all switches in a group group.setup_group(hass, GROUP_NAME_ALL_SWITCHES, - ent_to_switch.keys(), False) + switches.keys(), False) # Update state every 30 seconds hass.track_time_change(update_states, second=[0, 30]) diff --git a/homeassistant/helpers.py b/homeassistant/helpers.py index cdf645cb75f..8bd69d0b1a0 100644 --- a/homeassistant/helpers.py +++ b/homeassistant/helpers.py @@ -8,6 +8,7 @@ from homeassistant import NoEntitySpecifiedError from homeassistant.loader import get_component from homeassistant.const import ( 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): @@ -112,7 +113,9 @@ def config_per_platform(config, domain, logger): 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. Loads different platforms and retrieve domains. """ devices = [] @@ -146,11 +149,30 @@ def platform_devices_from_config(config, domain, hass, logger): if len(devices) == 0: 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): - """ ABC for devices that can be turned on and off. """ +class Device(object): + """ ABC for Home Assistant devices. """ # pylint: disable=no-self-use entity_id = None @@ -159,17 +181,9 @@ class ToggleDevice(object): """ Returns the name of the device if any. """ return None - 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 + def get_state(self): + """ Returns state of the device. """ + return "Unknown" def get_state_attributes(self): """ Returns optional state attributes. """ @@ -191,7 +205,26 @@ class ToggleDevice(object): if force_refresh: self.update() - state = STATE_ON if self.is_on() else STATE_OFF - - return hass.states.set(self.entity_id, state, + return hass.states.set(self.entity_id, self.get_state(), 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