mirror of
https://github.com/home-assistant/core.git
synced 2025-07-23 21:27:38 +00:00
Merge pull request #514 from balloob/scene-turn-off-remove
Remove turning off scenes
This commit is contained in:
commit
925cde200f
@ -1,2 +1,2 @@
|
|||||||
""" DO NOT MODIFY. Auto-generated by build_frontend script """
|
""" DO NOT MODIFY. Auto-generated by build_frontend script """
|
||||||
VERSION = "c4722afa376379bc4457d54bb9a38cee"
|
VERSION = "90c41bfbaa56f9a1c88db27a54f7d36b"
|
||||||
|
File diff suppressed because one or more lines are too long
@ -1 +1 @@
|
|||||||
Subproject commit 3d6792691a3d6beae5d446a6fbeb83c9025d040d
|
Subproject commit 5973dd41715f7aca1bd03cdc0aa6625afb34c94a
|
@ -6,34 +6,34 @@ Allows users to set and activate scenes within Home Assistant.
|
|||||||
|
|
||||||
A scene is a set of states that describe how you want certain entities to be.
|
A scene is a set of states that describe how you want certain entities to be.
|
||||||
For example, light A should be red with 100 brightness. Light B should be on.
|
For example, light A should be red with 100 brightness. Light B should be on.
|
||||||
|
|
||||||
A scene is active if all states of the scene match the real states.
|
|
||||||
|
|
||||||
If a scene is manually activated it will store the previous state of the
|
|
||||||
entities. These will be restored when the state is deactivated manually.
|
|
||||||
|
|
||||||
If one of the enties that are being tracked change state on its own, the
|
|
||||||
old state will not be restored when it is being deactivated.
|
|
||||||
"""
|
"""
|
||||||
import logging
|
import logging
|
||||||
from collections import namedtuple
|
from collections import namedtuple
|
||||||
|
|
||||||
from homeassistant.core import State
|
from homeassistant.core import State
|
||||||
from homeassistant.helpers.event import track_state_change
|
from homeassistant.helpers.entity import Entity
|
||||||
from homeassistant.helpers.entity import ToggleEntity
|
|
||||||
from homeassistant.helpers.entity_component import EntityComponent
|
from homeassistant.helpers.entity_component import EntityComponent
|
||||||
from homeassistant.helpers.state import reproduce_state
|
from homeassistant.helpers.state import reproduce_state
|
||||||
from homeassistant.const import (
|
from homeassistant.const import (
|
||||||
ATTR_ENTITY_ID, STATE_OFF, STATE_ON, SERVICE_TURN_ON, SERVICE_TURN_OFF)
|
ATTR_ENTITY_ID, STATE_OFF, STATE_ON, SERVICE_TURN_ON)
|
||||||
|
|
||||||
DOMAIN = 'scene'
|
DOMAIN = 'scene'
|
||||||
DEPENDENCIES = ['group']
|
DEPENDENCIES = ['group']
|
||||||
|
STATE = 'scening'
|
||||||
ATTR_ACTIVE_REQUESTED = "active_requested"
|
|
||||||
|
|
||||||
CONF_ENTITIES = "entities"
|
CONF_ENTITIES = "entities"
|
||||||
|
|
||||||
SceneConfig = namedtuple('SceneConfig', ['name', 'states', 'fuzzy_match'])
|
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):
|
def setup(hass, config):
|
||||||
@ -43,8 +43,9 @@ def setup(hass, config):
|
|||||||
|
|
||||||
scene_configs = config.get(DOMAIN)
|
scene_configs = config.get(DOMAIN)
|
||||||
|
|
||||||
if not isinstance(scene_configs, list):
|
if not isinstance(scene_configs, list) or \
|
||||||
logger.error('Scene config should be a list of scenes')
|
any(not isinstance(item, dict) for item in scene_configs):
|
||||||
|
logger.error('Scene config should be a list of dictionaries')
|
||||||
return False
|
return False
|
||||||
|
|
||||||
component = EntityComponent(logger, DOMAIN, hass)
|
component = EntityComponent(logger, DOMAIN, hass)
|
||||||
@ -57,12 +58,8 @@ def setup(hass, config):
|
|||||||
target_scenes = component.extract_from_service(service)
|
target_scenes = component.extract_from_service(service)
|
||||||
|
|
||||||
for scene in target_scenes:
|
for scene in target_scenes:
|
||||||
if service.service == SERVICE_TURN_ON:
|
scene.activate()
|
||||||
scene.turn_on()
|
|
||||||
else:
|
|
||||||
scene.turn_off()
|
|
||||||
|
|
||||||
hass.services.register(DOMAIN, SERVICE_TURN_OFF, handle_scene_service)
|
|
||||||
hass.services.register(DOMAIN, SERVICE_TURN_ON, handle_scene_service)
|
hass.services.register(DOMAIN, SERVICE_TURN_ON, handle_scene_service)
|
||||||
|
|
||||||
return True
|
return True
|
||||||
@ -72,14 +69,6 @@ def _process_config(scene_config):
|
|||||||
""" Process passed in config into a format to work with. """
|
""" Process passed in config into a format to work with. """
|
||||||
name = scene_config.get('name')
|
name = scene_config.get('name')
|
||||||
|
|
||||||
fuzzy_match = scene_config.get('fuzzy_match')
|
|
||||||
if fuzzy_match:
|
|
||||||
# default to 1%
|
|
||||||
if isinstance(fuzzy_match, int):
|
|
||||||
fuzzy_match /= 100.0
|
|
||||||
else:
|
|
||||||
fuzzy_match = 0.01
|
|
||||||
|
|
||||||
states = {}
|
states = {}
|
||||||
c_entities = dict(scene_config.get(CONF_ENTITIES, {}))
|
c_entities = dict(scene_config.get(CONF_ENTITIES, {}))
|
||||||
|
|
||||||
@ -100,23 +89,16 @@ def _process_config(scene_config):
|
|||||||
|
|
||||||
states[entity_id.lower()] = State(entity_id, state, attributes)
|
states[entity_id.lower()] = State(entity_id, state, attributes)
|
||||||
|
|
||||||
return SceneConfig(name, states, fuzzy_match)
|
return SceneConfig(name, states)
|
||||||
|
|
||||||
|
|
||||||
class Scene(ToggleEntity):
|
class Scene(Entity):
|
||||||
""" 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.is_active = False
|
|
||||||
self.prev_states = None
|
|
||||||
self.ignore_updates = False
|
|
||||||
|
|
||||||
track_state_change(
|
|
||||||
self.hass, self.entity_ids, self.entity_state_changed)
|
|
||||||
|
|
||||||
self.update()
|
self.update()
|
||||||
|
|
||||||
@property
|
@property
|
||||||
@ -128,8 +110,8 @@ class Scene(ToggleEntity):
|
|||||||
return self.scene_config.name
|
return self.scene_config.name
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def is_on(self):
|
def state(self):
|
||||||
return self.is_active
|
return STATE
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def entity_ids(self):
|
def entity_ids(self):
|
||||||
@ -141,82 +123,8 @@ class Scene(ToggleEntity):
|
|||||||
""" Scene state attributes. """
|
""" Scene state attributes. """
|
||||||
return {
|
return {
|
||||||
ATTR_ENTITY_ID: list(self.entity_ids),
|
ATTR_ENTITY_ID: list(self.entity_ids),
|
||||||
ATTR_ACTIVE_REQUESTED: self.prev_states is not None,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
def turn_on(self):
|
def activate(self):
|
||||||
""" Activates scene. Tries to get entities into requested state. """
|
""" Activates scene. Tries to get entities into requested state. """
|
||||||
self.prev_states = tuple(self.hass.states.get(entity_id)
|
reproduce_state(self.hass, self.scene_config.states.values(), True)
|
||||||
for entity_id in self.entity_ids)
|
|
||||||
|
|
||||||
self._reproduce_state(self.scene_config.states.values())
|
|
||||||
|
|
||||||
def turn_off(self):
|
|
||||||
""" Deactivates scene and restores old states. """
|
|
||||||
if self.prev_states:
|
|
||||||
self._reproduce_state(self.prev_states)
|
|
||||||
self.prev_states = None
|
|
||||||
|
|
||||||
def entity_state_changed(self, entity_id, old_state, new_state):
|
|
||||||
""" Called when an entity part of this scene changes state. """
|
|
||||||
if self.ignore_updates:
|
|
||||||
return
|
|
||||||
|
|
||||||
# If new state is not what we expect, it can never be active
|
|
||||||
if self._state_as_requested(new_state):
|
|
||||||
self.update()
|
|
||||||
else:
|
|
||||||
self.is_active = False
|
|
||||||
self.prev_states = None
|
|
||||||
|
|
||||||
self.update_ha_state()
|
|
||||||
|
|
||||||
def update(self):
|
|
||||||
"""
|
|
||||||
Update if the scene is active.
|
|
||||||
|
|
||||||
Will look at each requested state and see if the current entity
|
|
||||||
has the same state and has at least the same attributes with the
|
|
||||||
same values. The real state can have more attributes.
|
|
||||||
"""
|
|
||||||
self.is_active = all(
|
|
||||||
self._state_as_requested(self.hass.states.get(entity_id))
|
|
||||||
for entity_id in self.entity_ids)
|
|
||||||
|
|
||||||
def _state_as_requested(self, cur_state):
|
|
||||||
""" Returns if given state is as requested. """
|
|
||||||
state = self.scene_config.states.get(cur_state and cur_state.entity_id)
|
|
||||||
|
|
||||||
return (cur_state is not None and state.state == cur_state.state and
|
|
||||||
all(self._compare_state_attribites(
|
|
||||||
value, cur_state.attributes.get(key))
|
|
||||||
for key, value in state.attributes.items()))
|
|
||||||
|
|
||||||
def _fuzzy_attribute_compare(self, attr_a, attr_b):
|
|
||||||
"""
|
|
||||||
Compare the attributes passed, use fuzzy logic if they are floats.
|
|
||||||
"""
|
|
||||||
|
|
||||||
if not (isinstance(attr_a, float) and isinstance(attr_b, float)):
|
|
||||||
return False
|
|
||||||
diff = abs(attr_a - attr_b) / (abs(attr_a) + abs(attr_b))
|
|
||||||
return diff <= self.scene_config.fuzzy_match
|
|
||||||
|
|
||||||
def _compare_state_attribites(self, attr1, attr2):
|
|
||||||
""" Compare the attributes passed, using fuzzy logic if specified. """
|
|
||||||
if attr1 == attr2:
|
|
||||||
return True
|
|
||||||
if not self.scene_config.fuzzy_match:
|
|
||||||
return False
|
|
||||||
if isinstance(attr1, list):
|
|
||||||
return all(self._fuzzy_attribute_compare(a, b)
|
|
||||||
for a, b in zip(attr1, attr2))
|
|
||||||
return self._fuzzy_attribute_compare(attr1, attr2)
|
|
||||||
|
|
||||||
def _reproduce_state(self, states):
|
|
||||||
""" Wraps reproduce state with Scence specific logic. """
|
|
||||||
self.ignore_updates = True
|
|
||||||
reproduce_state(self.hass, states, True)
|
|
||||||
self.ignore_updates = False
|
|
||||||
|
|
||||||
self.update_ha_state(True)
|
|
||||||
|
68
tests/components/test_scene.py
Normal file
68
tests/components/test_scene.py
Normal file
@ -0,0 +1,68 @@
|
|||||||
|
"""
|
||||||
|
tests.components.test_scene
|
||||||
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
|
|
||||||
|
Tests scene component.
|
||||||
|
"""
|
||||||
|
import unittest
|
||||||
|
|
||||||
|
from homeassistant import loader
|
||||||
|
from homeassistant.components import light, scene
|
||||||
|
|
||||||
|
from tests.common import get_test_home_assistant
|
||||||
|
|
||||||
|
|
||||||
|
class TestScene(unittest.TestCase):
|
||||||
|
""" Test scene component. """
|
||||||
|
|
||||||
|
def setUp(self): # pylint: disable=invalid-name
|
||||||
|
self.hass = get_test_home_assistant()
|
||||||
|
|
||||||
|
def tearDown(self): # pylint: disable=invalid-name
|
||||||
|
""" Stop down stuff we started. """
|
||||||
|
self.hass.stop()
|
||||||
|
|
||||||
|
def test_config_not_list(self):
|
||||||
|
self.assertFalse(scene.setup(self.hass, {
|
||||||
|
'scene': {'some': 'dict'}
|
||||||
|
}))
|
||||||
|
|
||||||
|
def test_config_no_dict_in_list(self):
|
||||||
|
self.assertFalse(scene.setup(self.hass, {
|
||||||
|
'scene': [[]]
|
||||||
|
}))
|
||||||
|
|
||||||
|
def test_activate_scene(self):
|
||||||
|
test_light = loader.get_component('light.test')
|
||||||
|
test_light.init()
|
||||||
|
|
||||||
|
self.assertTrue(light.setup(self.hass, {
|
||||||
|
light.DOMAIN: {'platform': 'test'}
|
||||||
|
}))
|
||||||
|
|
||||||
|
light_1, light_2 = test_light.DEVICES[0:2]
|
||||||
|
|
||||||
|
light.turn_off(self.hass, [light_1.entity_id, light_2.entity_id])
|
||||||
|
|
||||||
|
self.hass.pool.block_till_done()
|
||||||
|
|
||||||
|
self.assertTrue(scene.setup(self.hass, {
|
||||||
|
'scene': [{
|
||||||
|
'name': 'test',
|
||||||
|
'entities': {
|
||||||
|
light_1.entity_id: 'on',
|
||||||
|
light_2.entity_id: {
|
||||||
|
'state': 'on',
|
||||||
|
'brightness': 100,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}]
|
||||||
|
}))
|
||||||
|
|
||||||
|
scene.activate(self.hass, 'scene.test')
|
||||||
|
self.hass.pool.block_till_done()
|
||||||
|
|
||||||
|
self.assertTrue(light_1.is_on)
|
||||||
|
self.assertTrue(light_2.is_on)
|
||||||
|
self.assertEqual(100,
|
||||||
|
light_2.last_call('turn_on')[1].get('brightness'))
|
Loading…
x
Reference in New Issue
Block a user