Skip to content

Commit

Permalink
Merge pull request #4 from m-rossi/3-if-a-station-is-offline-values-a…
Browse files Browse the repository at this point in the history
…re-still-sent-to-homeassistant

Raise ValueError if older is than one hour
  • Loading branch information
m-rossi authored Oct 13, 2024
2 parents db802f8 + c47e0cd commit d25f012
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions custom_components/dashboard_delbrueck/sensor.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import datetime
from zoneinfo import ZoneInfo

from homeassistant.components.sensor import SensorEntity, SensorDeviceClass
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
Expand Down Expand Up @@ -58,6 +61,13 @@ async def async_update(self):
def update(self):
try:
data = self._api.get_sensor_value(self._sensor_id)
if data["timestamp"] < datetime.datetime.now(
ZoneInfo("Europe/Berlin")
) - datetime.timedelta(hours=1):
raise ValueError(
"Data is older than an hour, weather station may be offline, value"
" will not be stored."
)
if self.device_class == SensorDeviceClass.ENUM:
self._attr_native_value = WIND_BEARINGS[data["value"]]
else:
Expand Down

0 comments on commit d25f012

Please sign in to comment.