X-Git-Url: https://wannabe.guru.org/gitweb/?a=blobdiff_plain;f=smart_home%2Fthermometers.py;h=4531c618fc17b749f172abaa301863646f67a0a0;hb=5979fc153c921d4b4074024f9021f894342739c1;hp=73117bc1a3cccddf0c22be8ab8cb10572b3b41f8;hpb=532df2c5b57c7517dfb3dddd8c1358fbadf8baf3;p=python_utils.git diff --git a/smart_home/thermometers.py b/smart_home/thermometers.py index 73117bc..4531c61 100644 --- a/smart_home/thermometers.py +++ b/smart_home/thermometers.py @@ -28,6 +28,22 @@ class ThermometerRegistry(object): } def read_temperature(self, location: str, *, convert_to_fahrenheit=False) -> Optional[float]: + """Read the current value of a thermometer (in celsius unless + convert_to_fahrenheit is True) and return it. Return None on + error. + + >>> registry = ThermometerRegistry() + >>> registry.read_temperature('unknown') is None + True + + >>> temp = registry.read_temperature('house_computer_closet') + >>> temp is None + False + >>> temp > 0.0 + True + + """ + record = self.thermometers.get(location, None) if record is None: logger.error( @@ -36,6 +52,7 @@ class ThermometerRegistry(object): self.thermometers.keys(), ) return None + url = f'http://{record[0]}/~pi/{record[1]}' logger.debug('Constructed URL: %s', url) try: @@ -54,3 +71,9 @@ class ThermometerRegistry(object): if www is not None: www.close() return temp + + +if __name__ == '__main__': + import doctest + + doctest.testmod()