mirror of
https://github.com/home-assistant/core.git
synced 2025-07-23 05:07:41 +00:00
Yr.no update entities every hour (#4521)
This commit is contained in:
parent
b4756e6dda
commit
0827a26642
@ -19,7 +19,8 @@ from homeassistant.const import (
|
|||||||
CONF_LATITUDE, CONF_LONGITUDE, CONF_ELEVATION, CONF_MONITORED_CONDITIONS,
|
CONF_LATITUDE, CONF_LONGITUDE, CONF_ELEVATION, CONF_MONITORED_CONDITIONS,
|
||||||
ATTR_ATTRIBUTION)
|
ATTR_ATTRIBUTION)
|
||||||
from homeassistant.helpers.entity import Entity
|
from homeassistant.helpers.entity import Entity
|
||||||
from homeassistant.helpers.event import async_track_point_in_utc_time
|
from homeassistant.helpers.event import (
|
||||||
|
async_track_point_in_utc_time, async_track_utc_time_change)
|
||||||
from homeassistant.util import dt as dt_util
|
from homeassistant.util import dt as dt_util
|
||||||
|
|
||||||
|
|
||||||
@ -76,6 +77,8 @@ def async_setup_platform(hass, config, async_add_devices, discovery_info=None):
|
|||||||
yield from async_add_devices(dev)
|
yield from async_add_devices(dev)
|
||||||
|
|
||||||
weather = YrData(hass, coordinates, dev)
|
weather = YrData(hass, coordinates, dev)
|
||||||
|
# Update weather on the hour
|
||||||
|
async_track_utc_time_change(hass, weather.async_update, minute=0, second=0)
|
||||||
yield from weather.async_update()
|
yield from weather.async_update()
|
||||||
|
|
||||||
|
|
||||||
@ -139,14 +142,18 @@ class YrData(object):
|
|||||||
self.hass = hass
|
self.hass = hass
|
||||||
|
|
||||||
@asyncio.coroutine
|
@asyncio.coroutine
|
||||||
def async_update(self):
|
def async_update(self, *_):
|
||||||
"""Get the latest data from yr.no."""
|
"""Get the latest data from yr.no."""
|
||||||
def try_again(err: str):
|
def try_again(err: str):
|
||||||
"""Schedule again later."""
|
"""Retry in 15 minutes."""
|
||||||
_LOGGER.warning('Retrying in 15 minutes: %s', err)
|
_LOGGER.warning('Retrying in 15 minutes: %s', err)
|
||||||
|
self._nextrun = None
|
||||||
nxt = dt_util.utcnow() + timedelta(minutes=15)
|
nxt = dt_util.utcnow() + timedelta(minutes=15)
|
||||||
async_track_point_in_utc_time(self.hass, self.async_update, nxt)
|
if nxt.minute >= 15:
|
||||||
|
async_track_point_in_utc_time(self.hass, self.async_update,
|
||||||
|
nxt)
|
||||||
|
|
||||||
|
if self._nextrun is None or dt_util.utcnow() >= self._nextrun:
|
||||||
try:
|
try:
|
||||||
with async_timeout.timeout(10, loop=self.hass.loop):
|
with async_timeout.timeout(10, loop=self.hass.loop):
|
||||||
resp = yield from self.hass.websession.get(self._url)
|
resp = yield from self.hass.websession.get(self._url)
|
||||||
@ -166,14 +173,11 @@ class YrData(object):
|
|||||||
model = self.data['meta']['model']
|
model = self.data['meta']['model']
|
||||||
if '@nextrun' not in model:
|
if '@nextrun' not in model:
|
||||||
model = model[0]
|
model = model[0]
|
||||||
next_run = dt_util.parse_datetime(model['@nextrun'])
|
self._nextrun = dt_util.parse_datetime(model['@nextrun'])
|
||||||
except (ExpatError, IndexError) as err:
|
except (ExpatError, IndexError) as err:
|
||||||
try_again(err)
|
try_again(err)
|
||||||
return
|
return
|
||||||
|
|
||||||
# Schedule next execution
|
|
||||||
async_track_point_in_utc_time(self.hass, self.async_update, next_run)
|
|
||||||
|
|
||||||
now = dt_util.utcnow()
|
now = dt_util.utcnow()
|
||||||
|
|
||||||
tasks = []
|
tasks = []
|
||||||
|
Loading…
x
Reference in New Issue
Block a user