mirror of
https://github.com/home-assistant/core.git
synced 2025-07-16 17:57:11 +00:00
first commit
This commit is contained in:
parent
88dc7a08c4
commit
fb449cbc82
84
homeassistant/components/scene/__init__.py
Normal file
84
homeassistant/components/scene/__init__.py
Normal file
@ -0,0 +1,84 @@
|
|||||||
|
"""
|
||||||
|
homeassistant.components.scene
|
||||||
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
|
Allows 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.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):
|
||||||
|
""" Sets up scenes. """
|
||||||
|
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
for entry in config:
|
||||||
|
if DOMAIN in entry:
|
||||||
|
if not any(CONF_PLATFORM in key for key in config[entry]):
|
||||||
|
config[entry] = {'platform': 'homeassistant', 'config': config[entry]}
|
||||||
|
|
||||||
|
component = EntityComponent(logger, DOMAIN, hass)
|
||||||
|
|
||||||
|
component.setup(config)
|
||||||
|
|
||||||
|
def handle_scene_service(service):
|
||||||
|
""" Handles 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):
|
||||||
|
return False
|
||||||
|
|
||||||
|
@property
|
||||||
|
def name(self):
|
||||||
|
raise NotImplementedError
|
||||||
|
|
||||||
|
@property
|
||||||
|
def state(self):
|
||||||
|
return STATE
|
||||||
|
|
||||||
|
@property
|
||||||
|
def entity_ids(self):
|
||||||
|
""" Entity IDs part of this scene. """
|
||||||
|
return None
|
||||||
|
|
||||||
|
def activate(self):
|
||||||
|
""" Activates scene. Tries to get entities into requested state. """
|
||||||
|
raise NotImplementedError
|
@ -9,14 +9,12 @@ https://home-assistant.io/components/scene/
|
|||||||
import logging
|
import logging
|
||||||
from collections import namedtuple
|
from collections import namedtuple
|
||||||
|
|
||||||
|
from homeassistant.components.scene import Scene
|
||||||
from homeassistant.const import (
|
from homeassistant.const import (
|
||||||
ATTR_ENTITY_ID, SERVICE_TURN_ON, STATE_OFF, STATE_ON)
|
ATTR_ENTITY_ID, STATE_OFF, STATE_ON)
|
||||||
from homeassistant.core import State
|
from homeassistant.core import State
|
||||||
from homeassistant.helpers.entity import Entity
|
|
||||||
from homeassistant.helpers.entity_component import EntityComponent
|
|
||||||
from homeassistant.helpers.state import reproduce_state
|
from homeassistant.helpers.state import reproduce_state
|
||||||
|
|
||||||
DOMAIN = 'scene'
|
|
||||||
DEPENDENCIES = ['group']
|
DEPENDENCIES = ['group']
|
||||||
STATE = 'scening'
|
STATE = 'scening'
|
||||||
|
|
||||||
@ -25,41 +23,21 @@ CONF_ENTITIES = "entities"
|
|||||||
SceneConfig = namedtuple('SceneConfig', ['name', 'states'])
|
SceneConfig = namedtuple('SceneConfig', ['name', 'states'])
|
||||||
|
|
||||||
|
|
||||||
def activate(hass, entity_id=None):
|
# pylint: disable=unused-argument
|
||||||
""" Activate a scene. """
|
def setup_platform(hass, config, add_devices, discovery_info=None):
|
||||||
data = {}
|
|
||||||
|
|
||||||
if entity_id:
|
|
||||||
data[ATTR_ENTITY_ID] = entity_id
|
|
||||||
|
|
||||||
hass.services.call(DOMAIN, SERVICE_TURN_ON, data)
|
|
||||||
|
|
||||||
|
|
||||||
def setup(hass, config):
|
|
||||||
""" Sets up scenes. """
|
""" Sets up scenes. """
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
scene_configs = config.get(DOMAIN)
|
scene_configs = config.get("config")
|
||||||
|
|
||||||
if not isinstance(scene_configs, list) or \
|
if not isinstance(scene_configs, list) or \
|
||||||
any(not isinstance(item, dict) for item in scene_configs):
|
any(not isinstance(item, dict) for item in scene_configs):
|
||||||
logger.error('Scene config should be a list of dictionaries')
|
logger.error('Scene config should be a list of dictionaries')
|
||||||
return False
|
return False
|
||||||
|
|
||||||
component = EntityComponent(logger, DOMAIN, hass)
|
add_devices(HomeAssistantScene(hass, _process_config(scene_config))
|
||||||
|
for scene_config in scene_configs)
|
||||||
component.add_entities(Scene(hass, _process_config(scene_config))
|
|
||||||
for scene_config in scene_configs)
|
|
||||||
|
|
||||||
def handle_scene_service(service):
|
|
||||||
""" Handles 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
|
return True
|
||||||
|
|
||||||
@ -92,37 +70,23 @@ def _process_config(scene_config):
|
|||||||
return SceneConfig(name, states)
|
return SceneConfig(name, states)
|
||||||
|
|
||||||
|
|
||||||
class Scene(Entity):
|
class HomeAssistantScene(Scene):
|
||||||
""" A scene is a group of entities and the states we want them to be. """
|
""" A scene is a group of entities and the states we want them to be. """
|
||||||
|
|
||||||
def __init__(self, hass, scene_config):
|
def __init__(self, hass, scene_config):
|
||||||
self.hass = hass
|
self.hass = hass
|
||||||
self.scene_config = scene_config
|
self.scene_config = scene_config
|
||||||
|
|
||||||
self.update()
|
self.update()
|
||||||
|
|
||||||
@property
|
|
||||||
def should_poll(self):
|
|
||||||
return False
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def name(self):
|
def name(self):
|
||||||
return self.scene_config.name
|
return self.scene_config.name
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def state(self):
|
def device_state_attributes(self):
|
||||||
return STATE
|
|
||||||
|
|
||||||
@property
|
|
||||||
def entity_ids(self):
|
|
||||||
""" Entity IDs part of this scene. """
|
|
||||||
return self.scene_config.states.keys()
|
|
||||||
|
|
||||||
@property
|
|
||||||
def state_attributes(self):
|
|
||||||
""" Scene state attributes. """
|
""" Scene state attributes. """
|
||||||
return {
|
return {
|
||||||
ATTR_ENTITY_ID: list(self.entity_ids),
|
ATTR_ENTITY_ID: list(self.scene_config.states.keys()),
|
||||||
}
|
}
|
||||||
|
|
||||||
def activate(self):
|
def activate(self):
|
Loading…
x
Reference in New Issue
Block a user