From 55ade0015242ba9e23e8697daf50fddad99e7ddc Mon Sep 17 00:00:00 2001 From: Alexander Rey Date: Thu, 15 Aug 2024 14:20:14 -0400 Subject: [PATCH] Minor fixes - Corrects a small bug about platforms re: Issue #273 - Changes sensor reported times to datetimes re: Issue #275 - Allows for updating locations via hass re: Issue #241 - Allows changing the scan_interval re: Issue #230 --- custom_components/pirateweather/__init__.py | 16 +++++++++++--- .../pirateweather/config_flow.py | 9 ++++++++ custom_components/pirateweather/manifest.json | 2 +- custom_components/pirateweather/sensor.py | 14 +++++++++++-- .../pirateweather/translations/en.json | 9 ++++---- .../weather_update_coordinator.py | 21 ++++++++++++++++--- 6 files changed, 58 insertions(+), 13 deletions(-) diff --git a/custom_components/pirateweather/__init__.py b/custom_components/pirateweather/__init__.py index cdbf107..b58ed3a 100644 --- a/custom_components/pirateweather/__init__.py +++ b/custom_components/pirateweather/__init__.py @@ -53,7 +53,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: forecast_hours = _get_config_value(entry, CONF_HOURLY_FORECAST) pw_entity_platform = _get_config_value(entry, PW_PLATFORM) pw_entity_rounding = _get_config_value(entry, PW_ROUND) - pw_scan_Int = entry.data[CONF_SCAN_INTERVAL] + pw_scan_Int = _get_config_value(entry, CONF_SCAN_INTERVAL) # Extract list of int from forecast days/ hours string if present # _LOGGER.warning('forecast_days_type: ' + str(type(forecast_days))) @@ -109,8 +109,18 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: CONF_SCAN_INTERVAL: pw_scan_Int, } - # Setup platforms - await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS) + # Setup platforms + # If both platforms + if (PW_PLATFORMS[0] in pw_entity_platform) and ( + PW_PLATFORMS[1] in pw_entity_platform + ): + await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS) + # If only sensor + elif PW_PLATFORMS[0] in pw_entity_platform: + await hass.config_entries.async_forward_entry_setups(entry, [PLATFORMS[0]]) + # If only weather + elif PW_PLATFORMS[1] in pw_entity_platform: + await hass.config_entries.async_forward_entry_setups(entry, [PLATFORMS[1]]) update_listener = entry.add_update_listener(async_update_options) hass.data[DOMAIN][entry.entry_id][UPDATE_LISTENER] = update_listener diff --git a/custom_components/pirateweather/config_flow.py b/custom_components/pirateweather/config_flow.py index 9eeb51e..5db9314 100644 --- a/custom_components/pirateweather/config_flow.py +++ b/custom_components/pirateweather/config_flow.py @@ -226,6 +226,15 @@ async def async_step_init(self, user_input=None): ), ), ): cv.longitude, + vol.Optional( + CONF_SCAN_INTERVAL, + default=self.config_entry.options.get( + CONF_SCAN_INTERVAL, + self.config_entry.data.get( + CONF_SCAN_INTERVAL, DEFAULT_SCAN_INTERVAL + ), + ), + ): int, vol.Required( PW_PLATFORM, default=self.config_entry.options.get( diff --git a/custom_components/pirateweather/manifest.json b/custom_components/pirateweather/manifest.json index fde62ae..95cf1b5 100644 --- a/custom_components/pirateweather/manifest.json +++ b/custom_components/pirateweather/manifest.json @@ -12,5 +12,5 @@ "requirements": [ "python-forecastio==1.4.0" ], - "version": "1.5.4" + "version": "1.5.5" } diff --git a/custom_components/pirateweather/sensor.py b/custom_components/pirateweather/sensor.py index 9ea9628..b1f520c 100644 --- a/custom_components/pirateweather/sensor.py +++ b/custom_components/pirateweather/sensor.py @@ -1,5 +1,5 @@ """Support for Pirate Weather (Dark Sky Compatable) weather service.""" - +from datetime import datetim import logging from dataclasses import dataclass, field from typing import Literal, NamedTuple @@ -1161,7 +1161,17 @@ def get_state(self, data): "precip_intensity_max", ]: outState = round(state, roundingPrecip) - + + # Convert unix times to datetimes times + elif self.type in [ + "temperature_high_time", + "temperature_low_time", + "apparent_temperature_high_time", + "apparent_temperature_low_time", + "sunrise_time", + "sunset_time", + ]: + outState = datetime.fromtimestamp(state) else: outState = state diff --git a/custom_components/pirateweather/translations/en.json b/custom_components/pirateweather/translations/en.json index f08c6f8..b1a8e56 100644 --- a/custom_components/pirateweather/translations/en.json +++ b/custom_components/pirateweather/translations/en.json @@ -12,8 +12,8 @@ "data": { "api_key": "API Key", "language": "Language", - "latitude": "Latitude", - "longitude": "Longitude", + "latitude": "Latitude. Set as 0 to use current location", + "longitude": "Longitude. Set as 0 to use current location", "mode": "Forecast mode for the Weather entity", "name": "Integration Name", "units": "Units for sensors. Only used for if sensors are requested.", @@ -38,8 +38,9 @@ "data": { "api_key": "API Key", "language": "Language", - "latitude": "Latitude", - "longitude": "Longitude", + "latitude": "Latitude. Set as 0 to use current location", + "longitude": "Longitude. Set as 0 to use current location", + "scan_interval": "Seconds to wait between updates. Reducing this below 900 seconds (15 minutes) is not recomended." "mode": "Forecast mode for the Weather entity", "name": "Integration Name", "units": "Units for sensors. Only used for if sensors are requested.", diff --git a/custom_components/pirateweather/weather_update_coordinator.py b/custom_components/pirateweather/weather_update_coordinator.py index 20dd3a2..a1cc0a2 100644 --- a/custom_components/pirateweather/weather_update_coordinator.py +++ b/custom_components/pirateweather/weather_update_coordinator.py @@ -43,20 +43,30 @@ async def _async_update_data(self): async with async_timeout.timeout(60): try: data = await self._get_pw_weather() - _LOGGER.debug("Pirate Weather data update") except HTTPException as err: raise UpdateFailed(f"Error communicating with API: {err}") from err return data async def _get_pw_weather(self): """Poll weather data from PW.""" + + if self.latitude == 0.0: + requestLatitude = self.hass.config.latitude + else: + requestLatitude = self.latitude + + if self.longitude == 0.0: + requestLongitude = self.hass.config.latitude + else: + requestLongitude = self.longitude + forecastString = ( "https://api.pirateweather.net/forecast/" + self._api_key + "/" - + str(self.latitude) + + str(requestLatitude) + "," - + str(self.longitude) + + str(requestLongitude) + "?units=" + self.requested_units + "&extend=hourly" @@ -72,4 +82,9 @@ async def _get_pw_weather(self): headers = resp.headers status = resp.raise_for_status() + _LOGGER.debug("Pirate Weather data update for " + + str(requestLatitude) + + "," + + str(requestLongitude)) + return Forecast(jsonText, status, headers)