Skip to content

Commit

Permalink
Use iso-formatted times in MetOffice weather forecast (#52672)
Browse files Browse the repository at this point in the history
* Fixed raw datetime in MetOffice weather forecast

* Use datetime in sensor attribute
  • Loading branch information
avee87 authored Jul 8, 2021
1 parent c4d8d1d commit 5ff7c77
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 114 deletions.
2 changes: 1 addition & 1 deletion homeassistant/components/metoffice/weather.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ async def async_setup_entry(

def _build_forecast_data(timestep):
data = {}
data[ATTR_FORECAST_TIME] = timestep.date
data[ATTR_FORECAST_TIME] = timestep.date.isoformat()
if timestep.weather:
data[ATTR_FORECAST_CONDITION] = _get_weather_condition(timestep.weather.value)
if timestep.precipitation:
Expand Down
3 changes: 1 addition & 2 deletions tests/components/metoffice/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@

from homeassistant.const import CONF_API_KEY, CONF_LATITUDE, CONF_LONGITUDE, CONF_NAME

DATETIME_FORMAT = "%Y-%m-%d %H:%M:%S%z"
TEST_DATETIME_STRING = "2020-04-25 12:00:00+0000"
TEST_DATETIME_STRING = "2020-04-25T12:00:00+00:00"

TEST_API_KEY = "test-metoffice-api-key"

Expand Down
18 changes: 6 additions & 12 deletions tests/components/metoffice/test_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

from . import NewDateTime
from .const import (
DATETIME_FORMAT,
KINGSLYNN_SENSOR_RESULTS,
METOFFICE_CONFIG_KINGSLYNN,
METOFFICE_CONFIG_WAVERTREE,
Expand Down Expand Up @@ -54,13 +53,10 @@ async def test_one_sensor_site_running(hass, requests_mock, legacy_patchable_tim
for running_id in running_sensor_ids:
sensor = hass.states.get(running_id)
sensor_id = sensor.attributes.get("sensor_id")
sensor_name, sensor_value = WAVERTREE_SENSOR_RESULTS[sensor_id]
_, sensor_value = WAVERTREE_SENSOR_RESULTS[sensor_id]

assert sensor.state == sensor_value
assert (
sensor.attributes.get("last_update").strftime(DATETIME_FORMAT)
== TEST_DATETIME_STRING
)
assert sensor.attributes.get("last_update").isoformat() == TEST_DATETIME_STRING
assert sensor.attributes.get("site_id") == "354107"
assert sensor.attributes.get("site_name") == TEST_SITE_NAME_WAVERTREE
assert sensor.attributes.get("attribution") == ATTRIBUTION
Expand Down Expand Up @@ -115,23 +111,21 @@ async def test_two_sensor_sites_running(hass, requests_mock, legacy_patchable_ti
sensor = hass.states.get(running_id)
sensor_id = sensor.attributes.get("sensor_id")
if sensor.attributes.get("site_id") == "354107":
sensor_name, sensor_value = WAVERTREE_SENSOR_RESULTS[sensor_id]
_, sensor_value = WAVERTREE_SENSOR_RESULTS[sensor_id]
assert sensor.state == sensor_value
assert (
sensor.attributes.get("last_update").strftime(DATETIME_FORMAT)
== TEST_DATETIME_STRING
sensor.attributes.get("last_update").isoformat() == TEST_DATETIME_STRING
)
assert sensor.attributes.get("sensor_id") == sensor_id
assert sensor.attributes.get("site_id") == "354107"
assert sensor.attributes.get("site_name") == TEST_SITE_NAME_WAVERTREE
assert sensor.attributes.get("attribution") == ATTRIBUTION

else:
sensor_name, sensor_value = KINGSLYNN_SENSOR_RESULTS[sensor_id]
_, sensor_value = KINGSLYNN_SENSOR_RESULTS[sensor_id]
assert sensor.state == sensor_value
assert (
sensor.attributes.get("last_update").strftime(DATETIME_FORMAT)
== TEST_DATETIME_STRING
sensor.attributes.get("last_update").isoformat() == TEST_DATETIME_STRING
)
assert sensor.attributes.get("sensor_id") == sensor_id
assert sensor.attributes.get("site_id") == "322380"
Expand Down
194 changes: 95 additions & 99 deletions tests/components/metoffice/test_weather.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

from . import NewDateTime
from .const import (
DATETIME_FORMAT,
METOFFICE_CONFIG_KINGSLYNN,
METOFFICE_CONFIG_WAVERTREE,
WAVERTREE_SENSOR_RESULTS,
Expand Down Expand Up @@ -74,11 +73,11 @@ async def test_site_cannot_update(hass, requests_mock, legacy_patchable_time):
await hass.config_entries.async_setup(entry.entry_id)
await hass.async_block_till_done()

entity = hass.states.get("weather.met_office_wavertree_3_hourly")
assert entity
weather = hass.states.get("weather.met_office_wavertree_3_hourly")
assert weather

entity = hass.states.get("weather.met_office_wavertree_daily")
assert entity
weather = hass.states.get("weather.met_office_wavertree_daily")
assert weather

requests_mock.get("/public/data/val/wxfcs/all/json/354107?res=3hourly", text="")
requests_mock.get("/public/data/val/wxfcs/all/json/354107?res=daily", text="")
Expand All @@ -87,11 +86,11 @@ async def test_site_cannot_update(hass, requests_mock, legacy_patchable_time):
async_fire_time_changed(hass, future_time)
await hass.async_block_till_done()

entity = hass.states.get("weather.met_office_wavertree_3_hourly")
assert entity.state == STATE_UNAVAILABLE
weather = hass.states.get("weather.met_office_wavertree_3_hourly")
assert weather.state == STATE_UNAVAILABLE

entity = hass.states.get("weather.met_office_wavertree_daily")
assert entity.state == STATE_UNAVAILABLE
weather = hass.states.get("weather.met_office_wavertree_daily")
assert weather.state == STATE_UNAVAILABLE


@patch(
Expand Down Expand Up @@ -126,50 +125,49 @@ async def test_one_weather_site_running(hass, requests_mock, legacy_patchable_ti
await hass.async_block_till_done()

# Wavertree 3-hourly weather platform expected results
entity = hass.states.get("weather.met_office_wavertree_3_hourly")
assert entity
weather = hass.states.get("weather.met_office_wavertree_3_hourly")
assert weather

assert entity.state == "sunny"
assert entity.attributes.get("temperature") == 17
assert entity.attributes.get("wind_speed") == 9
assert entity.attributes.get("wind_bearing") == "SSE"
assert entity.attributes.get("visibility") == "Good - 10-20"
assert entity.attributes.get("humidity") == 50
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

# Forecasts added - just pick out 1 entry to check
assert len(entity.attributes.get("forecast")) == 35
assert len(weather.attributes.get("forecast")) == 35

assert (
entity.attributes.get("forecast")[26]["datetime"].strftime(DATETIME_FORMAT)
== "2020-04-28 21:00:00+0000"
weather.attributes.get("forecast")[26]["datetime"]
== "2020-04-28T21:00:00+00:00"
)
assert entity.attributes.get("forecast")[26]["condition"] == "cloudy"
assert entity.attributes.get("forecast")[26]["temperature"] == 10
assert entity.attributes.get("forecast")[26]["wind_speed"] == 4
assert entity.attributes.get("forecast")[26]["wind_bearing"] == "NNE"
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"

# Wavertree daily weather platform expected results
entity = hass.states.get("weather.met_office_wavertree_daily")
assert entity
weather = hass.states.get("weather.met_office_wavertree_daily")
assert weather

assert entity.state == "sunny"
assert entity.attributes.get("temperature") == 19
assert entity.attributes.get("wind_speed") == 9
assert entity.attributes.get("wind_bearing") == "SSE"
assert entity.attributes.get("visibility") == "Good - 10-20"
assert entity.attributes.get("humidity") == 50
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

# Also has Forecasts added - again, just pick out 1 entry to check
assert len(entity.attributes.get("forecast")) == 8
assert len(weather.attributes.get("forecast")) == 8

assert (
entity.attributes.get("forecast")[7]["datetime"].strftime(DATETIME_FORMAT)
== "2020-04-29 12:00:00+0000"
weather.attributes.get("forecast")[7]["datetime"] == "2020-04-29T12:00:00+00:00"
)
assert entity.attributes.get("forecast")[7]["condition"] == "rainy"
assert entity.attributes.get("forecast")[7]["temperature"] == 13
assert entity.attributes.get("forecast")[7]["wind_speed"] == 13
assert entity.attributes.get("forecast")[7]["wind_bearing"] == "SE"
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"


@patch(
Expand Down Expand Up @@ -216,93 +214,91 @@ async def test_two_weather_sites_running(hass, requests_mock, legacy_patchable_t
await hass.async_block_till_done()

# Wavertree 3-hourly weather platform expected results
entity = hass.states.get("weather.met_office_wavertree_3_hourly")
assert entity
weather = hass.states.get("weather.met_office_wavertree_3_hourly")
assert weather

assert entity.state == "sunny"
assert entity.attributes.get("temperature") == 17
assert entity.attributes.get("wind_speed") == 9
assert entity.attributes.get("wind_bearing") == "SSE"
assert entity.attributes.get("visibility") == "Good - 10-20"
assert entity.attributes.get("humidity") == 50
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

# Forecasts added - just pick out 1 entry to check
assert len(entity.attributes.get("forecast")) == 35
assert len(weather.attributes.get("forecast")) == 35

assert (
entity.attributes.get("forecast")[18]["datetime"].strftime(DATETIME_FORMAT)
== "2020-04-27 21:00:00+0000"
weather.attributes.get("forecast")[18]["datetime"]
== "2020-04-27T21:00:00+00:00"
)
assert entity.attributes.get("forecast")[18]["condition"] == "sunny"
assert entity.attributes.get("forecast")[18]["temperature"] == 9
assert entity.attributes.get("forecast")[18]["wind_speed"] == 4
assert entity.attributes.get("forecast")[18]["wind_bearing"] == "NW"
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"

# Wavertree daily weather platform expected results
entity = hass.states.get("weather.met_office_wavertree_daily")
assert entity
weather = hass.states.get("weather.met_office_wavertree_daily")
assert weather

assert entity.state == "sunny"
assert entity.attributes.get("temperature") == 19
assert entity.attributes.get("wind_speed") == 9
assert entity.attributes.get("wind_bearing") == "SSE"
assert entity.attributes.get("visibility") == "Good - 10-20"
assert entity.attributes.get("humidity") == 50
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

# Also has Forecasts added - again, just pick out 1 entry to check
assert len(entity.attributes.get("forecast")) == 8
assert len(weather.attributes.get("forecast")) == 8

assert (
entity.attributes.get("forecast")[7]["datetime"].strftime(DATETIME_FORMAT)
== "2020-04-29 12:00:00+0000"
weather.attributes.get("forecast")[7]["datetime"] == "2020-04-29T12:00:00+00:00"
)
assert entity.attributes.get("forecast")[7]["condition"] == "rainy"
assert entity.attributes.get("forecast")[7]["temperature"] == 13
assert entity.attributes.get("forecast")[7]["wind_speed"] == 13
assert entity.attributes.get("forecast")[7]["wind_bearing"] == "SE"
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"

# King's Lynn 3-hourly weather platform expected results
entity = hass.states.get("weather.met_office_king_s_lynn_3_hourly")
assert entity
weather = hass.states.get("weather.met_office_king_s_lynn_3_hourly")
assert weather

assert entity.state == "sunny"
assert entity.attributes.get("temperature") == 14
assert entity.attributes.get("wind_speed") == 2
assert entity.attributes.get("wind_bearing") == "E"
assert entity.attributes.get("visibility") == "Very Good - 20-40"
assert entity.attributes.get("humidity") == 60
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

# Also has Forecast added - just pick out 1 entry to check
assert len(entity.attributes.get("forecast")) == 35
assert len(weather.attributes.get("forecast")) == 35

assert (
entity.attributes.get("forecast")[18]["datetime"].strftime(DATETIME_FORMAT)
== "2020-04-27 21:00:00+0000"
weather.attributes.get("forecast")[18]["datetime"]
== "2020-04-27T21:00:00+00:00"
)
assert entity.attributes.get("forecast")[18]["condition"] == "cloudy"
assert entity.attributes.get("forecast")[18]["temperature"] == 10
assert entity.attributes.get("forecast")[18]["wind_speed"] == 7
assert entity.attributes.get("forecast")[18]["wind_bearing"] == "SE"
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"

# King's Lynn daily weather platform expected results
entity = hass.states.get("weather.met_office_king_s_lynn_daily")
assert entity
weather = hass.states.get("weather.met_office_king_s_lynn_daily")
assert weather

assert entity.state == "cloudy"
assert entity.attributes.get("temperature") == 9
assert entity.attributes.get("wind_speed") == 4
assert entity.attributes.get("wind_bearing") == "ESE"
assert entity.attributes.get("visibility") == "Very Good - 20-40"
assert entity.attributes.get("humidity") == 75
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

# All should have Forecast added - again, just picking out 1 entry to check
assert len(entity.attributes.get("forecast")) == 8
assert len(weather.attributes.get("forecast")) == 8

assert (
entity.attributes.get("forecast")[5]["datetime"].strftime(DATETIME_FORMAT)
== "2020-04-28 12:00:00+0000"
weather.attributes.get("forecast")[5]["datetime"] == "2020-04-28T12:00:00+00:00"
)
assert entity.attributes.get("forecast")[5]["condition"] == "cloudy"
assert entity.attributes.get("forecast")[5]["temperature"] == 11
assert entity.attributes.get("forecast")[5]["wind_speed"] == 7
assert entity.attributes.get("forecast")[5]["wind_bearing"] == "ESE"
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"

0 comments on commit 5ff7c77

Please sign in to comment.