Skip to content

Commit

Permalink
Rain delay sensor: correctly set attributes by updating HA state afte…
Browse files Browse the repository at this point in the history
…r filling in data from rest API.

Fixes #11
  • Loading branch information
sebr committed Apr 24, 2020
1 parent 7aec50e commit c2caf88
Showing 1 changed file with 10 additions and 8 deletions.
18 changes: 10 additions & 8 deletions custom_components/bhyve/binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,22 +71,25 @@ def _on_ws_data(self, data):
def _update_device_soon(self):
if self._update_device_cb is not None:
self._update_device_cb() # unsubscribe
self._update_device_cb = async_call_later(self._hass, 5, self._update_device)
self._update_device_cb = async_call_later(self._hass, 1, self._update_device)

async def _update_device(self, time):
await self._refetch_device(force_update=True)
self.async_schedule_update_ha_state()

def _extract_rain_delay(self, rain_delay, device_status=None):
if rain_delay is not None and rain_delay > 0:
self._state = True
self._attrs = {ATTR_DELAY: rain_delay}
if device_status is not None:
self._attrs[ATTR_CAUSE] = device_status.get("rain_delay_cause")
self._attrs[ATTR_WEATHER_TYPE] = device_status.get(
"rain_delay_weather_type"
)
self._attrs[ATTR_STARTED_AT] = orbit_time_to_local_time(
device_status.get("rain_delay_started_at")
self._attrs.update(
{
ATTR_CAUSE: device_status.get("rain_delay_cause"),
ATTR_WEATHER_TYPE: device_status.get("rain_delay_weather_type"),
ATTR_STARTED_AT: orbit_time_to_local_time(
device_status.get("rain_delay_started_at")
),
}
)
else:
self._state = False
Expand All @@ -96,4 +99,3 @@ def _extract_rain_delay(self, rain_delay, device_status=None):
def is_on(self):
"""Return the status of the sensor."""
return self._state is True

0 comments on commit c2caf88

Please sign in to comment.