Skip to content

Commit

Permalink
Small cleanups to ambient station (#97421)
Browse files Browse the repository at this point in the history
  • Loading branch information
bdraco committed Jul 28, 2023
1 parent fc38451 commit a2555e7
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 34 deletions.
37 changes: 14 additions & 23 deletions homeassistant/components/ambient_station/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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,
)
)

Expand Down
9 changes: 3 additions & 6 deletions homeassistant/components/ambient_station/binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
8 changes: 3 additions & 5 deletions homeassistant/components/ambient_station/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit a2555e7

Please sign in to comment.