mirror of
https://github.com/home-assistant/core.git
synced 2025-11-09 10:59:40 +00:00
Improve demo test coverage (#77301)
This commit is contained in:
59
tests/components/demo/test_sensor.py
Normal file
59
tests/components/demo/test_sensor.py
Normal file
@@ -0,0 +1,59 @@
|
||||
"""The tests for the demo sensor component."""
|
||||
from datetime import timedelta
|
||||
|
||||
import pytest
|
||||
|
||||
from homeassistant import core as ha
|
||||
from homeassistant.components.demo import DOMAIN
|
||||
from homeassistant.components.sensor import DOMAIN as SENSOR_DOMAIN
|
||||
from homeassistant.setup import async_setup_component
|
||||
|
||||
from tests.common import mock_restore_cache_with_extra_data
|
||||
|
||||
|
||||
@pytest.mark.parametrize("entity_id, delta", (("sensor.total_energy_kwh", 0.5),))
|
||||
async def test_energy_sensor(hass: ha.HomeAssistant, entity_id, delta, freezer):
|
||||
"""Test energy sensors increase periodically."""
|
||||
assert await async_setup_component(
|
||||
hass, SENSOR_DOMAIN, {SENSOR_DOMAIN: {"platform": DOMAIN}}
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
state = hass.states.get(entity_id)
|
||||
assert state.state == "0"
|
||||
|
||||
freezer.tick(timedelta(minutes=5, seconds=1))
|
||||
await hass.async_block_till_done()
|
||||
await hass.async_block_till_done()
|
||||
|
||||
state = hass.states.get(entity_id)
|
||||
assert state.state == str(delta)
|
||||
|
||||
|
||||
@pytest.mark.parametrize("entity_id, delta", (("sensor.total_energy_kwh", 0.5),))
|
||||
async def test_restore_state(hass: ha.HomeAssistant, entity_id, delta, freezer):
|
||||
"""Test energy sensors restore state."""
|
||||
fake_state = ha.State(
|
||||
entity_id,
|
||||
"",
|
||||
)
|
||||
fake_extra_data = {
|
||||
"native_value": 2**20,
|
||||
"native_unit_of_measurement": None,
|
||||
}
|
||||
mock_restore_cache_with_extra_data(hass, ((fake_state, fake_extra_data),))
|
||||
|
||||
assert await async_setup_component(
|
||||
hass, SENSOR_DOMAIN, {SENSOR_DOMAIN: {"platform": DOMAIN}}
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
state = hass.states.get(entity_id)
|
||||
assert state.state == str(2**20)
|
||||
|
||||
freezer.tick(timedelta(minutes=5, seconds=1))
|
||||
await hass.async_block_till_done()
|
||||
await hass.async_block_till_done()
|
||||
|
||||
state = hass.states.get(entity_id)
|
||||
assert state.state == str(2**20 + delta)
|
||||
Reference in New Issue
Block a user