yr: use async syntax (#16563)

This commit is contained in:
Daniel Høyer Iversen 2018-09-12 04:52:01 +02:00 committed by Martin Hjelmare
parent 6bd120ff1d
commit e2465da7c2

View File

@ -66,9 +66,8 @@ PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
}) })
@asyncio.coroutine async def async_setup_platform(hass, config, async_add_entities,
def async_setup_platform(hass, config, async_add_entities, discovery_info=None):
discovery_info=None):
"""Set up the Yr.no sensor.""" """Set up the Yr.no sensor."""
elevation = config.get(CONF_ELEVATION, hass.config.elevation or 0) elevation = config.get(CONF_ELEVATION, hass.config.elevation or 0)
forecast = config.get(CONF_FORECAST) forecast = config.get(CONF_FORECAST)
@ -93,7 +92,7 @@ def async_setup_platform(hass, config, async_add_entities,
weather = YrData(hass, coordinates, forecast, dev) weather = YrData(hass, coordinates, forecast, dev)
async_track_utc_time_change(hass, weather.updating_devices, minute=31) async_track_utc_time_change(hass, weather.updating_devices, minute=31)
yield from weather.fetching_data() await weather.fetching_data()
class YrSensor(Entity): class YrSensor(Entity):
@ -156,8 +155,7 @@ class YrData:
self.data = {} self.data = {}
self.hass = hass self.hass = hass
@asyncio.coroutine async def fetching_data(self, *_):
def fetching_data(self, *_):
"""Get the latest data from yr.no.""" """Get the latest data from yr.no."""
import xmltodict import xmltodict
@ -169,12 +167,12 @@ class YrData:
try: try:
websession = async_get_clientsession(self.hass) websession = async_get_clientsession(self.hass)
with async_timeout.timeout(10, loop=self.hass.loop): with async_timeout.timeout(10, loop=self.hass.loop):
resp = yield from websession.get( resp = await websession.get(
self._url, params=self._urlparams) self._url, params=self._urlparams)
if resp.status != 200: if resp.status != 200:
try_again('{} returned {}'.format(resp.url, resp.status)) try_again('{} returned {}'.format(resp.url, resp.status))
return return
text = yield from resp.text() text = await resp.text()
except (asyncio.TimeoutError, aiohttp.ClientError) as err: except (asyncio.TimeoutError, aiohttp.ClientError) as err:
try_again(err) try_again(err)
@ -186,11 +184,10 @@ class YrData:
try_again(err) try_again(err)
return return
yield from self.updating_devices() await self.updating_devices()
async_call_later(self.hass, 60*60, self.fetching_data) async_call_later(self.hass, 60*60, self.fetching_data)
@asyncio.coroutine async def updating_devices(self, *_):
def updating_devices(self, *_):
"""Find the current data from self.data.""" """Find the current data from self.data."""
if not self.data: if not self.data:
return return
@ -256,4 +253,4 @@ class YrData:
tasks.append(dev.async_update_ha_state()) tasks.append(dev.async_update_ha_state())
if tasks: if tasks:
yield from asyncio.wait(tasks, loop=self.hass.loop) await asyncio.wait(tasks, loop=self.hass.loop)