More type annotations.
[python_utils.git] / cached / weather_forecast.py
index 78556586204dff83ac51926795ad9beff682d4cb..b34393832dec04120548aa08fb6822366cfc6ff6 100644 (file)
@@ -4,6 +4,7 @@ from dataclasses import dataclass
 import datetime
 import logging
 import os
+from typing import Any
 import urllib.request
 
 import astral  # type: ignore
@@ -18,6 +19,8 @@ import datetime_utils
 import dateparse.dateparse_utils as dp
 import persistent
 import text_utils
+import smart_home.thermometers as temps
+
 
 logger = logging.getLogger(__name__)
 
@@ -50,7 +53,7 @@ class WeatherForecast:
 
 
 @persistent.persistent_autoloaded_singleton()
-class CachedDetailedWeatherForecast(object):
+class CachedDetailedWeatherForecast(persistent.Persistent):
     def __init__(self, forecasts = None):
         if forecasts is not None:
             self.forecasts = forecasts
@@ -60,23 +63,9 @@ class CachedDetailedWeatherForecast(object):
         self.forecasts = {}
 
         # Ask the raspberry pi about the outside temperature.
-        www = None
-        try:
-            www = urllib.request.urlopen(
-                "http://10.0.0.75/~pi/outside_temp",
-                timeout=2,
-            )
-            current_temp = www.read().decode("utf-8")
-            current_temp = float(current_temp)
-            current_temp *= (9/5)
-            current_temp += 32.0
-            current_temp = round(current_temp)
-        except Exception:
-            logger.warning('Timed out reading 10.0.0.75/~pi/outside_temp?!')
-            current_temp = None
-        finally:
-            if www is not None:
-                www.close()
+        current_temp = temps.ThermometerRegistry().read_temperature(
+            'house_outside', convert_to_fahrenheit=True
+        )
 
         # Get a weather forecast for Bellevue.
         www = urllib.request.urlopen(
@@ -131,7 +120,7 @@ class CachedDetailedWeatherForecast(object):
 
     @classmethod
     @overrides
-    def load(cls):
+    def load(cls) -> Any:
         if persistent.was_file_written_within_n_seconds(
                 config.config['weather_forecast_cachefile'],
                 config.config['weather_forecast_stalest_acceptable'].total_seconds(),
@@ -143,7 +132,7 @@ class CachedDetailedWeatherForecast(object):
         return None
 
     @overrides
-    def save(self):
+    def save(self) -> bool:
         import pickle
         with open(config.config['weather_forecast_cachefile'], 'wb') as wf:
             pickle.dump(
@@ -151,3 +140,4 @@ class CachedDetailedWeatherForecast(object):
                 wf,
                 pickle.HIGHEST_PROTOCOL,
             )
+        return True