Have Utility Meter monitor Timezone changes in configuration (#131112)

* listen to config changes for possible DST changes

* Add test

* check tz actually changed

* Update tests/components/utility_meter/test_sensor.py

Co-authored-by: Abílio Costa <abmantis@users.noreply.github.com>

* Update tests/components/utility_meter/test_sensor.py

Co-authored-by: Abílio Costa <abmantis@users.noreply.github.com>

* Clean up comment

---------

Co-authored-by: Abílio Costa <abmantis@users.noreply.github.com>
Co-authored-by: Martin Hjelmare <marhje52@gmail.com>
This commit is contained in:
Diogo Gomes 2024-11-27 11:12:45 +00:00 committed by GitHub
parent 56b4733e4a
commit 345c1fe0b2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 40 additions and 0 deletions

View File

@ -27,6 +27,7 @@ from homeassistant.const import (
ATTR_UNIT_OF_MEASUREMENT,
CONF_NAME,
CONF_UNIQUE_ID,
EVENT_CORE_CONFIG_UPDATE,
STATE_UNAVAILABLE,
STATE_UNKNOWN,
)
@ -404,6 +405,10 @@ class UtilityMeterSensor(RestoreSensor):
self._tariff = tariff
self._tariff_entity = tariff_entity
self._next_reset = None
self._current_tz = None
self._config_scheduler()
def _config_scheduler(self):
self.scheduler = (
CronSim(
self._cron_pattern,
@ -565,6 +570,7 @@ class UtilityMeterSensor(RestoreSensor):
self._next_reset,
)
)
self.async_write_ha_state()
async def _async_reset_meter(self, event):
"""Reset the utility meter status."""
@ -601,6 +607,10 @@ class UtilityMeterSensor(RestoreSensor):
"""Handle entity which will be added."""
await super().async_added_to_hass()
# track current timezone in case it changes
# and we need to reconfigure the scheduler
self._current_tz = self.hass.config.time_zone
await self._program_reset()
self.async_on_remove(
@ -655,6 +665,19 @@ class UtilityMeterSensor(RestoreSensor):
self.async_on_remove(async_at_started(self.hass, async_source_tracking))
async def async_track_time_zone(event):
"""Reconfigure Scheduler after time zone changes."""
if self._current_tz != self.hass.config.time_zone:
self._current_tz = self.hass.config.time_zone
self._config_scheduler()
await self._program_reset()
self.async_on_remove(
self.hass.bus.async_listen(EVENT_CORE_CONFIG_UPDATE, async_track_time_zone)
)
async def async_will_remove_from_hass(self) -> None:
"""Run when entity will be removed from hass."""
if self._collecting:

View File

@ -1764,6 +1764,23 @@ async def test_self_reset_hourly_dst2(hass: HomeAssistant) -> None:
assert state.attributes.get("next_reset") == next_reset
async def test_tz_changes(hass: HomeAssistant) -> None:
"""Test that a timezone change changes the scheduler."""
await hass.config.async_update(time_zone="Europe/Prague")
await _test_self_reset(
hass, gen_config("daily"), "2024-10-26T23:59:00.000000+02:00"
)
state = hass.states.get("sensor.energy_bill")
assert state.attributes.get("next_reset") == "2024-10-28T00:00:00+01:00"
await hass.config.async_update(time_zone="Pacific/Fiji")
state = hass.states.get("sensor.energy_bill")
assert state.attributes.get("next_reset") != "2024-10-28T00:00:00+01:00"
async def test_self_reset_daily(hass: HomeAssistant) -> None:
"""Test daily reset of meter."""
await _test_self_reset(