mirror of
https://github.com/home-assistant/core.git
synced 2026-08-24 10:13:52 -05:00
Remove listener from holiday calendar when entity is disabled (#173759)
This commit is contained in:
@@ -151,6 +151,13 @@ class HolidayCalendarEntity(CalendarEntity):
|
||||
"""Set up first update."""
|
||||
self._update_state_and_setup_listener()
|
||||
|
||||
async def async_will_remove_from_hass(self) -> None:
|
||||
"""Cancel listener when removing."""
|
||||
await super().async_will_remove_from_hass()
|
||||
if self.unsub:
|
||||
self.unsub()
|
||||
self.unsub = None
|
||||
|
||||
def update_event(self, now: datetime) -> CalendarEvent | None:
|
||||
"""Return the next upcoming event."""
|
||||
next_holiday = None
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
"""Tests for calendar platform of Holiday integration."""
|
||||
|
||||
from datetime import datetime, timedelta
|
||||
import logging
|
||||
|
||||
from freezegun.api import FrozenDateTimeFactory
|
||||
from holidays import CATHOLIC
|
||||
@@ -17,6 +18,7 @@ from homeassistant.components.holiday.const import (
|
||||
)
|
||||
from homeassistant.const import CONF_COUNTRY
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers import entity_registry as er
|
||||
from homeassistant.setup import async_setup_component
|
||||
from homeassistant.util import dt as dt_util
|
||||
|
||||
@@ -431,3 +433,45 @@ async def test_categories(
|
||||
]
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
async def test_no_update_when_disabled(
|
||||
hass: HomeAssistant,
|
||||
freezer: FrozenDateTimeFactory,
|
||||
entity_registry: er.EntityRegistry,
|
||||
caplog: pytest.LogCaptureFixture,
|
||||
) -> None:
|
||||
"""Test that a disabled calendar entity does not trigger updates."""
|
||||
zone = await dt_util.async_get_time_zone("US/Hawaii")
|
||||
freezer.move_to(datetime(2023, 1, 1, 0, 1, 1, tzinfo=zone))
|
||||
config_entry = MockConfigEntry(
|
||||
domain=DOMAIN,
|
||||
data={CONF_COUNTRY: "US", CONF_PROVINCE: "AK"},
|
||||
title="United States, AK",
|
||||
)
|
||||
config_entry.add_to_hass(hass)
|
||||
|
||||
await hass.config_entries.async_setup(config_entry.entry_id)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
await async_setup_component(hass, "calendar", {})
|
||||
await hass.async_block_till_done()
|
||||
|
||||
entity_id = "calendar.united_states_ak"
|
||||
state = hass.states.get(entity_id)
|
||||
assert state is not None
|
||||
|
||||
entity_registry.async_update_entity(
|
||||
entity_id, disabled_by=er.RegistryEntryDisabler.USER
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
with caplog.at_level(logging.WARNING, logger="homeassistant.helpers.entity"):
|
||||
freezer.move_to(datetime(2023, 1, 2, 0, 1, 1, tzinfo=zone))
|
||||
async_fire_time_changed(hass)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert (
|
||||
"incorrectly being triggered for updates while it is disabled"
|
||||
not in caplog.text
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user