From a2555e71e2df4fdb3ac747cbc1ddf8c03a8eedbe Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Fri, 28 Jul 2023 16:30:29 -0500 Subject: [PATCH] Small cleanups to ambient station (#97421) --- .../components/ambient_station/__init__.py | 37 +++++++------------ .../ambient_station/binary_sensor.py | 9 ++--- .../components/ambient_station/sensor.py | 8 ++-- 3 files changed, 20 insertions(+), 34 deletions(-) diff --git a/homeassistant/components/ambient_station/__init__.py b/homeassistant/components/ambient_station/__init__.py index f68ae3df1144ac..364e5b2abb6649 100644 --- a/homeassistant/components/ambient_station/__init__.py +++ b/homeassistant/components/ambient_station/__init__.py @@ -148,6 +148,7 @@ def on_data(data: dict) -> None: """Define a handler to fire when the data is received.""" mac = data["macAddress"] + # If data has not changed, don't update: if data == self.stations[mac][ATTR_LAST_DATA]: return @@ -228,33 +229,23 @@ def __init__( self._mac_address = mac_address self.entity_description = description + @callback + def _async_update(self) -> None: + """Update the state.""" + last_data = self._ambient.stations[self._mac_address][ATTR_LAST_DATA] + key = self.entity_description.key + available_key = TYPE_SOLARRADIATION if key == TYPE_SOLARRADIATION_LX else key + self._attr_available = last_data[available_key] is not None + self.update_from_latest_data() + self.async_write_ha_state() + async def async_added_to_hass(self) -> None: """Register callbacks.""" - - @callback - def update() -> None: - """Update the state.""" - if self.entity_description.key == TYPE_SOLARRADIATION_LX: - self._attr_available = ( - self._ambient.stations[self._mac_address][ATTR_LAST_DATA][ - TYPE_SOLARRADIATION - ] - is not None - ) - else: - self._attr_available = ( - self._ambient.stations[self._mac_address][ATTR_LAST_DATA][ - self.entity_description.key - ] - is not None - ) - - self.update_from_latest_data() - self.async_write_ha_state() - self.async_on_remove( async_dispatcher_connect( - self.hass, f"ambient_station_data_update_{self._mac_address}", update + self.hass, + f"ambient_station_data_update_{self._mac_address}", + self._async_update, ) ) diff --git a/homeassistant/components/ambient_station/binary_sensor.py b/homeassistant/components/ambient_station/binary_sensor.py index 8876c1a5c627cc..ca32f16c758ceb 100644 --- a/homeassistant/components/ambient_station/binary_sensor.py +++ b/homeassistant/components/ambient_station/binary_sensor.py @@ -409,9 +409,6 @@ class AmbientWeatherBinarySensor(AmbientWeatherEntity, BinarySensorEntity): @callback def update_from_latest_data(self) -> None: """Fetch new state data for the entity.""" - self._attr_is_on = ( - self._ambient.stations[self._mac_address][ATTR_LAST_DATA][ - self.entity_description.key - ] - == self.entity_description.on_state - ) + description = self.entity_description + last_data = self._ambient.stations[self._mac_address][ATTR_LAST_DATA] + self._attr_is_on = last_data[description.key] == description.on_state diff --git a/homeassistant/components/ambient_station/sensor.py b/homeassistant/components/ambient_station/sensor.py index 0fc6e7643dbc1a..8bdc66133d6854 100644 --- a/homeassistant/components/ambient_station/sensor.py +++ b/homeassistant/components/ambient_station/sensor.py @@ -694,11 +694,9 @@ def __init__( @callback def update_from_latest_data(self) -> None: """Fetch new state data for the sensor.""" - raw = self._ambient.stations[self._mac_address][ATTR_LAST_DATA][ - self.entity_description.key - ] - - if self.entity_description.key == TYPE_LASTRAIN: + key = self.entity_description.key + raw = self._ambient.stations[self._mac_address][ATTR_LAST_DATA][key] + if key == TYPE_LASTRAIN: self._attr_native_value = datetime.strptime(raw, "%Y-%m-%dT%H:%M:%S.%f%z") else: self._attr_native_value = raw