Async EntitiesComponent (#3820)
* first version
* First draft component entities
* Change add_entities to callback from coroutine
* Fix bug add async_prepare_reload
* Group draft v1
* group async
* bugfix
* bugfix v2
* fix lint
* fix extract_entity_ids
* fix other things
* move get_component out of executor
* bugfix
* Address minor changes
* lint
* bugfix - should work now
* make group init async only
* change update handling to old stuff
* fix group handling, remove generator from init
* fix lint
* protect loop for spaming with updates
* fix lint
* update test_group
* fix
* update group handling
* fix __init__ async trouble
* move device_tracker to new layout
* lint
* fix group unittest
* Test with coroutine
* fix bug
* now it works 💯
* ups
* first part of suggestion
* add_entities to coroutine
* change group
* convert add async_add_entity to coroutine
* fix unit tests
* fix lint
* fix lint part 2
* fix wrong import delete
* change async_update_tracked_entity_ids to coroutine
* fix
* revert last change
* fix unittest entity id
* fix unittest
* fix unittest
* fix unittest entity_component
* fix group
* fix group_test
* try part 2 to fix test_group
* fix all entity_component
* rename _process_config
* Change Group to init with factory
* fix lint
* fix lint
* fix callback
* Tweak entity component and group
* More fixes
* Final fixes
* No longer needed blocks
* Address @bbangert comments
* Add test for group.stop
* More callbacks for automation
This commit is contained in:
1 parent
a0fdb2778d
commit
0b8b9ecb94
14 files changed
+503
-266
No files matched your search
@@ -4,7 +4,7 @@ from collections import OrderedDict
|
||||
import unittest
|
||||
from unittest.mock import patch
|
||||
|
||||
from homeassistant.bootstrap import _setup_component
|
||||
from homeassistant.bootstrap import setup_component
|
||||
from homeassistant.const import (
|
||||
STATE_ON, STATE_OFF, STATE_HOME, STATE_UNKNOWN, ATTR_ICON, ATTR_HIDDEN,
|
||||
ATTR_ASSUMED_STATE, STATE_NOT_HOME, )
|
||||
@@ -28,7 +28,7 @@ class TestComponentsGroup(unittest.TestCase):
|
||||
"""Try to setup a group with mixed groupable states."""
|
||||
self.hass.states.set('light.Bowl', STATE_ON)
|
||||
self.hass.states.set('device_tracker.Paulus', STATE_HOME)
|
||||
group.Group(
|
||||
group.Group.create_group(
|
||||
self.hass, 'person_and_light',
|
||||
['light.Bowl', 'device_tracker.Paulus'])
|
||||
|
||||
@@ -41,7 +41,7 @@ class TestComponentsGroup(unittest.TestCase):
|
||||
"""Try to setup a group with a non existing state."""
|
||||
self.hass.states.set('light.Bowl', STATE_ON)
|
||||
|
||||
grp = group.Group(
|
||||
grp = group.Group.create_group(
|
||||
self.hass, 'light_and_nothing',
|
||||
['light.Bowl', 'non.existing'])
|
||||
|
||||
@@ -52,7 +52,7 @@ class TestComponentsGroup(unittest.TestCase):
|
||||
self.hass.states.set('cast.living_room', "Plex")
|
||||
self.hass.states.set('cast.bedroom', "Netflix")
|
||||
|
||||
grp = group.Group(
|
||||
grp = group.Group.create_group(
|
||||
self.hass, 'chromecasts',
|
||||
['cast.living_room', 'cast.bedroom'])
|
||||
|
||||
@@ -60,7 +60,7 @@ class TestComponentsGroup(unittest.TestCase):
|
||||
|
||||
def test_setup_empty_group(self):
|
||||
"""Try to setup an empty group."""
|
||||
grp = group.Group(self.hass, 'nothing', [])
|
||||
grp = group.Group.create_group(self.hass, 'nothing', [])
|
||||
|
||||
self.assertEqual(STATE_UNKNOWN, grp.state)
|
||||
|
||||
@@ -68,7 +68,7 @@ class TestComponentsGroup(unittest.TestCase):
|
||||
"""Test if the group keeps track of states."""
|
||||
self.hass.states.set('light.Bowl', STATE_ON)
|
||||
self.hass.states.set('light.Ceiling', STATE_OFF)
|
||||
test_group = group.Group(
|
||||
test_group = group.Group.create_group(
|
||||
self.hass, 'init_group', ['light.Bowl', 'light.Ceiling'], False)
|
||||
|
||||
# Test if group setup in our init mode is ok
|
||||
@@ -82,7 +82,7 @@ class TestComponentsGroup(unittest.TestCase):
|
||||
"""Test if turn off if the last device that was on turns off."""
|
||||
self.hass.states.set('light.Bowl', STATE_OFF)
|
||||
self.hass.states.set('light.Ceiling', STATE_OFF)
|
||||
test_group = group.Group(
|
||||
test_group = group.Group.create_group(
|
||||
self.hass, 'init_group', ['light.Bowl', 'light.Ceiling'], False)
|
||||
|
||||
self.hass.block_till_done()
|
||||
@@ -94,7 +94,7 @@ class TestComponentsGroup(unittest.TestCase):
|
||||
"""Test if turn on if all devices were turned off and one turns on."""
|
||||
self.hass.states.set('light.Bowl', STATE_OFF)
|
||||
self.hass.states.set('light.Ceiling', STATE_OFF)
|
||||
test_group = group.Group(
|
||||
test_group = group.Group.create_group(
|
||||
self.hass, 'init_group', ['light.Bowl', 'light.Ceiling'], False)
|
||||
|
||||
# Turn one on
|
||||
@@ -108,7 +108,7 @@ class TestComponentsGroup(unittest.TestCase):
|
||||
"""Test is_on method."""
|
||||
self.hass.states.set('light.Bowl', STATE_ON)
|
||||
self.hass.states.set('light.Ceiling', STATE_OFF)
|
||||
test_group = group.Group(
|
||||
test_group = group.Group.create_group(
|
||||
self.hass, 'init_group', ['light.Bowl', 'light.Ceiling'], False)
|
||||
|
||||
self.assertTrue(group.is_on(self.hass, test_group.entity_id))
|
||||
@@ -123,7 +123,7 @@ class TestComponentsGroup(unittest.TestCase):
|
||||
"""Test expand_entity_ids method."""
|
||||
self.hass.states.set('light.Bowl', STATE_ON)
|
||||
self.hass.states.set('light.Ceiling', STATE_OFF)
|
||||
test_group = group.Group(
|
||||
test_group = group.Group.create_group(
|
||||
self.hass, 'init_group', ['light.Bowl', 'light.Ceiling'], False)
|
||||
|
||||
self.assertEqual(sorted(['light.ceiling', 'light.bowl']),
|
||||
@@ -134,7 +134,7 @@ class TestComponentsGroup(unittest.TestCase):
|
||||
"""Test that expand_entity_ids does not return duplicates."""
|
||||
self.hass.states.set('light.Bowl', STATE_ON)
|
||||
self.hass.states.set('light.Ceiling', STATE_OFF)
|
||||
test_group = group.Group(
|
||||
test_group = group.Group.create_group(
|
||||
self.hass, 'init_group', ['light.Bowl', 'light.Ceiling'], False)
|
||||
|
||||
self.assertEqual(
|
||||
@@ -155,7 +155,7 @@ class TestComponentsGroup(unittest.TestCase):
|
||||
"""Test get_entity_ids method."""
|
||||
self.hass.states.set('light.Bowl', STATE_ON)
|
||||
self.hass.states.set('light.Ceiling', STATE_OFF)
|
||||
test_group = group.Group(
|
||||
test_group = group.Group.create_group(
|
||||
self.hass, 'init_group', ['light.Bowl', 'light.Ceiling'], False)
|
||||
|
||||
self.assertEqual(
|
||||
@@ -166,7 +166,7 @@ class TestComponentsGroup(unittest.TestCase):
|
||||
"""Test if get_entity_ids works with a domain_filter."""
|
||||
self.hass.states.set('switch.AC', STATE_OFF)
|
||||
|
||||
mixed_group = group.Group(
|
||||
mixed_group = group.Group.create_group(
|
||||
self.hass, 'mixed_group', ['light.Bowl', 'switch.AC'], False)
|
||||
|
||||
self.assertEqual(
|
||||
@@ -188,7 +188,7 @@ class TestComponentsGroup(unittest.TestCase):
|
||||
If no states existed and now a state it is tracking is being added
|
||||
as ON.
|
||||
"""
|
||||
test_group = group.Group(
|
||||
test_group = group.Group.create_group(
|
||||
self.hass, 'test group', ['light.not_there_1'])
|
||||
|
||||
self.hass.states.set('light.not_there_1', STATE_ON)
|
||||
@@ -204,7 +204,7 @@ class TestComponentsGroup(unittest.TestCase):
|
||||
If no states existed and now a state it is tracking is being added
|
||||
as OFF.
|
||||
"""
|
||||
test_group = group.Group(
|
||||
test_group = group.Group.create_group(
|
||||
self.hass, 'test group', ['light.not_there_1'])
|
||||
|
||||
self.hass.states.set('light.not_there_1', STATE_OFF)
|
||||
@@ -218,7 +218,7 @@ class TestComponentsGroup(unittest.TestCase):
|
||||
"""Test setup method."""
|
||||
self.hass.states.set('light.Bowl', STATE_ON)
|
||||
self.hass.states.set('light.Ceiling', STATE_OFF)
|
||||
test_group = group.Group(
|
||||
test_group = group.Group.create_group(
|
||||
self.hass, 'init_group', ['light.Bowl', 'light.Ceiling'], False)
|
||||
|
||||
group_conf = OrderedDict()
|
||||
@@ -230,7 +230,7 @@ class TestComponentsGroup(unittest.TestCase):
|
||||
group_conf['test_group'] = 'hello.world,sensor.happy'
|
||||
group_conf['empty_group'] = {'name': 'Empty Group', 'entities': None}
|
||||
|
||||
_setup_component(self.hass, 'group', {'group': group_conf})
|
||||
setup_component(self.hass, 'group', {'group': group_conf})
|
||||
|
||||
group_state = self.hass.states.get(
|
||||
group.ENTITY_ID_FORMAT.format('second_group'))
|
||||
@@ -257,17 +257,19 @@ class TestComponentsGroup(unittest.TestCase):
|
||||
|
||||
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')
|
||||
grp1 = group.Group.create_group(self.hass, 'Je suis Charlie')
|
||||
grp2 = group.Group.create_group(self.hass, 'Je suis Charlie')
|
||||
|
||||
self.assertNotEqual(grp1.entity_id, grp2.entity_id)
|
||||
|
||||
def test_expand_entity_ids_expands_nested_groups(self):
|
||||
"""Test if entity ids epands to nested groups."""
|
||||
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'])
|
||||
group.Group.create_group(
|
||||
self.hass, 'light', ['light.test_1', 'light.test_2'])
|
||||
group.Group.create_group(
|
||||
self.hass, 'switch', ['switch.test_1', 'switch.test_2'])
|
||||
group.Group.create_group(self.hass, 'group_of_groups', ['group.light',
|
||||
'group.switch'])
|
||||
|
||||
self.assertEqual(
|
||||
['light.test_1', 'light.test_2', 'switch.test_1', 'switch.test_2'],
|
||||
@@ -278,7 +280,7 @@ class TestComponentsGroup(unittest.TestCase):
|
||||
"""Test assumed state."""
|
||||
self.hass.states.set('light.Bowl', STATE_ON)
|
||||
self.hass.states.set('light.Ceiling', STATE_OFF)
|
||||
test_group = group.Group(
|
||||
test_group = group.Group.create_group(
|
||||
self.hass, 'init_group',
|
||||
['light.Bowl', 'light.Ceiling', 'sensor.no_exist'])
|
||||
|
||||
@@ -304,7 +306,7 @@ class TestComponentsGroup(unittest.TestCase):
|
||||
self.hass.states.set('device_tracker.Adam', STATE_HOME)
|
||||
self.hass.states.set('device_tracker.Eve', STATE_NOT_HOME)
|
||||
self.hass.block_till_done()
|
||||
group.Group(
|
||||
group.Group.create_group(
|
||||
self.hass, 'peeps',
|
||||
['device_tracker.Adam', 'device_tracker.Eve'])
|
||||
self.hass.states.set('device_tracker.Adam', 'cool_state_not_home')
|
||||
@@ -315,7 +317,7 @@ class TestComponentsGroup(unittest.TestCase):
|
||||
|
||||
def test_reloading_groups(self):
|
||||
"""Test reloading the group config."""
|
||||
_setup_component(self.hass, 'group', {'group': {
|
||||
assert setup_component(self.hass, 'group', {'group': {
|
||||
'second_group': {
|
||||
'entities': 'light.Bowl',
|
||||
'icon': 'mdi:work',
|
||||
@@ -342,3 +344,11 @@ class TestComponentsGroup(unittest.TestCase):
|
||||
|
||||
assert self.hass.states.entity_ids() == ['group.hello']
|
||||
assert self.hass.bus.listeners['state_changed'] == 1
|
||||
|
||||
def test_stopping_a_group(self):
|
||||
"""Test that a group correctly removes itself."""
|
||||
grp = group.Group.create_group(
|
||||
self.hass, 'light', ['light.test_1', 'light.test_2'])
|
||||
assert self.hass.states.entity_ids() == ['group.light']
|
||||
grp.stop()
|
||||
assert self.hass.states.entity_ids() == []
|
||||
Reference in new issue
Block a user