mirror of
https://github.com/home-assistant/core.git
synced 2025-07-22 20:57:21 +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,40 +142,41 @@ 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)
|
||||||
|
|
||||||
try:
|
if self._nextrun is None or dt_util.utcnow() >= self._nextrun:
|
||||||
with async_timeout.timeout(10, loop=self.hass.loop):
|
try:
|
||||||
resp = yield from self.hass.websession.get(self._url)
|
with async_timeout.timeout(10, loop=self.hass.loop):
|
||||||
if resp.status != 200:
|
resp = yield from self.hass.websession.get(self._url)
|
||||||
try_again('{} returned {}'.format(self._url, resp.status))
|
if resp.status != 200:
|
||||||
|
try_again('{} returned {}'.format(self._url, resp.status))
|
||||||
|
return
|
||||||
|
text = yield from resp.text()
|
||||||
|
self.hass.async_add_job(resp.release())
|
||||||
|
except (asyncio.TimeoutError, aiohttp.errors.ClientError,
|
||||||
|
aiohttp.errors.ClientDisconnectedError) as err:
|
||||||
|
try_again(err)
|
||||||
return
|
return
|
||||||
text = yield from resp.text()
|
|
||||||
self.hass.async_add_job(resp.release())
|
|
||||||
except (asyncio.TimeoutError, aiohttp.errors.ClientError,
|
|
||||||
aiohttp.errors.ClientDisconnectedError) as err:
|
|
||||||
try_again(err)
|
|
||||||
return
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
import xmltodict
|
import xmltodict
|
||||||
self.data = xmltodict.parse(text)['weatherdata']
|
self.data = xmltodict.parse(text)['weatherdata']
|
||||||
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()
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user