Fix HomeKit sensor update check (#62705)

This commit is contained in:
Lorenzo Brescanzin 2021-12-24 19:39:37 +01:00 committed by GitHub
parent 6eb31def08
commit 0da710c4e0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 6 deletions

View File

@ -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)

View File

@ -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."""