From 0da710c4e083379e7ab81b1f3fd1ba4219872bde Mon Sep 17 00:00:00 2001 From: Lorenzo Brescanzin <30345822+br3sc4@users.noreply.github.com> Date: Fri, 24 Dec 2021 19:39:37 +0100 Subject: [PATCH] Fix HomeKit sensor update check (#62705) --- homeassistant/components/homekit/type_sensors.py | 12 ++++++------ tests/components/homekit/test_type_sensors.py | 12 ++++++++++++ 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/homeassistant/components/homekit/type_sensors.py b/homeassistant/components/homekit/type_sensors.py index 598d49155ec..881d91044ee 100644 --- a/homeassistant/components/homekit/type_sensors.py +++ b/homeassistant/components/homekit/type_sensors.py @@ -117,7 +117,7 @@ class TemperatureSensor(HomeAccessory): def async_update_state(self, new_state): """Update temperature after state changed.""" unit = new_state.attributes.get(ATTR_UNIT_OF_MEASUREMENT, TEMP_CELSIUS) - if temperature := convert_to_float(new_state.state): + if (temperature := convert_to_float(new_state.state)) is not None: temperature = temperature_to_homekit(temperature, unit) self.char_temp.set_value(temperature) _LOGGER.debug( @@ -144,7 +144,7 @@ class HumiditySensor(HomeAccessory): @callback def async_update_state(self, new_state): """Update accessory after state change.""" - if humidity := convert_to_float(new_state.state): + if (humidity := convert_to_float(new_state.state)) is not None: self.char_humidity.set_value(humidity) _LOGGER.debug("%s: Percent set to %d%%", self.entity_id, humidity) @@ -171,7 +171,7 @@ class AirQualitySensor(HomeAccessory): @callback def async_update_state(self, new_state): """Update accessory after state change.""" - if density := convert_to_float(new_state.state): + if (density := convert_to_float(new_state.state)) is not None: if self.char_density.value != density: self.char_density.set_value(density) _LOGGER.debug("%s: Set density to %d", self.entity_id, density) @@ -206,7 +206,7 @@ class CarbonMonoxideSensor(HomeAccessory): @callback def async_update_state(self, new_state): """Update accessory after state change.""" - if value := convert_to_float(new_state.state): + if (value := convert_to_float(new_state.state)) is not None: self.char_level.set_value(value) if value > self.char_peak.value: self.char_peak.set_value(value) @@ -241,7 +241,7 @@ class CarbonDioxideSensor(HomeAccessory): @callback def async_update_state(self, new_state): """Update accessory after state change.""" - if value := convert_to_float(new_state.state): + if (value := convert_to_float(new_state.state)) is not None: self.char_level.set_value(value) if value > self.char_peak.value: self.char_peak.set_value(value) @@ -269,7 +269,7 @@ class LightSensor(HomeAccessory): @callback def async_update_state(self, new_state): """Update accessory after state change.""" - if luminance := convert_to_float(new_state.state): + if (luminance := convert_to_float(new_state.state)) is not None: self.char_light.set_value(luminance) _LOGGER.debug("%s: Set to %d", self.entity_id, luminance) diff --git a/tests/components/homekit/test_type_sensors.py b/tests/components/homekit/test_type_sensors.py index 0b51e44660c..958306e026f 100644 --- a/tests/components/homekit/test_type_sensors.py +++ b/tests/components/homekit/test_type_sensors.py @@ -61,6 +61,10 @@ async def test_temperature(hass, hk_driver): await hass.async_block_till_done() assert acc.char_temp.value == 20 + hass.states.async_set(entity_id, "0", {ATTR_UNIT_OF_MEASUREMENT: TEMP_CELSIUS}) + await hass.async_block_till_done() + assert acc.char_temp.value == 0 + hass.states.async_set( entity_id, "75.2", {ATTR_UNIT_OF_MEASUREMENT: TEMP_FAHRENHEIT} ) @@ -91,6 +95,10 @@ async def test_humidity(hass, hk_driver): await hass.async_block_till_done() assert acc.char_humidity.value == 20 + hass.states.async_set(entity_id, "0") + await hass.async_block_till_done() + assert acc.char_humidity.value == 0 + async def test_air_quality(hass, hk_driver): """Test if accessory is updated after state change.""" @@ -227,6 +235,10 @@ async def test_light(hass, hk_driver): await hass.async_block_till_done() assert acc.char_light.value == 300 + hass.states.async_set(entity_id, "0") + await hass.async_block_till_done() + assert acc.char_light.value == 0.0001 + async def test_binary(hass, hk_driver): """Test if accessory is updated after state change."""