mirror of
https://github.com/home-assistant/core.git
synced 2025-04-23 16:57:53 +00:00
Environment Canada weather format fix (#123960)
* Add missing isoformat. * Move fixture loading to common conftest.py * Add deepcopy.
This commit is contained in:
parent
983806817b
commit
a50aeb0a66
@ -2,6 +2,8 @@
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Any
|
||||
|
||||
from homeassistant.components.weather import (
|
||||
ATTR_CONDITION_CLEAR_NIGHT,
|
||||
ATTR_CONDITION_CLOUDY,
|
||||
@ -190,10 +192,12 @@ def get_forecast(ec_data, hourly) -> list[Forecast] | None:
|
||||
if not (half_days := ec_data.daily_forecasts):
|
||||
return None
|
||||
|
||||
def get_day_forecast(fcst: list[dict[str, str]]) -> Forecast:
|
||||
def get_day_forecast(
|
||||
fcst: list[dict[str, Any]],
|
||||
) -> Forecast:
|
||||
high_temp = int(fcst[0]["temperature"]) if len(fcst) == 2 else None
|
||||
return {
|
||||
ATTR_FORECAST_TIME: fcst[0]["timestamp"],
|
||||
ATTR_FORECAST_TIME: fcst[0]["timestamp"].isoformat(),
|
||||
ATTR_FORECAST_NATIVE_TEMP: high_temp,
|
||||
ATTR_FORECAST_NATIVE_TEMP_LOW: int(fcst[-1]["temperature"]),
|
||||
ATTR_FORECAST_PRECIPITATION_PROBABILITY: int(
|
||||
|
27
tests/components/environment_canada/conftest.py
Normal file
27
tests/components/environment_canada/conftest.py
Normal file
@ -0,0 +1,27 @@
|
||||
"""Common fixture for Environment Canada tests."""
|
||||
|
||||
import contextlib
|
||||
from datetime import datetime
|
||||
import json
|
||||
|
||||
import pytest
|
||||
|
||||
from tests.common import load_fixture
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def ec_data():
|
||||
"""Load Environment Canada data."""
|
||||
|
||||
def date_hook(weather):
|
||||
"""Convert timestamp string to datetime."""
|
||||
|
||||
if t := weather.get("timestamp"):
|
||||
with contextlib.suppress(ValueError):
|
||||
weather["timestamp"] = datetime.fromisoformat(t)
|
||||
return weather
|
||||
|
||||
return json.loads(
|
||||
load_fixture("environment_canada/current_conditions_data.json"),
|
||||
object_hook=date_hook,
|
||||
)
|
@ -5,35 +5,35 @@
|
||||
'forecast': list([
|
||||
dict({
|
||||
'condition': 'sunny',
|
||||
'datetime': '2022-10-04 15:00:00+00:00',
|
||||
'datetime': '2022-10-04T15:00:00+00:00',
|
||||
'precipitation_probability': 0,
|
||||
'temperature': 18.0,
|
||||
'templow': 3.0,
|
||||
}),
|
||||
dict({
|
||||
'condition': 'sunny',
|
||||
'datetime': '2022-10-05 15:00:00+00:00',
|
||||
'datetime': '2022-10-05T15:00:00+00:00',
|
||||
'precipitation_probability': 0,
|
||||
'temperature': 20.0,
|
||||
'templow': 9.0,
|
||||
}),
|
||||
dict({
|
||||
'condition': 'partlycloudy',
|
||||
'datetime': '2022-10-06 15:00:00+00:00',
|
||||
'datetime': '2022-10-06T15:00:00+00:00',
|
||||
'precipitation_probability': 0,
|
||||
'temperature': 20.0,
|
||||
'templow': 7.0,
|
||||
}),
|
||||
dict({
|
||||
'condition': 'rainy',
|
||||
'datetime': '2022-10-07 15:00:00+00:00',
|
||||
'datetime': '2022-10-07T15:00:00+00:00',
|
||||
'precipitation_probability': 40,
|
||||
'temperature': 13.0,
|
||||
'templow': 1.0,
|
||||
}),
|
||||
dict({
|
||||
'condition': 'partlycloudy',
|
||||
'datetime': '2022-10-08 15:00:00+00:00',
|
||||
'datetime': '2022-10-08T15:00:00+00:00',
|
||||
'precipitation_probability': 0,
|
||||
'temperature': 10.0,
|
||||
'templow': 3.0,
|
||||
@ -48,42 +48,42 @@
|
||||
'forecast': list([
|
||||
dict({
|
||||
'condition': 'clear-night',
|
||||
'datetime': '2022-10-03 15:00:00+00:00',
|
||||
'datetime': '2022-10-03T15:00:00+00:00',
|
||||
'precipitation_probability': 0,
|
||||
'temperature': None,
|
||||
'templow': -1.0,
|
||||
}),
|
||||
dict({
|
||||
'condition': 'sunny',
|
||||
'datetime': '2022-10-04 15:00:00+00:00',
|
||||
'datetime': '2022-10-04T15:00:00+00:00',
|
||||
'precipitation_probability': 0,
|
||||
'temperature': 18.0,
|
||||
'templow': 3.0,
|
||||
}),
|
||||
dict({
|
||||
'condition': 'sunny',
|
||||
'datetime': '2022-10-05 15:00:00+00:00',
|
||||
'datetime': '2022-10-05T15:00:00+00:00',
|
||||
'precipitation_probability': 0,
|
||||
'temperature': 20.0,
|
||||
'templow': 9.0,
|
||||
}),
|
||||
dict({
|
||||
'condition': 'partlycloudy',
|
||||
'datetime': '2022-10-06 15:00:00+00:00',
|
||||
'datetime': '2022-10-06T15:00:00+00:00',
|
||||
'precipitation_probability': 0,
|
||||
'temperature': 20.0,
|
||||
'templow': 7.0,
|
||||
}),
|
||||
dict({
|
||||
'condition': 'rainy',
|
||||
'datetime': '2022-10-07 15:00:00+00:00',
|
||||
'datetime': '2022-10-07T15:00:00+00:00',
|
||||
'precipitation_probability': 40,
|
||||
'temperature': 13.0,
|
||||
'templow': 1.0,
|
||||
}),
|
||||
dict({
|
||||
'condition': 'partlycloudy',
|
||||
'datetime': '2022-10-08 15:00:00+00:00',
|
||||
'datetime': '2022-10-08T15:00:00+00:00',
|
||||
'precipitation_probability': 0,
|
||||
'temperature': 10.0,
|
||||
'templow': 3.0,
|
||||
|
@ -1,6 +1,7 @@
|
||||
"""Test Environment Canada diagnostics."""
|
||||
|
||||
import json
|
||||
from typing import Any
|
||||
|
||||
from syrupy import SnapshotAssertion
|
||||
|
||||
@ -26,6 +27,7 @@ async def test_entry_diagnostics(
|
||||
hass: HomeAssistant,
|
||||
hass_client: ClientSessionGenerator,
|
||||
snapshot: SnapshotAssertion,
|
||||
ec_data: dict[str, Any],
|
||||
) -> None:
|
||||
"""Test config entry diagnostics."""
|
||||
|
||||
|
@ -1,6 +1,7 @@
|
||||
"""Test weather."""
|
||||
|
||||
import json
|
||||
import copy
|
||||
from typing import Any
|
||||
|
||||
from syrupy.assertion import SnapshotAssertion
|
||||
|
||||
@ -12,23 +13,17 @@ from homeassistant.core import HomeAssistant
|
||||
|
||||
from . import init_integration
|
||||
|
||||
from tests.common import load_fixture
|
||||
|
||||
|
||||
async def test_forecast_daily(
|
||||
hass: HomeAssistant,
|
||||
snapshot: SnapshotAssertion,
|
||||
hass: HomeAssistant, snapshot: SnapshotAssertion, ec_data: dict[str, Any]
|
||||
) -> None:
|
||||
"""Test basic forecast."""
|
||||
|
||||
ec_data = json.loads(
|
||||
load_fixture("environment_canada/current_conditions_data.json")
|
||||
)
|
||||
|
||||
# First entry in test data is a half day; we don't want that for this test
|
||||
del ec_data["daily_forecasts"][0]
|
||||
local_ec_data = copy.deepcopy(ec_data)
|
||||
del local_ec_data["daily_forecasts"][0]
|
||||
|
||||
await init_integration(hass, ec_data)
|
||||
await init_integration(hass, local_ec_data)
|
||||
|
||||
response = await hass.services.async_call(
|
||||
WEATHER_DOMAIN,
|
||||
@ -44,15 +39,10 @@ async def test_forecast_daily(
|
||||
|
||||
|
||||
async def test_forecast_daily_with_some_previous_days_data(
|
||||
hass: HomeAssistant,
|
||||
snapshot: SnapshotAssertion,
|
||||
hass: HomeAssistant, snapshot: SnapshotAssertion, ec_data: dict[str, Any]
|
||||
) -> None:
|
||||
"""Test forecast with half day at start."""
|
||||
|
||||
ec_data = json.loads(
|
||||
load_fixture("environment_canada/current_conditions_data.json")
|
||||
)
|
||||
|
||||
await init_integration(hass, ec_data)
|
||||
|
||||
response = await hass.services.async_call(
|
||||
|
Loading…
x
Reference in New Issue
Block a user