Fix homekit: temperature calculation (#12720)

This commit is contained in:
cdce8p 2018-02-26 22:29:52 +01:00 committed by Pascal Vizeli
parent 10570f5ad6
commit 6a665ffb84
2 changed files with 7 additions and 8 deletions

View file

@ -2,7 +2,7 @@
import logging
from homeassistant.const import (
STATE_UNKNOWN, ATTR_UNIT_OF_MEASUREMENT, TEMP_FAHRENHEIT, TEMP_CELSIUS)
ATTR_UNIT_OF_MEASUREMENT, TEMP_FAHRENHEIT, TEMP_CELSIUS)
from homeassistant.helpers.event import async_track_state_change
from . import TYPES
@ -21,21 +21,19 @@ def calc_temperature(state, unit=TEMP_CELSIUS):
Always return temperature as Celsius value.
Conversion is handled on the device.
"""
if state == STATE_UNKNOWN:
try:
value = float(state)
except ValueError:
return None
if unit == TEMP_FAHRENHEIT:
value = round((float(state) - 32) / 1.8, 2)
else:
value = float(state)
return value
return round((value - 32) / 1.8, 2) if unit == TEMP_FAHRENHEIT else value
@TYPES.register('TemperatureSensor')
class TemperatureSensor(HomeAccessory):
"""Generate a TemperatureSensor accessory for a temperature sensor.
Sensor entity must return temperature in °C, °F or STATE_UNKNOWN.
Sensor entity must return temperature in °C, °F.
"""
def __init__(self, hass, entity_id, display_name):

View file

@ -12,6 +12,7 @@ from tests.common import get_test_home_assistant
def test_calc_temperature():
"""Test if temperature in Celsius is calculated correctly."""
assert calc_temperature(STATE_UNKNOWN) is None
assert calc_temperature('test') is None
assert calc_temperature('20') == 20
assert calc_temperature('20.12', TEMP_CELSIUS) == 20.12