changed to use requestes in stead of urllib for yr sensor
parent
8e16a443e5
commit
9a1883bb49
|
@ -38,6 +38,7 @@ sensor:
|
|||
import logging
|
||||
import datetime
|
||||
import urllib.request
|
||||
import requests
|
||||
|
||||
from homeassistant.const import ATTR_ENTITY_PICTURE
|
||||
from homeassistant.helpers.entity import Entity
|
||||
|
@ -161,7 +162,9 @@ class YrSensor(Entity):
|
|||
self._weather.update()
|
||||
now = datetime.datetime.now()
|
||||
# check if data should be updated
|
||||
if now > self._update:
|
||||
if now <= self._update:
|
||||
return
|
||||
|
||||
time_data = self._weather.data['product']['time']
|
||||
|
||||
# pylint: disable=consider-using-enumerate
|
||||
|
@ -176,7 +179,8 @@ class YrSensor(Entity):
|
|||
+ " and " + time_data[k]['@to'] + ". "
|
||||
|
||||
temp_data = time_data[k]['location']
|
||||
if self.type in temp_data and now < valid_to:
|
||||
if self.type not in temp_data and now >= valid_to:
|
||||
continue
|
||||
if self.type == 'precipitation' and valid_from < now:
|
||||
self._state = temp_data[self.type]['@value']
|
||||
return
|
||||
|
@ -233,14 +237,15 @@ class YrData(object):
|
|||
""" Gets the latest data from yr.no """
|
||||
now = datetime.datetime.now()
|
||||
# check if new will be available
|
||||
if now > self._nextrun:
|
||||
if now <= self._nextrun:
|
||||
return
|
||||
try:
|
||||
response = urllib.request.urlopen(self._url)
|
||||
except urllib.error.URLError:
|
||||
response = requests.get(self._url)
|
||||
except requests.RequestException:
|
||||
return
|
||||
if response.status != 200:
|
||||
if response.status_code != 200:
|
||||
return
|
||||
data = response.read().decode('utf-8')
|
||||
data = response.text
|
||||
|
||||
import xmltodict
|
||||
self.data = xmltodict.parse(data)['weatherdata']
|
||||
|
|
Loading…
Reference in New Issue