Skip to content
Snippets Groups Projects
Unverified Commit 0534b0de authored by Erik Montnemery's avatar Erik Montnemery Committed by GitHub
Browse files

Improve entity tests (#106175)

parent 39a956ce
No related branches found
No related tags found
No related merge requests found
...@@ -31,7 +31,6 @@ from tests.common import ( ...@@ -31,7 +31,6 @@ from tests.common import (
MockEntityPlatform, MockEntityPlatform,
MockModule, MockModule,
MockPlatform, MockPlatform,
get_test_home_assistant,
mock_integration, mock_integration,
mock_registry, mock_registry,
) )
...@@ -61,6 +60,17 @@ def test_generate_entity_id_given_keys() -> None: ...@@ -61,6 +60,17 @@ def test_generate_entity_id_given_keys() -> None:
) )
async def test_generate_entity_id_given_hass(hass: HomeAssistant) -> None:
"""Test generating an entity id given hass object."""
hass.states.async_set("test.overwrite_hidden_true", "test")
fmt = "test.{}"
assert (
entity.generate_entity_id(fmt, "overwrite hidden true", hass=hass)
== "test.overwrite_hidden_true_2"
)
async def test_async_update_support(hass: HomeAssistant) -> None: async def test_async_update_support(hass: HomeAssistant) -> None:
"""Test async update getting called.""" """Test async update getting called."""
sync_update = [] sync_update = []
...@@ -95,40 +105,19 @@ async def test_async_update_support(hass: HomeAssistant) -> None: ...@@ -95,40 +105,19 @@ async def test_async_update_support(hass: HomeAssistant) -> None:
assert len(async_update) == 1 assert len(async_update) == 1
class TestHelpersEntity: async def test_device_class(hass: HomeAssistant) -> None:
"""Test homeassistant.helpers.entity module.""" """Test device class attribute."""
ent = entity.Entity()
def setup_method(self, method): ent.entity_id = "test.overwrite_hidden_true"
"""Set up things to be run when tests are started.""" ent.hass = hass
self.entity = entity.Entity() ent.async_write_ha_state()
self.entity.entity_id = "test.overwrite_hidden_true" state = hass.states.get(ent.entity_id)
self.hass = self.entity.hass = get_test_home_assistant() assert state.attributes.get(ATTR_DEVICE_CLASS) is None
self.entity.schedule_update_ha_state()
self.hass.block_till_done()
def teardown_method(self, method):
"""Stop everything that was started."""
self.hass.stop()
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"
)
def test_device_class(self): ent._attr_device_class = "test_class"
"""Test device class attribute.""" ent.async_write_ha_state()
state = self.hass.states.get(self.entity.entity_id) state = hass.states.get(ent.entity_id)
assert state.attributes.get(ATTR_DEVICE_CLASS) is None assert state.attributes.get(ATTR_DEVICE_CLASS) == "test_class"
with patch(
"homeassistant.helpers.entity.Entity.device_class", new="test_class"
):
self.entity.schedule_update_ha_state()
self.hass.block_till_done()
state = self.hass.states.get(self.entity.entity_id)
assert state.attributes.get(ATTR_DEVICE_CLASS) == "test_class"
async def test_warn_slow_update( async def test_warn_slow_update(
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment