Easier and more self documenting patterns for loading/saving Persistent
[python_utils.git] / smart_home / thermometers.py
1 #!/usr/bin/env python3
2
3 # © Copyright 2021-2022, Scott Gasch
4
5 """Code involving querying various smart home thermometers."""
6
7 import logging
8 import urllib.request
9 from typing import Optional
10
11 logger = logging.getLogger()
12
13
14 class ThermometerRegistry(object):
15     """A registry of thermometer hosts / names and how to talk with them."""
16
17     def __init__(self):
18         self.thermometers = {
19             'house_outside': ('10.0.0.75', 'outside_temp'),
20             'house_inside_downstairs': ('10.0.0.75', 'inside_downstairs_temp'),
21             'house_inside_upstairs': ('10.0.0.75', 'inside_upstairs_temp'),
22             'house_computer_closet': ('10.0.0.75', 'computer_closet_temp'),
23             'house_crawlspace': ('10.0.0.75', 'crawlspace_temp'),
24             'cabin_outside': ('192.168.0.107', 'outside_temp'),
25             'cabin_inside': ('192.168.0.107', 'inside_temp'),
26             'cabin_crawlspace': ('192.168.0.107', 'crawlspace_temp'),
27             'cabin_hottub': ('192.168.0.107', 'hottub_temp'),
28         }
29
30     def read_temperature(self, location: str, *, convert_to_fahrenheit=False) -> Optional[float]:
31         """Read the current value of a thermometer (in celsius unless
32         convert_to_fahrenheit is True) and return it.  Return None on
33         error.
34
35         >>> registry = ThermometerRegistry()
36         >>> registry.read_temperature('unknown') is None
37         True
38
39         >>> temp = registry.read_temperature('house_computer_closet')
40         >>> temp is None
41         False
42         >>> temp > 0.0
43         True
44
45         """
46
47         record = self.thermometers.get(location, None)
48         if record is None:
49             logger.error(
50                 'Location %s is not known.  Valid locations are %s.',
51                 location,
52                 self.thermometers.keys(),
53             )
54             return None
55
56         url = f'http://{record[0]}/~pi/{record[1]}'
57         logger.debug('Constructed URL: %s', url)
58         try:
59             www = urllib.request.urlopen(url, timeout=3)
60             temp = www.read().decode('utf-8')
61             temp = float(temp)
62             if convert_to_fahrenheit:
63                 temp *= 9 / 5
64                 temp += 32.0
65                 temp = round(temp)
66         except Exception as e:
67             logger.exception(e)
68             logger.error('Failed to read temperature at URL: %s', url)
69             temp = None
70         finally:
71             if www is not None:
72                 www.close()
73         return temp
74
75
76 if __name__ == '__main__':
77     import doctest
78
79     doctest.testmod()