Fix lingering timer in buienradar (#91378)

This commit is contained in:
epenet 2023-04-16 14:20:54 +02:00 committed by GitHub
parent b0e0ada512
commit 3ff03eef46
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 18 additions and 4 deletions

View file

@ -12,7 +12,7 @@ PLATFORMS = [Platform.CAMERA, Platform.SENSOR, Platform.WEATHER]
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""Set up buienradar from a config entry."""
hass.data.setdefault(DOMAIN, {})
hass.data.setdefault(DOMAIN, {}).setdefault(entry.entry_id, {})
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
entry.async_on_unload(entry.add_update_listener(async_update_options))
return True
@ -20,7 +20,13 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""Unload a config entry."""
unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
if unload_ok := await hass.config_entries.async_unload_platforms(entry, PLATFORMS):
entry_data = hass.data[DOMAIN].pop(entry.entry_id)
for platform in PLATFORMS:
if (data := entry_data.get(platform)) and (
unsub := data.unsub_schedule_update
):
unsub()
return unload_ok

View file

@ -35,6 +35,7 @@ from homeassistant.const import (
CONF_NAME,
DEGREE,
PERCENTAGE,
Platform,
UnitOfIrradiance,
UnitOfLength,
UnitOfPrecipitationDepth,
@ -47,7 +48,7 @@ from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.util import dt as dt_util
from .const import CONF_TIMEFRAME, DEFAULT_TIMEFRAME
from .const import CONF_TIMEFRAME, DEFAULT_TIMEFRAME, DOMAIN
from .util import BrData
_LOGGER = logging.getLogger(__name__)
@ -684,6 +685,7 @@ async def async_setup_entry(
data = BrData(hass, coordinates, timeframe, entities)
# schedule the first update in 1 minute from now:
await data.schedule_update(1)
hass.data[DOMAIN][entry.entry_id][Platform.SENSOR] = data
class BrSensor(SensorEntity):

View file

@ -27,6 +27,7 @@ from buienradar.constants import (
from buienradar.urls import JSON_FEED_URL, json_precipitation_forecast_url
from homeassistant.const import CONF_LATITUDE, CONF_LONGITUDE
from homeassistant.core import CALLBACK_TYPE
from homeassistant.helpers.aiohttp_client import async_get_clientsession
from homeassistant.helpers.event import async_track_point_in_utc_time
from homeassistant.util import dt as dt_util
@ -65,6 +66,7 @@ class BrData:
self.hass = hass
self.coordinates = coordinates
self.timeframe = timeframe
self.unsub_schedule_update: CALLBACK_TYPE | None = None
async def update_devices(self):
"""Update all devices/sensors."""
@ -79,7 +81,9 @@ class BrData:
"""Schedule an update after minute minutes."""
_LOGGER.debug("Scheduling next update in %s minutes", minute)
nxt = dt_util.utcnow() + timedelta(minutes=minute)
async_track_point_in_utc_time(self.hass, self.async_update, nxt)
self.unsub_schedule_update = async_track_point_in_utc_time(
self.hass, self.async_update, nxt
)
async def get_data(self, url):
"""Load data from specified url."""

View file

@ -41,6 +41,7 @@ from homeassistant.const import (
CONF_LATITUDE,
CONF_LONGITUDE,
CONF_NAME,
Platform,
UnitOfLength,
UnitOfPrecipitationDepth,
UnitOfPressure,
@ -100,6 +101,7 @@ async def async_setup_entry(
# create weather data:
data = BrData(hass, coordinates, DEFAULT_TIMEFRAME, None)
hass.data[DOMAIN][entry.entry_id][Platform.WEATHER] = data
# create weather device:
_LOGGER.debug("Initializing buienradar weather: coordinates %s", coordinates)