Spread async love (#3575)

* Convert Entity.update_ha_state to be async

* Make Service.call async

* Update entity.py

* Add Entity.async_update

* Make automation zone trigger async

* Fix linting

* Reduce flakiness in hass.block_till_done

* Make automation.numeric_state async

* Make mqtt.subscribe async

* Make automation.mqtt async

* Make automation.time async

* Make automation.sun async

* Add async_track_point_in_utc_time

* Make helpers.track_sunrise/set async

* Add async_track_state_change

* Make automation.state async

* Clean up helpers/entity.py tests

* Lint

* Lint

* Core.is_state and Core.is_state_attr are async friendly

* Lint

* Lint
This commit is contained in:
Paulus Schoutsen authored and GitHub committed 2016-09-30 12:57:24 -07:00
1 parent 7e50ccd32a
commit b650b2b0db
17 files changed
+323 -151

No files matched your search

+2 -1
View File
@@ -77,7 +77,8 @@ class TestComponentsCore(unittest.TestCase):
service_call = ha.ServiceCall('homeassistant', 'turn_on', {
'entity_id': ['light.test', 'sensor.bla', 'light.bla']
})
self.hass.services._services['homeassistant']['turn_on'](service_call)
service = self.hass.services._services['homeassistant']['turn_on']
service.func(service_call)
self.assertEqual(2, mock_call.call_count)
self.assertEqual(
+8 -5
View File
@@ -1,7 +1,8 @@
"""The tests for the logbook component."""
# pylint: disable=protected-access,too-many-public-methods
import unittest
from datetime import timedelta
import unittest
from unittest.mock import patch
from homeassistant.components import sun
import homeassistant.core as ha
@@ -18,13 +19,17 @@ from tests.common import mock_http_component, get_test_home_assistant
class TestComponentLogbook(unittest.TestCase):
"""Test the History component."""
EMPTY_CONFIG = logbook.CONFIG_SCHEMA({ha.DOMAIN: {}, logbook.DOMAIN: {}})
EMPTY_CONFIG = logbook.CONFIG_SCHEMA({logbook.DOMAIN: {}})
def setUp(self):
"""Setup things to be run when tests are started."""
self.hass = get_test_home_assistant()
mock_http_component(self.hass)
assert setup_component(self.hass, logbook.DOMAIN, self.EMPTY_CONFIG)
self.hass.config.components += ['frontend', 'recorder', 'api']
with patch('homeassistant.components.logbook.'
'register_built_in_panel'):
assert setup_component(self.hass, logbook.DOMAIN,
self.EMPTY_CONFIG)
def tearDown(self):
"""Stop everything that was started."""
@@ -44,7 +49,6 @@ class TestComponentLogbook(unittest.TestCase):
logbook.ATTR_DOMAIN: 'switch',
logbook.ATTR_ENTITY_ID: 'switch.test_switch'
}, True)
self.hass.block_till_done()
self.assertEqual(1, len(calls))
last_call = calls[-1]
@@ -65,7 +69,6 @@ class TestComponentLogbook(unittest.TestCase):
self.hass.bus.listen(logbook.EVENT_LOGBOOK_ENTRY, event_listener)
self.hass.services.call(logbook.DOMAIN, 'log', {}, True)
self.hass.block_till_done()
self.assertEqual(0, len(calls))
+64 -32
View File
@@ -1,6 +1,9 @@
"""Test the entity helper."""
# pylint: disable=protected-access,too-many-public-methods
import unittest
import asyncio
from unittest.mock import MagicMock
import pytest
import homeassistant.helpers.entity as entity
from homeassistant.const import ATTR_HIDDEN
@@ -8,26 +11,75 @@ from homeassistant.const import ATTR_HIDDEN
from tests.common import get_test_home_assistant
class TestHelpersEntity(unittest.TestCase):
def test_generate_entity_id_requires_hass_or_ids():
"""Ensure we require at least hass or current ids."""
fmt = 'test.{}'
with pytest.raises(ValueError):
entity.generate_entity_id(fmt, 'hello world')
def test_generate_entity_id_given_keys():
"""Test generating an entity id given current ids."""
fmt = 'test.{}'
assert entity.generate_entity_id(
fmt, 'overwrite hidden true', current_ids=[
'test.overwrite_hidden_true']) == 'test.overwrite_hidden_true_2'
assert entity.generate_entity_id(
fmt, 'overwrite hidden true', current_ids=[
'test.another_entity']) == 'test.overwrite_hidden_true'
def test_async_update_support(event_loop):
"""Test async update getting called."""
sync_update = []
async_update = []
class AsyncEntity(entity.Entity):
hass = MagicMock()
entity_id = 'sensor.test'
def update(self):
sync_update.append([1])
ent = AsyncEntity()
ent.hass.loop = event_loop
@asyncio.coroutine
def test():
yield from ent.async_update_ha_state(True)
event_loop.run_until_complete(test())
assert len(sync_update) == 1
assert len(async_update) == 0
ent.async_update = lambda: async_update.append(1)
event_loop.run_until_complete(test())
assert len(sync_update) == 1
assert len(async_update) == 1
class TestHelpersEntity(object):
"""Test homeassistant.helpers.entity module."""
def setUp(self): # pylint: disable=invalid-name
def setup_method(self, method):
"""Setup things to be run when tests are started."""
self.entity = entity.Entity()
self.entity.entity_id = 'test.overwrite_hidden_true'
self.hass = self.entity.hass = get_test_home_assistant()
self.entity.update_ha_state()
def tearDown(self): # pylint: disable=invalid-name
def teardown_method(self, method):
"""Stop everything that was started."""
self.hass.stop()
entity.set_customize({})
self.hass.stop()
def test_default_hidden_not_in_attributes(self):
"""Test that the default hidden property is set to False."""
self.assertNotIn(
ATTR_HIDDEN,
self.hass.states.get(self.entity.entity_id).attributes)
assert ATTR_HIDDEN not in self.hass.states.get(
self.entity.entity_id).attributes
def test_overwriting_hidden_property_to_true(self):
"""Test we can overwrite hidden property to True."""
@@ -35,31 +87,11 @@ class TestHelpersEntity(unittest.TestCase):
self.entity.update_ha_state()
state = self.hass.states.get(self.entity.entity_id)
self.assertTrue(state.attributes.get(ATTR_HIDDEN))
def test_generate_entity_id_requires_hass_or_ids(self):
"""Ensure we require at least hass or current ids."""
fmt = 'test.{}'
with self.assertRaises(ValueError):
entity.generate_entity_id(fmt, 'hello world')
assert state.attributes.get(ATTR_HIDDEN)
def test_generate_entity_id_given_hass(self):
"""Test generating an entity id given hass object."""
fmt = 'test.{}'
self.assertEqual(
'test.overwrite_hidden_true_2',
entity.generate_entity_id(fmt, 'overwrite hidden true',
hass=self.hass))
def test_generate_entity_id_given_keys(self):
"""Test generating an entity id given current ids."""
fmt = 'test.{}'
self.assertEqual(
'test.overwrite_hidden_true_2',
entity.generate_entity_id(
fmt, 'overwrite hidden true',
current_ids=['test.overwrite_hidden_true']))
self.assertEqual(
'test.overwrite_hidden_true',
entity.generate_entity_id(fmt, 'overwrite hidden true',
current_ids=['test.another_entity']))
assert entity.generate_entity_id(
fmt, 'overwrite hidden true',
hass=self.hass) == 'test.overwrite_hidden_true_2'
+23 -2
View File
@@ -1,6 +1,7 @@
"""Test to verify that Home Assistant core works."""
# pylint: disable=protected-access,too-many-public-methods
# pylint: disable=too-few-public-methods
import asyncio
import os
import signal
import unittest
@@ -362,7 +363,6 @@ class TestServiceRegistry(unittest.TestCase):
self.hass = get_test_home_assistant()
self.services = self.hass.services
self.services.register("Test_Domain", "TEST_SERVICE", lambda x: None)
self.hass.block_till_done()
def tearDown(self): # pylint: disable=invalid-name
"""Stop down stuff we started."""
@@ -387,8 +387,13 @@ class TestServiceRegistry(unittest.TestCase):
def test_call_with_blocking_done_in_time(self):
"""Test call with blocking."""
calls = []
def service_handler(call):
"""Service handler."""
calls.append(call)
self.services.register("test_domain", "register_calls",
lambda x: calls.append(1))
service_handler)
self.assertTrue(
self.services.call('test_domain', 'REGISTER_CALLS', blocking=True))
@@ -404,6 +409,22 @@ class TestServiceRegistry(unittest.TestCase):
finally:
ha.SERVICE_CALL_LIMIT = prior
def test_async_service(self):
"""Test registering and calling an async service."""
calls = []
@asyncio.coroutine
def service_handler(call):
"""Service handler coroutine."""
calls.append(call)
self.services.register('test_domain', 'register_calls',
service_handler)
self.assertTrue(
self.services.call('test_domain', 'REGISTER_CALLS', blocking=True))
self.hass.block_till_done()
self.assertEqual(1, len(calls))
class TestConfig(unittest.TestCase):
"""Test configuration methods."""