From 2a6247cf209505bd6b114bc427d81f9082da5733 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sun, 24 Oct 2021 11:53:11 -1000 Subject: [PATCH] Fix lookin push updates when sensor entities disabled (#58346) --- homeassistant/components/lookin/__init__.py | 25 +++++++++++++++++++-- homeassistant/components/lookin/sensor.py | 23 +------------------ 2 files changed, 24 insertions(+), 24 deletions(-) diff --git a/homeassistant/components/lookin/__init__.py b/homeassistant/components/lookin/__init__.py index a096c08dfeb..f749621beaf 100644 --- a/homeassistant/components/lookin/__init__.py +++ b/homeassistant/components/lookin/__init__.py @@ -5,11 +5,17 @@ from datetime import timedelta import logging import aiohttp -from aiolookin import LookInHttpProtocol, LookinUDPSubscriptions, start_lookin_udp +from aiolookin import ( + LookInHttpProtocol, + LookinUDPSubscriptions, + MeteoSensor, + SensorID, + start_lookin_udp, +) from homeassistant.config_entries import ConfigEntry from homeassistant.const import CONF_HOST -from homeassistant.core import HomeAssistant +from homeassistant.core import HomeAssistant, callback from homeassistant.exceptions import ConfigEntryNotReady from homeassistant.helpers.aiohttp_client import async_get_clientsession from homeassistant.helpers.update_coordinator import DataUpdateCoordinator @@ -45,7 +51,22 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: ) await meteo_coordinator.async_config_entry_first_refresh() + @callback + def _async_meteo_push_update(msg: dict[str, str]) -> None: + """Process an update pushed via UDP.""" + if int(msg["event_id"]): + return + LOGGER.debug("Processing push message for meteo sensor: %s", msg) + meteo: MeteoSensor = meteo_coordinator.data + meteo.update_from_value(msg["value"]) + meteo_coordinator.async_set_updated_data(meteo) + lookin_udp_subs = LookinUDPSubscriptions() + entry.async_on_unload( + lookin_udp_subs.subscribe_sensor( + lookin_device.id, SensorID.Meteo, None, _async_meteo_push_update + ) + ) entry.async_on_unload(await start_lookin_udp(lookin_udp_subs)) hass.data.setdefault(DOMAIN, {})[entry.entry_id] = LookinData( diff --git a/homeassistant/components/lookin/sensor.py b/homeassistant/components/lookin/sensor.py index d7d4a7a937a..b320f5d537a 100644 --- a/homeassistant/components/lookin/sensor.py +++ b/homeassistant/components/lookin/sensor.py @@ -3,8 +3,6 @@ from __future__ import annotations import logging -from aiolookin import MeteoSensor, SensorID - from homeassistant.components.sensor import ( DEVICE_CLASS_HUMIDITY, DEVICE_CLASS_TEMPERATURE, @@ -14,7 +12,7 @@ from homeassistant.components.sensor import ( ) from homeassistant.config_entries import ConfigEntry from homeassistant.const import PERCENTAGE, TEMP_CELSIUS -from homeassistant.core import HomeAssistant, callback +from homeassistant.core import HomeAssistant from homeassistant.helpers.entity_platform import AddEntitiesCallback from .const import DOMAIN @@ -74,22 +72,3 @@ class LookinSensorEntity(LookinDeviceCoordinatorEntity, SensorEntity): self.coordinator.data, self.entity_description.key ) super()._handle_coordinator_update() - - @callback - def _async_push_update(self, msg: dict[str, str]) -> None: - """Process an update pushed via UDP.""" - if int(msg["event_id"]): - return - LOGGER.debug("Processing push message for meteo sensor: %s", msg) - meteo: MeteoSensor = self.coordinator.data - meteo.update_from_value(msg["value"]) - self.coordinator.async_set_updated_data(meteo) - - async def async_added_to_hass(self) -> None: - """Call when the entity is added to hass.""" - self.async_on_remove( - self._lookin_udp_subs.subscribe_sensor( - self._lookin_device.id, SensorID.Meteo, None, self._async_push_update - ) - ) - return await super().async_added_to_hass()