From 28a060e0b6c33e5e9fe78e9a68a7df265940e564 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=81lvaro=20Fern=C3=A1ndez=20Rojas?= Date: Wed, 23 Aug 2023 15:16:47 +0200 Subject: [PATCH] Update town.py --- aemet_opendata/town.py | 39 +++++++++++++++++++++++++++++++++++---- 1 file changed, 35 insertions(+), 4 deletions(-) diff --git a/aemet_opendata/town.py b/aemet_opendata/town.py index ece6d6d..59e0e19 100644 --- a/aemet_opendata/town.py +++ b/aemet_opendata/town.py @@ -4,15 +4,20 @@ from typing import Any from .const import ( + AEMET_ATTR_DAY, AEMET_ATTR_ELABORATED, + AEMET_ATTR_FORECAST, AEMET_ATTR_ID, AEMET_ATTR_NAME, + AEMET_ATTR_SKY_STATE, AEMET_ATTR_TOWN_ALTITUDE, AEMET_ATTR_TOWN_LATITUDE_DECIMAL, AEMET_ATTR_TOWN_LONGITUDE_DECIMAL, AOD_ALTITUDE, + AOD_CONDITION, AOD_COORDS, AOD_DISTANCE, + AOD_FORECAST, AOD_FORECAST_DAILY, AOD_FORECAST_HOURLY, AOD_ID, @@ -32,11 +37,11 @@ def __init__(self, data: dict[str, Any]) -> None: self.timestamp = str(data[AEMET_ATTR_ELABORATED]) def get_datetime(self) -> datetime: - """Return Station datetime of data.""" + """Return Town daily forecast datetime of data.""" return parse_api_timestamp(self.timestamp) def get_timestamp(self) -> str: - """Return Station timestamp.""" + """Return Town daily forecast timestamp.""" return self.timestamp def data(self) -> dict[str, Any]: @@ -48,27 +53,53 @@ def data(self) -> dict[str, Any]: return data +class HourlyForecastValue: + """AEMET OpenData Town Hourly Forecast value.""" + def __init__(self, data: dict[str, Any]) -> None: + """Init AEMET OpenData Town Hourly Forecast.""" + self.condition = str(data[AEMET_ATTR_SKY_STATE]) + + def get_condition(self) -> str: + return self.condition + + def data(self) -> dict[str, Any]: + """Return Town hourly forecast data.""" + data: dict[str, Any] = { + AOD_CONDITION: self.get_condition(), + } + return data + + + class HourlyForecast: """AEMET OpenData Town Hourly Forecast.""" def __init__(self, data: dict[str, Any]) -> None: """Init AEMET OpenData Town Hourly Forecast.""" self.timestamp = str(data[AEMET_ATTR_ELABORATED]) + self.forecast: list[HourlyForecastValue] = [] + + for forecast in data[AEMET_ATTR_FORECAST][AEMET_ATTR_DAY]: + self.forecast += [HourlyForecastValue(forecast)] def get_datetime(self) -> datetime: - """Return Station datetime of data.""" + """Return Town hourly forecast datetime of data.""" return parse_api_timestamp(self.timestamp) def get_timestamp(self) -> str: - """Return Station timestamp.""" + """Return Town hourly forecast timestamp.""" return self.timestamp def data(self) -> dict[str, Any]: """Return Town hourly forecast data.""" data: dict[str, Any] = { AOD_TIMESTAMP: self.get_timestamp(), + AOD_FORECAST = [], } + for forecast in self.forecast: + data[AOD_FORECAST] += [forecast.data()] + return data