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:
parent
7e50ccd32a
commit
b650b2b0db
17 changed files with 323 additions and 151 deletions
|
@ -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'
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue