Skip to content

Commit

Permalink
Sensor flow optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
briis committed Oct 16, 2023
1 parent 9e5a888 commit 4d89d72
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 24 deletions.
40 changes: 22 additions & 18 deletions async_test_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,24 +44,28 @@ async def main() -> None:
except Exception as err:
print(err)

try:
sensor_data: WeatherFlowSensorData = await weatherflow.async_fetch_sensor_data()
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("FREEZING ALT: ", sensor_data.freezing_altitude)
print("VOLTAGE: ", sensor_data.voltage)
print("BATTERY: ", sensor_data.battery)

except Exception as err:
print(err)
cnt = 1
while cnt < 3:
try:
sensor_data: WeatherFlowSensorData = await weatherflow.async_fetch_sensor_data()
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("FREEZING ALT: ", sensor_data.freezing_altitude)
print("VOLTAGE: ", sensor_data.voltage)
print("BATTERY: ", sensor_data.battery)

cnt += 1

except Exception as err:
print(err)


try:
Expand Down
16 changes: 10 additions & 6 deletions pyweatherflow_forecast/wffcst_lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ def api_request(self, url: str) -> dict[str, Any]:
async def async_api_request(self, url: str) -> dict[str, Any]:
"""Get data from WeatherFlow API."""

_LOGGER.debug("URL CALLED: %s", url)

is_new_session = False
if self.session is None:
self.session = aiohttp.ClientSession()
Expand Down Expand Up @@ -167,11 +169,13 @@ def fetch_sensor_data(self, voltage: float = None) -> list[WeatherFlowSensorData
device_data = None
sensor_data = None

station_url = f"{WEATHERFLOW_STATION_URL}{self._station_id}?token={self._api_token}"
json_station_data = self._api.api_request(station_url)
station_data: WeatherFlowStationData = _get_station(json_station_data)
if self._device_id is None:
station_url = f"{WEATHERFLOW_STATION_URL}{self._station_id}?token={self._api_token}"
json_station_data = self._api.api_request(station_url)
station_data: WeatherFlowStationData = _get_station(json_station_data)
self._device_id = station_data.device_id

if station_data is not None:
if self._device_id is not None:
_device_id = station_data.device_id
device_url = f"{WEATHERFLOW_DEVICE_URL}{_device_id}?token={self._api_token}"
json_device_data = self._api.api_request(device_url)
Expand All @@ -194,9 +198,9 @@ async def async_fetch_sensor_data(self) -> list[WeatherFlowSensorData]:
station_url = f"{WEATHERFLOW_STATION_URL}{self._station_id}?token={self._api_token}"
json_station_data = await self._api.async_api_request(station_url)
station_data: WeatherFlowStationData = _get_station(json_station_data)

if station_data is not None:
self._device_id = station_data.device_id

if self._device_id is not None:
device_url = f"{WEATHERFLOW_DEVICE_URL}{self._device_id}?token={self._api_token}"
json_device_data = await self._api.async_api_request(device_url)
device_data: WeatherFlowDeviceData = _get_device_data(json_device_data, self._device_id)
Expand Down

0 comments on commit 4d89d72

Please sign in to comment.