5 from typing import Optional
7 logger = logging.getLogger()
10 class ThermometerRegistry(object):
13 'house_outside': ('10.0.0.75', 'outside_temp'),
14 'house_inside_downstairs': ('10.0.0.75', 'inside_downstairs_temp'),
15 'house_inside_upstairs': ('10.0.0.75', 'inside_upstairs_temp'),
16 'house_computer_closet': ('10.0.0.75', 'computer_closet_temp'),
17 'house_crawlspace': ('10.0.0.75', 'crawlspace_temp'),
18 'cabin_outside': ('192.168.0.107', 'outside_temp'),
19 'cabin_inside': ('192.168.0.107', 'inside_temp'),
20 'cabin_crawlspace': ('192.168.0.107', 'crawlspace_temp'),
21 'cabin_hottub': ('192.168.0.107', 'hottub_temp'),
24 def read_temperature(self, location: str, *, convert_to_fahrenheit=False) -> Optional[float]:
25 record = self.thermometers.get(location, None)
28 f'Location {location} is not known. Valid locations are {self.thermometers.keys()}.'
31 url = f'http://{record[0]}/~pi/{record[1]}'
32 logger.debug(f'Constructed URL: {url}')
34 www = urllib.request.urlopen(url, timeout=3)
35 temp = www.read().decode('utf-8')
37 if convert_to_fahrenheit:
41 except Exception as e:
43 logger.error(f'Failed to read temperature at URL: {url}')