From 4d89d72f13daa9a741ca727c0ce07e31088fadf9 Mon Sep 17 00:00:00 2001 From: Bjarne Riis Date: Mon, 16 Oct 2023 06:51:09 +0200 Subject: [PATCH] Sensor flow optimization --- async_test_module.py | 40 +++++++++++++++------------- pyweatherflow_forecast/wffcst_lib.py | 16 ++++++----- 2 files changed, 32 insertions(+), 24 deletions(-) diff --git a/async_test_module.py b/async_test_module.py index bac86fc..63bbb3b 100644 --- a/async_test_module.py +++ b/async_test_module.py @@ -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: diff --git a/pyweatherflow_forecast/wffcst_lib.py b/pyweatherflow_forecast/wffcst_lib.py index ac13e09..cdb8539 100644 --- a/pyweatherflow_forecast/wffcst_lib.py +++ b/pyweatherflow_forecast/wffcst_lib.py @@ -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() @@ -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) @@ -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)