Use async_on_unload in Netatmo (#58461)
This commit is contained in:
parent
e9ba5f3b4b
commit
ac4496b985
8 changed files with 11 additions and 22 deletions
|
@ -229,8 +229,6 @@ async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
|||
await hass.data[DOMAIN][entry.entry_id][AUTH].async_dropwebhook()
|
||||
_LOGGER.info("Unregister Netatmo webhook")
|
||||
|
||||
await hass.data[DOMAIN][entry.entry_id][DATA_HANDLER].async_cleanup()
|
||||
|
||||
unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
|
||||
|
||||
if unload_ok:
|
||||
|
|
|
@ -152,7 +152,7 @@ class NetatmoCamera(NetatmoBase, Camera):
|
|||
await super().async_added_to_hass()
|
||||
|
||||
for event_type in (EVENT_TYPE_LIGHT_MODE, EVENT_TYPE_OFF, EVENT_TYPE_ON):
|
||||
self._listeners.append(
|
||||
self.data_handler.config_entry.async_on_unload(
|
||||
async_dispatcher_connect(
|
||||
self.hass,
|
||||
f"signal-{DOMAIN}-webhook-{event_type}",
|
||||
|
|
|
@ -244,7 +244,7 @@ class NetatmoThermostat(NetatmoBase, ClimateEntity):
|
|||
EVENT_TYPE_CANCEL_SET_POINT,
|
||||
EVENT_TYPE_SCHEDULE,
|
||||
):
|
||||
self._listeners.append(
|
||||
self.data_handler.config_entry.async_on_unload(
|
||||
async_dispatcher_connect(
|
||||
self.hass,
|
||||
f"signal-{DOMAIN}-webhook-{event_type}",
|
||||
|
@ -485,7 +485,7 @@ class NetatmoThermostat(NetatmoBase, ClimateEntity):
|
|||
return
|
||||
|
||||
self._room_status = self._home_status.rooms.get(self._id)
|
||||
self._room_data = self._data.rooms.get(self._home_id, {}).get(self._id)
|
||||
self._room_data = self._data.rooms.get(self._home_id, {}).get(self._id, {})
|
||||
|
||||
if not self._room_status or not self._room_data:
|
||||
if self._connected:
|
||||
|
|
|
@ -70,11 +70,11 @@ class NetatmoDataClass:
|
|||
class NetatmoDataHandler:
|
||||
"""Manages the Netatmo data handling."""
|
||||
|
||||
def __init__(self, hass: HomeAssistant, entry: ConfigEntry) -> None:
|
||||
def __init__(self, hass: HomeAssistant, config_entry: ConfigEntry) -> None:
|
||||
"""Initialize self."""
|
||||
self.hass = hass
|
||||
self._auth = hass.data[DOMAIN][entry.entry_id][AUTH]
|
||||
self.listeners: list[CALLBACK_TYPE] = []
|
||||
self.config_entry = config_entry
|
||||
self._auth = hass.data[DOMAIN][config_entry.entry_id][AUTH]
|
||||
self.data_classes: dict = {}
|
||||
self.data: dict = {}
|
||||
self._queue: deque = deque()
|
||||
|
@ -87,7 +87,7 @@ class NetatmoDataHandler:
|
|||
self.hass, self.async_update, timedelta(seconds=SCAN_INTERVAL)
|
||||
)
|
||||
|
||||
self.listeners.append(
|
||||
self.config_entry.async_on_unload(
|
||||
async_dispatcher_connect(
|
||||
self.hass,
|
||||
f"signal-{DOMAIN}-webhook-None",
|
||||
|
@ -121,11 +121,6 @@ class NetatmoDataHandler:
|
|||
self.data_classes[data_class_entry].next_scan = time()
|
||||
self._queue.rotate(-(self._queue.index(self.data_classes[data_class_entry])))
|
||||
|
||||
async def async_cleanup(self) -> None:
|
||||
"""Clean up the Netatmo data handler."""
|
||||
for listener in self.listeners:
|
||||
listener()
|
||||
|
||||
async def handle_event(self, event: dict) -> None:
|
||||
"""Handle webhook events."""
|
||||
if event["data"][WEBHOOK_PUSH_TYPE] == WEBHOOK_ACTIVATION:
|
||||
|
|
|
@ -99,7 +99,7 @@ class NetatmoLight(NetatmoBase, LightEntity):
|
|||
"""Entity created."""
|
||||
await super().async_added_to_hass()
|
||||
|
||||
self._listeners.append(
|
||||
self.data_handler.config_entry.async_on_unload(
|
||||
async_dispatcher_connect(
|
||||
self.hass,
|
||||
f"signal-{DOMAIN}-webhook-{EVENT_TYPE_LIGHT_MODE}",
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
from __future__ import annotations
|
||||
|
||||
from homeassistant.const import ATTR_ATTRIBUTION
|
||||
from homeassistant.core import CALLBACK_TYPE, callback
|
||||
from homeassistant.core import callback
|
||||
from homeassistant.helpers.entity import DeviceInfo, Entity
|
||||
|
||||
from .const import (
|
||||
|
@ -23,7 +23,6 @@ class NetatmoBase(Entity):
|
|||
"""Set up Netatmo entity base."""
|
||||
self.data_handler = data_handler
|
||||
self._data_classes: list[dict] = []
|
||||
self._listeners: list[CALLBACK_TYPE] = []
|
||||
|
||||
self._device_name: str = ""
|
||||
self._id: str = ""
|
||||
|
@ -76,9 +75,6 @@ class NetatmoBase(Entity):
|
|||
"""Run when entity will be removed from hass."""
|
||||
await super().async_will_remove_from_hass()
|
||||
|
||||
for listener in self._listeners:
|
||||
listener()
|
||||
|
||||
for data_class in self._data_classes:
|
||||
await self.data_handler.unregister_data_class(
|
||||
data_class[SIGNAL_NAME], self.async_update_callback
|
||||
|
|
|
@ -102,7 +102,7 @@ class NetatmoScheduleSelect(NetatmoBase, SelectEntity):
|
|||
await super().async_added_to_hass()
|
||||
|
||||
for event_type in (EVENT_TYPE_SCHEDULE,):
|
||||
self._listeners.append(
|
||||
self.data_handler.config_entry.async_on_unload(
|
||||
async_dispatcher_connect(
|
||||
self.hass,
|
||||
f"signal-{DOMAIN}-webhook-{event_type}",
|
||||
|
|
|
@ -705,7 +705,7 @@ class NetatmoPublicSensor(NetatmoBase, SensorEntity):
|
|||
await super().async_added_to_hass()
|
||||
|
||||
assert self.device_info and "name" in self.device_info
|
||||
self.data_handler.listeners.append(
|
||||
self.data_handler.config_entry.async_on_unload(
|
||||
async_dispatcher_connect(
|
||||
self.hass,
|
||||
f"netatmo-config-{self.device_info['name']}",
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue