diff --git a/homeassistant/components/homekit/type_air_purifiers.py b/homeassistant/components/homekit/type_air_purifiers.py index 25d305a0aa9..feb75f4a856 100644 --- a/homeassistant/components/homekit/type_air_purifiers.py +++ b/homeassistant/components/homekit/type_air_purifiers.py @@ -8,7 +8,13 @@ from pyhap.const import CATEGORY_AIR_PURIFIER from pyhap.service import Service from pyhap.util import callback as pyhap_callback -from homeassistant.const import STATE_ON, STATE_UNAVAILABLE, STATE_UNKNOWN +from homeassistant.const import ( + ATTR_UNIT_OF_MEASUREMENT, + STATE_ON, + STATE_UNAVAILABLE, + STATE_UNKNOWN, + UnitOfTemperature, +) from homeassistant.core import ( Event, EventStateChangedData, @@ -43,7 +49,12 @@ from .const import ( THRESHOLD_FILTER_CHANGE_NEEDED, ) from .type_fans import ATTR_PRESET_MODE, CHAR_ROTATION_SPEED, Fan -from .util import cleanup_name_for_homekit, convert_to_float, density_to_air_quality +from .util import ( + cleanup_name_for_homekit, + convert_to_float, + density_to_air_quality, + temperature_to_homekit, +) _LOGGER = logging.getLogger(__name__) @@ -345,8 +356,13 @@ class AirPurifier(Fan): ): return + unit = new_state.attributes.get( + ATTR_UNIT_OF_MEASUREMENT, UnitOfTemperature.CELSIUS + ) + current_temperature = temperature_to_homekit(current_temperature, unit) + _LOGGER.debug( - "%s: Linked temperature sensor %s changed to %d", + "%s: Linked temperature sensor %s changed to %d °C", self.entity_id, self.linked_temperature_sensor, current_temperature, diff --git a/tests/components/homekit/test_type_air_purifiers.py b/tests/components/homekit/test_type_air_purifiers.py index 90b0e0047de..acc7838652d 100644 --- a/tests/components/homekit/test_type_air_purifiers.py +++ b/tests/components/homekit/test_type_air_purifiers.py @@ -34,9 +34,11 @@ from homeassistant.const import ( ATTR_DEVICE_CLASS, ATTR_ENTITY_ID, ATTR_SUPPORTED_FEATURES, + ATTR_UNIT_OF_MEASUREMENT, STATE_OFF, STATE_ON, STATE_UNAVAILABLE, + UnitOfTemperature, ) from homeassistant.core import Event, HomeAssistant @@ -437,6 +439,22 @@ async def test_expose_linked_sensors( assert acc.char_air_quality.value == 1 assert len(broker.mock_calls) == 0 + # Updated temperature with different unit should reflect in HomeKit + broker = MagicMock() + acc.char_current_temperature.broker = broker + hass.states.async_set( + temperature_entity_id, + 60, + { + ATTR_DEVICE_CLASS: SensorDeviceClass.TEMPERATURE, + ATTR_UNIT_OF_MEASUREMENT: UnitOfTemperature.FAHRENHEIT, + }, + ) + await hass.async_block_till_done() + assert acc.char_current_temperature.value == 15.6 + assert len(broker.mock_calls) == 2 + broker.reset_mock() + # Updated temperature should reflect in HomeKit broker = MagicMock() acc.char_current_temperature.broker = broker