diff --git a/custom_components/hilo/const.py b/custom_components/hilo/const.py index e61d071..50ab127 100755 --- a/custom_components/hilo/const.py +++ b/custom_components/hilo/const.py @@ -46,6 +46,7 @@ MAX_SUB_INTERVAL = 120 MIN_SCAN_INTERVAL = 60 REWARD_SCAN_INTERVAL = 7200 +WEATHER_SCAN_INTERVAL = 1800 CONF_TARIFF = { "rate d": { diff --git a/custom_components/hilo/sensor.py b/custom_components/hilo/sensor.py index a4a9e49..3ce23aa 100755 --- a/custom_components/hilo/sensor.py +++ b/custom_components/hilo/sensor.py @@ -67,6 +67,7 @@ REWARD_SCAN_INTERVAL, TARIFF_LIST, WEATHER_CONDITIONS, + WEATHER_SCAN_INTERVAL, ) from .entity import HiloEntity from .managers import EnergyManager, UtilityManager @@ -1247,10 +1248,9 @@ def __init__(self, hilo, device, scan_interval): f"{slugify(device.identifier)}-{slugify(self._attr_name)}" ) LOG.debug("Setting up OutdoorWeatherSensor entity: %s", self._attr_name) - self.scan_interval = timedelta(seconds=EVENT_SCAN_INTERVAL_REDUCTION) self._state = STATE_UNKNOWN self._weather = {} - self.async_update = Throttle(self.scan_interval)(self._async_update) + self._last_weather_update = None @property def state(self): @@ -1288,9 +1288,34 @@ async def async_added_to_hass(self): """Handle entity about to be added to hass event.""" await super().async_added_to_hass() - async def _async_update(self): - self._weather = {} - self._weather = await self._hilo._api.get_weather( - self._hilo.devices.location_id + async def async_update(self): + """Update the weather data with proper throttling. + + Weather data from Hilo API changes every hour, using 1800s (30min) to + make sure we get the latest data without overloading the API. + """ + + now = dt_util.utcnow() + if self._last_weather_update is not None: + elapsed = (now - self._last_weather_update).total_seconds() + if elapsed < WEATHER_SCAN_INTERVAL: + LOG.debug( + "Skipping weather update, only %s seconds since last update", + elapsed, + ) + return + + LOG.debug( + "Calling Hilo API to get weather data with location id %s", + self._hilo.devices.location_id, ) - self._state = self._weather.get("temperature") + self._last_weather_update = now + + try: + self._weather = await self._hilo._api.get_weather( + self._hilo.devices.location_id + ) + LOG.debug("Weather data received: %s", self._weather) + self._state = self._weather.get("temperature") + except Exception as err: + LOG.error("Error fetching weather data: %s", err)