X-Git-Url: https://wannabe.guru.org/gitweb/?a=blobdiff_plain;f=cached%2Fweather_forecast.py;h=b34393832dec04120548aa08fb6822366cfc6ff6;hb=e2b1ec0d293fd3d17854194189ed5ee1c28f705f;hp=a413d9f424b3c76d0839a44695310516dee2cdf4;hpb=351e77c767c9084aa486eedbdc9902c635b06261;p=python_utils.git diff --git a/cached/weather_forecast.py b/cached/weather_forecast.py index a413d9f..b343938 100644 --- a/cached/weather_forecast.py +++ b/cached/weather_forecast.py @@ -4,11 +4,13 @@ from dataclasses import dataclass import datetime import logging import os +from typing import Any import urllib.request import astral # type: ignore from astral.sun import sun # type: ignore from bs4 import BeautifulSoup # type: ignore +from overrides import overrides import pytz import argparse_utils @@ -17,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__) @@ -27,7 +31,7 @@ cfg = config.add_commandline_args( cfg.add_argument( '--weather_forecast_cachefile', type=str, - default=f'{os.environ["HOME"]}/.weather_forecast_cache', + default=f'{os.environ["HOME"]}/cache/.weather_forecast_cache', metavar='FILENAME', help='File in which to cache weather data' ) @@ -49,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 @@ -59,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( @@ -129,7 +119,8 @@ class CachedDetailedWeatherForecast(object): ) @classmethod - def load(cls): + @overrides + 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(), @@ -140,7 +131,8 @@ class CachedDetailedWeatherForecast(object): return cls(weather_data) return None - def save(self): + @overrides + def save(self) -> bool: import pickle with open(config.config['weather_forecast_cachefile'], 'wb') as wf: pickle.dump( @@ -148,3 +140,4 @@ class CachedDetailedWeatherForecast(object): wf, pickle.HIGHEST_PROTOCOL, ) + return True