RENAME: StateMachine tracks now entities, not categories.
This commit is contained in:
1 parent
e7f5953362
commit
e9e1b007ed
13 files changed
+236
-236
No files matched your search
@@ -11,9 +11,9 @@ import homeassistant as ha
|
||||
|
||||
DOMAIN = "group"
|
||||
|
||||
STATE_CATEGORY_FORMAT = DOMAIN + ".{}"
|
||||
ENTITY_ID_FORMAT = DOMAIN + ".{}"
|
||||
|
||||
STATE_ATTR_CATEGORIES = "categories"
|
||||
STATE_ATTR_ENTITY_IDS = "entity_ids"
|
||||
|
||||
_GROUP_TYPES = {
|
||||
"on_off": (ha.STATE_ON, ha.STATE_OFF),
|
||||
@@ -46,29 +46,32 @@ def is_on(statemachine, group):
|
||||
return False
|
||||
|
||||
|
||||
def get_categories(statemachine, group):
|
||||
""" Get the categories that make up this group. """
|
||||
state = statemachine.get_state(group)
|
||||
|
||||
return state.attributes[STATE_ATTR_CATEGORIES] if state else []
|
||||
def get_entity_ids(statemachine, group):
|
||||
""" Get the entity ids that make up this group. """
|
||||
try:
|
||||
return statemachine.get_state(group).attributes[STATE_ATTR_ENTITY_IDS]
|
||||
except (AttributeError, KeyError):
|
||||
# AttributeError if state did not exist
|
||||
# KeyError if key did not exist in attributes
|
||||
return []
|
||||
|
||||
|
||||
# pylint: disable=too-many-branches
|
||||
def setup(bus, statemachine, name, categories):
|
||||
def setup(bus, statemachine, name, entity_ids):
|
||||
""" Sets up a group state that is the combined state of
|
||||
several states. Supports ON/OFF and DEVICE_HOME/DEVICE_NOT_HOME. """
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
# Loop over the given categories to:
|
||||
# Loop over the given entities to:
|
||||
# - determine which group type this is (on_off, device_home)
|
||||
# - if all states exist and have valid states
|
||||
# - retrieve the current state of the group
|
||||
errors = []
|
||||
group_type, group_on, group_off, group_state = None, None, None, None
|
||||
|
||||
for cat in categories:
|
||||
state = statemachine.get_state(cat)
|
||||
for entity_id in entity_ids:
|
||||
state = statemachine.get_state(entity_id)
|
||||
|
||||
# Try to determine group type if we didn't yet
|
||||
if not group_type and state:
|
||||
@@ -85,15 +88,15 @@ def setup(bus, statemachine, name, categories):
|
||||
|
||||
break
|
||||
|
||||
# Check if category exists
|
||||
# Check if entity exists
|
||||
if not state:
|
||||
errors.append("Category {} does not exist".format(cat))
|
||||
errors.append("Entity {} does not exist".format(entity_id))
|
||||
|
||||
# Check if category is valid state
|
||||
# Check if entity is valid state
|
||||
elif state.state != group_off and state.state != group_on:
|
||||
|
||||
errors.append("State of {} is {} (expected: {}, {})".format(
|
||||
cat, state.state, group_off, group_on))
|
||||
entity_id, state.state, group_off, group_on))
|
||||
|
||||
# Keep track of the group state to init later on
|
||||
elif group_state == group_off and state.state == group_on:
|
||||
@@ -105,15 +108,15 @@ def setup(bus, statemachine, name, categories):
|
||||
|
||||
return False
|
||||
|
||||
group_cat = STATE_CATEGORY_FORMAT.format(name)
|
||||
state_attr = {STATE_ATTR_CATEGORIES: categories}
|
||||
group_entity_id = ENTITY_ID_FORMAT.format(name)
|
||||
state_attr = {STATE_ATTR_ENTITY_IDS: entity_ids}
|
||||
|
||||
# pylint: disable=unused-argument
|
||||
def _update_group_state(category, old_state, new_state):
|
||||
def _update_group_state(entity_id, old_state, new_state):
|
||||
""" Updates the group state based on a state change by a tracked
|
||||
category. """
|
||||
entity. """
|
||||
|
||||
cur_group_state = statemachine.get_state(group_cat).state
|
||||
cur_group_state = statemachine.get_state(group_entity_id).state
|
||||
|
||||
# if cur_group_state = OFF and new_state = ON: set ON
|
||||
# if cur_group_state = ON and new_state = OFF: research
|
||||
@@ -121,18 +124,18 @@ def setup(bus, statemachine, name, categories):
|
||||
|
||||
if cur_group_state == group_off and new_state.state == group_on:
|
||||
|
||||
statemachine.set_state(group_cat, group_on, state_attr)
|
||||
statemachine.set_state(group_entity_id, group_on, state_attr)
|
||||
|
||||
elif cur_group_state == group_on and new_state.state == group_off:
|
||||
|
||||
# Check if any of the other states is still on
|
||||
if not any([statemachine.is_state(cat, group_on)
|
||||
for cat in categories if cat != category]):
|
||||
statemachine.set_state(group_cat, group_off, state_attr)
|
||||
if not any([statemachine.is_state(ent_id, group_on)
|
||||
for ent_id in entity_ids if entity_id != ent_id]):
|
||||
statemachine.set_state(group_entity_id, group_off, state_attr)
|
||||
|
||||
for cat in categories:
|
||||
ha.track_state_change(bus, cat, _update_group_state)
|
||||
for entity_id in entity_ids:
|
||||
ha.track_state_change(bus, entity_id, _update_group_state)
|
||||
|
||||
statemachine.set_state(group_cat, group_state, state_attr)
|
||||
statemachine.set_state(group_entity_id, group_state, state_attr)
|
||||
|
||||
return True
|
||||
Reference in new issue
Block a user