From b7404f5a055ad0f48928fa0581f155d60ab54713 Mon Sep 17 00:00:00 2001 From: Tsvi Mostovicz Date: Tue, 10 Jun 2025 22:25:47 +0300 Subject: [PATCH] Fix Jewish calendar not updating (#146465) --- .../components/jewish_calendar/sensor.py | 9 ++---- .../components/jewish_calendar/test_sensor.py | 31 ++++++++++++++++++- 2 files changed, 32 insertions(+), 8 deletions(-) diff --git a/homeassistant/components/jewish_calendar/sensor.py b/homeassistant/components/jewish_calendar/sensor.py index 230adef9894..cb38a3797eb 100644 --- a/homeassistant/components/jewish_calendar/sensor.py +++ b/homeassistant/components/jewish_calendar/sensor.py @@ -225,7 +225,7 @@ async def async_setup_entry( JewishCalendarTimeSensor(config_entry, description) for description in TIME_SENSORS ) - async_add_entities(sensors) + async_add_entities(sensors, update_before_add=True) class JewishCalendarBaseSensor(JewishCalendarEntity, SensorEntity): @@ -233,12 +233,7 @@ class JewishCalendarBaseSensor(JewishCalendarEntity, SensorEntity): _attr_entity_category = EntityCategory.DIAGNOSTIC - async def async_added_to_hass(self) -> None: - """Call when entity is added to hass.""" - await super().async_added_to_hass() - await self.async_update_data() - - async def async_update_data(self) -> None: + async def async_update(self) -> None: """Update the state of the sensor.""" now = dt_util.now() _LOGGER.debug("Now: %s Location: %r", now, self.data.location) diff --git a/tests/components/jewish_calendar/test_sensor.py b/tests/components/jewish_calendar/test_sensor.py index 0cc1e60efc8..38a3dd12206 100644 --- a/tests/components/jewish_calendar/test_sensor.py +++ b/tests/components/jewish_calendar/test_sensor.py @@ -3,6 +3,7 @@ from datetime import datetime as dt from typing import Any +from freezegun.api import FrozenDateTimeFactory from hdate.holidays import HolidayDatabase from hdate.parasha import Parasha import pytest @@ -14,7 +15,7 @@ from homeassistant.core import HomeAssistant from homeassistant.setup import async_setup_component from homeassistant.util import dt as dt_util -from tests.common import MockConfigEntry +from tests.common import MockConfigEntry, async_fire_time_changed @pytest.mark.parametrize("language", ["en", "he"]) @@ -542,6 +543,34 @@ async def test_dafyomi_sensor(hass: HomeAssistant, results: str) -> None: assert hass.states.get("sensor.jewish_calendar_daf_yomi").state == results +@pytest.mark.parametrize( + ("test_time", "results"), + [ + ( + dt(2025, 6, 10, 17), + { + "initial_state": "14 Sivan 5785", + "move_to": dt(2025, 6, 10, 23, 0), + "new_state": "15 Sivan 5785", + }, + ), + ], + indirect=True, +) +@pytest.mark.usefixtures("setup_at_time") +async def test_sensor_does_not_update_on_time_change( + hass: HomeAssistant, freezer: FrozenDateTimeFactory, results: dict[str, Any] +) -> None: + """Test that the Jewish calendar sensor does not update after time advances (regression test for update bug).""" + sensor_id = "sensor.jewish_calendar_date" + assert hass.states.get(sensor_id).state == results["initial_state"] + + freezer.move_to(results["move_to"]) + async_fire_time_changed(hass) + await hass.async_block_till_done() + assert hass.states.get(sensor_id).state == results["new_state"] + + async def test_no_discovery_info( hass: HomeAssistant, caplog: pytest.LogCaptureFixture ) -> None: