Skip to content

Commit

Permalink
Add some weather logging
Browse files Browse the repository at this point in the history
  • Loading branch information
theyosh committed Nov 25, 2024
1 parent 70e13c0 commit 2f5e58b
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 14 deletions.
8 changes: 1 addition & 7 deletions terrariumEngine.py
Original file line number Diff line number Diff line change
Expand Up @@ -384,20 +384,14 @@ def load_settings(self):
try:
start = time.time()
self.weather = terrariumWeather(self.settings["weather_source"], self.units, self.active_language)
weatherLogger.info(
f'Loaded weather data from source {self.settings["weather_source"]} in {time.time()-start:.2f} seconds'
)
except Exception as ex:
weatherLogger.error(f"Loading exception: {ex}")
weatherLogger.error(f"Failed loading weather data: {ex}")

elif self.weather is not None:
start = time.time()
self.weather.address = self.settings["weather_source"]
# Force an update, due to maybe changing speed units or temperature.... lazy fix... :(
self.weather.update()
weatherLogger.info(
f'Updated weather source data to {self.settings["weather_source"]} in {time.time()-start:.2f} seconds'
)

# Loading Meross cloud
if "" != settings["meross_cloud_username"] and "" != settings["meross_cloud_password"] and meross_login:
Expand Down
18 changes: 11 additions & 7 deletions weather/openweathermap_org_weather.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ def __init__(self, address, unit_values, language):
def __load_general_data(self):
address = self.address + "&units=metric&lang=" + self._device["language"][0:2]
logger.debug("Loading weather source {}".format(address))
start = time()
data = terrariumUtils.get_remote_data(address)
if data:
self._data["city"] = data["name"]
Expand All @@ -36,19 +37,21 @@ def __load_general_data(self):

address = terrariumUtils.parse_url(self.address.lower())
self.__appid = address["query_params"]["appid"]
logger.info(f"Loaded basic weather data from source {address} in {time()-start:.2f} seconds.")

return True

return False

def __load_minimal_forecast_data(self):
data = terrariumUtils.get_remote_data(
"https://api.openweathermap.org/data/2.5/forecast?lat={}&lon={}&appid={}&units=metric&lang={}".format(
self._data["geo"]["lat"], self._data["geo"]["long"], self.__appid, self._device["language"][0:2]
)
start = time()
url = "https://api.openweathermap.org/data/2.5/forecast?lat={}&lon={}&appid={}&units=metric&lang={}".format(
self._data["geo"]["lat"], self._data["geo"]["long"], self.__appid, self._device["language"][0:2]
)
data = terrariumUtils.get_remote_data(url)

if not data:
logger.error("Failed loading minimal weather forecast data. Make sure your source address is correct!")
return False

# Reset data
Expand Down Expand Up @@ -87,12 +90,13 @@ def __load_minimal_forecast_data(self):
}
)

logger.info("Using Openweathermap Free API")
logger.info(f"Loaded minimal forecast data from source {url} in {time()-start:.2f} seconds.")
self.__one_call_version = "free"
return True

def __load_forecast_data_one_call(self):
data = None
start = time()
if self.__one_call_version is None or self.__one_call_version == "3.0":
data = terrariumUtils.get_remote_data(
"https://api.openweathermap.org/data/3.0/onecall?lat={}&lon={}&units=metric&exclude=minutely&appid={}&lang={}".format(
Expand All @@ -108,13 +112,12 @@ def __load_forecast_data_one_call(self):
)
if data:
self.__one_call_version = "2.5"
logger.info(f"Using Openweathermap One Call API {self.__one_call_version}")

elif self.__one_call_version is None:
self.__one_call_version = "3.0"
logger.info(f"Using Openweathermap One Call API {self.__one_call_version}")

if not data:
logger.error("Failed loaded forecast data!")
return False

# Reset data
Expand Down Expand Up @@ -175,6 +178,7 @@ def __load_forecast_data_one_call(self):
}
)

logger.info(f"Loaded weather forecast with API {self.__one_call_version} in {time()-start} seconds")
return True

def __load_forecast_data(self):
Expand Down

0 comments on commit 2f5e58b

Please sign in to comment.