From d98432c328b6a2680e1df4f75c3f209264f6c5ba Mon Sep 17 00:00:00 2001 From: Aaron Bach Date: Tue, 17 Mar 2020 05:02:05 -0600 Subject: [PATCH] Add cleanup to Ambient PWS (#32888) --- homeassistant/components/ambient_station/__init__.py | 11 ++++++++++- .../components/ambient_station/binary_sensor.py | 4 +++- homeassistant/components/ambient_station/sensor.py | 4 +++- 3 files changed, 16 insertions(+), 3 deletions(-) diff --git a/homeassistant/components/ambient_station/__init__.py b/homeassistant/components/ambient_station/__init__.py index 4fd6590b286..b840a1b7171 100644 --- a/homeassistant/components/ambient_station/__init__.py +++ b/homeassistant/components/ambient_station/__init__.py @@ -520,13 +520,22 @@ class AmbientWeatherEntity(Entity): @callback def update(): """Update the state.""" - self.async_schedule_update_ha_state(True) + self.update_from_latest_data() + self.async_write_ha_state() self._async_unsub_dispatcher_connect = async_dispatcher_connect( self.hass, f"ambient_station_data_update_{self._mac_address}", update ) + self.update_from_latest_data() + async def async_will_remove_from_hass(self): """Disconnect dispatcher listener when removed.""" if self._async_unsub_dispatcher_connect: self._async_unsub_dispatcher_connect() + self._async_unsub_dispatcher_connect = None + + @callback + def update_from_latest_data(self): + """Update the entity from the latest data.""" + raise NotImplementedError diff --git a/homeassistant/components/ambient_station/binary_sensor.py b/homeassistant/components/ambient_station/binary_sensor.py index e4c1c8ccdac..d1b1f9b8f1d 100644 --- a/homeassistant/components/ambient_station/binary_sensor.py +++ b/homeassistant/components/ambient_station/binary_sensor.py @@ -3,6 +3,7 @@ import logging from homeassistant.components.binary_sensor import BinarySensorDevice from homeassistant.const import ATTR_NAME +from homeassistant.core import callback from . import ( SENSOR_TYPES, @@ -76,7 +77,8 @@ class AmbientWeatherBinarySensor(AmbientWeatherEntity, BinarySensorDevice): return self._state == 1 - async def async_update(self): + @callback + def update_from_latest_data(self): """Fetch new state data for the entity.""" self._state = self._ambient.stations[self._mac_address][ATTR_LAST_DATA].get( self._sensor_type diff --git a/homeassistant/components/ambient_station/sensor.py b/homeassistant/components/ambient_station/sensor.py index c400d2ec97b..b3b76715368 100644 --- a/homeassistant/components/ambient_station/sensor.py +++ b/homeassistant/components/ambient_station/sensor.py @@ -2,6 +2,7 @@ import logging from homeassistant.const import ATTR_NAME +from homeassistant.core import callback from . import ( SENSOR_TYPES, @@ -74,7 +75,8 @@ class AmbientWeatherSensor(AmbientWeatherEntity): """Return the unit of measurement.""" return self._unit - async def async_update(self): + @callback + def update_from_latest_data(self): """Fetch new state data for the sensor.""" if self._sensor_type == TYPE_SOLARRADIATION_LX: # If the user requests the solarradiation_lx sensor, use the