2014-01-05 01:55:05 +00:00
|
|
|
"""
|
|
|
|
homeassistant.components.groups
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
Provides functionality to group devices that can be turned on or off.
|
|
|
|
"""
|
|
|
|
|
|
|
|
import logging
|
|
|
|
|
|
|
|
import homeassistant as ha
|
2014-01-24 07:26:00 +00:00
|
|
|
import homeassistant.components as components
|
2014-01-05 01:55:05 +00:00
|
|
|
|
|
|
|
DOMAIN = "group"
|
|
|
|
|
2014-01-20 07:37:40 +00:00
|
|
|
ENTITY_ID_FORMAT = DOMAIN + ".{}"
|
2014-01-05 01:55:05 +00:00
|
|
|
|
2014-01-20 07:37:40 +00:00
|
|
|
STATE_ATTR_ENTITY_IDS = "entity_ids"
|
2014-01-05 01:55:05 +00:00
|
|
|
|
|
|
|
_GROUP_TYPES = {
|
2014-01-24 07:26:00 +00:00
|
|
|
"on_off": (components.STATE_ON, components.STATE_OFF),
|
|
|
|
"home_not_home": (components.STATE_HOME, components.STATE_NOT_HOME)
|
2014-01-05 01:55:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
def _get_group_type(state):
|
|
|
|
""" Determine the group type based on the given group type. """
|
|
|
|
for group_type, states in _GROUP_TYPES.items():
|
|
|
|
if state in states:
|
|
|
|
return group_type
|
|
|
|
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
2014-01-24 06:03:13 +00:00
|
|
|
def is_on(statemachine, entity_id):
|
2014-01-12 19:29:30 +00:00
|
|
|
""" Returns if the group state is in its ON-state. """
|
2014-01-24 06:03:13 +00:00
|
|
|
state = statemachine.get_state(entity_id)
|
2014-01-12 19:29:30 +00:00
|
|
|
|
|
|
|
if state:
|
2014-01-20 03:10:40 +00:00
|
|
|
group_type = _get_group_type(state.state)
|
2014-01-12 19:29:30 +00:00
|
|
|
|
|
|
|
if group_type:
|
2014-01-20 03:10:40 +00:00
|
|
|
# We found group_type, compare to ON-state
|
|
|
|
return state.state == _GROUP_TYPES[group_type][0]
|
2014-01-12 19:29:30 +00:00
|
|
|
else:
|
|
|
|
return False
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
2014-01-24 06:03:13 +00:00
|
|
|
def get_entity_ids(statemachine, entity_id):
|
2014-01-20 07:37:40 +00:00
|
|
|
""" Get the entity ids that make up this group. """
|
|
|
|
try:
|
2014-01-24 06:03:13 +00:00
|
|
|
return \
|
|
|
|
statemachine.get_state(entity_id).attributes[STATE_ATTR_ENTITY_IDS]
|
2014-01-20 07:37:40 +00:00
|
|
|
except (AttributeError, KeyError):
|
|
|
|
# AttributeError if state did not exist
|
|
|
|
# KeyError if key did not exist in attributes
|
|
|
|
return []
|
2014-01-05 01:55:05 +00:00
|
|
|
|
|
|
|
|
2014-01-24 06:03:13 +00:00
|
|
|
# pylint: disable=too-many-branches, too-many-locals
|
2014-01-20 07:37:40 +00:00
|
|
|
def setup(bus, statemachine, name, entity_ids):
|
2014-01-05 01:55:05 +00:00
|
|
|
""" 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__)
|
|
|
|
|
2014-01-20 07:37:40 +00:00
|
|
|
# Loop over the given entities to:
|
2014-01-05 01:55:05 +00:00
|
|
|
# - 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
|
|
|
|
|
2014-01-20 07:37:40 +00:00
|
|
|
for entity_id in entity_ids:
|
|
|
|
state = statemachine.get_state(entity_id)
|
2014-01-05 01:55:05 +00:00
|
|
|
|
|
|
|
# Try to determine group type if we didn't yet
|
|
|
|
if not group_type and state:
|
2014-01-20 03:10:40 +00:00
|
|
|
group_type = _get_group_type(state.state)
|
2014-01-05 01:55:05 +00:00
|
|
|
|
|
|
|
if group_type:
|
|
|
|
group_on, group_off = _GROUP_TYPES[group_type]
|
|
|
|
group_state = group_off
|
|
|
|
|
|
|
|
else:
|
|
|
|
# We did not find a matching group_type
|
|
|
|
errors.append("Found unexpected state '{}'".format(
|
2014-01-20 03:10:40 +00:00
|
|
|
name, state.state))
|
2014-01-05 01:55:05 +00:00
|
|
|
|
|
|
|
break
|
|
|
|
|
2014-01-20 07:37:40 +00:00
|
|
|
# Check if entity exists
|
2014-01-05 01:55:05 +00:00
|
|
|
if not state:
|
2014-01-20 07:37:40 +00:00
|
|
|
errors.append("Entity {} does not exist".format(entity_id))
|
2014-01-05 01:55:05 +00:00
|
|
|
|
2014-01-20 07:37:40 +00:00
|
|
|
# Check if entity is valid state
|
2014-01-20 03:10:40 +00:00
|
|
|
elif state.state != group_off and state.state != group_on:
|
2014-01-05 01:55:05 +00:00
|
|
|
|
|
|
|
errors.append("State of {} is {} (expected: {}, {})".format(
|
2014-01-20 07:37:40 +00:00
|
|
|
entity_id, state.state, group_off, group_on))
|
2014-01-05 01:55:05 +00:00
|
|
|
|
|
|
|
# Keep track of the group state to init later on
|
2014-01-20 03:10:40 +00:00
|
|
|
elif group_state == group_off and state.state == group_on:
|
2014-01-05 01:55:05 +00:00
|
|
|
group_state = group_on
|
|
|
|
|
|
|
|
if errors:
|
|
|
|
logger.error("Error setting up state group {}: {}".format(
|
|
|
|
name, ", ".join(errors)))
|
|
|
|
|
|
|
|
return False
|
|
|
|
|
2014-01-20 07:37:40 +00:00
|
|
|
group_entity_id = ENTITY_ID_FORMAT.format(name)
|
|
|
|
state_attr = {STATE_ATTR_ENTITY_IDS: entity_ids}
|
2014-01-05 01:55:05 +00:00
|
|
|
|
|
|
|
# pylint: disable=unused-argument
|
2014-01-30 06:47:50 +00:00
|
|
|
def update_group_state(entity_id, old_state, new_state):
|
2014-01-05 01:55:05 +00:00
|
|
|
""" Updates the group state based on a state change by a tracked
|
2014-01-20 07:37:40 +00:00
|
|
|
entity. """
|
2014-01-05 01:55:05 +00:00
|
|
|
|
2014-01-20 07:37:40 +00:00
|
|
|
cur_group_state = statemachine.get_state(group_entity_id).state
|
2014-01-05 01:55:05 +00:00
|
|
|
|
|
|
|
# if cur_group_state = OFF and new_state = ON: set ON
|
|
|
|
# if cur_group_state = ON and new_state = OFF: research
|
|
|
|
# else: ignore
|
|
|
|
|
2014-01-20 03:10:40 +00:00
|
|
|
if cur_group_state == group_off and new_state.state == group_on:
|
2014-01-05 01:55:05 +00:00
|
|
|
|
2014-01-20 07:37:40 +00:00
|
|
|
statemachine.set_state(group_entity_id, group_on, state_attr)
|
2014-01-05 01:55:05 +00:00
|
|
|
|
2014-01-20 03:10:40 +00:00
|
|
|
elif cur_group_state == group_on and new_state.state == group_off:
|
2014-01-05 01:55:05 +00:00
|
|
|
|
|
|
|
# Check if any of the other states is still on
|
2014-01-20 07:37:40 +00:00
|
|
|
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)
|
2014-01-05 01:55:05 +00:00
|
|
|
|
2014-01-20 07:37:40 +00:00
|
|
|
for entity_id in entity_ids:
|
2014-01-30 06:47:50 +00:00
|
|
|
ha.track_state_change(bus, entity_id, update_group_state)
|
2014-01-05 01:55:05 +00:00
|
|
|
|
2014-01-24 06:03:13 +00:00
|
|
|
# group.setup is called to setup each group. Only the first time will we
|
|
|
|
# register a turn_on and turn_off method for groups.
|
|
|
|
|
2014-01-24 07:26:00 +00:00
|
|
|
if not bus.has_service(DOMAIN, components.SERVICE_TURN_ON):
|
2014-01-30 06:47:50 +00:00
|
|
|
def turn_group_on_service(service):
|
2014-01-24 07:26:00 +00:00
|
|
|
""" Call components.turn_on for each entity_id from this group. """
|
2014-01-24 06:03:13 +00:00
|
|
|
for entity_id in get_entity_ids(statemachine,
|
|
|
|
service.data.get(
|
2014-01-24 07:26:00 +00:00
|
|
|
components.ATTR_ENTITY_ID)):
|
2014-01-24 06:03:13 +00:00
|
|
|
|
2014-01-24 07:26:00 +00:00
|
|
|
components.turn_on(bus, entity_id)
|
2014-01-24 06:03:13 +00:00
|
|
|
|
2014-01-24 07:26:00 +00:00
|
|
|
bus.register_service(DOMAIN, components.SERVICE_TURN_ON,
|
2014-01-30 06:47:50 +00:00
|
|
|
turn_group_on_service)
|
2014-01-24 06:03:13 +00:00
|
|
|
|
2014-01-24 07:26:00 +00:00
|
|
|
if not bus.has_service(DOMAIN, components.SERVICE_TURN_OFF):
|
2014-01-30 06:47:50 +00:00
|
|
|
def turn_group_off_service(service):
|
2014-01-24 07:26:00 +00:00
|
|
|
""" Call components.turn_off for each entity_id in this group. """
|
2014-01-24 06:03:13 +00:00
|
|
|
for entity_id in get_entity_ids(statemachine,
|
|
|
|
service.data.get(
|
2014-01-24 07:26:00 +00:00
|
|
|
components.ATTR_ENTITY_ID)):
|
2014-01-24 06:03:13 +00:00
|
|
|
|
2014-01-24 07:26:00 +00:00
|
|
|
components.turn_off(bus, entity_id)
|
2014-01-24 06:03:13 +00:00
|
|
|
|
2014-01-24 07:26:00 +00:00
|
|
|
bus.register_service(DOMAIN, components.SERVICE_TURN_OFF,
|
2014-01-30 06:47:50 +00:00
|
|
|
turn_group_off_service)
|
2014-01-24 06:03:13 +00:00
|
|
|
|
2014-01-20 07:37:40 +00:00
|
|
|
statemachine.set_state(group_entity_id, group_state, state_attr)
|
2014-01-05 01:55:05 +00:00
|
|
|
|
|
|
|
return True
|