Skip to content

Commit

Permalink
Merge pull request #10 from anand-p-r/fix/fmi-update-calls
Browse files Browse the repository at this point in the history
Fix/fmi update calls
  • Loading branch information
anand-p-r authored Jan 17, 2021
2 parents 80b7786 + a6e3058 commit da71804
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 33 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -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

Expand Down
22 changes: 5 additions & 17 deletions __init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand All @@ -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])
Expand Down Expand Up @@ -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 {}
Expand Down
1 change: 0 additions & 1 deletion const.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
18 changes: 15 additions & 3 deletions sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
ATTR_HUMIDITY,
ATTR_WIND_SPEED,
ATTR_PRECIPITATION,
MIN_TIME_BETWEEN_LIGHTNING_UPDATES,
BASE_URL,
LIGHTNING_LIMIT,
ATTR_DISTANCE,
Expand Down Expand Up @@ -113,6 +112,8 @@ def name(self):
@property
def state(self):
"""Return the state of the sensor."""

self.update()
return self._state

@property
Expand Down Expand Up @@ -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":
Expand All @@ -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
Expand All @@ -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":
Expand Down Expand Up @@ -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
Expand All @@ -243,6 +251,8 @@ def name(self):
@property
def state(self):
"""Return the state of the sensor."""

self.update()
return self._state

@property
Expand Down Expand Up @@ -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
17 changes: 6 additions & 11 deletions weather.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()),
Expand All @@ -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

0 comments on commit da71804

Please sign in to comment.