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