Skip to content

Commit

Permalink
Check for data availability
Browse files Browse the repository at this point in the history
  • Loading branch information
astrandb committed Jan 16, 2024
1 parent a5a02c8 commit 939465f
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 1 deletion.
21 changes: 20 additions & 1 deletion custom_components/weatherlink/binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@
from .const import (
CONF_API_VERSION,
CONFIG_URL,
DISCONNECTED_AFTER_SECONDS,
DOMAIN,
MANUFACTURER,
UNAVAILABLE_AFTER_SECONDS,
ApiVersion,
DataKey,
)
Expand Down Expand Up @@ -237,7 +239,7 @@ def is_on(self):
)
dt_now = dt_util.now()
diff = dt_now - dt_update
return (diff.total_seconds()) / 60 < 32
return (diff.total_seconds()) / 60 < DISCONNECTED_AFTER_SECONDS
return None

@property
Expand All @@ -255,3 +257,20 @@ def extra_state_attributes(self) -> dict[str, str] | None:
"last_update": dt_object,
}
return None

@property
def available(self):
"""Return the availability of the entity."""

if not self.coordinator.last_update_success:
return False

if self.entity_description.key != "Timestamp":
dt_update = dt_util.utc_from_timestamp(
self.coordinator.data[self.tx_id].get(DataKey.TIMESTAMP)
)
dt_now = dt_util.now()
diff = dt_now - dt_update
return (diff.total_seconds()) / 60 < UNAVAILABLE_AFTER_SECONDS

return True
3 changes: 3 additions & 0 deletions custom_components/weatherlink/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
CONF_API_TOKEN = "apitoken"
CONF_STATION_ID = "station_id"

DISCONNECTED_AFTER_SECONDS = 1830
UNAVAILABLE_AFTER_SECONDS = 3630


class ApiVersion(StrEnum):
"""Supported API versions."""
Expand Down
15 changes: 15 additions & 0 deletions custom_components/weatherlink/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
CONFIG_URL,
DOMAIN,
MANUFACTURER,
UNAVAILABLE_AFTER_SECONDS,
ApiVersion,
DataKey,
)
Expand Down Expand Up @@ -772,3 +773,17 @@ def extra_state_attributes(self) -> dict[str, str] | None:
"rain_storm_end": dt_object_end,
}
return None

@property
def available(self):
"""Return the availability of the entity."""

if not self.coordinator.last_update_success:
return False

dt_update = dt_util.utc_from_timestamp(
self.coordinator.data[self.tx_id].get(DataKey.TIMESTAMP)
)
dt_now = dt_util.now()
diff = dt_now - dt_update
return (diff.total_seconds()) / 60 < UNAVAILABLE_AFTER_SECONDS

0 comments on commit 939465f

Please sign in to comment.