2014-01-05 01:55:05 +00:00
|
|
|
"""
|
2015-05-13 17:18:30 +00:00
|
|
|
homeassistant.components.group
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
2014-01-05 01:55:05 +00:00
|
|
|
Provides functionality to group devices that can be turned on or off.
|
|
|
|
|
2015-10-25 14:09:01 +00:00
|
|
|
For more details about this component, please refer to the documentation at
|
2015-11-09 12:12:18 +00:00
|
|
|
https://home-assistant.io/components/group/
|
2015-10-25 14:09:01 +00:00
|
|
|
"""
|
2015-08-17 03:44:46 +00:00
|
|
|
import homeassistant.core as ha
|
2015-08-04 16:13:35 +00:00
|
|
|
from homeassistant.helpers.event import track_state_change
|
2016-01-24 07:00:46 +00:00
|
|
|
from homeassistant.helpers.entity import (
|
|
|
|
Entity, split_entity_id, generate_entity_id)
|
2014-12-07 07:57:02 +00:00
|
|
|
from homeassistant.const import (
|
2015-04-23 05:19:21 +00:00
|
|
|
ATTR_ENTITY_ID, STATE_ON, STATE_OFF,
|
2015-10-02 15:53:36 +00:00
|
|
|
STATE_HOME, STATE_NOT_HOME, STATE_OPEN, STATE_CLOSED,
|
2016-01-24 22:13:39 +00:00
|
|
|
STATE_UNKNOWN, CONF_NAME, CONF_ICON)
|
2014-01-05 01:55:05 +00:00
|
|
|
|
2016-01-24 22:13:39 +00:00
|
|
|
DOMAIN = 'group'
|
2014-01-05 01:55:05 +00:00
|
|
|
|
2016-01-24 22:13:39 +00:00
|
|
|
ENTITY_ID_FORMAT = DOMAIN + '.{}'
|
2014-01-05 01:55:05 +00:00
|
|
|
|
2016-01-24 22:13:39 +00:00
|
|
|
CONF_ENTITIES = 'entities'
|
|
|
|
CONF_VIEW = 'view'
|
|
|
|
|
|
|
|
ATTR_AUTO = 'auto'
|
|
|
|
ATTR_ORDER = 'order'
|
|
|
|
ATTR_VIEW = 'view'
|
2014-10-29 07:47:55 +00:00
|
|
|
|
2014-12-12 01:31:01 +00:00
|
|
|
# List of ON/OFF state tuples for groupable states
|
2015-10-02 15:53:36 +00:00
|
|
|
_GROUP_TYPES = [(STATE_ON, STATE_OFF), (STATE_HOME, STATE_NOT_HOME),
|
|
|
|
(STATE_OPEN, STATE_CLOSED)]
|
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:
|
2015-02-09 06:18:54 +00:00
|
|
|
if not isinstance(entity_id, str):
|
|
|
|
continue
|
|
|
|
|
|
|
|
entity_id = entity_id.lower()
|
|
|
|
|
2014-04-13 19:59:45 +00:00
|
|
|
try:
|
|
|
|
# If entity_id points at a group, expand it
|
2016-01-24 06:49:49 +00:00
|
|
|
domain, _ = split_entity_id(entity_id)
|
2014-04-13 19:59:45 +00:00
|
|
|
|
|
|
|
if domain == DOMAIN:
|
|
|
|
found_ids.extend(
|
|
|
|
ent_id for ent_id
|
2016-02-10 06:43:07 +00:00
|
|
|
in expand_entity_ids(hass, 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:
|
2016-01-24 06:49:49 +00:00
|
|
|
# Raised by split_entity_id if entity_id is not a string
|
2014-04-13 19:59:45 +00:00
|
|
|
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. """
|
2015-02-09 06:18:54 +00:00
|
|
|
entity_id = entity_id.lower()
|
|
|
|
|
2014-01-20 07:37:40 +00:00
|
|
|
try:
|
2014-04-24 07:40:45 +00:00
|
|
|
entity_ids = hass.states.get(entity_id).attributes[ATTR_ENTITY_ID]
|
|
|
|
|
|
|
|
if domain_filter:
|
2015-02-09 06:18:54 +00:00
|
|
|
domain_filter = domain_filter.lower()
|
|
|
|
|
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. """
|
2016-01-24 22:13:39 +00:00
|
|
|
for object_id, conf in config.get(DOMAIN, {}).items():
|
|
|
|
if not isinstance(conf, dict):
|
|
|
|
conf = {CONF_ENTITIES: conf}
|
|
|
|
|
|
|
|
name = conf.get(CONF_NAME, object_id)
|
|
|
|
entity_ids = conf.get(CONF_ENTITIES)
|
|
|
|
icon = conf.get(CONF_ICON)
|
|
|
|
view = conf.get(CONF_VIEW)
|
|
|
|
|
2015-08-08 20:44:07 +00:00
|
|
|
if isinstance(entity_ids, str):
|
2015-08-31 03:44:38 +00:00
|
|
|
entity_ids = [ent.strip() for ent in entity_ids.split(",")]
|
2016-01-24 22:13:39 +00:00
|
|
|
|
|
|
|
Group(hass, name, entity_ids, icon=icon, view=view,
|
|
|
|
object_id=object_id)
|
2014-08-13 12:28:45 +00:00
|
|
|
|
|
|
|
return True
|
|
|
|
|
2014-10-22 07:38:22 +00:00
|
|
|
|
2015-04-23 05:19:21 +00:00
|
|
|
class Group(Entity):
|
2015-01-09 04:02:34 +00:00
|
|
|
""" Tracks a group of entity ids. """
|
2015-04-15 06:05:34 +00:00
|
|
|
|
2016-01-24 22:13:39 +00:00
|
|
|
# pylint: disable=too-many-instance-attributes, too-many-arguments
|
2015-04-23 05:19:21 +00:00
|
|
|
|
2016-01-24 22:13:39 +00:00
|
|
|
def __init__(self, hass, name, entity_ids=None, user_defined=True,
|
|
|
|
icon=None, view=False, object_id=None):
|
2015-01-09 04:02:34 +00:00
|
|
|
self.hass = hass
|
2015-04-23 05:19:21 +00:00
|
|
|
self._name = name
|
|
|
|
self._state = STATE_UNKNOWN
|
2016-01-24 22:13:39 +00:00
|
|
|
self._order = len(hass.states.entity_ids(DOMAIN))
|
|
|
|
self._user_defined = user_defined
|
|
|
|
self._icon = icon
|
|
|
|
self._view = view
|
|
|
|
self.entity_id = generate_entity_id(
|
|
|
|
ENTITY_ID_FORMAT, object_id or name, hass=hass)
|
2015-01-09 04:02:34 +00:00
|
|
|
self.tracking = []
|
2015-04-23 05:19:21 +00:00
|
|
|
self.group_on = None
|
|
|
|
self.group_off = 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:
|
2015-04-23 05:19:21 +00:00
|
|
|
self.update_ha_state(True)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def should_poll(self):
|
|
|
|
return False
|
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
|
|
|
return self._name
|
2014-12-12 01:31:01 +00:00
|
|
|
|
2015-01-09 04:02:34 +00:00
|
|
|
@property
|
|
|
|
def state(self):
|
2015-04-23 05:19:21 +00:00
|
|
|
return self._state
|
2014-12-12 01:31:01 +00:00
|
|
|
|
2016-01-24 22:13:39 +00:00
|
|
|
@property
|
|
|
|
def icon(self):
|
|
|
|
return self._icon
|
|
|
|
|
2016-01-25 07:45:06 +00:00
|
|
|
@property
|
|
|
|
def hidden(self):
|
|
|
|
return not self._user_defined or self._view
|
|
|
|
|
2015-01-09 04:02:34 +00:00
|
|
|
@property
|
2015-04-23 05:19:21 +00:00
|
|
|
def state_attributes(self):
|
2016-01-24 22:13:39 +00:00
|
|
|
data = {
|
2015-01-09 04:02:34 +00:00
|
|
|
ATTR_ENTITY_ID: self.tracking,
|
2016-01-24 22:13:39 +00:00
|
|
|
ATTR_ORDER: self._order,
|
2015-01-09 04:02:34 +00:00
|
|
|
}
|
2016-01-24 22:13:39 +00:00
|
|
|
if not self._user_defined:
|
|
|
|
data[ATTR_AUTO] = True
|
|
|
|
if self._view:
|
|
|
|
data[ATTR_VIEW] = True
|
|
|
|
return data
|
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-02-09 06:18:54 +00:00
|
|
|
self.tracking = tuple(ent_id.lower() for ent_id in 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-04-23 05:19:21 +00:00
|
|
|
self.update_ha_state(True)
|
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 start(self):
|
|
|
|
""" Starts the tracking. """
|
2015-08-04 16:13:35 +00:00
|
|
|
track_state_change(
|
|
|
|
self.hass, self.tracking, self._state_changed_listener)
|
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(
|
2015-04-23 05:19:21 +00:00
|
|
|
ha.EVENT_STATE_CHANGED, self._state_changed_listener)
|
2014-12-03 05:53:00 +00:00
|
|
|
|
2015-04-23 05:19:21 +00:00
|
|
|
def update(self):
|
|
|
|
""" Query all the tracked states and determine current group state. """
|
|
|
|
self._state = STATE_UNKNOWN
|
|
|
|
|
|
|
|
for entity_id in self.tracking:
|
|
|
|
state = self.hass.states.get(entity_id)
|
|
|
|
|
|
|
|
if state is not None:
|
|
|
|
self._process_tracked_state(state)
|
|
|
|
|
|
|
|
def _state_changed_listener(self, entity_id, old_state, new_state):
|
|
|
|
""" Listener to receive state changes of tracked entities. """
|
|
|
|
self._process_tracked_state(new_state)
|
|
|
|
self.update_ha_state()
|
|
|
|
|
|
|
|
def _process_tracked_state(self, tr_state):
|
|
|
|
""" Updates group state based on a new state of a tracked entity. """
|
2014-12-03 05:53:00 +00:00
|
|
|
|
2015-01-09 04:02:34 +00:00
|
|
|
# We have not determined type of group yet
|
|
|
|
if self.group_on is None:
|
2015-04-23 05:19:21 +00:00
|
|
|
self.group_on, self.group_off = _get_group_on_off(tr_state.state)
|
2015-01-09 04:02:34 +00:00
|
|
|
|
|
|
|
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
|
2015-04-23 05:19:21 +00:00
|
|
|
self._state = tr_state.state
|
2015-01-09 04:02:34 +00:00
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
# There is already a group state
|
2015-04-23 05:19:21 +00:00
|
|
|
cur_gr_state = self._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
|
|
|
|
2015-04-23 05:19:21 +00:00
|
|
|
# if cur_gr_state = OFF and tr_state = ON: set ON
|
|
|
|
# if cur_gr_state = ON and tr_state = OFF: research
|
2014-12-03 05:53:00 +00:00
|
|
|
# else: ignore
|
2014-01-05 01:55:05 +00:00
|
|
|
|
2015-04-23 05:19:21 +00:00
|
|
|
if cur_gr_state == group_off and tr_state.state == group_on:
|
|
|
|
self._state = group_on
|
2014-01-05 01:55:05 +00:00
|
|
|
|
2015-04-23 05:19:21 +00:00
|
|
|
elif cur_gr_state == group_on and tr_state.state == group_off:
|
2014-01-05 01:55:05 +00:00
|
|
|
|
2015-04-23 05:19:21 +00:00
|
|
|
# Set to off if no other states are on
|
2015-01-09 08:07:58 +00:00
|
|
|
if not any(self.hass.states.is_state(ent_id, group_on)
|
2015-04-23 05:19:21 +00:00
|
|
|
for ent_id in self.tracking
|
|
|
|
if tr_state.entity_id != ent_id):
|
|
|
|
self._state = group_off
|