From a093f943d7136af12eb88acb993332b7882c76fe Mon Sep 17 00:00:00 2001 From: Sid <27780930+autinerd@users.noreply.github.com> Date: Fri, 12 Apr 2024 00:03:10 +0200 Subject: [PATCH] Use library classes instead of namedtuple in ipma tests (#115372) --- tests/components/ipma/__init__.py | 149 ++++++++---------- .../ipma/snapshots/test_diagnostics.ambr | 57 ++----- .../ipma/snapshots/test_weather.ambr | 8 +- 3 files changed, 83 insertions(+), 131 deletions(-) diff --git a/tests/components/ipma/__init__.py b/tests/components/ipma/__init__.py index 65cff43c8d4..799120e3966 100644 --- a/tests/components/ipma/__init__.py +++ b/tests/components/ipma/__init__.py @@ -1,8 +1,12 @@ """Tests for the IPMA component.""" -from collections import namedtuple from datetime import UTC, datetime +from pyipma.forecast import Forecast, Forecast_Location, Weather_Type +from pyipma.observation import Observation +from pyipma.rcm import RCM +from pyipma.uv import UV + from homeassistant.const import CONF_LATITUDE, CONF_LONGITUDE, CONF_MODE, CONF_NAME ENTRY_CONFIG = { @@ -18,109 +22,90 @@ class MockLocation: async def fire_risk(self, api): """Mock Fire Risk.""" - RCM = namedtuple( - "RCM", - [ - "dico", - "rcm", - "coordinates", - ], - ) return RCM("some place", 3, (0, 0)) async def uv_risk(self, api): """Mock UV Index.""" - UV = namedtuple( - "UV", - ["idPeriodo", "intervaloHora", "data", "globalIdLocal", "iUv"], - ) - return UV(0, "0", datetime.now(), 0, 5.7) + return UV(0, "0", datetime(2020, 1, 16, 0, 0, 0), 0, 5.7) async def observation(self, api): """Mock Observation.""" - Observation = namedtuple( - "Observation", - [ - "accumulated_precipitation", - "humidity", - "pressure", - "radiation", - "temperature", - "wind_direction", - "wind_intensity_km", - ], + return Observation( + precAcumulada=0.0, + humidade=71.0, + pressao=1000.0, + radiacao=0.0, + temperatura=18.0, + idDireccVento=8, + intensidadeVentoKM=3.94, + intensidadeVento=1.0944, + timestamp=datetime(2020, 1, 16, 0, 0, 0), + idEstacao=0, ) - return Observation(0.0, 71.0, 1000.0, 0.0, 18.0, "NW", 3.94) - async def forecast(self, api, period): """Mock Forecast.""" - Forecast = namedtuple( - "Forecast", - [ - "feels_like_temperature", - "forecast_date", - "forecasted_hours", - "humidity", - "max_temperature", - "min_temperature", - "precipitation_probability", - "temperature", - "update_date", - "weather_type", - "wind_direction", - "wind_strength", - ], - ) - - WeatherType = namedtuple("WeatherType", ["id", "en", "pt"]) if period == 24: return [ Forecast( - None, - datetime(2020, 1, 16, 0, 0, 0), - 24, - None, - 16.2, - 10.6, - "100.0", - 13.4, - "2020-01-15T07:51:00", - WeatherType(9, "Rain/showers", "Chuva/aguaceiros"), - "S", - "10", + utci=None, + dataPrev=datetime(2020, 1, 16, 0, 0, 0), + idPeriodo=24, + hR=None, + tMax=16.2, + tMin=10.6, + probabilidadePrecipita=100.0, + tMed=13.4, + dataUpdate=datetime(2020, 1, 15, 7, 51, 0), + idTipoTempo=Weather_Type(9, "Rain/showers", "Chuva/aguaceiros"), + ddVento="S", + ffVento=10, + idFfxVento=0, + iUv=0, + intervaloHora="", + location=Forecast_Location(0, "", 0, 0, 0, "", (0, 0)), ), ] if period == 1: return [ Forecast( - "7.7", - datetime(2020, 1, 15, 1, 0, 0, tzinfo=UTC), - 1, - "86.9", - 12.0, - None, - 80.0, - 10.6, - "2020-01-15T02:51:00", - WeatherType(10, "Light rain", "Chuva fraca ou chuvisco"), - "S", - "32.7", + utci=7.7, + dataPrev=datetime(2020, 1, 15, 1, 0, 0, tzinfo=UTC), + idPeriodo=1, + hR=86.9, + tMax=12.0, + tMin=None, + probabilidadePrecipita=80.0, + tMed=10.6, + dataUpdate=datetime(2020, 1, 15, 2, 51, 0), + idTipoTempo=Weather_Type( + 10, "Light rain", "Chuva fraca ou chuvisco" + ), + ddVento="S", + ffVento=32.7, + idFfxVento=0, + iUv=0, + intervaloHora="", + location=Forecast_Location(0, "", 0, 0, 0, "", (0, 0)), ), Forecast( - "5.7", - datetime(2020, 1, 15, 2, 0, 0, tzinfo=UTC), - 1, - "86.9", - 12.0, - None, - 80.0, - 10.6, - "2020-01-15T02:51:00", - WeatherType(1, "Clear sky", "C\u00e9u limpo"), - "S", - "32.7", + utci=5.7, + dataPrev=datetime(2020, 1, 15, 2, 0, 0, tzinfo=UTC), + idPeriodo=1, + hR=86.9, + tMax=12.0, + tMin=None, + probabilidadePrecipita=80.0, + tMed=10.6, + dataUpdate=datetime(2020, 1, 15, 2, 51, 0), + idTipoTempo=Weather_Type(1, "Clear sky", "C\u00e9u limpo"), + ddVento="S", + ffVento=32.7, + idFfxVento=0, + iUv=0, + intervaloHora="", + location=Forecast_Location(0, "", 0, 0, 0, "", (0, 0)), ), ] diff --git a/tests/components/ipma/snapshots/test_diagnostics.ambr b/tests/components/ipma/snapshots/test_diagnostics.ambr index c95364b6e4a..9d7d38db8c3 100644 --- a/tests/components/ipma/snapshots/test_diagnostics.ambr +++ b/tests/components/ipma/snapshots/test_diagnostics.ambr @@ -1,15 +1,10 @@ # serializer version: 1 # name: test_diagnostics dict({ - 'current_weather': list([ - 0.0, - 71.0, - 1000.0, - 0.0, - 18.0, - 'NW', - 3.94, - ]), + 'current_weather': dict({ + '__type': "<class 'pyipma.observation.Observation'>", + 'repr': 'Observation(intensidadeVentoKM=3.94, temperatura=18.0, radiacao=0.0, idDireccVento=8, precAcumulada=0.0, intensidadeVento=1.0944, humidade=71.0, pressao=1000.0, timestamp=datetime.datetime(2020, 1, 16, 0, 0), idEstacao=0)', + }), 'location_information': dict({ 'global_id_local': 1130600, 'id_station': 1200545, @@ -19,42 +14,14 @@ 'station': 'HomeTown Station', }), 'weather_forecast': list([ - list([ - '7.7', - '2020-01-15T01:00:00+00:00', - 1, - '86.9', - 12.0, - None, - 80.0, - 10.6, - '2020-01-15T02:51:00', - list([ - 10, - 'Light rain', - 'Chuva fraca ou chuvisco', - ]), - 'S', - '32.7', - ]), - list([ - '5.7', - '2020-01-15T02:00:00+00:00', - 1, - '86.9', - 12.0, - None, - 80.0, - 10.6, - '2020-01-15T02:51:00', - list([ - 1, - 'Clear sky', - 'Céu limpo', - ]), - 'S', - '32.7', - ]), + dict({ + '__type': "<class 'pyipma.forecast.Forecast'>", + 'repr': "Forecast(tMed=10.6, tMin=None, ffVento=32.7, idFfxVento=0, dataUpdate=datetime.datetime(2020, 1, 15, 2, 51), tMax=12.0, iUv=0, intervaloHora='', idTipoTempo=Weather_Type(id=10, en='Light rain', pt='Chuva fraca ou chuvisco'), hR=86.9, location=Forecast_Location(globalIdLocal=0, local='', idRegiao=0, idDistrito=0, idConcelho=0, idAreaAviso='', coordinates=(0, 0)), probabilidadePrecipita=80.0, idPeriodo=1, dataPrev=datetime.datetime(2020, 1, 15, 1, 0, tzinfo=datetime.timezone.utc), ddVento='S', utci=7.7)", + }), + dict({ + '__type': "<class 'pyipma.forecast.Forecast'>", + 'repr': "Forecast(tMed=10.6, tMin=None, ffVento=32.7, idFfxVento=0, dataUpdate=datetime.datetime(2020, 1, 15, 2, 51), tMax=12.0, iUv=0, intervaloHora='', idTipoTempo=Weather_Type(id=1, en='Clear sky', pt='Céu limpo'), hR=86.9, location=Forecast_Location(globalIdLocal=0, local='', idRegiao=0, idDistrito=0, idConcelho=0, idAreaAviso='', coordinates=(0, 0)), probabilidadePrecipita=80.0, idPeriodo=1, dataPrev=datetime.datetime(2020, 1, 15, 2, 0, tzinfo=datetime.timezone.utc), ddVento='S', utci=5.7)", + }), ]), }) # --- diff --git a/tests/components/ipma/snapshots/test_weather.ambr b/tests/components/ipma/snapshots/test_weather.ambr index 0a778776329..1142cb7cfe5 100644 --- a/tests/components/ipma/snapshots/test_weather.ambr +++ b/tests/components/ipma/snapshots/test_weather.ambr @@ -83,7 +83,7 @@ dict({ 'condition': 'rainy', 'datetime': datetime.datetime(2020, 1, 16, 0, 0), - 'precipitation_probability': '100.0', + 'precipitation_probability': 100.0, 'temperature': 16.2, 'templow': 10.6, 'wind_bearing': 'S', @@ -121,7 +121,7 @@ dict({ 'condition': 'rainy', 'datetime': datetime.datetime(2020, 1, 16, 0, 0), - 'precipitation_probability': '100.0', + 'precipitation_probability': 100.0, 'temperature': 16.2, 'templow': 10.6, 'wind_bearing': 'S', @@ -160,7 +160,7 @@ dict({ 'condition': 'rainy', 'datetime': '2020-01-16T00:00:00', - 'precipitation_probability': '100.0', + 'precipitation_probability': 100.0, 'temperature': 16.2, 'templow': 10.6, 'wind_bearing': 'S', @@ -173,7 +173,7 @@ dict({ 'condition': 'rainy', 'datetime': '2020-01-16T00:00:00', - 'precipitation_probability': '100.0', + 'precipitation_probability': 100.0, 'temperature': 16.2, 'templow': 10.6, 'wind_bearing': 'S',