diff --git a/homeassistant/components/homekit/type_thermostats.py b/homeassistant/components/homekit/type_thermostats.py index 24a137e4957..c34e9066160 100644 --- a/homeassistant/components/homekit/type_thermostats.py +++ b/homeassistant/components/homekit/type_thermostats.py @@ -188,8 +188,14 @@ class Thermostat(HomeAccessory): (CHAR_COOLING_THRESHOLD_TEMPERATURE, CHAR_HEATING_THRESHOLD_TEMPERATURE) ) + if ( + ATTR_CURRENT_HUMIDITY in attributes + or features & ClimateEntityFeature.TARGET_HUMIDITY + ): + self.chars.append(CHAR_CURRENT_HUMIDITY) + if features & ClimateEntityFeature.TARGET_HUMIDITY: - self.chars.extend((CHAR_TARGET_HUMIDITY, CHAR_CURRENT_HUMIDITY)) + self.chars.append(CHAR_TARGET_HUMIDITY) serv_thermostat = self.add_preload_service(SERV_THERMOSTAT, self.chars) self.set_primary_service(serv_thermostat) @@ -253,7 +259,6 @@ class Thermostat(HomeAccessory): properties={PROP_MIN_VALUE: hc_min_temp, PROP_MAX_VALUE: hc_max_temp}, ) self.char_target_humidity = None - self.char_current_humidity = None if CHAR_TARGET_HUMIDITY in self.chars: self.char_target_humidity = serv_thermostat.configure_char( CHAR_TARGET_HUMIDITY, @@ -265,6 +270,8 @@ class Thermostat(HomeAccessory): # of 0-80% properties={PROP_MIN_VALUE: min_humidity}, ) + self.char_current_humidity = None + if CHAR_CURRENT_HUMIDITY in self.chars: self.char_current_humidity = serv_thermostat.configure_char( CHAR_CURRENT_HUMIDITY, value=50 ) diff --git a/tests/components/homekit/test_type_thermostats.py b/tests/components/homekit/test_type_thermostats.py index 1dd191f5303..1a18c7fe805 100644 --- a/tests/components/homekit/test_type_thermostats.py +++ b/tests/components/homekit/test_type_thermostats.py @@ -742,6 +742,29 @@ async def test_thermostat_humidity(hass, hk_driver, events): assert events[-1].data[ATTR_VALUE] == "35%" +async def test_thermostat_humidity_with_target_humidity(hass, hk_driver, events): + """Test if accessory and HA are updated accordingly with humidity without target hudmidity. + + This test is for thermostats that do not support target humidity but + have a current humidity sensor. + """ + entity_id = "climate.test" + + # support_auto = True + hass.states.async_set(entity_id, HVACMode.OFF, {ATTR_CURRENT_HUMIDITY: 40}) + await hass.async_block_till_done() + acc = Thermostat(hass, hk_driver, "Climate", entity_id, 1, None) + hk_driver.add_accessory(acc) + + await acc.run() + await hass.async_block_till_done() + + assert acc.char_current_humidity.value == 40 + hass.states.async_set(entity_id, HVACMode.HEAT_COOL, {ATTR_CURRENT_HUMIDITY: 65}) + await hass.async_block_till_done() + assert acc.char_current_humidity.value == 65 + + async def test_thermostat_power_state(hass, hk_driver, events): """Test if accessory and HA are updated accordingly.""" entity_id = "climate.test"