Tweak integration sensor (#114384)

* Tweak integration sensor

* Improve tests
This commit is contained in:
Erik Montnemery 2024-04-02 09:19:25 +02:00 committed by GitHub
parent 667e119d32
commit 0030c97f59
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 65 additions and 33 deletions

View file

@ -629,8 +629,17 @@ async def test_device_class(hass: HomeAssistant, method) -> None:
assert state.attributes.get("device_class") == SensorDeviceClass.ENERGY
@pytest.mark.parametrize("method", ["trapezoidal", "left", "right"])
async def test_calc_errors(hass: HomeAssistant, method) -> None:
@pytest.mark.parametrize(
("method", "expected_states"),
[
("trapezoidal", [STATE_UNKNOWN, "0.500", "0.500"]),
("left", [STATE_UNKNOWN, "0.000", "1.000"]),
("right", ["0.000", "1.000", "1.000"]),
],
)
async def test_calc_errors(
hass: HomeAssistant, method: str, expected_states: list[str]
) -> None:
"""Test integration sensor units using a power source."""
config = {
"sensor": {
@ -649,9 +658,9 @@ async def test_calc_errors(hass: HomeAssistant, method) -> None:
hass.states.async_set(entity_id, None, {})
await hass.async_block_till_done()
state = hass.states.get("sensor.integration")
# With the source sensor in a None state, the Reimann sensor should be
# unknown
state = hass.states.get("sensor.integration")
assert state is not None
assert state.state == STATE_UNKNOWN
@ -665,7 +674,7 @@ async def test_calc_errors(hass: HomeAssistant, method) -> None:
state = hass.states.get("sensor.integration")
assert state is not None
assert state.state == STATE_UNKNOWN if method != "right" else "0.000"
assert state.state == expected_states[0]
# With the source sensor updated successfully, the Reimann sensor
# should have a zero (known) value.
@ -677,7 +686,18 @@ async def test_calc_errors(hass: HomeAssistant, method) -> None:
state = hass.states.get("sensor.integration")
assert state is not None
assert round(float(state.state)) == 0 if method != "right" else 1
assert state.state == expected_states[1]
# Set the source sensor back to a non numeric state
now += timedelta(seconds=3600)
with freeze_time(now):
hass.states.async_set(entity_id, "unexpected", {"device_class": None})
await hass.async_block_till_done()
await hass.async_block_till_done()
state = hass.states.get("sensor.integration")
assert state is not None
assert state.state == expected_states[2]
async def test_device_id(