core/tests/components/test_group.py

244 lines
9.5 KiB
Python
Raw Normal View History

2014-11-24 00:46:59 +00:00
"""
2015-01-09 04:05:12 +00:00
tests.test_component_group
~~~~~~~~~~~~~~~~~~~~~~~~~~
2014-11-24 00:46:59 +00:00
Tests the group compoments.
"""
# pylint: disable=protected-access,too-many-public-methods
import unittest
2015-08-17 03:44:46 +00:00
import homeassistant.core as ha
2016-01-24 22:13:39 +00:00
from homeassistant.const import (
STATE_ON, STATE_OFF, STATE_HOME, STATE_UNKNOWN, ATTR_ICON, ATTR_HIDDEN)
2014-11-24 00:46:59 +00:00
import homeassistant.components.group as group
class TestComponentsGroup(unittest.TestCase):
""" Tests homeassistant.components.group module. """
def setUp(self): # pylint: disable=invalid-name
""" Init needed objects. """
self.hass = ha.HomeAssistant()
self.hass.states.set('light.Bowl', STATE_ON)
self.hass.states.set('light.Ceiling', STATE_OFF)
test_group = group.Group(
self.hass, 'init_group', ['light.Bowl', 'light.Ceiling'], False)
2014-11-24 00:46:59 +00:00
self.group_entity_id = test_group.entity_id
2014-11-24 00:46:59 +00:00
def tearDown(self): # pylint: disable=invalid-name
""" Stop down stuff we started. """
self.hass.stop()
2015-01-09 04:02:34 +00:00
def test_setup_group_with_mixed_groupable_states(self):
""" Try to setup a group with mixed groupable states """
self.hass.states.set('device_tracker.Paulus', STATE_HOME)
2016-01-24 22:13:39 +00:00
group.Group(
self.hass, 'person_and_light',
2015-01-09 04:02:34 +00:00
['light.Bowl', 'device_tracker.Paulus'])
self.assertEqual(
STATE_ON,
self.hass.states.get(
group.ENTITY_ID_FORMAT.format('person_and_light')).state)
2015-01-09 04:02:34 +00:00
def test_setup_group_with_a_non_existing_state(self):
""" Try to setup a group with a non existing state """
2016-01-24 22:13:39 +00:00
grp = group.Group(
self.hass, 'light_and_nothing',
2015-01-09 04:02:34 +00:00
['light.Bowl', 'non.existing'])
2015-04-23 05:19:21 +00:00
self.assertEqual(STATE_ON, grp.state)
2015-01-09 04:02:34 +00:00
def test_setup_group_with_non_groupable_states(self):
self.hass.states.set('cast.living_room', "Plex")
self.hass.states.set('cast.bedroom', "Netflix")
2016-01-24 22:13:39 +00:00
grp = group.Group(
2015-01-09 04:02:34 +00:00
self.hass, 'chromecasts',
['cast.living_room', 'cast.bedroom'])
2015-04-23 05:19:21 +00:00
self.assertEqual(STATE_UNKNOWN, grp.state)
2015-01-09 04:02:34 +00:00
def test_setup_empty_group(self):
""" Try to setup an empty group. """
2016-01-24 22:13:39 +00:00
grp = group.Group(self.hass, 'nothing', [])
2015-01-09 04:02:34 +00:00
2015-04-23 05:19:21 +00:00
self.assertEqual(STATE_UNKNOWN, grp.state)
def test_monitor_group(self):
""" Test if the group keeps track of states. """
2014-11-24 00:46:59 +00:00
# Test if group setup in our init mode is ok
self.assertIn(self.group_entity_id, self.hass.states.entity_ids())
2014-11-24 00:46:59 +00:00
group_state = self.hass.states.get(self.group_entity_id)
self.assertEqual(STATE_ON, group_state.state)
2016-01-24 22:13:39 +00:00
self.assertTrue(group_state.attributes.get(group.ATTR_AUTO))
2014-11-24 00:46:59 +00:00
def test_group_turns_off_if_all_off(self):
"""
Test if the group turns off if the last device that was on turns off.
"""
self.hass.states.set('light.Bowl', STATE_OFF)
2014-11-24 00:46:59 +00:00
self.hass.pool.block_till_done()
2014-11-24 00:46:59 +00:00
group_state = self.hass.states.get(self.group_entity_id)
self.assertEqual(STATE_OFF, group_state.state)
2014-11-24 00:46:59 +00:00
def test_group_turns_on_if_all_are_off_and_one_turns_on(self):
"""
Test if group turns on if all devices were turned off and one turns on.
"""
# Make sure all are off.
self.hass.states.set('light.Bowl', STATE_OFF)
self.hass.pool.block_till_done()
2014-11-24 00:46:59 +00:00
# Turn one on
self.hass.states.set('light.Ceiling', STATE_ON)
self.hass.pool.block_till_done()
2014-11-24 00:46:59 +00:00
group_state = self.hass.states.get(self.group_entity_id)
self.assertEqual(STATE_ON, group_state.state)
2014-11-24 00:46:59 +00:00
def test_is_on(self):
""" Test is_on method. """
self.assertTrue(group.is_on(self.hass, self.group_entity_id))
self.hass.states.set('light.Bowl', STATE_OFF)
self.hass.pool.block_till_done()
self.assertFalse(group.is_on(self.hass, self.group_entity_id))
2014-11-24 00:46:59 +00:00
2014-11-24 01:06:19 +00:00
# Try on non existing state
self.assertFalse(group.is_on(self.hass, 'non.existing'))
2014-11-24 00:46:59 +00:00
def test_expand_entity_ids(self):
""" Test expand_entity_ids method. """
self.assertEqual(sorted(['light.ceiling', 'light.bowl']),
2014-11-24 00:46:59 +00:00
sorted(group.expand_entity_ids(
self.hass, [self.group_entity_id])))
2014-11-24 00:46:59 +00:00
def test_expand_entity_ids_does_not_return_duplicates(self):
""" Test that expand_entity_ids does not return duplicates. """
2014-11-24 00:46:59 +00:00
self.assertEqual(
['light.bowl', 'light.ceiling'],
2014-11-24 00:46:59 +00:00
sorted(group.expand_entity_ids(
self.hass, [self.group_entity_id, 'light.Ceiling'])))
2014-11-24 00:46:59 +00:00
self.assertEqual(
['light.bowl', 'light.ceiling'],
sorted(group.expand_entity_ids(
self.hass, ['light.bowl', self.group_entity_id])))
def test_expand_entity_ids_ignores_non_strings(self):
""" Test that non string elements in lists are ignored. """
2014-11-24 01:06:19 +00:00
self.assertEqual([], group.expand_entity_ids(self.hass, [5, True]))
2014-11-24 00:46:59 +00:00
def test_get_entity_ids(self):
""" Test get_entity_ids method. """
self.assertEqual(
['light.bowl', 'light.ceiling'],
sorted(group.get_entity_ids(self.hass, self.group_entity_id)))
def test_get_entity_ids_with_domain_filter(self):
""" Test if get_entity_ids works with a domain_filter. """
self.hass.states.set('switch.AC', STATE_OFF)
mixed_group = group.Group(
self.hass, 'mixed_group', ['light.Bowl', 'switch.AC'], False)
2014-11-24 00:46:59 +00:00
2014-11-24 01:06:19 +00:00
self.assertEqual(
['switch.ac'],
2014-11-24 01:06:19 +00:00
group.get_entity_ids(
self.hass, mixed_group.entity_id, domain_filter="switch"))
2014-11-24 01:06:19 +00:00
def test_get_entity_ids_with_non_existing_group_name(self):
""" Tests get_entity_ids with a non existing group. """
2014-11-24 01:06:19 +00:00
self.assertEqual([], group.get_entity_ids(self.hass, 'non_existing'))
def test_get_entity_ids_with_non_group_state(self):
""" Tests get_entity_ids with a non group state. """
2014-11-24 01:06:19 +00:00
self.assertEqual([], group.get_entity_ids(self.hass, 'switch.AC'))
def test_group_being_init_before_first_tracked_state_is_set_to_on(self):
""" Test if the group turns on if no states existed and now a state it is
tracking is being added as ON. """
test_group = group.Group(
self.hass, 'test group', ['light.not_there_1'])
self.hass.states.set('light.not_there_1', STATE_ON)
self.hass.pool.block_till_done()
group_state = self.hass.states.get(test_group.entity_id)
self.assertEqual(STATE_ON, group_state.state)
def test_group_being_init_before_first_tracked_state_is_set_to_off(self):
""" Test if the group turns off if no states existed and now a state it is
tracking is being added as OFF. """
test_group = group.Group(
self.hass, 'test group', ['light.not_there_1'])
self.hass.states.set('light.not_there_1', STATE_OFF)
self.hass.pool.block_till_done()
group_state = self.hass.states.get(test_group.entity_id)
self.assertEqual(STATE_OFF, group_state.state)
2014-11-24 00:46:59 +00:00
def test_setup(self):
""" Test setup method. """
self.assertTrue(
group.setup(
self.hass,
{
group.DOMAIN: {
2016-01-24 22:13:39 +00:00
'second_group': {
'entities': 'light.Bowl, ' + self.group_entity_id,
'icon': 'mdi:work',
'view': True,
},
'test_group': 'hello.world,sensor.happy',
2014-11-24 00:46:59 +00:00
}
}))
group_state = self.hass.states.get(
group.ENTITY_ID_FORMAT.format('second_group'))
self.assertEqual(STATE_ON, group_state.state)
2015-08-31 03:44:38 +00:00
self.assertEqual(set((self.group_entity_id, 'light.bowl')),
set(group_state.attributes['entity_id']))
2016-01-24 22:13:39 +00:00
self.assertIsNone(group_state.attributes.get(group.ATTR_AUTO))
self.assertEqual('mdi:work',
group_state.attributes.get(ATTR_ICON))
self.assertTrue(group_state.attributes.get(group.ATTR_VIEW))
self.assertTrue(group_state.attributes.get(ATTR_HIDDEN))
2016-01-24 22:13:39 +00:00
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))
self.assertIsNone(group_state.attributes.get(ATTR_HIDDEN))
def test_groups_get_unique_names(self):
""" Two groups with same name should both have a unique entity id. """
grp1 = group.Group(self.hass, 'Je suis Charlie')
grp2 = group.Group(self.hass, 'Je suis Charlie')
self.assertNotEqual(grp1.entity_id, grp2.entity_id)
2016-02-10 06:43:07 +00:00
def test_expand_entity_ids_expands_nested_groups(self):
group.Group(self.hass, 'light', ['light.test_1', 'light.test_2'])
group.Group(self.hass, 'switch', ['switch.test_1', 'switch.test_2'])
group.Group(self.hass, 'group_of_groups', ['group.light',
'group.switch'])
self.assertEqual(
['light.test_1', 'light.test_2', 'switch.test_1', 'switch.test_2'],
sorted(group.expand_entity_ids(self.hass,
['group.group_of_groups'])))