3 # © Copyright 2021-2022, Scott Gasch
5 """Code involving querying various smart home thermometers."""
9 from typing import Optional
11 logger = logging.getLogger()
14 class ThermometerRegistry(object):
15 """A registry of thermometer hosts / names and how to talk with them."""
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'),
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
35 >>> registry = ThermometerRegistry()
36 >>> registry.read_temperature('unknown') is None
39 >>> temp = registry.read_temperature('house_computer_closet')
47 record = self.thermometers.get(location, None)
50 'Location %s is not known. Valid locations are %s.',
52 self.thermometers.keys(),
56 url = f'http://{record[0]}/~pi/{record[1]}'
57 logger.debug('Constructed URL: %s', url)
59 www = urllib.request.urlopen(url, timeout=3)
60 temp = www.read().decode('utf-8')
62 if convert_to_fahrenheit:
66 except Exception as e:
68 logger.error('Failed to read temperature at URL: %s', url)
76 if __name__ == '__main__':