Skip to content

Commit

Permalink
Update town.py
Browse files Browse the repository at this point in the history
  • Loading branch information
Noltari authored Aug 23, 2023
1 parent d162c4d commit 28a060e
Showing 1 changed file with 35 additions and 4 deletions.
39 changes: 35 additions & 4 deletions aemet_opendata/town.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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]:
Expand All @@ -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


Expand Down

0 comments on commit 28a060e

Please sign in to comment.