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