Add a state attribute enum to tod (#189161)

This commit is contained in:
epenet
2026-10-28 10:58:02 +01:00
committed by GitHub
parent 7a9c0b008a
commit 8c43c16fbb
4 changed files with 193 additions and 58 deletions
+18 -6
View File
@@ -2,6 +2,7 @@
from collections.abc import Callable
from datetime import datetime, time, timedelta
from enum import StrEnum
import logging
from typing import Any, Literal, TypeGuard, override
@@ -41,9 +42,14 @@ type SunEventType = Literal["sunrise", "sunset"]
_LOGGER = logging.getLogger(__name__)
ATTR_AFTER = "after"
ATTR_BEFORE = "before"
ATTR_NEXT_UPDATE = "next_update"
class TodEntityStateAttribute(StrEnum):
"""State attributes for times of the day entities."""
AFTER = "after"
BEFORE = "before"
NEXT_UPDATE = "next_update"
PLATFORM_SCHEMA = BINARY_SENSOR_PLATFORM_SCHEMA.extend(
{
@@ -150,9 +156,15 @@ class TodSensor(BinarySensorEntity):
"""Return the state attributes of the sensor."""
if time_zone := dt_util.get_default_time_zone():
return {
ATTR_AFTER: self._time_after.astimezone(time_zone).isoformat(),
ATTR_BEFORE: self._time_before.astimezone(time_zone).isoformat(),
ATTR_NEXT_UPDATE: self._next_update.astimezone(time_zone).isoformat(),
TodEntityStateAttribute.AFTER: self._time_after.astimezone(
time_zone
).isoformat(),
TodEntityStateAttribute.BEFORE: self._time_before.astimezone(
time_zone
).isoformat(),
TodEntityStateAttribute.NEXT_UPDATE: self._next_update.astimezone(
time_zone
).isoformat(),
}
return None
+161 -48
View File
@@ -6,6 +6,7 @@ from freezegun.api import FrozenDateTimeFactory
import pytest
from pytest_homeassistant.common import assert_setup_component, async_fire_time_changed
from homeassistant.components.tod.binary_sensor import TodEntityStateAttribute
from homeassistant.const import STATE_OFF, STATE_ON
from homeassistant.core import HomeAssistant
from homeassistant.helpers import entity_registry as er
@@ -639,9 +640,16 @@ async def test_dst1(
await hass.async_block_till_done()
state = hass.states.get(entity_id)
assert state.attributes["after"] == "2019-03-31T03:00:00+02:00"
assert state.attributes["before"] == "2019-03-31T03:00:00+02:00"
assert state.attributes["next_update"] == "2019-03-31T03:00:00+02:00"
assert (
state.attributes[TodEntityStateAttribute.AFTER] == "2019-03-31T03:00:00+02:00"
)
assert (
state.attributes[TodEntityStateAttribute.BEFORE] == "2019-03-31T03:00:00+02:00"
)
assert (
state.attributes[TodEntityStateAttribute.NEXT_UPDATE]
== "2019-03-31T03:00:00+02:00"
)
assert state.state == STATE_OFF
# But the following day, the sensor should resume it normal operation.
@@ -650,9 +658,16 @@ async def test_dst1(
await hass.async_block_till_done()
state = hass.states.get(entity_id)
assert state.attributes["after"] == "2019-04-01T02:30:00+02:00"
assert state.attributes["before"] == "2019-04-01T02:40:00+02:00"
assert state.attributes["next_update"] == "2019-04-01T02:30:00+02:00"
assert (
state.attributes[TodEntityStateAttribute.AFTER] == "2019-04-01T02:30:00+02:00"
)
assert (
state.attributes[TodEntityStateAttribute.BEFORE] == "2019-04-01T02:40:00+02:00"
)
assert (
state.attributes[TodEntityStateAttribute.NEXT_UPDATE]
== "2019-04-01T02:30:00+02:00"
)
assert state.state == STATE_OFF
@@ -679,9 +694,16 @@ async def test_dst2(
await hass.async_block_till_done()
state = hass.states.get(entity_id)
assert state.attributes["after"] == "2019-03-31T04:30:00+02:00"
assert state.attributes["before"] == "2019-03-31T04:40:00+02:00"
assert state.attributes["next_update"] == "2019-03-31T04:30:00+02:00"
assert (
state.attributes[TodEntityStateAttribute.AFTER] == "2019-03-31T04:30:00+02:00"
)
assert (
state.attributes[TodEntityStateAttribute.BEFORE] == "2019-03-31T04:40:00+02:00"
)
assert (
state.attributes[TodEntityStateAttribute.NEXT_UPDATE]
== "2019-03-31T04:30:00+02:00"
)
assert state.state == STATE_OFF
@@ -709,9 +731,16 @@ async def test_dst3(
await hass.async_block_till_done()
state = hass.states.get(entity_id)
assert state.attributes["after"] == "2023-03-12T04:30:00-07:00"
assert state.attributes["before"] == "2023-03-12T04:40:00-07:00"
assert state.attributes["next_update"] == "2023-03-12T04:30:00-07:00"
assert (
state.attributes[TodEntityStateAttribute.AFTER] == "2023-03-12T04:30:00-07:00"
)
assert (
state.attributes[TodEntityStateAttribute.BEFORE] == "2023-03-12T04:40:00-07:00"
)
assert (
state.attributes[TodEntityStateAttribute.NEXT_UPDATE]
== "2023-03-12T04:30:00-07:00"
)
assert state.state == STATE_OFF
@@ -739,9 +768,16 @@ async def test_dst4(
await hass.async_block_till_done()
state = hass.states.get(entity_id)
assert state.attributes["after"] == "2023-11-05T04:30:00-08:00"
assert state.attributes["before"] == "2023-11-05T04:40:00-08:00"
assert state.attributes["next_update"] == "2023-11-05T04:30:00-08:00"
assert (
state.attributes[TodEntityStateAttribute.AFTER] == "2023-11-05T04:30:00-08:00"
)
assert (
state.attributes[TodEntityStateAttribute.BEFORE] == "2023-11-05T04:40:00-08:00"
)
assert (
state.attributes[TodEntityStateAttribute.NEXT_UPDATE]
== "2023-11-05T04:30:00-08:00"
)
assert state.state == STATE_OFF
@@ -769,9 +805,16 @@ async def test_dst5(
await hass.async_block_till_done()
state = hass.states.get(entity_id)
assert state.attributes["after"] == "2019-03-31T01:50:00+01:00"
assert state.attributes["before"] == "2019-03-31T03:00:00+02:00"
assert state.attributes["next_update"] == "2019-03-31T01:50:00+01:00"
assert (
state.attributes[TodEntityStateAttribute.AFTER] == "2019-03-31T01:50:00+01:00"
)
assert (
state.attributes[TodEntityStateAttribute.BEFORE] == "2019-03-31T03:00:00+02:00"
)
assert (
state.attributes[TodEntityStateAttribute.NEXT_UPDATE]
== "2019-03-31T01:50:00+01:00"
)
assert state.state == STATE_OFF
# Second, test state when sensor is ON but end time has rolled
@@ -781,9 +824,16 @@ async def test_dst5(
await hass.async_block_till_done()
state = hass.states.get(entity_id)
assert state.attributes["after"] == "2019-03-31T01:50:00+01:00"
assert state.attributes["before"] == "2019-03-31T03:00:00+02:00"
assert state.attributes["next_update"] == "2019-03-31T03:00:00+02:00"
assert (
state.attributes[TodEntityStateAttribute.AFTER] == "2019-03-31T01:50:00+01:00"
)
assert (
state.attributes[TodEntityStateAttribute.BEFORE] == "2019-03-31T03:00:00+02:00"
)
assert (
state.attributes[TodEntityStateAttribute.NEXT_UPDATE]
== "2019-03-31T03:00:00+02:00"
)
assert state.state == STATE_ON
@@ -812,9 +862,16 @@ async def test_dst6(
await hass.async_block_till_done()
state = hass.states.get(entity_id)
assert state.attributes["after"] == "2019-03-31T03:00:00+02:00"
assert state.attributes["before"] == "2019-03-31T03:10:00+02:00"
assert state.attributes["next_update"] == "2019-03-31T03:00:00+02:00"
assert (
state.attributes[TodEntityStateAttribute.AFTER] == "2019-03-31T03:00:00+02:00"
)
assert (
state.attributes[TodEntityStateAttribute.BEFORE] == "2019-03-31T03:10:00+02:00"
)
assert (
state.attributes[TodEntityStateAttribute.NEXT_UPDATE]
== "2019-03-31T03:00:00+02:00"
)
assert state.state == STATE_OFF
# Second, test state when sensor is ON but end time has rolled
@@ -824,9 +881,16 @@ async def test_dst6(
await hass.async_block_till_done()
state = hass.states.get(entity_id)
assert state.attributes["after"] == "2019-03-31T03:00:00+02:00"
assert state.attributes["before"] == "2019-03-31T03:10:00+02:00"
assert state.attributes["next_update"] == "2019-03-31T03:10:00+02:00"
assert (
state.attributes[TodEntityStateAttribute.AFTER] == "2019-03-31T03:00:00+02:00"
)
assert (
state.attributes[TodEntityStateAttribute.BEFORE] == "2019-03-31T03:10:00+02:00"
)
assert (
state.attributes[TodEntityStateAttribute.NEXT_UPDATE]
== "2019-03-31T03:10:00+02:00"
)
assert state.state == STATE_ON
@@ -852,9 +916,16 @@ async def test_simple_before_after_does_not_loop_utc_not_in_range(
state = hass.states.get("binary_sensor.night")
assert state.state == STATE_OFF
assert state.attributes["after"] == "2019-01-10T22:00:00+00:00"
assert state.attributes["before"] == "2019-01-11T06:00:00+00:00"
assert state.attributes["next_update"] == "2019-01-10T22:00:00+00:00"
assert (
state.attributes[TodEntityStateAttribute.AFTER] == "2019-01-10T22:00:00+00:00"
)
assert (
state.attributes[TodEntityStateAttribute.BEFORE] == "2019-01-11T06:00:00+00:00"
)
assert (
state.attributes[TodEntityStateAttribute.NEXT_UPDATE]
== "2019-01-10T22:00:00+00:00"
)
@pytest.mark.freeze_time("2019-01-10 22:43:00")
@@ -878,9 +949,16 @@ async def test_simple_before_after_does_not_loop_utc_in_range(
state = hass.states.get("binary_sensor.night")
assert state.state == STATE_ON
assert state.attributes["after"] == "2019-01-10T22:00:00+00:00"
assert state.attributes["before"] == "2019-01-11T06:00:00+00:00"
assert state.attributes["next_update"] == "2019-01-11T06:00:00+00:00"
assert (
state.attributes[TodEntityStateAttribute.AFTER] == "2019-01-10T22:00:00+00:00"
)
assert (
state.attributes[TodEntityStateAttribute.BEFORE] == "2019-01-11T06:00:00+00:00"
)
assert (
state.attributes[TodEntityStateAttribute.NEXT_UPDATE]
== "2019-01-11T06:00:00+00:00"
)
@pytest.mark.freeze_time("2019-01-11 06:00:00")
@@ -904,9 +982,16 @@ async def test_simple_before_after_does_not_loop_utc_fire_at_before(
state = hass.states.get("binary_sensor.night")
assert state.state == STATE_OFF
assert state.attributes["after"] == "2019-01-11T22:00:00+00:00"
assert state.attributes["before"] == "2019-01-12T06:00:00+00:00"
assert state.attributes["next_update"] == "2019-01-11T22:00:00+00:00"
assert (
state.attributes[TodEntityStateAttribute.AFTER] == "2019-01-11T22:00:00+00:00"
)
assert (
state.attributes[TodEntityStateAttribute.BEFORE] == "2019-01-12T06:00:00+00:00"
)
assert (
state.attributes[TodEntityStateAttribute.NEXT_UPDATE]
== "2019-01-11T22:00:00+00:00"
)
@pytest.mark.freeze_time("2019-01-10 22:00:00")
@@ -930,9 +1015,16 @@ async def test_simple_before_after_does_not_loop_utc_fire_at_after(
state = hass.states.get("binary_sensor.night")
assert state.state == STATE_ON
assert state.attributes["after"] == "2019-01-10T22:00:00+00:00"
assert state.attributes["before"] == "2019-01-11T06:00:00+00:00"
assert state.attributes["next_update"] == "2019-01-11T06:00:00+00:00"
assert (
state.attributes[TodEntityStateAttribute.AFTER] == "2019-01-10T22:00:00+00:00"
)
assert (
state.attributes[TodEntityStateAttribute.BEFORE] == "2019-01-11T06:00:00+00:00"
)
assert (
state.attributes[TodEntityStateAttribute.NEXT_UPDATE]
== "2019-01-11T06:00:00+00:00"
)
@pytest.mark.freeze_time("2019-01-10 22:00:00")
@@ -956,9 +1048,16 @@ async def test_simple_before_after_does_not_loop_utc_both_before_now(
state = hass.states.get("binary_sensor.morning")
assert state.state == STATE_OFF
assert state.attributes["after"] == "2019-01-11T00:00:00+00:00"
assert state.attributes["before"] == "2019-01-11T08:00:00+00:00"
assert state.attributes["next_update"] == "2019-01-11T00:00:00+00:00"
assert (
state.attributes[TodEntityStateAttribute.AFTER] == "2019-01-11T00:00:00+00:00"
)
assert (
state.attributes[TodEntityStateAttribute.BEFORE] == "2019-01-11T08:00:00+00:00"
)
assert (
state.attributes[TodEntityStateAttribute.NEXT_UPDATE]
== "2019-01-11T00:00:00+00:00"
)
@pytest.mark.freeze_time("2019-01-10 17:43:00+01:00")
@@ -982,9 +1081,16 @@ async def test_simple_before_after_does_not_loop_berlin_not_in_range(
state = hass.states.get("binary_sensor.dark")
assert state.state == STATE_OFF
assert state.attributes["after"] == "2019-01-11T00:00:00+01:00"
assert state.attributes["before"] == "2019-01-11T06:00:00+01:00"
assert state.attributes["next_update"] == "2019-01-11T00:00:00+01:00"
assert (
state.attributes[TodEntityStateAttribute.AFTER] == "2019-01-11T00:00:00+01:00"
)
assert (
state.attributes[TodEntityStateAttribute.BEFORE] == "2019-01-11T06:00:00+01:00"
)
assert (
state.attributes[TodEntityStateAttribute.NEXT_UPDATE]
== "2019-01-11T00:00:00+01:00"
)
@pytest.mark.freeze_time("2019-01-11 00:43:00+01:00")
@@ -1008,9 +1114,16 @@ async def test_simple_before_after_does_not_loop_berlin_in_range(
state = hass.states.get("binary_sensor.dark")
assert state.state == STATE_ON
assert state.attributes["after"] == "2019-01-11T00:00:00+01:00"
assert state.attributes["before"] == "2019-01-11T06:00:00+01:00"
assert state.attributes["next_update"] == "2019-01-11T06:00:00+01:00"
assert (
state.attributes[TodEntityStateAttribute.AFTER] == "2019-01-11T00:00:00+01:00"
)
assert (
state.attributes[TodEntityStateAttribute.BEFORE] == "2019-01-11T06:00:00+01:00"
)
assert (
state.attributes[TodEntityStateAttribute.NEXT_UPDATE]
== "2019-01-11T06:00:00+01:00"
)
async def test_unique_id(
+7 -2
View File
@@ -6,6 +6,7 @@ import pytest
from pytest_homeassistant.common import MockConfigEntry, get_schema_suggested_value
from homeassistant import config_entries
from homeassistant.components.tod.binary_sensor import TodEntityStateAttribute
from homeassistant.components.tod.const import DOMAIN
from homeassistant.core import HomeAssistant
from homeassistant.data_entry_flow import FlowResultType
@@ -109,5 +110,9 @@ async def test_options(hass: HomeAssistant) -> None:
# Check the state of the entity has changed as expected
state = hass.states.get("binary_sensor.my_tod")
assert state.state == "off"
assert state.attributes["after"] == "2022-03-16T10:00:00-07:00"
assert state.attributes["before"] == "2022-03-16T17:05:00-07:00"
assert (
state.attributes[TodEntityStateAttribute.AFTER] == "2022-03-16T10:00:00-07:00"
)
assert (
state.attributes[TodEntityStateAttribute.BEFORE] == "2022-03-16T17:05:00-07:00"
)
+7 -2
View File
@@ -3,6 +3,7 @@
import pytest
from pytest_homeassistant.common import MockConfigEntry
from homeassistant.components.tod.binary_sensor import TodEntityStateAttribute
from homeassistant.components.tod.const import DOMAIN
from homeassistant.core import HomeAssistant
from homeassistant.helpers import entity_registry as er
@@ -38,8 +39,12 @@ async def test_setup_and_remove_config_entry(
# Check the state of the entity is as expected
state = hass.states.get("binary_sensor.my_tod")
assert state.state == "off"
assert state.attributes["after"] == "2022-03-16T10:00:00-07:00"
assert state.attributes["before"] == "2022-03-16T18:05:00-07:00"
assert (
state.attributes[TodEntityStateAttribute.AFTER] == "2022-03-16T10:00:00-07:00"
)
assert (
state.attributes[TodEntityStateAttribute.BEFORE] == "2022-03-16T18:05:00-07:00"
)
# Remove the config entry
assert await hass.config_entries.async_remove(config_entry.entry_id)