Small cleanups to ambient station (#97421)

This commit is contained in:
J. Nick Koston 2023-07-28 16:30:29 -05:00 committed by GitHub
parent fc38451faf
commit a2555e71e2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 20 additions and 34 deletions

View file

@ -148,6 +148,7 @@ class AmbientStation:
"""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 @@ class AmbientWeatherEntity(Entity):
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,
)
)

View file

@ -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

View file

@ -694,11 +694,9 @@ class AmbientWeatherSensor(AmbientWeatherEntity, SensorEntity):
@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