Homekit: Bugfix Thermostat Fahrenheit support (#13477)
* Bugfix thermostat temperature conversion * util -> temperature_to_homekit * util -> temperature_to_states * util -> convert_to_float * Added tests, deleted log msg
This commit is contained in:
1 parent
d897a07d0b
commit
020669fc60
8 files changed
+126
-65
No files matched your search
@@ -16,6 +16,20 @@ _LOGGER = logging.getLogger(__name__)
|
||||
CONFIG = {}
|
||||
|
||||
|
||||
def test_get_accessory_invalid_aid(caplog):
|
||||
"""Test with unsupported component."""
|
||||
assert get_accessory(None, State('light.demo', 'on'),
|
||||
aid=None, config=None) is None
|
||||
assert caplog.records[0].levelname == 'WARNING'
|
||||
assert 'invalid aid' in caplog.records[0].msg
|
||||
|
||||
|
||||
def test_not_supported():
|
||||
"""Test if none is returned if entity isn't supported."""
|
||||
assert get_accessory(None, State('demo.demo', 'on'), aid=2, config=None) \
|
||||
is None
|
||||
|
||||
|
||||
class TestGetAccessories(unittest.TestCase):
|
||||
"""Methods to test the get_accessory method."""
|
||||
|
||||
|
||||
@@ -3,32 +3,13 @@ import unittest
|
||||
|
||||
from homeassistant.components.homekit.const import PROP_CELSIUS
|
||||
from homeassistant.components.homekit.type_sensors import (
|
||||
TemperatureSensor, HumiditySensor, calc_temperature, calc_humidity)
|
||||
TemperatureSensor, HumiditySensor)
|
||||
from homeassistant.const import (
|
||||
ATTR_UNIT_OF_MEASUREMENT, STATE_UNKNOWN, TEMP_CELSIUS, TEMP_FAHRENHEIT)
|
||||
|
||||
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
|
||||
assert calc_temperature('75.2', TEMP_FAHRENHEIT) == 24
|
||||
|
||||
|
||||
def test_calc_humidity():
|
||||
"""Test if humidity is a integer."""
|
||||
assert calc_humidity(STATE_UNKNOWN) is None
|
||||
assert calc_humidity('test') is None
|
||||
|
||||
assert calc_humidity('20') == 20
|
||||
assert calc_humidity('75.2') == 75.2
|
||||
|
||||
|
||||
class TestHomekitSensors(unittest.TestCase):
|
||||
"""Test class for all accessory types regarding sensors."""
|
||||
|
||||
|
||||
@@ -10,7 +10,7 @@ from homeassistant.components.homekit.type_thermostats import (
|
||||
Thermostat, STATE_OFF)
|
||||
from homeassistant.const import (
|
||||
ATTR_SERVICE, EVENT_CALL_SERVICE, ATTR_SERVICE_DATA,
|
||||
ATTR_UNIT_OF_MEASUREMENT, TEMP_CELSIUS)
|
||||
ATTR_UNIT_OF_MEASUREMENT, TEMP_CELSIUS, TEMP_FAHRENHEIT)
|
||||
|
||||
from tests.common import get_test_home_assistant
|
||||
|
||||
@@ -238,3 +238,42 @@ class TestHomekitThermostats(unittest.TestCase):
|
||||
self.events[1].data[ATTR_SERVICE_DATA][ATTR_TARGET_TEMP_HIGH],
|
||||
25.0)
|
||||
self.assertEqual(acc.char_cooling_thresh_temp.value, 25.0)
|
||||
|
||||
def test_thermostat_fahrenheit(self):
|
||||
"""Test if accessory and HA are updated accordingly."""
|
||||
climate = 'climate.test'
|
||||
|
||||
acc = Thermostat(self.hass, climate, 'Climate', True)
|
||||
acc.run()
|
||||
|
||||
self.hass.states.set(climate, STATE_AUTO,
|
||||
{ATTR_OPERATION_MODE: STATE_AUTO,
|
||||
ATTR_TARGET_TEMP_HIGH: 75.2,
|
||||
ATTR_TARGET_TEMP_LOW: 68,
|
||||
ATTR_TEMPERATURE: 71.6,
|
||||
ATTR_CURRENT_TEMPERATURE: 73.4,
|
||||
ATTR_UNIT_OF_MEASUREMENT: TEMP_FAHRENHEIT})
|
||||
self.hass.block_till_done()
|
||||
self.assertEqual(acc.char_heating_thresh_temp.value, 20.0)
|
||||
self.assertEqual(acc.char_cooling_thresh_temp.value, 24.0)
|
||||
self.assertEqual(acc.char_current_temp.value, 23.0)
|
||||
self.assertEqual(acc.char_target_temp.value, 22.0)
|
||||
self.assertEqual(acc.char_display_units.value, 1)
|
||||
|
||||
# Set from HomeKit
|
||||
acc.char_cooling_thresh_temp.set_value(23)
|
||||
self.hass.block_till_done()
|
||||
service_data = self.events[-1].data[ATTR_SERVICE_DATA]
|
||||
self.assertEqual(service_data[ATTR_TARGET_TEMP_HIGH], 73.4)
|
||||
self.assertEqual(service_data[ATTR_TARGET_TEMP_LOW], 68)
|
||||
|
||||
acc.char_heating_thresh_temp.set_value(22)
|
||||
self.hass.block_till_done()
|
||||
service_data = self.events[-1].data[ATTR_SERVICE_DATA]
|
||||
self.assertEqual(service_data[ATTR_TARGET_TEMP_HIGH], 73.4)
|
||||
self.assertEqual(service_data[ATTR_TARGET_TEMP_LOW], 71.6)
|
||||
|
||||
acc.char_target_temp.set_value(24.0)
|
||||
self.hass.block_till_done()
|
||||
service_data = self.events[-1].data[ATTR_SERVICE_DATA]
|
||||
self.assertEqual(service_data[ATTR_TEMPERATURE], 75.2)
|
||||
@@ -7,13 +7,15 @@ from homeassistant.core import callback
|
||||
from homeassistant.components.homekit.accessories import HomeBridge
|
||||
from homeassistant.components.homekit.const import HOMEKIT_NOTIFY_ID
|
||||
from homeassistant.components.homekit.util import (
|
||||
show_setup_message, dismiss_setup_message, ATTR_CODE)
|
||||
show_setup_message, dismiss_setup_message, convert_to_float,
|
||||
temperature_to_homekit, temperature_to_states, ATTR_CODE)
|
||||
from homeassistant.components.homekit.util import validate_entity_config \
|
||||
as vec
|
||||
from homeassistant.components.persistent_notification import (
|
||||
SERVICE_CREATE, SERVICE_DISMISS, ATTR_NOTIFICATION_ID)
|
||||
from homeassistant.const import (
|
||||
EVENT_CALL_SERVICE, ATTR_DOMAIN, ATTR_SERVICE, ATTR_SERVICE_DATA)
|
||||
EVENT_CALL_SERVICE, ATTR_DOMAIN, ATTR_SERVICE, ATTR_SERVICE_DATA,
|
||||
TEMP_CELSIUS, TEMP_FAHRENHEIT, STATE_UNKNOWN)
|
||||
|
||||
from tests.common import get_test_home_assistant
|
||||
|
||||
@@ -81,3 +83,20 @@ class TestUtil(unittest.TestCase):
|
||||
self.assertEqual(
|
||||
data[ATTR_SERVICE_DATA].get(ATTR_NOTIFICATION_ID, None),
|
||||
HOMEKIT_NOTIFY_ID)
|
||||
|
||||
def test_convert_to_float(self):
|
||||
"""Test convert_to_float method."""
|
||||
self.assertEqual(convert_to_float(12), 12)
|
||||
self.assertEqual(convert_to_float(12.4), 12.4)
|
||||
self.assertIsNone(convert_to_float(STATE_UNKNOWN))
|
||||
self.assertIsNone(convert_to_float(None))
|
||||
|
||||
def test_temperature_to_homekit(self):
|
||||
"""Test temperature conversion from HA to HomeKit."""
|
||||
self.assertEqual(temperature_to_homekit(20.46, TEMP_CELSIUS), 20.5)
|
||||
self.assertEqual(temperature_to_homekit(92.1, TEMP_FAHRENHEIT), 33.4)
|
||||
|
||||
def test_temperature_to_states(self):
|
||||
"""Test temperature conversion from HomeKit to HA."""
|
||||
self.assertEqual(temperature_to_states(20, TEMP_CELSIUS), 20.0)
|
||||
self.assertEqual(temperature_to_states(20.2, TEMP_FAHRENHEIT), 68.4)
|
||||
Reference in new issue
Block a user