Skip to content

Commit 96a8902

Browse files
authored
fix homekit air purifier temperature sensor to convert unit (#144435)
1 parent d1b85cd commit 96a8902

File tree

2 files changed

+37
-3
lines changed

2 files changed

+37
-3
lines changed

homeassistant/components/homekit/type_air_purifiers.py

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,13 @@
88
from pyhap.service import Service
99
from pyhap.util import callback as pyhap_callback
1010

11-
from homeassistant.const import STATE_ON, STATE_UNAVAILABLE, STATE_UNKNOWN
11+
from homeassistant.const import (
12+
ATTR_UNIT_OF_MEASUREMENT,
13+
STATE_ON,
14+
STATE_UNAVAILABLE,
15+
STATE_UNKNOWN,
16+
UnitOfTemperature,
17+
)
1218
from homeassistant.core import (
1319
Event,
1420
EventStateChangedData,
@@ -43,7 +49,12 @@
4349
THRESHOLD_FILTER_CHANGE_NEEDED,
4450
)
4551
from .type_fans import ATTR_PRESET_MODE, CHAR_ROTATION_SPEED, Fan
46-
from .util import cleanup_name_for_homekit, convert_to_float, density_to_air_quality
52+
from .util import (
53+
cleanup_name_for_homekit,
54+
convert_to_float,
55+
density_to_air_quality,
56+
temperature_to_homekit,
57+
)
4758

4859
_LOGGER = logging.getLogger(__name__)
4960

@@ -345,8 +356,13 @@ def _async_update_current_temperature(self, new_state: State | None) -> None:
345356
):
346357
return
347358

359+
unit = new_state.attributes.get(
360+
ATTR_UNIT_OF_MEASUREMENT, UnitOfTemperature.CELSIUS
361+
)
362+
current_temperature = temperature_to_homekit(current_temperature, unit)
363+
348364
_LOGGER.debug(
349-
"%s: Linked temperature sensor %s changed to %d",
365+
"%s: Linked temperature sensor %s changed to %d °C",
350366
self.entity_id,
351367
self.linked_temperature_sensor,
352368
current_temperature,

tests/components/homekit/test_type_air_purifiers.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,11 @@
3434
ATTR_DEVICE_CLASS,
3535
ATTR_ENTITY_ID,
3636
ATTR_SUPPORTED_FEATURES,
37+
ATTR_UNIT_OF_MEASUREMENT,
3738
STATE_OFF,
3839
STATE_ON,
3940
STATE_UNAVAILABLE,
41+
UnitOfTemperature,
4042
)
4143
from homeassistant.core import Event, HomeAssistant
4244

@@ -437,6 +439,22 @@ async def test_expose_linked_sensors(
437439
assert acc.char_air_quality.value == 1
438440
assert len(broker.mock_calls) == 0
439441

442+
# Updated temperature with different unit should reflect in HomeKit
443+
broker = MagicMock()
444+
acc.char_current_temperature.broker = broker
445+
hass.states.async_set(
446+
temperature_entity_id,
447+
60,
448+
{
449+
ATTR_DEVICE_CLASS: SensorDeviceClass.TEMPERATURE,
450+
ATTR_UNIT_OF_MEASUREMENT: UnitOfTemperature.FAHRENHEIT,
451+
},
452+
)
453+
await hass.async_block_till_done()
454+
assert acc.char_current_temperature.value == 15.6
455+
assert len(broker.mock_calls) == 2
456+
broker.reset_mock()
457+
440458
# Updated temperature should reflect in HomeKit
441459
broker = MagicMock()
442460
acc.char_current_temperature.broker = broker

0 commit comments

Comments
 (0)