changed to use requestes in stead of urllib for yr sensor

pull/693/head
Daniel 2015-12-21 10:29:12 +01:00
parent 8e16a443e5
commit 9a1883bb49
1 changed files with 73 additions and 68 deletions

View File

@ -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,61 +162,64 @@ 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:
time_data = self._weather.data['product']['time'] return
# pylint: disable=consider-using-enumerate time_data = self._weather.data['product']['time']
# find sensor
for k in range(len(time_data)):
valid_from = datetime.datetime.strptime(time_data[k]['@from'],
"%Y-%m-%dT%H:%M:%SZ")
valid_to = datetime.datetime.strptime(time_data[k]['@to'],
"%Y-%m-%dT%H:%M:%SZ")
self._update = valid_to
self._info = "Forecast between " + time_data[k]['@from'] \
+ " and " + time_data[k]['@to'] + ". "
temp_data = time_data[k]['location'] # pylint: disable=consider-using-enumerate
if self.type in temp_data and now < valid_to: # find sensor
if self.type == 'precipitation' and valid_from < now: for k in range(len(time_data)):
self._state = temp_data[self.type]['@value'] valid_from = datetime.datetime.strptime(time_data[k]['@from'],
return "%Y-%m-%dT%H:%M:%SZ")
elif self.type == 'symbol' and valid_from < now: valid_to = datetime.datetime.strptime(time_data[k]['@to'],
self._state = temp_data[self.type]['@number'] "%Y-%m-%dT%H:%M:%SZ")
return self._update = valid_to
elif self.type == 'temperature': self._info = "Forecast between " + time_data[k]['@from'] \
self._state = temp_data[self.type]['@value'] + " and " + time_data[k]['@to'] + ". "
return
elif self.type == 'windSpeed': temp_data = time_data[k]['location']
self._state = temp_data[self.type]['@mps'] if self.type not in temp_data and now >= valid_to:
return continue
elif self.type == 'pressure': if self.type == 'precipitation' and valid_from < now:
self._state = temp_data[self.type]['@value'] self._state = temp_data[self.type]['@value']
return return
elif self.type == 'windDirection': elif self.type == 'symbol' and valid_from < now:
self._state = float(temp_data[self.type]['@deg']) self._state = temp_data[self.type]['@number']
return return
elif self.type == 'humidity': elif self.type == 'temperature':
self._state = temp_data[self.type]['@value'] self._state = temp_data[self.type]['@value']
return return
elif self.type == 'fog': elif self.type == 'windSpeed':
self._state = temp_data[self.type]['@percent'] self._state = temp_data[self.type]['@mps']
return return
elif self.type == 'cloudiness': elif self.type == 'pressure':
self._state = temp_data[self.type]['@percent'] self._state = temp_data[self.type]['@value']
return return
elif self.type == 'lowClouds': elif self.type == 'windDirection':
self._state = temp_data[self.type]['@percent'] self._state = float(temp_data[self.type]['@deg'])
return return
elif self.type == 'mediumClouds': elif self.type == 'humidity':
self._state = temp_data[self.type]['@percent'] self._state = temp_data[self.type]['@value']
return return
elif self.type == 'highClouds': elif self.type == 'fog':
self._state = temp_data[self.type]['@percent'] self._state = temp_data[self.type]['@percent']
return return
elif self.type == 'dewpointTemperature': elif self.type == 'cloudiness':
self._state = temp_data[self.type]['@value'] self._state = temp_data[self.type]['@percent']
return return
elif self.type == 'lowClouds':
self._state = temp_data[self.type]['@percent']
return
elif self.type == 'mediumClouds':
self._state = temp_data[self.type]['@percent']
return
elif self.type == 'highClouds':
self._state = temp_data[self.type]['@percent']
return
elif self.type == 'dewpointTemperature':
self._state = temp_data[self.type]['@value']
return
# pylint: disable=too-few-public-methods # pylint: disable=too-few-public-methods
@ -233,19 +237,20 @@ 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:
try: return
response = urllib.request.urlopen(self._url) try:
except urllib.error.URLError: response = requests.get(self._url)
return except requests.RequestException:
if response.status != 200: return
return if response.status_code != 200:
data = response.read().decode('utf-8') return
data = response.text
import xmltodict import xmltodict
self.data = xmltodict.parse(data)['weatherdata'] self.data = xmltodict.parse(data)['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]
self._nextrun = datetime.datetime.strptime(model['@nextrun'], self._nextrun = datetime.datetime.strptime(model['@nextrun'],
"%Y-%m-%dT%H:%M:%SZ") "%Y-%m-%dT%H:%M:%SZ")