Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions custom_components/hilo/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
MAX_SUB_INTERVAL = 120
MIN_SCAN_INTERVAL = 60
REWARD_SCAN_INTERVAL = 7200
WEATHER_SCAN_INTERVAL = 1800

CONF_TARIFF = {
"rate d": {
Expand Down
39 changes: 32 additions & 7 deletions custom_components/hilo/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
REWARD_SCAN_INTERVAL,
TARIFF_LIST,
WEATHER_CONDITIONS,
WEATHER_SCAN_INTERVAL,
)
from .entity import HiloEntity
from .managers import EnergyManager, UtilityManager
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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)