diff --git a/custom_components/sws12500/__init__.py b/custom_components/sws12500/__init__.py index 62f14b8..42d119a 100644 --- a/custom_components/sws12500/__init__.py +++ b/custom_components/sws12500/__init__.py @@ -161,7 +161,7 @@ class WeatherDataUpdateCoordinator(DataUpdateCoordinator): # Optional forwarding to external services. This is kept here (in the webhook handler) # to avoid additional background polling tasks. if self.config.options.get(WINDY_ENABLED, False): - await self.windy.push_data_to_windy(data) + await self.windy.push_data_to_windy(data, _wslink) if self.config.options.get(POCASI_CZ_ENABLED, False): await self.pocasi.push_data_to_server(data, "WSLINK" if _wslink else "WU") diff --git a/custom_components/sws12500/config_flow.py b/custom_components/sws12500/config_flow.py index 3852e31..65951ef 100644 --- a/custom_components/sws12500/config_flow.py +++ b/custom_components/sws12500/config_flow.py @@ -95,16 +95,23 @@ class ConfigOptionsFlowHandler(OptionsFlow): } self.windy_data = { - WINDY_STATION_ID: entry_data.get(WINDY_STATION_ID), - WINDY_STATION_PW: entry_data.get(WINDY_STATION_PW), - WINDY_ENABLED: entry_data.get(WINDY_ENABLED, False), - WINDY_LOGGER_ENABLED: entry_data.get(WINDY_LOGGER_ENABLED, False), + WINDY_STATION_ID: self.config_entry.options.get(WINDY_STATION_ID, ""), + WINDY_STATION_PW: self.config_entry.options.get(WINDY_STATION_PW, ""), + WINDY_LOGGER_ENABLED: self.config_entry.options.get( + WINDY_LOGGER_ENABLED, False + ), } self.windy_data_schema = { - vol.Optional(WINDY_STATION_ID, default=self.windy_data.get(WINDY_STATION_ID, "")): str, - vol.Optional(WINDY_STATION_PW, default=self.windy_data.get(WINDY_STATION_PW, "")): str, - vol.Optional(WINDY_ENABLED, default=self.windy_data[WINDY_ENABLED]): bool or False, + vol.Optional( + WINDY_STATION_ID, default=self.windy_data.get(WINDY_STATION_ID, "") + ): str, + vol.Optional( + WINDY_STATION_PW, + default=self.windy_data.get(WINDY_STATION_PW, ""), + ): str, + vol.Optional(WINDY_ENABLED, default=self.windy_data[WINDY_ENABLED]): bool + or False, vol.Optional( WINDY_LOGGER_ENABLED, default=self.windy_data[WINDY_LOGGER_ENABLED], @@ -190,19 +197,15 @@ class ConfigOptionsFlowHandler(OptionsFlow): errors=errors, ) - station_id = (user_input.get(WINDY_STATION_ID) or "").strip() - station_pw = (user_input.get(WINDY_STATION_PW) or "").strip() - if user_input.get(WINDY_ENABLED): - if not station_id: - errors[WINDY_STATION_ID] = "windy_id_required" - if not station_pw: - errors[WINDY_STATION_PW] = "windy_pw_required" - if errors: - return self.async_show_form( - step_id="windy", - data_schema=vol.Schema(self.windy_data_schema), - errors=errors, - ) + if (user_input[WINDY_ENABLED] is True) and ( + (user_input[WINDY_STATION_ID] == "") or (user_input[WINDY_STATION_PW] == "") + ): + errors[WINDY_STATION_ID] = "windy_key_required" + return self.async_show_form( + step_id="windy", + data_schema=vol.Schema(self.windy_data_schema), + errors=errors, + ) user_input = self.retain_data(user_input) diff --git a/custom_components/sws12500/translations/cs.json b/custom_components/sws12500/translations/cs.json index d4ae648..417c54f 100644 --- a/custom_components/sws12500/translations/cs.json +++ b/custom_components/sws12500/translations/cs.json @@ -74,7 +74,7 @@ }, "data_description": { "WINDY_STATION_ID": "ID stanice získaný z https://stations.windy.com/station", - "WINDY_STATION_PWD": "Heslo stanice získané z https://stations.windy.com/station", + "WINDY_STATION_PWD": "Heslo stanice získané z https://stations.windy.com/station", "windy_logger_checkbox": "Zapnout pouze v případě, že chcete poslat ladící informace vývojáři." } }, diff --git a/custom_components/sws12500/windy_func.py b/custom_components/sws12500/windy_func.py index 8d9dd47..6433c1b 100644 --- a/custom_components/sws12500/windy_func.py +++ b/custom_components/sws12500/windy_func.py @@ -4,7 +4,7 @@ from datetime import datetime, timedelta import logging from aiohttp.client_exceptions import ClientError -from py_typecheck.core import checked +from py_typecheck import checked from homeassistant.components import persistent_notification from homeassistant.config_entries import ConfigEntry @@ -86,7 +86,34 @@ class WindyPush: if "Unauthorized" in response: raise WindyApiKeyError - async def push_data_to_windy(self, data: dict[str, str]) -> bool: + def _covert_wslink_to_pws(self, indata: dict[str, str]) -> dict[str, str]: + """Convert WSLink API data to Windy API data protocol.""" + if "t1ws" in indata: + indata["wind"] = indata.pop("t1ws") + if "t1wgust" in indata: + indata["gust"] = indata.pop("t1wgust") + if "t1wdir" in indata: + indata["winddir"] = indata.pop("t1wdir") + if "t1hum" in indata: + indata["humidity"] = indata.pop("t1hum") + if "t1dew" in indata: + indata["dewpoint"] = indata.pop("t1dew") + if "t1tem" in indata: + indata["temp"] = indata.pop("t1tem") + if "rbar" in indata: + indata["mbar"] = indata.pop("rbar") + if "t1rainhr" in indata: + indata["precip"] = indata.pop("t1rainhr") + if "t1uvi" in indata: + indata["uv"] = indata.pop("t1uvi") + if "t1solrad" in indata: + indata["solarradiation"] = indata.pop("t1solrad") + + return indata + + async def push_data_to_windy( + self, data: dict[str, str], wslink: bool = False + ) -> bool: """Pushes weather data do Windy stations. Interval is 5 minutes, otherwise Windy would not accepts data. @@ -113,40 +140,37 @@ class WindyPush: if wslink: # WSLink -> Windy params - if "t1ws" in purged_data: - purged_data["wind"] = purged_data.pop("t1ws") - if "t1wgust" in purged_data: - purged_data["gust"] = purged_data.pop("t1wgust") - if "t1wdir" in purged_data: - purged_data["winddir"] = purged_data.pop("t1wdir") - if "t1hum" in purged_data: - purged_data["humidity"] = purged_data.pop("t1hum") - if "t1dew" in purged_data: - purged_data["dewpoint"] = purged_data.pop("t1dew") - if "t1tem" in purged_data: - purged_data["temp"] = purged_data.pop("t1tem") - if "rbar" in purged_data: - purged_data["mbar"] = purged_data.pop("rbar") - if "t1rainhr" in purged_data: - purged_data["precip"] = purged_data.pop("t1rainhr") - if "t1uvi" in purged_data: - purged_data["uv"] = purged_data.pop("t1uvi") - if "t1solrad" in purged_data: - purged_data["solarradiation"] = purged_data.pop("t1solrad") + self._covert_wslink_to_pws(purged_data) if ( - windy_api_key := checked(self.config.options.get(WINDY_API_KEY), str) + windy_station_id := checked(self.config.options.get(WINDY_STATION_ID), str) ) is None: _LOGGER.error("Windy API key is not provided! Check your configuration.") return False - request_url = f"{WINDY_URL}{windy_api_key}" + if ( + windy_station_pw := checked(self.config.options.get(WINDY_STATION_PW), str) + ) is None: + _LOGGER.error( + "Windy station password is missing! Check your configuration." + ) + return False + + request_url = f"{WINDY_URL}" + + purged_data["id"] = windy_station_id + + purged_data["time"] = "now" + + headers = {"Authorization": f"Bearer {windy_station_pw}"} if self.log: _LOGGER.info("Dataset for windy: %s", purged_data) session = async_get_clientsession(self.hass, verify_ssl=False) try: - async with session.get(request_url, params=purged_data, headers=headers) as resp: + async with session.get( + request_url, params=purged_data, headers=headers + ) as resp: status = await resp.text() try: self.verify_windy_response(status)