-
Notifications
You must be signed in to change notification settings - Fork 40
feat: enhance weather attribute normalization to support wind gust data across all API endpoints #546
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: development
Are you sure you want to change the base?
feat: enhance weather attribute normalization to support wind gust data across all API endpoints #546
Changes from 1 commit
f771989
90f7cca
7f77323
e889a78
7dce6e3
6ff21df
c6cb1c1
6a77016
9d885b4
1a58e51
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,13 +5,64 @@ | |
| import logging | ||
| from typing import TYPE_CHECKING, Any, cast | ||
|
|
||
| from pyatmo.const import HOME | ||
| from pyatmo.exceptions import NoDeviceError | ||
|
|
||
| if TYPE_CHECKING: | ||
| from pyatmo.const import RawData | ||
|
|
||
| LOG: logging.Logger = logging.getLogger(__name__) | ||
|
|
||
| ATTRIBUTES_TO_FIX: dict[str, str] = { | ||
| "_id": "id", | ||
| "firmware": "firmware_revision", | ||
| "firmware_revision": "firmware_revision", | ||
| "firmware_name": "firmware_name", | ||
| "wifi_status": "wifi_strength", | ||
| "rf_status": "rf_strength", | ||
| "Temperature": "temperature", | ||
| "Humidity": "humidity", | ||
| "Pressure": "pressure", | ||
| "CO2": "co2", | ||
| "AbsolutePressure": "absolute_pressure", | ||
| "Noise": "noise", | ||
| "Rain": "rain", | ||
| "WindStrength": "wind_strength", | ||
| "WindAngle": "wind_angle", | ||
| "GustStrength": "gust_strength", | ||
| "GustAngle": "gust_angle", | ||
| "wind_gust": "gust_strength", | ||
| "wind_gust_angle": "gust_angle", | ||
| } | ||
|
|
||
|
|
||
| def normalize_weather_attributes(raw_data: RawData) -> RawData: | ||
| """Normalize weather-related attributes recursively.""" | ||
|
|
||
| if isinstance(raw_data, dict): | ||
| normalized: dict[str, Any] = {} | ||
| has_internal_id = "_id" in raw_data | ||
| for key, value in raw_data.items(): | ||
| if key == "_id": | ||
| normalized["_id"] = value | ||
| if "id" not in raw_data: | ||
| normalized.setdefault("id", value) | ||
| continue | ||
| if key == "dashboard_data" and isinstance(value, dict): | ||
| normalized.update(normalize_weather_attributes(value)) | ||
| continue | ||
| normalized[ATTRIBUTES_TO_FIX.get(key, key)] = normalize_weather_attributes( | ||
| value | ||
| ) | ||
| if has_internal_id and "id" not in normalized: | ||
| normalized["id"] = raw_data["_id"] | ||
| return normalized | ||
sourcery-ai[bot] marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| if isinstance(raw_data, list): | ||
| return [normalize_weather_attributes(item) for item in raw_data] | ||
|
|
||
| return raw_data | ||
sourcery-ai[bot] marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
|
|
||
| def fix_id(raw_data: list[RawData | str]) -> list[RawData | str]: | ||
| """Fix known errors in station ids like superfluous spaces.""" | ||
|
|
@@ -36,27 +87,33 @@ def fix_id(raw_data: list[RawData | str]) -> list[RawData | str]: | |
| def extract_raw_data(resp: RawData, tag: str) -> RawData: | ||
| """Extract raw data from server response.""" | ||
| if tag == "body": | ||
| return {"public": resp["body"], "errors": []} | ||
| return {"public": normalize_weather_attributes(resp["body"]), "errors": []} | ||
|
|
||
| if resp is None or "body" not in resp or tag not in resp["body"]: | ||
| LOG.debug("Server response (tag: %s): %s", tag, resp) | ||
| msg = "No device found, errors in response" | ||
| raise NoDeviceError(msg) | ||
|
|
||
| body = normalize_weather_attributes(resp["body"]) | ||
|
||
| if tag == HOME and "modules" in body.get(HOME, {}): | ||
| body[HOME]["modules"] = [ | ||
| normalize_weather_attributes(module) for module in body[HOME]["modules"] | ||
| ] | ||
sourcery-ai[bot] marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| if tag == "homes": | ||
| homes: list[dict[str, Any] | str] = fix_id(resp["body"].get(tag)) | ||
| homes: list[dict[str, Any] | str] = fix_id(body.get(tag)) | ||
| if not homes: | ||
| LOG.debug("Server response (tag: %s): %s", tag, resp) | ||
| msg = "No homes found" | ||
| raise NoDeviceError(msg) | ||
| return { | ||
| tag: homes, | ||
| "errors": resp["body"].get("errors", []), | ||
| "errors": body.get("errors", []), | ||
| } | ||
|
|
||
| if not (raw_data := fix_id(resp["body"].get(tag))): | ||
| if not (raw_data := fix_id(body.get(tag))): | ||
| LOG.debug("Server response (tag: %s): %s", tag, resp) | ||
| msg = "No device data available" | ||
| raise NoDeviceError(msg) | ||
|
|
||
| return {tag: raw_data, "errors": resp["body"].get("errors", [])} | ||
| return {tag: raw_data, "errors": body.get("errors", [])} | ||
Uh oh!
There was an error while loading. Please reload this page.