Allow groups to be used as views

This commit is contained in:
Paulus Schoutsen 2016-01-24 14:13:39 -08:00
parent e541b9ba77
commit f6c53896e3
10 changed files with 256 additions and 67 deletions

View file

@ -62,10 +62,10 @@ def setup(hass, config):
lights = sorted(hass.states.entity_ids('light')) lights = sorted(hass.states.entity_ids('light'))
switches = sorted(hass.states.entity_ids('switch')) switches = sorted(hass.states.entity_ids('switch'))
media_players = sorted(hass.states.entity_ids('media_player')) media_players = sorted(hass.states.entity_ids('media_player'))
group.setup_group(hass, 'living room', [lights[2], lights[1], switches[0], group.Group(hass, 'living room', [lights[2], lights[1], switches[0],
media_players[1]]) media_players[1]])
group.setup_group(hass, 'bedroom', [lights[0], switches[1], group.Group(hass, 'bedroom', [lights[0], switches[1],
media_players[0]]) media_players[0]])
# Setup scripts # Setup scripts
bootstrap.setup_component( bootstrap.setup_component(

View file

@ -229,7 +229,7 @@ class DeviceTracker(object):
""" Initializes group for all tracked devices. """ """ Initializes group for all tracked devices. """
entity_ids = (dev.entity_id for dev in self.devices.values() entity_ids = (dev.entity_id for dev in self.devices.values()
if dev.track) if dev.track)
self.group = group.setup_group( self.group = group.Group(
self.hass, GROUP_NAME_ALL_DEVICES, entity_ids, False) self.hass, GROUP_NAME_ALL_DEVICES, entity_ids, False)
def update_stale(self, now): def update_stale(self, now):

View file

@ -1,2 +1,2 @@
""" DO NOT MODIFY. Auto-generated by build_frontend script """ """ DO NOT MODIFY. Auto-generated by build_frontend script """
VERSION = "1003c31441ec44b3db84b49980f736a7" VERSION = "5acc1c32156966aef67ca45a1e677eae"

File diff suppressed because one or more lines are too long

@ -1 +1 @@
Subproject commit 2ecd6a818443780dc5d0d981996d165218b2b094 Subproject commit 83ac27db0f7e4a9ae2499130be13940d7b5a030f

View file

@ -13,13 +13,18 @@ from homeassistant.helpers.entity import (
from homeassistant.const import ( from homeassistant.const import (
ATTR_ENTITY_ID, STATE_ON, STATE_OFF, ATTR_ENTITY_ID, STATE_ON, STATE_OFF,
STATE_HOME, STATE_NOT_HOME, STATE_OPEN, STATE_CLOSED, STATE_HOME, STATE_NOT_HOME, STATE_OPEN, STATE_CLOSED,
STATE_UNKNOWN) STATE_UNKNOWN, CONF_NAME, CONF_ICON)
DOMAIN = "group" DOMAIN = 'group'
ENTITY_ID_FORMAT = DOMAIN + ".{}" ENTITY_ID_FORMAT = DOMAIN + '.{}'
ATTR_AUTO = "auto" CONF_ENTITIES = 'entities'
CONF_VIEW = 'view'
ATTR_AUTO = 'auto'
ATTR_ORDER = 'order'
ATTR_VIEW = 'view'
# List of ON/OFF state tuples for groupable states # List of ON/OFF state tuples for groupable states
_GROUP_TYPES = [(STATE_ON, STATE_OFF), (STATE_HOME, STATE_NOT_HOME), _GROUP_TYPES = [(STATE_ON, STATE_OFF), (STATE_HOME, STATE_NOT_HOME),
@ -103,10 +108,20 @@ def get_entity_ids(hass, entity_id, domain_filter=None):
def setup(hass, config): def setup(hass, config):
""" Sets up all groups found definded in the configuration. """ """ Sets up all groups found definded in the configuration. """
for name, entity_ids in config.get(DOMAIN, {}).items(): 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)
if isinstance(entity_ids, str): if isinstance(entity_ids, str):
entity_ids = [ent.strip() for ent in entity_ids.split(",")] entity_ids = [ent.strip() for ent in entity_ids.split(",")]
setup_group(hass, name, entity_ids)
Group(hass, name, entity_ids, icon=icon, view=view,
object_id=object_id)
return True return True
@ -114,14 +129,19 @@ def setup(hass, config):
class Group(Entity): class Group(Entity):
""" Tracks a group of entity ids. """ """ Tracks a group of entity ids. """
# pylint: disable=too-many-instance-attributes # pylint: disable=too-many-instance-attributes, too-many-arguments
def __init__(self, hass, name, entity_ids=None, user_defined=True): def __init__(self, hass, name, entity_ids=None, user_defined=True,
icon=None, view=False, object_id=None):
self.hass = hass self.hass = hass
self._name = name self._name = name
self._state = STATE_UNKNOWN self._state = STATE_UNKNOWN
self.user_defined = user_defined self._order = len(hass.states.entity_ids(DOMAIN))
self.entity_id = generate_entity_id(ENTITY_ID_FORMAT, name, hass=hass) 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)
self.tracking = [] self.tracking = []
self.group_on = None self.group_on = None
self.group_off = None self.group_off = None
@ -143,12 +163,21 @@ class Group(Entity):
def state(self): def state(self):
return self._state return self._state
@property
def icon(self):
return self._icon
@property @property
def state_attributes(self): def state_attributes(self):
return { data = {
ATTR_ENTITY_ID: self.tracking, ATTR_ENTITY_ID: self.tracking,
ATTR_AUTO: not self.user_defined, ATTR_ORDER: self._order,
} }
if not self._user_defined:
data[ATTR_AUTO] = True
if self._view:
data[ATTR_VIEW] = True
return data
def update_tracked_entity_ids(self, entity_ids): def update_tracked_entity_ids(self, entity_ids):
""" Update the tracked entity IDs. """ """ Update the tracked entity IDs. """
@ -219,10 +248,3 @@ class Group(Entity):
for ent_id in self.tracking for ent_id in self.tracking
if tr_state.entity_id != ent_id): if tr_state.entity_id != ent_id):
self._state = group_off self._state = group_off
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. """
return Group(hass, name, entity_ids, user_defined)

View file

@ -10,6 +10,7 @@ MATCH_ALL = '*'
DEVICE_DEFAULT_NAME = "Unnamed Device" DEVICE_DEFAULT_NAME = "Unnamed Device"
# #### CONFIG #### # #### CONFIG ####
CONF_ICON = "icon"
CONF_LATITUDE = "latitude" CONF_LATITUDE = "latitude"
CONF_LONGITUDE = "longitude" CONF_LONGITUDE = "longitude"
CONF_TEMPERATURE_UNIT = "temperature_unit" CONF_TEMPERATURE_UNIT = "temperature_unit"

View file

@ -9,7 +9,8 @@ import unittest
import logging import logging
import homeassistant.core as ha import homeassistant.core as ha
from homeassistant.const import STATE_ON, STATE_OFF, STATE_HOME, STATE_UNKNOWN from homeassistant.const import (
STATE_ON, STATE_OFF, STATE_HOME, STATE_UNKNOWN, ATTR_ICON)
import homeassistant.components.group as group import homeassistant.components.group as group
@ -39,7 +40,7 @@ class TestComponentsGroup(unittest.TestCase):
def test_setup_group_with_mixed_groupable_states(self): def test_setup_group_with_mixed_groupable_states(self):
""" Try to setup a group with mixed groupable states """ """ Try to setup a group with mixed groupable states """
self.hass.states.set('device_tracker.Paulus', STATE_HOME) self.hass.states.set('device_tracker.Paulus', STATE_HOME)
group.setup_group( group.Group(
self.hass, 'person_and_light', self.hass, 'person_and_light',
['light.Bowl', 'device_tracker.Paulus']) ['light.Bowl', 'device_tracker.Paulus'])
@ -50,7 +51,7 @@ class TestComponentsGroup(unittest.TestCase):
def test_setup_group_with_a_non_existing_state(self): def test_setup_group_with_a_non_existing_state(self):
""" Try to setup a group with a non existing state """ """ Try to setup a group with a non existing state """
grp = group.setup_group( grp = group.Group(
self.hass, 'light_and_nothing', self.hass, 'light_and_nothing',
['light.Bowl', 'non.existing']) ['light.Bowl', 'non.existing'])
@ -60,7 +61,7 @@ class TestComponentsGroup(unittest.TestCase):
self.hass.states.set('cast.living_room', "Plex") self.hass.states.set('cast.living_room', "Plex")
self.hass.states.set('cast.bedroom', "Netflix") self.hass.states.set('cast.bedroom', "Netflix")
grp = group.setup_group( grp = group.Group(
self.hass, 'chromecasts', self.hass, 'chromecasts',
['cast.living_room', 'cast.bedroom']) ['cast.living_room', 'cast.bedroom'])
@ -68,7 +69,7 @@ class TestComponentsGroup(unittest.TestCase):
def test_setup_empty_group(self): def test_setup_empty_group(self):
""" Try to setup an empty group. """ """ Try to setup an empty group. """
grp = group.setup_group(self.hass, 'nothing', []) grp = group.Group(self.hass, 'nothing', [])
self.assertEqual(STATE_UNKNOWN, grp.state) self.assertEqual(STATE_UNKNOWN, grp.state)
@ -80,7 +81,7 @@ class TestComponentsGroup(unittest.TestCase):
group_state = self.hass.states.get(self.group_entity_id) group_state = self.hass.states.get(self.group_entity_id)
self.assertEqual(STATE_ON, group_state.state) self.assertEqual(STATE_ON, group_state.state)
self.assertTrue(group_state.attributes[group.ATTR_AUTO]) self.assertTrue(group_state.attributes.get(group.ATTR_AUTO))
def test_group_turns_off_if_all_off(self): def test_group_turns_off_if_all_off(self):
""" """
@ -199,17 +200,34 @@ class TestComponentsGroup(unittest.TestCase):
self.hass, self.hass,
{ {
group.DOMAIN: { group.DOMAIN: {
'second_group': 'light.Bowl, ' + self.group_entity_id 'second_group': {
'entities': 'light.Bowl, ' + self.group_entity_id,
'icon': 'mdi:work',
'view': True,
},
'test_group': 'hello.world,sensor.happy',
} }
})) }))
group_state = self.hass.states.get( group_state = self.hass.states.get(
group.ENTITY_ID_FORMAT.format('second_group')) group.ENTITY_ID_FORMAT.format('second_group'))
self.assertEqual(STATE_ON, group_state.state) self.assertEqual(STATE_ON, group_state.state)
self.assertEqual(set((self.group_entity_id, 'light.bowl')), self.assertEqual(set((self.group_entity_id, 'light.bowl')),
set(group_state.attributes['entity_id'])) set(group_state.attributes['entity_id']))
self.assertFalse(group_state.attributes[group.ATTR_AUTO]) self.assertIsNone(group_state.attributes.get(group.ATTR_AUTO))
self.assertEqual('mdi:work',
group_state.attributes.get(ATTR_ICON))
self.assertEqual(True,
group_state.attributes.get(group.ATTR_VIEW))
group_state = self.hass.states.get(
group.ENTITY_ID_FORMAT.format('test_group'))
self.assertEqual(STATE_UNKNOWN, group_state.state)
self.assertEqual(set(('sensor.happy', 'hello.world')),
set(group_state.attributes['entity_id']))
self.assertIsNone(group_state.attributes.get(group.ATTR_AUTO))
self.assertIsNone(group_state.attributes.get(ATTR_ICON))
self.assertIsNone(group_state.attributes.get(group.ATTR_VIEW))
def test_groups_get_unique_names(self): def test_groups_get_unique_names(self):
""" Two groups with same name should both have a unique entity id. """ """ Two groups with same name should both have a unique entity id. """

View file

@ -1,8 +1,8 @@
""" """
tests.components.automation.test_location tests.components.test_zone
±±±~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ±±±~~~~~~~~~~~~~~~~~~~~~~~
Tests location automation. Tests zone component.
""" """
import unittest import unittest
@ -11,8 +11,8 @@ from homeassistant.components import zone
from tests.common import get_test_home_assistant from tests.common import get_test_home_assistant
class TestAutomationZone(unittest.TestCase): class TestComponentZone(unittest.TestCase):
""" Test the event automation. """ """ Test the zone component. """
def setUp(self): # pylint: disable=invalid-name def setUp(self): # pylint: disable=invalid-name
self.hass = get_test_home_assistant() self.hass = get_test_home_assistant()

View file

@ -74,7 +74,7 @@ class TestServiceHelpers(unittest.TestCase):
self.hass.states.set('light.Ceiling', STATE_OFF) self.hass.states.set('light.Ceiling', STATE_OFF)
self.hass.states.set('light.Kitchen', STATE_OFF) self.hass.states.set('light.Kitchen', STATE_OFF)
loader.get_component('group').setup_group( loader.get_component('group').Group(
self.hass, 'test', ['light.Ceiling', 'light.Kitchen']) self.hass, 'test', ['light.Ceiling', 'light.Kitchen'])
call = ha.ServiceCall('light', 'turn_on', call = ha.ServiceCall('light', 'turn_on',