Skip to content

Commit

Permalink
Reduce precision loss when converting HomeKit temperature (#131973)
Browse files Browse the repository at this point in the history
  • Loading branch information
bdraco authored Nov 30, 2024
1 parent 2b907ee commit a0d5fda
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 18 deletions.
12 changes: 2 additions & 10 deletions homeassistant/components/homekit/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -434,20 +434,12 @@ def cleanup_name_for_homekit(name: str | None) -> str:

def temperature_to_homekit(temperature: float, unit: str) -> float:
"""Convert temperature to Celsius for HomeKit."""
return round(
TemperatureConverter.convert(temperature, unit, UnitOfTemperature.CELSIUS), 1
)
return TemperatureConverter.convert(temperature, unit, UnitOfTemperature.CELSIUS)


def temperature_to_states(temperature: float, unit: str) -> float:
"""Convert temperature back from Celsius to Home Assistant unit."""
return (
round(
TemperatureConverter.convert(temperature, UnitOfTemperature.CELSIUS, unit)
* 2
)
/ 2
)
return TemperatureConverter.convert(temperature, UnitOfTemperature.CELSIUS, unit)


def density_to_air_quality(density: float) -> int:
Expand Down
10 changes: 5 additions & 5 deletions tests/components/homekit/test_type_thermostats.py
Original file line number Diff line number Diff line change
Expand Up @@ -921,8 +921,8 @@ async def test_thermostat_fahrenheit(
await hass.async_block_till_done()
assert call_set_temperature[0]
assert call_set_temperature[0].data[ATTR_ENTITY_ID] == entity_id
assert call_set_temperature[0].data[ATTR_TARGET_TEMP_HIGH] == 73.5
assert call_set_temperature[0].data[ATTR_TARGET_TEMP_LOW] == 68
assert call_set_temperature[0].data[ATTR_TARGET_TEMP_HIGH] == 73.4
assert call_set_temperature[0].data[ATTR_TARGET_TEMP_LOW] == 68.18
assert len(events) == 1
assert events[-1].data[ATTR_VALUE] == "CoolingThresholdTemperature to 23°C"

Expand All @@ -942,8 +942,8 @@ async def test_thermostat_fahrenheit(
await hass.async_block_till_done()
assert call_set_temperature[1]
assert call_set_temperature[1].data[ATTR_ENTITY_ID] == entity_id
assert call_set_temperature[1].data[ATTR_TARGET_TEMP_HIGH] == 73.5
assert call_set_temperature[1].data[ATTR_TARGET_TEMP_LOW] == 71.5
assert call_set_temperature[1].data[ATTR_TARGET_TEMP_HIGH] == 73.4
assert call_set_temperature[1].data[ATTR_TARGET_TEMP_LOW] == 71.6
assert len(events) == 2
assert events[-1].data[ATTR_VALUE] == "HeatingThresholdTemperature to 22°C"

Expand All @@ -962,7 +962,7 @@ async def test_thermostat_fahrenheit(
await hass.async_block_till_done()
assert call_set_temperature[2]
assert call_set_temperature[2].data[ATTR_ENTITY_ID] == entity_id
assert call_set_temperature[2].data[ATTR_TEMPERATURE] == 75.0
assert call_set_temperature[2].data[ATTR_TEMPERATURE] == 75.2
assert len(events) == 3
assert events[-1].data[ATTR_VALUE] == "TargetTemperature to 24.0°C"

Expand Down
8 changes: 5 additions & 3 deletions tests/components/homekit/test_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -280,14 +280,16 @@ def test_cleanup_name_for_homekit() -> None:

def test_temperature_to_homekit() -> None:
"""Test temperature conversion from HA to HomeKit."""
assert temperature_to_homekit(20.46, UnitOfTemperature.CELSIUS) == 20.5
assert temperature_to_homekit(92.1, UnitOfTemperature.FAHRENHEIT) == 33.4
assert temperature_to_homekit(20.46, UnitOfTemperature.CELSIUS) == 20.46
assert temperature_to_homekit(92.1, UnitOfTemperature.FAHRENHEIT) == pytest.approx(
33.388888888888886
)


def test_temperature_to_states() -> None:
"""Test temperature conversion from HomeKit to HA."""
assert temperature_to_states(20, UnitOfTemperature.CELSIUS) == 20.0
assert temperature_to_states(20.2, UnitOfTemperature.FAHRENHEIT) == 68.5
assert temperature_to_states(20.2, UnitOfTemperature.FAHRENHEIT) == 68.36


def test_density_to_air_quality() -> None:
Expand Down

0 comments on commit a0d5fda

Please sign in to comment.