Fix Jewish calendar not updating (#146465)

This commit is contained in:
Tsvi Mostovicz 2025-06-10 22:25:47 +03:00 committed by GitHub
parent d015dff855
commit b7404f5a05
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 32 additions and 8 deletions

View File

@ -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)

View File

@ -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: