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:
Pascal Vizeli authored and Paulus Schoutsen committed 2016-10-16 09:35:46 -07:00
1 parent a0fdb2778d
commit 0b8b9ecb94
14 files changed
+503 -266

No files matched your search

@@ -37,12 +37,6 @@ class TestNX584SensorSetup(unittest.TestCase):
"""Stop everything that was started."""
self._mock_client.stop()
def test_setup_no_config(self):
"""Test the setup with no configuration."""
hass = mock.MagicMock()
hass.pool.worker_count = 2
assert setup_component(hass, 'binary_sensor', {'nx584': {}})
@mock.patch('homeassistant.components.binary_sensor.nx584.NX584Watcher')
@mock.patch('homeassistant.components.binary_sensor.nx584.NX584ZoneSensor')
def test_setup_defaults(self, mock_nx, mock_watcher):
+18 -24
View File
@@ -9,11 +9,22 @@ from uvcclient import nvr
from homeassistant.bootstrap import setup_component
from homeassistant.components.camera import uvc
from tests.common import get_test_home_assistant
class TestUVCSetup(unittest.TestCase):
"""Test the UVC camera platform."""
def setUp(self):
"""Setup things to be run when tests are started."""
self.hass = get_test_home_assistant()
self.hass.wsgi = mock.MagicMock()
self.hass.config.components = ['http']
def tearDown(self):
"""Stop everything that was started."""
self.hass.stop()
@mock.patch('uvcclient.nvr.UVCRemote')
@mock.patch.object(uvc, 'UnifiVideoCamera')
def test_setup_full_config(self, mock_uvc, mock_remote):
@@ -37,14 +48,11 @@ class TestUVCSetup(unittest.TestCase):
else:
return {'model': 'UVC'}
hass = mock.MagicMock()
hass.pool.worker_count = 2
hass.config.components = ['http']
mock_remote.return_value.index.return_value = fake_cameras
mock_remote.return_value.get_camera.side_effect = fake_get_camera
mock_remote.return_value.server_version = (3, 2, 0)
assert setup_component(hass, 'camera', {'camera': config})
assert setup_component(self.hass, 'camera', {'camera': config})
mock_remote.assert_called_once_with('foo', 123, 'secret')
mock_uvc.assert_has_calls([
@@ -65,14 +73,11 @@ class TestUVCSetup(unittest.TestCase):
{'uuid': 'one', 'name': 'Front', 'id': 'id1'},
{'uuid': 'two', 'name': 'Back', 'id': 'id2'},
]
hass = mock.MagicMock()
hass.pool.worker_count = 2
hass.config.components = ['http']
mock_remote.return_value.index.return_value = fake_cameras
mock_remote.return_value.get_camera.return_value = {'model': 'UVC'}
mock_remote.return_value.server_version = (3, 2, 0)
assert setup_component(hass, 'camera', {'camera': config})
assert setup_component(self.hass, 'camera', {'camera': config})
mock_remote.assert_called_once_with('foo', 7080, 'secret')
mock_uvc.assert_has_calls([
@@ -93,14 +98,11 @@ class TestUVCSetup(unittest.TestCase):
{'uuid': 'one', 'name': 'Front', 'id': 'id1'},
{'uuid': 'two', 'name': 'Back', 'id': 'id2'},
]
hass = mock.MagicMock()
hass.pool.worker_count = 2
hass.config.components = ['http']
mock_remote.return_value.index.return_value = fake_cameras
mock_remote.return_value.get_camera.return_value = {'model': 'UVC'}
mock_remote.return_value.server_version = (3, 1, 3)
assert setup_component(hass, 'camera', {'camera': config})
assert setup_component(self.hass, 'camera', {'camera': config})
mock_remote.assert_called_once_with('foo', 7080, 'secret')
mock_uvc.assert_has_calls([
@@ -111,18 +113,14 @@ class TestUVCSetup(unittest.TestCase):
@mock.patch.object(uvc, 'UnifiVideoCamera')
def test_setup_incomplete_config(self, mock_uvc):
""""Test the setup with incomplete configuration."""
hass = mock.MagicMock()
hass.pool.worker_count = 2
hass.config.components = ['http']
assert setup_component(
hass, 'camera', {'platform': 'uvc', 'nvr': 'foo'})
self.hass, 'camera', {'platform': 'uvc', 'nvr': 'foo'})
assert not mock_uvc.called
assert setup_component(
hass, 'camera', {'platform': 'uvc', 'key': 'secret'})
self.hass, 'camera', {'platform': 'uvc', 'key': 'secret'})
assert not mock_uvc.called
assert setup_component(
hass, 'camera', {'platform': 'uvc', 'port': 'invalid'})
self.hass, 'camera', {'platform': 'uvc', 'port': 'invalid'})
assert not mock_uvc.called
@mock.patch.object(uvc, 'UnifiVideoCamera')
@@ -136,13 +134,9 @@ class TestUVCSetup(unittest.TestCase):
'nvr': 'foo',
'key': 'secret',
}
hass = mock.MagicMock()
hass.pool.worker_count = 2
hass.config.components = ['http']
for error in errors:
mock_remote.return_value.index.side_effect = error
assert setup_component(hass, 'camera', config)
assert setup_component(self.hass, 'camera', config)
assert not mock_uvc.called
+36 -26
View File
@@ -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() == []