Skip to content

Commit

Permalink
Added option to select number of hours in the hourly forecast
Browse files Browse the repository at this point in the history
  • Loading branch information
briis committed Jan 27, 2024
1 parent c463d97 commit 092421c
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 54 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -162,3 +162,4 @@ stations.json
stations_wrong.json
test.py
data/
.DS_Store
103 changes: 52 additions & 51 deletions async_test_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,66 +33,67 @@ async def main() -> None:
elevation = 60

session = aiohttp.ClientSession()
weatherflow = WeatherFlow(station_id=station_id, api_token=api_token, elevation=elevation, session=session)
weatherflow = WeatherFlow(station_id=station_id, api_token=api_token, elevation=elevation, session=session, forecast_hours=24)

try:
station_data: WeatherFlowStationData = await weatherflow.async_get_station()
print("###########################################")
print("STATION NAME: ", station_data.station_name)
print("DEVICE ID: ", station_data.device_id)
print("FIRMWARE: ", station_data.firmware_revision)
print("SERIAL: ", station_data.serial_number)

except Exception as err:
print(err)

try:
sensor_data: WeatherFlowSensorData = await weatherflow.async_fetch_sensor_data()
print("###########################################")
print("DATA AVAILABLE:", sensor_data.data_available)
print("TEMPERATURE:", sensor_data.air_temperature)
print("APPARENT:", sensor_data.feels_like)
print("WIND GUST:", sensor_data.wind_gust)
print("LAST LIGHTNING:", sensor_data.lightning_strike_last_epoch)
print("WIND DIRECTION: ", sensor_data.wind_direction)
print("WIND CARDINAL: ", sensor_data.wind_cardinal)
print("PRECIP CHECKED: ", sensor_data.precip_accum_local_day_final)
print("ABSOLUTE HUMIDITY: ", sensor_data.absolute_humidity)
print("VISIBILITY: ", sensor_data.visibility)
print("BEAUFORT: ", sensor_data.beaufort)
print("BEAUFORT: ", sensor_data.beaufort_description)
print("FREEZING ALT: ", sensor_data.freezing_altitude)
print("VOLTAGE: ", sensor_data.voltage)
print("BATTERY: ", sensor_data.battery)
print("POWER SAVE MODE: ", sensor_data.power_save_mode)
print("IS FREEZING: ", sensor_data.is_freezing)
print("IS LIGHTNING: ", sensor_data.is_lightning)
print("IS RAINING: ", sensor_data.is_raining)
print("UV INDEX: ", sensor_data.uv)
print("UV DESCRIPTION: ", sensor_data.uv_description)
print("STATION NAME: ", sensor_data.station_name)
print("PRECIP INTENSITY: ", sensor_data.precip_intensity)
print("PRECIP: ", sensor_data.precip)
print("PRECIP TYPE: ", sensor_data.precip_type)

except Exception as err:
print(err)
# try:
# station_data: WeatherFlowStationData = await weatherflow.async_get_station()
# print("###########################################")
# print("STATION NAME: ", station_data.station_name)
# print("DEVICE ID: ", station_data.device_id)
# print("FIRMWARE: ", station_data.firmware_revision)
# print("SERIAL: ", station_data.serial_number)

# except Exception as err:
# print(err)

# try:
# data: WeatherFlowForecastData = await weatherflow.async_get_forecast()
# print("TEMPERATURE: ", data.temperature)
# print("***** DAILY DATA *****")
# for item in data.forecast_daily:
# print(item.temperature, item.temp_low, item.icon, item.condition, item.precipitation_probability, item.precipitation, item.wind_bearing, item.wind_speed, item.wind_gust)
# print("***** HOURLY DATA *****")
# for item in data.forecast_hourly:
# print(item.datetime, item.temperature, item.apparent_temperature, item.icon, item.condition, item.precipitation, item.precipitation_probability)
# sensor_data: WeatherFlowSensorData = await weatherflow.async_fetch_sensor_data()
# print("###########################################")
# print("DATA AVAILABLE:", sensor_data.data_available)
# print("TEMPERATURE:", sensor_data.air_temperature)
# print("APPARENT:", sensor_data.feels_like)
# print("WIND GUST:", sensor_data.wind_gust)
# print("LAST LIGHTNING:", sensor_data.lightning_strike_last_epoch)
# print("WIND DIRECTION: ", sensor_data.wind_direction)
# print("WIND CARDINAL: ", sensor_data.wind_cardinal)
# print("PRECIP CHECKED: ", sensor_data.precip_accum_local_day_final)
# print("ABSOLUTE HUMIDITY: ", sensor_data.absolute_humidity)
# print("VISIBILITY: ", sensor_data.visibility)
# print("BEAUFORT: ", sensor_data.beaufort)
# print("BEAUFORT: ", sensor_data.beaufort_description)
# print("FREEZING ALT: ", sensor_data.freezing_altitude)
# print("VOLTAGE: ", sensor_data.voltage)
# print("BATTERY: ", sensor_data.battery)
# print("POWER SAVE MODE: ", sensor_data.power_save_mode)
# print("IS FREEZING: ", sensor_data.is_freezing)
# print("IS LIGHTNING: ", sensor_data.is_lightning)
# print("IS RAINING: ", sensor_data.is_raining)
# print("UV INDEX: ", sensor_data.uv)
# print("UV DESCRIPTION: ", sensor_data.uv_description)
# print("STATION NAME: ", sensor_data.station_name)
# print("PRECIP INTENSITY: ", sensor_data.precip_intensity)
# print("PRECIP: ", sensor_data.precip)
# print("PRECIP TYPE: ", sensor_data.precip_type)

# except Exception as err:
# print(err)


try:
data: WeatherFlowForecastData = await weatherflow.async_get_forecast()
print("TEMPERATURE: ", data.temperature)
print("***** DAILY DATA *****")
for item in data.forecast_daily:
print(item.temperature, item.temp_low, item.icon, item.condition, item.precipitation_probability, item.precipitation, item.wind_bearing, item.wind_speed, item.wind_gust)
print("***** HOURLY DATA *****")
cnt = 1
for item in data.forecast_hourly:
print("**", cnt, "** ", item.datetime, item.temperature, item.apparent_temperature, item.icon, item.condition, item.precipitation, item.precipitation_probability)
cnt += 1
except Exception as err:
print(err)


if session is not None:
await session.close()

Expand Down
13 changes: 10 additions & 3 deletions pyweatherflow_forecast/wffcst_lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,13 +127,15 @@ def __init__(
self,
station_id: int,
api_token: str,
forecast_hours: int = 48,
session: aiohttp.ClientSession = None,
elevation = None,
api: WeatherFlowAPIBase = WeatherFlowAPI(),
) -> None:
"""Return data from WeatherFlow API."""
self._station_id = station_id
self._api_token = api_token
self._forecast_hours = forecast_hours
self._elevation = elevation
self._api = api
self._device_id = None
Expand All @@ -152,7 +154,7 @@ def get_forecast(self) -> list[WeatherFlowForecastData]:
api_url = f"{WEATHERFLOW_FORECAST_URL}{self._station_id}&token={self._api_token}"
self._json_data = self._api.api_request(api_url)

return _get_forecast(self._json_data)
return _get_forecast(self._json_data, self._forecast_hours)

def get_station(self) -> list[WeatherFlowStationData]:
"""Return list of station information."""
Expand Down Expand Up @@ -220,7 +222,7 @@ async def async_get_forecast(self) -> list[WeatherFlowForecastData]:
api_url = f"{WEATHERFLOW_FORECAST_URL}{self._station_id}&token={self._api_token}"
self._json_data = await self._api.async_api_request(api_url)

return _get_forecast(self._json_data)
return _get_forecast(self._json_data, self._forecast_hours)

async def async_get_station(self) -> list[WeatherFlowStationData]:
"""Return list with Station information."""
Expand Down Expand Up @@ -309,7 +311,7 @@ def _calced_day_values(day_number, hourly_data) -> dict[str, Any]:


# pylint: disable=R0914, R0912, W0212, R0915
def _get_forecast(api_result: dict) -> list[WeatherFlowForecastData]:
def _get_forecast(api_result: dict, forecast_hours: int) -> list[WeatherFlowForecastData]:
"""Return WeatherFlowForecast list from API."""

# Get Current Conditions
Expand Down Expand Up @@ -351,7 +353,11 @@ def _get_forecast(api_result: dict) -> list[WeatherFlowForecastData]:
current_conditions.forecast_daily = forecasts_daily

# Add Hourly Forecast
_hour_counter = 1
for item in api_result["forecast"]["hourly"]:
if _hour_counter > forecast_hours:
break

timestamp = item["time"]
valid_time = datetime.datetime.fromtimestamp(timestamp)
condition = item.get("conditions", None)
Expand Down Expand Up @@ -384,6 +390,7 @@ def _get_forecast(api_result: dict) -> list[WeatherFlowForecastData]:
uv_index,
)
forecasts_hourly.append(forecast)
_hour_counter += 1

current_conditions.forecast_hourly = forecasts_hourly

Expand Down

0 comments on commit 092421c

Please sign in to comment.