Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reduce precision loss when converting HomeKit temperature #131973

Merged
merged 2 commits into from
Nov 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading