MartinHjelmare c56701baaf Refactor reproduce_state for scene component
* Add tests to reach full coverage for helpers/state.py.
* Refactor reproduce_state function in helpers/state.py. Add two dicts,
	as global constants, service_attributes and service_to_state. Use
	these in combination with the dict of services per domain from
	ServiceRegistry, to find the correct service to use in a scene state
	change.
* Use break statement in for loop, to break if service was selected
	to update state, in preference to update state attributes, ie state
	update takes precedence.
* Add ATTR_CODE and ATTR_CODE_FORMAT in const. Import these in
	alarm_control_panel and lock platforms instead of making duplicate
	constants in multiple modules.
* Use ATTR_MEDIA_CONTENT_TYPE and ATTR_MEDIA_CONTENT_ID in media_player
	platform in SERVICE_PLAY_MEDIA and play_media methods, instead of
	'media_type' and 'media_id'.
* Fix PEP257 in modified files.
2016-03-09 18:52:05 +01:00

85 lines
2.2 KiB
Python

"""
Allow users to set and activate scenes.
For more details about this component, please refer to the documentation at
https://home-assistant.io/components/scene/
"""
import logging
from collections import namedtuple
from homeassistant.const import (
ATTR_ENTITY_ID, SERVICE_TURN_ON, CONF_PLATFORM)
from homeassistant.helpers import extract_domain_configs
from homeassistant.helpers.entity import Entity
from homeassistant.helpers.entity_component import EntityComponent
DOMAIN = 'scene'
DEPENDENCIES = ['group']
STATE = 'scening'
CONF_ENTITIES = "entities"
SceneConfig = namedtuple('SceneConfig', ['name', 'states'])
def activate(hass, entity_id=None):
"""Activate a scene."""
data = {}
if entity_id:
data[ATTR_ENTITY_ID] = entity_id
hass.services.call(DOMAIN, SERVICE_TURN_ON, data)
def setup(hass, config):
"""Setup scenes."""
logger = logging.getLogger(__name__)
# You are not allowed to mutate the original config so make a copy
config = dict(config)
for config_key in extract_domain_configs(config, DOMAIN):
platform_config = config[config_key]
if not isinstance(platform_config, list):
platform_config = [platform_config]
if not any(CONF_PLATFORM in entry for entry in platform_config):
platform_config = [{'platform': 'homeassistant', 'states': entry}
for entry in platform_config]
config[config_key] = platform_config
component = EntityComponent(logger, DOMAIN, hass)
component.setup(config)
def handle_scene_service(service):
"""Handle calls to the switch services."""
target_scenes = component.extract_from_service(service)
for scene in target_scenes:
scene.activate()
hass.services.register(DOMAIN, SERVICE_TURN_ON, handle_scene_service)
return True
class Scene(Entity):
"""A scene is a group of entities and the states we want them to be."""
@property
def should_poll(self):
"""No polling needed."""
return False
@property
def state(self):
"""Return the state of the scene."""
return STATE
def activate(self):
"""Activate scene. Try to get entities into requested state."""
raise NotImplementedError