mirror of
https://github.com/home-assistant/core.git
synced 2025-04-23 08:47:57 +00:00
Yr.no: New aiohttp client needs params to form websession URL (#4634)
* Yr.no: New aiohttp client needs params to form websession URL * Support params in aiohttp mocking
This commit is contained in:
parent
bde7176b3c
commit
c6c8cd4f51
@ -70,7 +70,9 @@ def async_setup_platform(hass, config, async_add_devices, discovery_info=None):
|
||||
_LOGGER.error("Latitude or longitude not set in Home Assistant config")
|
||||
return False
|
||||
|
||||
coordinates = dict(lat=latitude, lon=longitude, msl=elevation)
|
||||
coordinates = {'lat': str(latitude),
|
||||
'lon': str(longitude),
|
||||
'msl': str(elevation)}
|
||||
|
||||
dev = []
|
||||
for sensor_type in config[CONF_MONITORED_CONDITIONS]:
|
||||
@ -135,8 +137,8 @@ class YrData(object):
|
||||
|
||||
def __init__(self, hass, coordinates, devices):
|
||||
"""Initialize the data object."""
|
||||
self._url = 'http://api.yr.no/weatherapi/locationforecast/1.9/?' \
|
||||
'lat={lat};lon={lon};msl={msl}'.format(**coordinates)
|
||||
self._url = 'http://api.yr.no/weatherapi/locationforecast/1.9/'
|
||||
self._urlparams = coordinates
|
||||
self._nextrun = None
|
||||
self.devices = devices
|
||||
self.data = {}
|
||||
@ -159,9 +161,10 @@ class YrData(object):
|
||||
try:
|
||||
websession = async_get_clientsession(self.hass)
|
||||
with async_timeout.timeout(10, loop=self.hass.loop):
|
||||
resp = yield from websession.get(self._url)
|
||||
resp = yield from websession.get(self._url,
|
||||
params=self._urlparams)
|
||||
if resp.status != 200:
|
||||
try_again('{} returned {}'.format(self._url, resp.status))
|
||||
try_again('{} returned {}'.format(resp.url, resp.status))
|
||||
return
|
||||
text = yield from resp.text()
|
||||
|
||||
|
@ -5,6 +5,7 @@ import functools
|
||||
import json as _json
|
||||
from unittest import mock
|
||||
from urllib.parse import urlparse, parse_qs
|
||||
import yarl
|
||||
|
||||
|
||||
class AiohttpClientMocker:
|
||||
@ -20,7 +21,8 @@ class AiohttpClientMocker:
|
||||
status=200,
|
||||
text=None,
|
||||
content=None,
|
||||
json=None):
|
||||
json=None,
|
||||
params=None):
|
||||
"""Mock a request."""
|
||||
if json:
|
||||
text = _json.dumps(json)
|
||||
@ -28,6 +30,8 @@ class AiohttpClientMocker:
|
||||
content = text.encode('utf-8')
|
||||
if content is None:
|
||||
content = b''
|
||||
if params:
|
||||
url = str(yarl.URL(url).with_query(params))
|
||||
|
||||
self._mocks.append(AiohttpClientMockResponse(
|
||||
method, url, status, content))
|
||||
@ -58,11 +62,11 @@ class AiohttpClientMocker:
|
||||
return len(self.mock_calls)
|
||||
|
||||
@asyncio.coroutine
|
||||
def match_request(self, method, url, *, auth=None): \
|
||||
def match_request(self, method, url, *, auth=None, params=None): \
|
||||
# pylint: disable=unused-variable
|
||||
"""Match a request against pre-registered requests."""
|
||||
for response in self._mocks:
|
||||
if response.match_request(method, url):
|
||||
if response.match_request(method, url, params):
|
||||
self.mock_calls.append((method, url))
|
||||
return response
|
||||
|
||||
@ -82,11 +86,14 @@ class AiohttpClientMockResponse:
|
||||
self.status = status
|
||||
self.response = response
|
||||
|
||||
def match_request(self, method, url):
|
||||
def match_request(self, method, url, params=None):
|
||||
"""Test if response answers request."""
|
||||
if method.lower() != self.method.lower():
|
||||
return False
|
||||
|
||||
if params:
|
||||
url = str(yarl.URL(url).with_query(params))
|
||||
|
||||
# regular expression matching
|
||||
if self._url_parts is None:
|
||||
return self._url.search(url) is not None
|
||||
|
Loading…
x
Reference in New Issue
Block a user