2020-06-15 10:02:25 +00:00
|
|
|
"""The tests for the Met Office sensor component."""
|
2020-08-23 07:57:58 +00:00
|
|
|
from datetime import timedelta
|
2020-06-15 10:02:25 +00:00
|
|
|
import json
|
2021-01-01 21:31:56 +00:00
|
|
|
from unittest.mock import patch
|
2020-06-15 10:02:25 +00:00
|
|
|
|
|
|
|
from homeassistant.components.metoffice.const import DOMAIN
|
|
|
|
from homeassistant.const import STATE_UNAVAILABLE
|
|
|
|
from homeassistant.util import utcnow
|
|
|
|
|
2020-08-23 07:57:58 +00:00
|
|
|
from . import NewDateTime
|
2020-06-15 10:02:25 +00:00
|
|
|
from .const import (
|
|
|
|
METOFFICE_CONFIG_KINGSLYNN,
|
|
|
|
METOFFICE_CONFIG_WAVERTREE,
|
|
|
|
WAVERTREE_SENSOR_RESULTS,
|
|
|
|
)
|
|
|
|
|
|
|
|
from tests.common import MockConfigEntry, async_fire_time_changed, load_fixture
|
|
|
|
|
|
|
|
|
|
|
|
@patch(
|
2020-08-27 11:56:20 +00:00
|
|
|
"datapoint.Forecast.datetime.datetime",
|
|
|
|
NewDateTime,
|
2020-06-15 10:02:25 +00:00
|
|
|
)
|
2020-06-29 16:39:24 +00:00
|
|
|
async def test_site_cannot_connect(hass, requests_mock, legacy_patchable_time):
|
2020-06-15 10:02:25 +00:00
|
|
|
"""Test we handle cannot connect error."""
|
|
|
|
|
|
|
|
requests_mock.get("/public/data/val/wxfcs/all/json/sitelist/", text="")
|
|
|
|
requests_mock.get("/public/data/val/wxfcs/all/json/354107?res=3hourly", text="")
|
2021-06-27 19:04:42 +00:00
|
|
|
requests_mock.get("/public/data/val/wxfcs/all/json/354107?res=daily", text="")
|
2020-06-15 10:02:25 +00:00
|
|
|
|
2020-08-27 11:56:20 +00:00
|
|
|
entry = MockConfigEntry(
|
|
|
|
domain=DOMAIN,
|
|
|
|
data=METOFFICE_CONFIG_WAVERTREE,
|
|
|
|
)
|
2020-06-15 10:02:25 +00:00
|
|
|
entry.add_to_hass(hass)
|
|
|
|
await hass.config_entries.async_setup(entry.entry_id)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
2021-06-27 19:04:42 +00:00
|
|
|
assert hass.states.get("weather.met_office_wavertree_3hourly") is None
|
|
|
|
assert hass.states.get("weather.met_office_wavertree_daily") is None
|
2020-06-15 10:02:25 +00:00
|
|
|
for sensor_id in WAVERTREE_SENSOR_RESULTS:
|
2021-06-27 19:04:42 +00:00
|
|
|
sensor_name, _ = WAVERTREE_SENSOR_RESULTS[sensor_id]
|
2020-06-15 10:02:25 +00:00
|
|
|
sensor = hass.states.get(f"sensor.wavertree_{sensor_name}")
|
|
|
|
assert sensor is None
|
|
|
|
|
|
|
|
|
|
|
|
@patch(
|
2020-08-27 11:56:20 +00:00
|
|
|
"datapoint.Forecast.datetime.datetime",
|
|
|
|
NewDateTime,
|
2020-06-15 10:02:25 +00:00
|
|
|
)
|
2020-06-29 16:39:24 +00:00
|
|
|
async def test_site_cannot_update(hass, requests_mock, legacy_patchable_time):
|
2020-06-15 10:02:25 +00:00
|
|
|
"""Test we handle cannot connect error."""
|
|
|
|
|
|
|
|
# all metoffice test data encapsulated in here
|
|
|
|
mock_json = json.loads(load_fixture("metoffice.json"))
|
|
|
|
all_sites = json.dumps(mock_json["all_sites"])
|
|
|
|
wavertree_hourly = json.dumps(mock_json["wavertree_hourly"])
|
2021-06-27 19:04:42 +00:00
|
|
|
wavertree_daily = json.dumps(mock_json["wavertree_daily"])
|
2020-06-15 10:02:25 +00:00
|
|
|
|
|
|
|
requests_mock.get("/public/data/val/wxfcs/all/json/sitelist/", text=all_sites)
|
|
|
|
requests_mock.get(
|
|
|
|
"/public/data/val/wxfcs/all/json/354107?res=3hourly", text=wavertree_hourly
|
|
|
|
)
|
2021-06-27 19:04:42 +00:00
|
|
|
requests_mock.get(
|
|
|
|
"/public/data/val/wxfcs/all/json/354107?res=daily", text=wavertree_daily
|
|
|
|
)
|
2020-06-15 10:02:25 +00:00
|
|
|
|
2020-08-27 11:56:20 +00:00
|
|
|
entry = MockConfigEntry(
|
|
|
|
domain=DOMAIN,
|
|
|
|
data=METOFFICE_CONFIG_WAVERTREE,
|
|
|
|
)
|
2020-06-15 10:02:25 +00:00
|
|
|
entry.add_to_hass(hass)
|
|
|
|
await hass.config_entries.async_setup(entry.entry_id)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
2021-07-08 08:01:06 +00:00
|
|
|
weather = hass.states.get("weather.met_office_wavertree_3_hourly")
|
|
|
|
assert weather
|
2021-06-27 19:04:42 +00:00
|
|
|
|
2021-07-08 08:01:06 +00:00
|
|
|
weather = hass.states.get("weather.met_office_wavertree_daily")
|
|
|
|
assert weather
|
2020-06-15 10:02:25 +00:00
|
|
|
|
|
|
|
requests_mock.get("/public/data/val/wxfcs/all/json/354107?res=3hourly", text="")
|
2021-06-27 19:04:42 +00:00
|
|
|
requests_mock.get("/public/data/val/wxfcs/all/json/354107?res=daily", text="")
|
2020-06-15 10:02:25 +00:00
|
|
|
|
|
|
|
future_time = utcnow() + timedelta(minutes=20)
|
|
|
|
async_fire_time_changed(hass, future_time)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
2021-07-08 08:01:06 +00:00
|
|
|
weather = hass.states.get("weather.met_office_wavertree_3_hourly")
|
|
|
|
assert weather.state == STATE_UNAVAILABLE
|
2021-06-27 19:04:42 +00:00
|
|
|
|
2021-07-08 08:01:06 +00:00
|
|
|
weather = hass.states.get("weather.met_office_wavertree_daily")
|
|
|
|
assert weather.state == STATE_UNAVAILABLE
|
2020-06-15 10:02:25 +00:00
|
|
|
|
|
|
|
|
|
|
|
@patch(
|
2020-08-27 11:56:20 +00:00
|
|
|
"datapoint.Forecast.datetime.datetime",
|
|
|
|
NewDateTime,
|
2020-06-15 10:02:25 +00:00
|
|
|
)
|
2020-06-29 16:39:24 +00:00
|
|
|
async def test_one_weather_site_running(hass, requests_mock, legacy_patchable_time):
|
2020-06-15 10:02:25 +00:00
|
|
|
"""Test the Met Office weather platform."""
|
|
|
|
|
|
|
|
# all metoffice test data encapsulated in here
|
|
|
|
mock_json = json.loads(load_fixture("metoffice.json"))
|
|
|
|
all_sites = json.dumps(mock_json["all_sites"])
|
|
|
|
wavertree_hourly = json.dumps(mock_json["wavertree_hourly"])
|
2021-06-27 19:04:42 +00:00
|
|
|
wavertree_daily = json.dumps(mock_json["wavertree_daily"])
|
2020-06-15 10:02:25 +00:00
|
|
|
|
|
|
|
requests_mock.get("/public/data/val/wxfcs/all/json/sitelist/", text=all_sites)
|
|
|
|
requests_mock.get(
|
2020-08-27 11:56:20 +00:00
|
|
|
"/public/data/val/wxfcs/all/json/354107?res=3hourly",
|
|
|
|
text=wavertree_hourly,
|
2020-06-15 10:02:25 +00:00
|
|
|
)
|
2021-06-27 19:04:42 +00:00
|
|
|
requests_mock.get(
|
|
|
|
"/public/data/val/wxfcs/all/json/354107?res=daily",
|
|
|
|
text=wavertree_daily,
|
|
|
|
)
|
2020-06-15 10:02:25 +00:00
|
|
|
|
2020-08-27 11:56:20 +00:00
|
|
|
entry = MockConfigEntry(
|
|
|
|
domain=DOMAIN,
|
|
|
|
data=METOFFICE_CONFIG_WAVERTREE,
|
|
|
|
)
|
2020-06-15 10:02:25 +00:00
|
|
|
entry.add_to_hass(hass)
|
|
|
|
await hass.config_entries.async_setup(entry.entry_id)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
2021-06-27 19:04:42 +00:00
|
|
|
# Wavertree 3-hourly weather platform expected results
|
2021-07-08 08:01:06 +00:00
|
|
|
weather = hass.states.get("weather.met_office_wavertree_3_hourly")
|
|
|
|
assert weather
|
2020-06-15 10:02:25 +00:00
|
|
|
|
2021-07-08 08:01:06 +00:00
|
|
|
assert weather.state == "sunny"
|
|
|
|
assert weather.attributes.get("temperature") == 17
|
|
|
|
assert weather.attributes.get("wind_speed") == 9
|
|
|
|
assert weather.attributes.get("wind_bearing") == "SSE"
|
|
|
|
assert weather.attributes.get("visibility") == "Good - 10-20"
|
|
|
|
assert weather.attributes.get("humidity") == 50
|
2020-06-15 10:02:25 +00:00
|
|
|
|
2021-06-27 19:04:42 +00:00
|
|
|
# Forecasts added - just pick out 1 entry to check
|
2021-07-08 08:01:06 +00:00
|
|
|
assert len(weather.attributes.get("forecast")) == 35
|
2021-06-27 19:04:42 +00:00
|
|
|
|
|
|
|
assert (
|
2021-07-08 08:01:06 +00:00
|
|
|
weather.attributes.get("forecast")[26]["datetime"]
|
|
|
|
== "2020-04-28T21:00:00+00:00"
|
2021-06-27 19:04:42 +00:00
|
|
|
)
|
2021-07-08 08:01:06 +00:00
|
|
|
assert weather.attributes.get("forecast")[26]["condition"] == "cloudy"
|
|
|
|
assert weather.attributes.get("forecast")[26]["temperature"] == 10
|
|
|
|
assert weather.attributes.get("forecast")[26]["wind_speed"] == 4
|
|
|
|
assert weather.attributes.get("forecast")[26]["wind_bearing"] == "NNE"
|
2021-06-27 19:04:42 +00:00
|
|
|
|
|
|
|
# Wavertree daily weather platform expected results
|
2021-07-08 08:01:06 +00:00
|
|
|
weather = hass.states.get("weather.met_office_wavertree_daily")
|
|
|
|
assert weather
|
2021-06-27 19:04:42 +00:00
|
|
|
|
2021-07-08 08:01:06 +00:00
|
|
|
assert weather.state == "sunny"
|
|
|
|
assert weather.attributes.get("temperature") == 19
|
|
|
|
assert weather.attributes.get("wind_speed") == 9
|
|
|
|
assert weather.attributes.get("wind_bearing") == "SSE"
|
|
|
|
assert weather.attributes.get("visibility") == "Good - 10-20"
|
|
|
|
assert weather.attributes.get("humidity") == 50
|
2021-06-27 19:04:42 +00:00
|
|
|
|
|
|
|
# Also has Forecasts added - again, just pick out 1 entry to check
|
2021-07-08 08:01:06 +00:00
|
|
|
assert len(weather.attributes.get("forecast")) == 8
|
2021-06-27 19:04:42 +00:00
|
|
|
|
|
|
|
assert (
|
2021-07-08 08:01:06 +00:00
|
|
|
weather.attributes.get("forecast")[7]["datetime"] == "2020-04-29T12:00:00+00:00"
|
2021-06-27 19:04:42 +00:00
|
|
|
)
|
2021-07-08 08:01:06 +00:00
|
|
|
assert weather.attributes.get("forecast")[7]["condition"] == "rainy"
|
|
|
|
assert weather.attributes.get("forecast")[7]["temperature"] == 13
|
|
|
|
assert weather.attributes.get("forecast")[7]["wind_speed"] == 13
|
|
|
|
assert weather.attributes.get("forecast")[7]["wind_bearing"] == "SE"
|
2021-06-27 19:04:42 +00:00
|
|
|
|
2020-06-15 10:02:25 +00:00
|
|
|
|
|
|
|
@patch(
|
2020-08-27 11:56:20 +00:00
|
|
|
"datapoint.Forecast.datetime.datetime",
|
|
|
|
NewDateTime,
|
2020-06-15 10:02:25 +00:00
|
|
|
)
|
2020-06-29 16:39:24 +00:00
|
|
|
async def test_two_weather_sites_running(hass, requests_mock, legacy_patchable_time):
|
2020-06-15 10:02:25 +00:00
|
|
|
"""Test we handle two different weather sites both running."""
|
|
|
|
|
|
|
|
# all metoffice test data encapsulated in here
|
|
|
|
mock_json = json.loads(load_fixture("metoffice.json"))
|
|
|
|
all_sites = json.dumps(mock_json["all_sites"])
|
|
|
|
wavertree_hourly = json.dumps(mock_json["wavertree_hourly"])
|
2021-06-27 19:04:42 +00:00
|
|
|
wavertree_daily = json.dumps(mock_json["wavertree_daily"])
|
2020-06-15 10:02:25 +00:00
|
|
|
kingslynn_hourly = json.dumps(mock_json["kingslynn_hourly"])
|
2021-06-27 19:04:42 +00:00
|
|
|
kingslynn_daily = json.dumps(mock_json["kingslynn_daily"])
|
2020-06-15 10:02:25 +00:00
|
|
|
|
|
|
|
requests_mock.get("/public/data/val/wxfcs/all/json/sitelist/", text=all_sites)
|
|
|
|
requests_mock.get(
|
|
|
|
"/public/data/val/wxfcs/all/json/354107?res=3hourly", text=wavertree_hourly
|
|
|
|
)
|
2021-06-27 19:04:42 +00:00
|
|
|
requests_mock.get(
|
|
|
|
"/public/data/val/wxfcs/all/json/354107?res=daily", text=wavertree_daily
|
|
|
|
)
|
2020-06-15 10:02:25 +00:00
|
|
|
requests_mock.get(
|
|
|
|
"/public/data/val/wxfcs/all/json/322380?res=3hourly", text=kingslynn_hourly
|
|
|
|
)
|
2021-06-27 19:04:42 +00:00
|
|
|
requests_mock.get(
|
|
|
|
"/public/data/val/wxfcs/all/json/322380?res=daily", text=kingslynn_daily
|
|
|
|
)
|
2020-06-15 10:02:25 +00:00
|
|
|
|
2020-08-27 11:56:20 +00:00
|
|
|
entry = MockConfigEntry(
|
|
|
|
domain=DOMAIN,
|
|
|
|
data=METOFFICE_CONFIG_WAVERTREE,
|
|
|
|
)
|
2020-06-15 10:02:25 +00:00
|
|
|
entry.add_to_hass(hass)
|
|
|
|
await hass.config_entries.async_setup(entry.entry_id)
|
2020-08-27 11:56:20 +00:00
|
|
|
entry2 = MockConfigEntry(
|
|
|
|
domain=DOMAIN,
|
|
|
|
data=METOFFICE_CONFIG_KINGSLYNN,
|
|
|
|
)
|
2020-06-15 10:02:25 +00:00
|
|
|
entry2.add_to_hass(hass)
|
|
|
|
await hass.config_entries.async_setup(entry2.entry_id)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
2021-06-27 19:04:42 +00:00
|
|
|
# Wavertree 3-hourly weather platform expected results
|
2021-07-08 08:01:06 +00:00
|
|
|
weather = hass.states.get("weather.met_office_wavertree_3_hourly")
|
|
|
|
assert weather
|
2020-06-15 10:02:25 +00:00
|
|
|
|
2021-07-08 08:01:06 +00:00
|
|
|
assert weather.state == "sunny"
|
|
|
|
assert weather.attributes.get("temperature") == 17
|
|
|
|
assert weather.attributes.get("wind_speed") == 9
|
|
|
|
assert weather.attributes.get("wind_bearing") == "SSE"
|
|
|
|
assert weather.attributes.get("visibility") == "Good - 10-20"
|
|
|
|
assert weather.attributes.get("humidity") == 50
|
2020-06-15 10:02:25 +00:00
|
|
|
|
2021-06-27 19:04:42 +00:00
|
|
|
# Forecasts added - just pick out 1 entry to check
|
2021-07-08 08:01:06 +00:00
|
|
|
assert len(weather.attributes.get("forecast")) == 35
|
2021-06-27 19:04:42 +00:00
|
|
|
|
|
|
|
assert (
|
2021-07-08 08:01:06 +00:00
|
|
|
weather.attributes.get("forecast")[18]["datetime"]
|
|
|
|
== "2020-04-27T21:00:00+00:00"
|
2021-06-27 19:04:42 +00:00
|
|
|
)
|
2021-07-08 08:01:06 +00:00
|
|
|
assert weather.attributes.get("forecast")[18]["condition"] == "sunny"
|
|
|
|
assert weather.attributes.get("forecast")[18]["temperature"] == 9
|
|
|
|
assert weather.attributes.get("forecast")[18]["wind_speed"] == 4
|
|
|
|
assert weather.attributes.get("forecast")[18]["wind_bearing"] == "NW"
|
2021-06-27 19:04:42 +00:00
|
|
|
|
|
|
|
# Wavertree daily weather platform expected results
|
2021-07-08 08:01:06 +00:00
|
|
|
weather = hass.states.get("weather.met_office_wavertree_daily")
|
|
|
|
assert weather
|
2021-06-27 19:04:42 +00:00
|
|
|
|
2021-07-08 08:01:06 +00:00
|
|
|
assert weather.state == "sunny"
|
|
|
|
assert weather.attributes.get("temperature") == 19
|
|
|
|
assert weather.attributes.get("wind_speed") == 9
|
|
|
|
assert weather.attributes.get("wind_bearing") == "SSE"
|
|
|
|
assert weather.attributes.get("visibility") == "Good - 10-20"
|
|
|
|
assert weather.attributes.get("humidity") == 50
|
2021-06-27 19:04:42 +00:00
|
|
|
|
|
|
|
# Also has Forecasts added - again, just pick out 1 entry to check
|
2021-07-08 08:01:06 +00:00
|
|
|
assert len(weather.attributes.get("forecast")) == 8
|
2021-06-27 19:04:42 +00:00
|
|
|
|
|
|
|
assert (
|
2021-07-08 08:01:06 +00:00
|
|
|
weather.attributes.get("forecast")[7]["datetime"] == "2020-04-29T12:00:00+00:00"
|
2021-06-27 19:04:42 +00:00
|
|
|
)
|
2021-07-08 08:01:06 +00:00
|
|
|
assert weather.attributes.get("forecast")[7]["condition"] == "rainy"
|
|
|
|
assert weather.attributes.get("forecast")[7]["temperature"] == 13
|
|
|
|
assert weather.attributes.get("forecast")[7]["wind_speed"] == 13
|
|
|
|
assert weather.attributes.get("forecast")[7]["wind_bearing"] == "SE"
|
2021-06-27 19:04:42 +00:00
|
|
|
|
|
|
|
# King's Lynn 3-hourly weather platform expected results
|
2021-07-08 08:01:06 +00:00
|
|
|
weather = hass.states.get("weather.met_office_king_s_lynn_3_hourly")
|
|
|
|
assert weather
|
2020-06-15 10:02:25 +00:00
|
|
|
|
2021-07-08 08:01:06 +00:00
|
|
|
assert weather.state == "sunny"
|
|
|
|
assert weather.attributes.get("temperature") == 14
|
|
|
|
assert weather.attributes.get("wind_speed") == 2
|
|
|
|
assert weather.attributes.get("wind_bearing") == "E"
|
|
|
|
assert weather.attributes.get("visibility") == "Very Good - 20-40"
|
|
|
|
assert weather.attributes.get("humidity") == 60
|
2021-06-27 19:04:42 +00:00
|
|
|
|
|
|
|
# Also has Forecast added - just pick out 1 entry to check
|
2021-07-08 08:01:06 +00:00
|
|
|
assert len(weather.attributes.get("forecast")) == 35
|
2021-06-27 19:04:42 +00:00
|
|
|
|
|
|
|
assert (
|
2021-07-08 08:01:06 +00:00
|
|
|
weather.attributes.get("forecast")[18]["datetime"]
|
|
|
|
== "2020-04-27T21:00:00+00:00"
|
2021-06-27 19:04:42 +00:00
|
|
|
)
|
2021-07-08 08:01:06 +00:00
|
|
|
assert weather.attributes.get("forecast")[18]["condition"] == "cloudy"
|
|
|
|
assert weather.attributes.get("forecast")[18]["temperature"] == 10
|
|
|
|
assert weather.attributes.get("forecast")[18]["wind_speed"] == 7
|
|
|
|
assert weather.attributes.get("forecast")[18]["wind_bearing"] == "SE"
|
2021-06-27 19:04:42 +00:00
|
|
|
|
|
|
|
# King's Lynn daily weather platform expected results
|
2021-07-08 08:01:06 +00:00
|
|
|
weather = hass.states.get("weather.met_office_king_s_lynn_daily")
|
|
|
|
assert weather
|
2021-06-27 19:04:42 +00:00
|
|
|
|
2021-07-08 08:01:06 +00:00
|
|
|
assert weather.state == "cloudy"
|
|
|
|
assert weather.attributes.get("temperature") == 9
|
|
|
|
assert weather.attributes.get("wind_speed") == 4
|
|
|
|
assert weather.attributes.get("wind_bearing") == "ESE"
|
|
|
|
assert weather.attributes.get("visibility") == "Very Good - 20-40"
|
|
|
|
assert weather.attributes.get("humidity") == 75
|
2021-06-27 19:04:42 +00:00
|
|
|
|
|
|
|
# All should have Forecast added - again, just picking out 1 entry to check
|
2021-07-08 08:01:06 +00:00
|
|
|
assert len(weather.attributes.get("forecast")) == 8
|
2021-06-27 19:04:42 +00:00
|
|
|
|
|
|
|
assert (
|
2021-07-08 08:01:06 +00:00
|
|
|
weather.attributes.get("forecast")[5]["datetime"] == "2020-04-28T12:00:00+00:00"
|
2021-06-27 19:04:42 +00:00
|
|
|
)
|
2021-07-08 08:01:06 +00:00
|
|
|
assert weather.attributes.get("forecast")[5]["condition"] == "cloudy"
|
|
|
|
assert weather.attributes.get("forecast")[5]["temperature"] == 11
|
|
|
|
assert weather.attributes.get("forecast")[5]["wind_speed"] == 7
|
|
|
|
assert weather.attributes.get("forecast")[5]["wind_bearing"] == "ESE"
|