core/tests/helpers/test_entity.py

98 lines
3.0 KiB
Python
Raw Normal View History

2016-03-09 09:25:50 +00:00
"""Test the entity helper."""
2015-04-23 13:41:41 +00:00
# pylint: disable=protected-access,too-many-public-methods
import asyncio
from unittest.mock import MagicMock
import pytest
2015-04-23 13:41:41 +00:00
import homeassistant.helpers.entity as entity
from homeassistant.const import ATTR_HIDDEN
2016-02-14 23:08:23 +00:00
from tests.common import get_test_home_assistant
2015-04-23 13:41:41 +00:00
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):
2016-03-09 09:25:50 +00:00
"""Test homeassistant.helpers.entity module."""
2015-04-23 13:41:41 +00:00
def setup_method(self, method):
2016-03-09 10:15:04 +00:00
"""Setup things to be run when tests are started."""
2015-04-23 13:41:41 +00:00
self.entity = entity.Entity()
self.entity.entity_id = 'test.overwrite_hidden_true'
2016-02-14 23:08:23 +00:00
self.hass = self.entity.hass = get_test_home_assistant()
2015-04-23 14:33:59 +00:00
self.entity.update_ha_state()
2015-04-23 13:41:41 +00:00
def teardown_method(self, method):
2016-03-09 09:25:50 +00:00
"""Stop everything that was started."""
entity.set_customize({})
self.hass.stop()
2015-04-23 13:41:41 +00:00
def test_default_hidden_not_in_attributes(self):
2016-03-09 09:25:50 +00:00
"""Test that the default hidden property is set to False."""
assert ATTR_HIDDEN not in self.hass.states.get(
self.entity.entity_id).attributes
2015-04-23 13:41:41 +00:00
def test_overwriting_hidden_property_to_true(self):
2016-03-09 09:25:50 +00:00
"""Test we can overwrite hidden property to True."""
entity.set_customize({self.entity.entity_id: {ATTR_HIDDEN: True}})
2015-04-23 13:41:41 +00:00
self.entity.update_ha_state()
state = self.hass.states.get(self.entity.entity_id)
assert state.attributes.get(ATTR_HIDDEN)
def test_generate_entity_id_given_hass(self):
"""Test generating an entity id given hass object."""
fmt = 'test.{}'
assert entity.generate_entity_id(
fmt, 'overwrite hidden true',
hass=self.hass) == 'test.overwrite_hidden_true_2'