From 15af18ba19916f8bf5adc620d20c1ad9891354da Mon Sep 17 00:00:00 2001 From: Anand Radhakrishnan Date: Sun, 17 Jan 2021 12:29:39 +0200 Subject: [PATCH 1/2] updated publish frequency for API call --- __init__.py | 22 +++++----------------- const.py | 1 - sensor.py | 18 +++++++++++++++--- weather.py | 17 ++++++----------- 4 files changed, 26 insertions(+), 32 deletions(-) diff --git a/__init__.py b/__init__.py index 96daa83..864e5f1 100755 --- a/__init__.py +++ b/__init__.py @@ -156,7 +156,7 @@ def __init__(self, hass, session, config_entry): # Lightning strikes self.lightning_data = None - _LOGGER.debug("Data will be updated every %s min", MIN_TIME_BETWEEN_UPDATES) + _LOGGER.debug("FMI: Data will be updated every %s min", MIN_TIME_BETWEEN_UPDATES) super().__init__( hass, _LOGGER, name=DOMAIN, update_interval=MIN_TIME_BETWEEN_UPDATES @@ -247,9 +247,6 @@ def update_lightning_strikes(): for loc_indx, val in enumerate(val_list): if val != "": val_split = val.split(" ") - #val_split[0] --> latitude - #val_split[1] --> longitude - #val_split[2] --> epoch time lightning_coords = (float(val_split[0]), float(val_split[1])) distance = 0 try: @@ -265,9 +262,6 @@ def update_lightning_strikes(): for indx, val in enumerate(val_list): if val != "": val_split = val.split(" ") - #val_split[1] --> cloud_cover - #val_split[2] --> peak_current - #val_split[3] --> ellipse_major exist_tuple = loc_time_list[indx] if indx == exist_tuple[4]: add_tuple = (exist_tuple[0], exist_tuple[1], exist_tuple[2], exist_tuple[3], val_split[0], val_split[1], val_split[2], val_split[3]) @@ -305,27 +299,21 @@ def update_lightning_strikes(): try: async with timeout(10): - self.current = await self._hass.async_add_executor_job( - fmi.weather_by_coordinates, self.latitude, self.longitude - ) - self.forecast = await self._hass.async_add_executor_job( - fmi.forecast_by_coordinates, - self.latitude, - self.longitude, - self.time_step, - ) + self.current = await fmi.async_weather_by_coordinates(self.latitude, self.longitude) + self.forecast = await fmi.async_forecast_by_coordinates(self.latitude, self.longitude, self.time_step) # Update best time parameters await self._hass.async_add_executor_job( update_best_weather_condition ) + _LOGGER.debug("FMI: Best Conditions updated!") # Update lightning strikes await self._hass.async_add_executor_job( update_lightning_strikes ) + _LOGGER.debug("FMI: Lightning Conditions updated!") - except (ClientError, ServerError) as error: raise UpdateFailed(error) from error return {} diff --git a/const.py b/const.py index 622378e..f7c8043 100644 --- a/const.py +++ b/const.py @@ -10,7 +10,6 @@ COORDINATOR = "coordinator" MIN_TIME_BETWEEN_UPDATES = timedelta(minutes=30) -MIN_TIME_BETWEEN_LIGHTNING_UPDATES = timedelta(minutes=60) UNDO_UPDATE_LISTENER = "undo_update_listener" CONF_MIN_HUMIDITY = "min_relative_humidity" diff --git a/sensor.py b/sensor.py index 7fff707..553a355 100755 --- a/sensor.py +++ b/sensor.py @@ -31,7 +31,6 @@ ATTR_HUMIDITY, ATTR_WIND_SPEED, ATTR_PRECIPITATION, - MIN_TIME_BETWEEN_LIGHTNING_UPDATES, BASE_URL, LIGHTNING_LIMIT, ATTR_DISTANCE, @@ -113,6 +112,8 @@ def name(self): @property def state(self): """Return the state of the sensor.""" + + self.update() return self._state @property @@ -147,9 +148,14 @@ def update(self): """Get the latest data from FMI and updates the states.""" if self._fmi is None: + _LOGGER.debug("FMI: Coordinator is not available") return + # Refresh data from API call. + self._fmi.async_request_refresh() + if self._fmi.current is None: + _LOGGER.debug("FMI: Sensor _FMI Current Forecast is unavailable") return if self.type == "place": @@ -167,6 +173,7 @@ def update(self): # Forecasted weather based on configured time_step - next forecasted hour, if available if self._fmi.forecast is None: + _LOGGER.debug("FMI: Sensor _FMI Hourly Forecast is unavailable") return # If current time is half past or more then use the hour next to next hour @@ -178,8 +185,8 @@ def update(self): else: source_data = self._fmi.forecast.forecasts[0] - if source_data is None: + _LOGGER.debug("FMI: Sensor Source data is unavailable") return if self.type == "forecast_time": @@ -222,6 +229,7 @@ def __init__(self, name, coordinator, sensor_type): self.type = sensor_type self._unit_of_measurement = SENSOR_LIGHTNING_TYPES[sensor_type][1] self.lightning_data = coordinator.lightning_data + self._fmi = coordinator try: self._fmi_name = coordinator.current.place @@ -243,6 +251,8 @@ def name(self): @property def state(self): """Return the state of the sensor.""" + + self.update() return self._state @property @@ -290,10 +300,12 @@ def device_state_attributes(self): def update(self): """Get the latest data from FMI and updates the states.""" - + + self._fmi.async_request_refresh() try: self._state = self.lightning_data[0].location except: + _LOGGER.debug("FMI: Sensor Lightning is unavailable") self._state = "Unavailable" return \ No newline at end of file diff --git a/weather.py b/weather.py index a888576..7a8b88b 100755 --- a/weather.py +++ b/weather.py @@ -150,13 +150,15 @@ def condition(self): def forecast(self): """Return the forecast array.""" if self._fmi is None: + _LOGGER.debug("FMI: Coordinator is not available!") return None + # Get latest weather data from API call + self._fmi.async_request_refresh() + if self._fmi.forecast is None: return None - data = None - data = [ { ATTR_FORECAST_TIME: forecast.time.astimezone(tz.tzlocal()), @@ -171,13 +173,6 @@ def forecast(self): for forecast in self._fmi.forecast.forecasts ] - _LOGGER.debug("FMI- Forecast data: %s", data) - - return data - - def update(self): - """Get the latest data from FMI.""" - if self._fmi is None: - return None + _LOGGER.debug("FMI: Forecast data: %s", data) - self._fmi.update() + return data \ No newline at end of file From a6e3058e29f2f54e506d7a6d305c7aa201be568d Mon Sep 17 00:00:00 2001 From: Anand Radhakrishnan Date: Sun, 17 Jan 2021 12:31:51 +0200 Subject: [PATCH 2/2] update frequency note --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index a3ed49a..d1befb8 100755 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # fmi-hass-custom -`fmi-hass-custom` is a Home Assistant custom component for weather and sensor platform. It uses [FMI's Open-Data](https://en.ilmatieteenlaitos.fi/open-data) as a source for current and forecasted meteorological data for a given location. +`fmi-hass-custom` is a Home Assistant custom component for weather and sensor platform. It uses [FMI's Open-Data](https://en.ilmatieteenlaitos.fi/open-data) as a source for current and forecasted meteorological data for a given location. Data update frequency is hard-fixed at 30mins. ## Pre-Installation