2014-01-05 01:55:05 +00:00
|
|
|
"""
|
|
|
|
homeassistant.components.groups
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
Provides functionality to group devices that can be turned on or off.
|
|
|
|
"""
|
|
|
|
|
2014-12-03 05:53:00 +00:00
|
|
|
import homeassistant as ha
|
2015-02-06 08:17:30 +00:00
|
|
|
from homeassistant.helpers import generate_entity_id
|
2014-04-13 19:59:45 +00:00
|
|
|
import homeassistant.util as util
|
2014-12-07 07:57:02 +00:00
|
|
|
from homeassistant.const import (
|
2015-01-09 04:17:05 +00:00
|
|
|
ATTR_ENTITY_ID, ATTR_FRIENDLY_NAME, STATE_ON, STATE_OFF,
|
|
|
|
STATE_HOME, STATE_NOT_HOME, STATE_UNKNOWN)
|
2014-01-05 01:55:05 +00:00
|
|
|
|
|
|
|
DOMAIN = "group"
|
2014-08-13 12:28:45 +00:00
|
|
|
DEPENDENCIES = []
|
2014-01-05 01:55:05 +00:00
|
|
|
|
2014-01-20 07:37:40 +00:00
|
|
|
ENTITY_ID_FORMAT = DOMAIN + ".{}"
|
2014-01-05 01:55:05 +00:00
|
|
|
|
2014-10-29 07:47:55 +00:00
|
|
|
ATTR_AUTO = "auto"
|
|
|
|
|
2014-12-12 01:31:01 +00:00
|
|
|
# List of ON/OFF state tuples for groupable states
|
|
|
|
_GROUP_TYPES = [(STATE_ON, STATE_OFF), (STATE_HOME, STATE_NOT_HOME)]
|
2014-01-05 01:55:05 +00:00
|
|
|
|
|
|
|
|
2014-12-12 01:31:01 +00:00
|
|
|
def _get_group_on_off(state):
|
|
|
|
""" Determine the group on/off states based on a state. """
|
|
|
|
for states in _GROUP_TYPES:
|
2014-01-05 01:55:05 +00:00
|
|
|
if state in states:
|
2014-12-12 01:31:01 +00:00
|
|
|
return states
|
2014-01-05 01:55:05 +00:00
|
|
|
|
2014-12-12 01:31:01 +00:00
|
|
|
return None, None
|
2014-01-05 01:55:05 +00:00
|
|
|
|
|
|
|
|
2014-04-24 07:40:45 +00:00
|
|
|
def is_on(hass, entity_id):
|
2014-01-12 19:29:30 +00:00
|
|
|
""" Returns if the group state is in its ON-state. """
|
2014-04-24 07:40:45 +00:00
|
|
|
state = hass.states.get(entity_id)
|
2014-01-12 19:29:30 +00:00
|
|
|
|
|
|
|
if state:
|
2014-12-12 01:31:01 +00:00
|
|
|
group_on, _ = _get_group_on_off(state.state)
|
2014-01-12 19:29:30 +00:00
|
|
|
|
2014-04-15 06:48:00 +00:00
|
|
|
# If we found a group_type, compare to ON-state
|
2014-12-12 01:31:01 +00:00
|
|
|
return group_on is not None and state.state == group_on
|
2014-04-15 06:48:00 +00:00
|
|
|
|
|
|
|
return False
|
2014-01-12 19:29:30 +00:00
|
|
|
|
|
|
|
|
2014-04-24 07:40:45 +00:00
|
|
|
def expand_entity_ids(hass, entity_ids):
|
2014-04-13 19:59:45 +00:00
|
|
|
""" Returns the given list of entity ids and expands group ids into
|
|
|
|
the entity ids it represents if found. """
|
|
|
|
found_ids = []
|
|
|
|
|
|
|
|
for entity_id in entity_ids:
|
|
|
|
try:
|
|
|
|
# If entity_id points at a group, expand it
|
|
|
|
domain, _ = util.split_entity_id(entity_id)
|
|
|
|
|
|
|
|
if domain == DOMAIN:
|
|
|
|
found_ids.extend(
|
|
|
|
ent_id for ent_id
|
2014-04-24 07:40:45 +00:00
|
|
|
in get_entity_ids(hass, entity_id)
|
2014-04-13 19:59:45 +00:00
|
|
|
if ent_id not in found_ids)
|
|
|
|
|
|
|
|
else:
|
|
|
|
if entity_id not in found_ids:
|
|
|
|
found_ids.append(entity_id)
|
|
|
|
|
|
|
|
except AttributeError:
|
|
|
|
# Raised by util.split_entity_id if entity_id is not a string
|
|
|
|
pass
|
|
|
|
|
|
|
|
return found_ids
|
|
|
|
|
|
|
|
|
2014-04-24 07:40:45 +00:00
|
|
|
def get_entity_ids(hass, entity_id, domain_filter=None):
|
2014-01-20 07:37:40 +00:00
|
|
|
""" Get the entity ids that make up this group. """
|
|
|
|
try:
|
2014-04-24 07:40:45 +00:00
|
|
|
entity_ids = hass.states.get(entity_id).attributes[ATTR_ENTITY_ID]
|
|
|
|
|
|
|
|
if domain_filter:
|
2014-11-17 06:18:52 +00:00
|
|
|
return [ent_id for ent_id in entity_ids
|
|
|
|
if ent_id.startswith(domain_filter)]
|
2014-04-24 07:40:45 +00:00
|
|
|
else:
|
|
|
|
return 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-08-13 12:28:45 +00:00
|
|
|
def setup(hass, config):
|
|
|
|
""" Sets up all groups found definded in the configuration. """
|
2014-10-22 08:07:58 +00:00
|
|
|
for name, entity_ids in config.get(DOMAIN, {}).items():
|
2015-01-09 04:17:05 +00:00
|
|
|
setup_group(hass, name, entity_ids.split(","))
|
2014-08-13 12:28:45 +00:00
|
|
|
|
|
|
|
return True
|
|
|
|
|
2014-10-22 07:38:22 +00:00
|
|
|
|
2015-01-09 04:02:34 +00:00
|
|
|
class Group(object):
|
|
|
|
""" Tracks a group of entity ids. """
|
|
|
|
def __init__(self, hass, name, entity_ids=None, user_defined=True):
|
|
|
|
self.hass = hass
|
|
|
|
self.name = name
|
|
|
|
self.user_defined = user_defined
|
2015-01-09 04:17:05 +00:00
|
|
|
|
2015-02-06 08:17:30 +00:00
|
|
|
self.entity_id = generate_entity_id(ENTITY_ID_FORMAT, name, hass=hass)
|
2014-12-12 01:31:01 +00:00
|
|
|
|
2015-01-09 04:02:34 +00:00
|
|
|
self.tracking = []
|
|
|
|
self.group_on, self.group_off = None, None
|
2014-08-14 10:46:19 +00:00
|
|
|
|
2015-01-09 04:02:34 +00:00
|
|
|
if entity_ids is not None:
|
|
|
|
self.update_tracked_entity_ids(entity_ids)
|
2015-01-09 04:17:05 +00:00
|
|
|
else:
|
|
|
|
self.force_update()
|
2014-12-12 01:31:01 +00:00
|
|
|
|
2015-01-09 04:02:34 +00:00
|
|
|
@property
|
|
|
|
def state(self):
|
|
|
|
""" Return the current state from the group. """
|
|
|
|
return self.hass.states.get(self.entity_id)
|
2014-12-12 01:31:01 +00:00
|
|
|
|
2015-01-09 04:02:34 +00:00
|
|
|
@property
|
|
|
|
def state_attr(self):
|
|
|
|
""" State attributes of this group. """
|
|
|
|
return {
|
|
|
|
ATTR_ENTITY_ID: self.tracking,
|
2015-01-09 04:17:05 +00:00
|
|
|
ATTR_AUTO: not self.user_defined,
|
|
|
|
ATTR_FRIENDLY_NAME: self.name
|
2015-01-09 04:02:34 +00:00
|
|
|
}
|
2014-01-05 01:55:05 +00:00
|
|
|
|
2015-01-09 04:02:34 +00:00
|
|
|
def update_tracked_entity_ids(self, entity_ids):
|
|
|
|
""" Update the tracked entity IDs. """
|
|
|
|
self.stop()
|
2015-01-09 08:07:58 +00:00
|
|
|
self.tracking = tuple(entity_ids)
|
2015-01-09 04:02:34 +00:00
|
|
|
self.group_on, self.group_off = None, None
|
2014-01-05 01:55:05 +00:00
|
|
|
|
2015-01-09 04:02:34 +00:00
|
|
|
self.force_update()
|
2014-01-05 01:55:05 +00:00
|
|
|
|
2015-01-09 04:02:34 +00:00
|
|
|
self.start()
|
2014-01-05 01:55:05 +00:00
|
|
|
|
2015-01-09 04:02:34 +00:00
|
|
|
def force_update(self):
|
|
|
|
""" Query all the tracked states and update group state. """
|
|
|
|
for entity_id in self.tracking:
|
|
|
|
state = self.hass.states.get(entity_id)
|
2014-01-05 01:55:05 +00:00
|
|
|
|
2015-01-09 04:02:34 +00:00
|
|
|
if state is not None:
|
|
|
|
self._update_group_state(state.entity_id, None, state)
|
2014-01-05 01:55:05 +00:00
|
|
|
|
2015-01-09 04:02:34 +00:00
|
|
|
# If parsing the entitys did not result in a state, set UNKNOWN
|
|
|
|
if self.state is None:
|
2015-01-09 08:07:58 +00:00
|
|
|
self.hass.states.set(
|
|
|
|
self.entity_id, STATE_UNKNOWN, self.state_attr)
|
2014-01-05 01:55:05 +00:00
|
|
|
|
2015-01-09 04:02:34 +00:00
|
|
|
def start(self):
|
|
|
|
""" Starts the tracking. """
|
|
|
|
self.hass.states.track_change(self.tracking, self._update_group_state)
|
2014-01-05 01:55:05 +00:00
|
|
|
|
2015-01-09 04:02:34 +00:00
|
|
|
def stop(self):
|
|
|
|
""" Unregisters the group from Home Assistant. """
|
|
|
|
self.hass.states.remove(self.entity_id)
|
2014-01-05 01:55:05 +00:00
|
|
|
|
2015-01-09 04:02:34 +00:00
|
|
|
self.hass.bus.remove_listener(
|
|
|
|
ha.EVENT_STATE_CHANGED, self._update_group_state)
|
2014-12-03 05:53:00 +00:00
|
|
|
|
2015-01-09 04:02:34 +00:00
|
|
|
def _update_group_state(self, entity_id, old_state, new_state):
|
2014-12-03 05:53:00 +00:00
|
|
|
""" Updates the group state based on a state change by
|
|
|
|
a tracked entity. """
|
|
|
|
|
2015-01-09 04:02:34 +00:00
|
|
|
# We have not determined type of group yet
|
|
|
|
if self.group_on is None:
|
|
|
|
self.group_on, self.group_off = _get_group_on_off(new_state.state)
|
|
|
|
|
|
|
|
if self.group_on is not None:
|
|
|
|
# New state of the group is going to be based on the first
|
|
|
|
# state that we can recognize
|
|
|
|
self.hass.states.set(
|
|
|
|
self.entity_id, new_state.state, self.state_attr)
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
# There is already a group state
|
|
|
|
cur_gr_state = self.hass.states.get(self.entity_id).state
|
2015-01-09 08:07:58 +00:00
|
|
|
group_on, group_off = self.group_on, self.group_off
|
2014-01-05 01:55:05 +00:00
|
|
|
|
2014-12-03 05:53:00 +00:00
|
|
|
# if cur_gr_state = OFF and new_state = ON: set ON
|
|
|
|
# if cur_gr_state = ON and new_state = OFF: research
|
|
|
|
# else: ignore
|
2014-01-05 01:55:05 +00:00
|
|
|
|
2015-01-09 08:07:58 +00:00
|
|
|
if cur_gr_state == group_off and new_state.state == group_on:
|
2014-01-05 01:55:05 +00:00
|
|
|
|
2015-01-09 04:02:34 +00:00
|
|
|
self.hass.states.set(
|
2015-01-09 08:07:58 +00:00
|
|
|
self.entity_id, group_on, self.state_attr)
|
2014-01-05 01:55:05 +00:00
|
|
|
|
2015-01-09 08:07:58 +00:00
|
|
|
elif (cur_gr_state == group_on and
|
|
|
|
new_state.state == group_off):
|
2014-01-05 01:55:05 +00:00
|
|
|
|
2014-12-03 05:53:00 +00:00
|
|
|
# Check if any of the other states is still on
|
2015-01-09 08:07:58 +00:00
|
|
|
if not any(self.hass.states.is_state(ent_id, group_on)
|
|
|
|
for ent_id in self.tracking if entity_id != ent_id):
|
2015-01-09 04:02:34 +00:00
|
|
|
self.hass.states.set(
|
2015-01-09 08:07:58 +00:00
|
|
|
self.entity_id, group_off, self.state_attr)
|
2014-01-05 01:55:05 +00:00
|
|
|
|
|
|
|
|
2015-01-09 04:02:34 +00:00
|
|
|
def setup_group(hass, name, entity_ids, user_defined=True):
|
|
|
|
""" Sets up a group state that is the combined state of
|
|
|
|
several states. Supports ON/OFF and DEVICE_HOME/DEVICE_NOT_HOME. """
|
2014-01-05 01:55:05 +00:00
|
|
|
|
2015-01-09 04:02:34 +00:00
|
|
|
return Group(hass, name, entity_ids, user_defined)
|