Add cleanup to Ambient PWS (#32888)

This commit is contained in:
Aaron Bach 2020-03-17 05:02:05 -06:00 committed by GitHub
parent 1da35e2939
commit d98432c328
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 16 additions and 3 deletions

View File

@ -520,13 +520,22 @@ class AmbientWeatherEntity(Entity):
@callback @callback
def update(): def update():
"""Update the state.""" """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._async_unsub_dispatcher_connect = async_dispatcher_connect(
self.hass, f"ambient_station_data_update_{self._mac_address}", update self.hass, f"ambient_station_data_update_{self._mac_address}", update
) )
self.update_from_latest_data()
async def async_will_remove_from_hass(self): async def async_will_remove_from_hass(self):
"""Disconnect dispatcher listener when removed.""" """Disconnect dispatcher listener when removed."""
if self._async_unsub_dispatcher_connect: if self._async_unsub_dispatcher_connect:
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

View File

@ -3,6 +3,7 @@ import logging
from homeassistant.components.binary_sensor import BinarySensorDevice from homeassistant.components.binary_sensor import BinarySensorDevice
from homeassistant.const import ATTR_NAME from homeassistant.const import ATTR_NAME
from homeassistant.core import callback
from . import ( from . import (
SENSOR_TYPES, SENSOR_TYPES,
@ -76,7 +77,8 @@ class AmbientWeatherBinarySensor(AmbientWeatherEntity, BinarySensorDevice):
return self._state == 1 return self._state == 1
async def async_update(self): @callback
def update_from_latest_data(self):
"""Fetch new state data for the entity.""" """Fetch new state data for the entity."""
self._state = self._ambient.stations[self._mac_address][ATTR_LAST_DATA].get( self._state = self._ambient.stations[self._mac_address][ATTR_LAST_DATA].get(
self._sensor_type self._sensor_type

View File

@ -2,6 +2,7 @@
import logging import logging
from homeassistant.const import ATTR_NAME from homeassistant.const import ATTR_NAME
from homeassistant.core import callback
from . import ( from . import (
SENSOR_TYPES, SENSOR_TYPES,
@ -74,7 +75,8 @@ class AmbientWeatherSensor(AmbientWeatherEntity):
"""Return the unit of measurement.""" """Return the unit of measurement."""
return self._unit return self._unit
async def async_update(self): @callback
def update_from_latest_data(self):
"""Fetch new state data for the sensor.""" """Fetch new state data for the sensor."""
if self._sensor_type == TYPE_SOLARRADIATION_LX: if self._sensor_type == TYPE_SOLARRADIATION_LX:
# If the user requests the solarradiation_lx sensor, use the # If the user requests the solarradiation_lx sensor, use the