From 37054bb79331706d5b78bf2821838710833d64ff Mon Sep 17 00:00:00 2001 From: Michael <35783820+mib1185@users.noreply.github.com> Date: Sat, 22 Jul 2023 18:18:39 +0200 Subject: [PATCH] improve station class (#13) --- aiopegelonline/__init__.py | 50 ++++++++++++-------------------------- 1 file changed, 15 insertions(+), 35 deletions(-) diff --git a/aiopegelonline/__init__.py b/aiopegelonline/__init__.py index 6183d38..1b2dd25 100644 --- a/aiopegelonline/__init__.py +++ b/aiopegelonline/__init__.py @@ -11,17 +11,21 @@ from .exceptions import PegelonlineDataError -@dataclass class Station: """Representation of a station.""" - uuid: str - name: str - agency: str - river_kilometer: float | None - longitude: float | None - latitude: float | None - water_name: str + def __init__(self, data: dict) -> None: + """Initialize station class.""" + self.uuid: str = data["uuid"] + self.name: str = data["longname"] + self.agency: str = data["agency"] + self.river_kilometer: float | None = data.get("km") + self.longitude: float | None = data.get("longitude") + self.latitude: float | None = data.get("latitude") + self.water_name: str = data["water"]["longname"] + self.base_data_url: str = ( + f"https://www.pegelonline.wsv.de/gast/stammdaten?pegelnr={data['number']}" + ) @dataclass @@ -67,15 +71,7 @@ async def async_get_all_stations(self) -> dict[str, Station]: result = {} for station in stations: - result[station["uuid"]] = Station( - station["uuid"], - station["longname"], - station["agency"], - station.get("km"), - station.get("longitude"), - station.get("latitude"), - station["water"]["longname"], - ) + result[station["uuid"]] = Station(station) return result @@ -95,15 +91,7 @@ async def async_get_nearby_stations( result = {} for station in stations: - result[station["uuid"]] = Station( - station["uuid"], - station["longname"], - station["agency"], - station.get("km"), - station.get("longitude"), - station.get("latitude"), - station["water"]["longname"], - ) + result[station["uuid"]] = Station(station) return result @@ -113,15 +101,7 @@ async def async_get_station_details(self, uuid: str) -> Station: f"{BASE_URL}/stations/{uuid}.json", {"prettyprint": "false"} ) - return Station( - station["uuid"], - station["longname"], - station["agency"], - station.get("km"), - station.get("longitude"), - station.get("latitude"), - station["water"]["longname"], - ) + return Station(station) async def async_get_station_measurement(self, uuid: str) -> CurrentMeasurement: """Get current measurement of a station."""