From 1b6ee8301a5c076f93d0799b9f7fcb82cc6eb902 Mon Sep 17 00:00:00 2001 From: Christopher Gozdziewski Date: Thu, 4 Feb 2021 01:32:43 -0600 Subject: [PATCH] Convert ozw climate values to correct units (#45369) * Convert ozw climate values to correct units * Remove debugger logging * Fix black code formatting * Remove extra spaces * Add method descriptions and change to use setpoint * Fix build and respond to comments * Remove self from convert_units call * Move method to top * Move method outside class * Add blank lines * Fix test to use farenheit * Update another value to farenheit * Change to celsius * Another test fix * test fix * Fix a value * missed one * Add unit test for convert_units * fix unit test import * Add new line to end of test file * fix convert units import * Reorder imports * Grab const from different import Co-authored-by: Trevor --- homeassistant/components/ozw/climate.py | 51 +++++++++++++++++++++---- tests/components/ozw/test_climate.py | 20 ++++++---- 2 files changed, 56 insertions(+), 15 deletions(-) diff --git a/homeassistant/components/ozw/climate.py b/homeassistant/components/ozw/climate.py index a74fd869f0f..67bbe5cdc4d 100644 --- a/homeassistant/components/ozw/climate.py +++ b/homeassistant/components/ozw/climate.py @@ -28,6 +28,7 @@ from homeassistant.components.climate.const import ( from homeassistant.const import ATTR_TEMPERATURE, TEMP_CELSIUS, TEMP_FAHRENHEIT from homeassistant.core import callback from homeassistant.helpers.dispatcher import async_dispatcher_connect +from homeassistant.util.temperature import convert as convert_temperature from .const import DATA_UNSUBSCRIBE, DOMAIN from .entity import ZWaveDeviceEntity @@ -154,6 +155,13 @@ async def async_setup_entry(hass, config_entry, async_add_entities): ) +def convert_units(units): + """Return units as a farenheit or celsius constant.""" + if units == "F": + return TEMP_FAHRENHEIT + return TEMP_CELSIUS + + class ZWaveClimateEntity(ZWaveDeviceEntity, ClimateEntity): """Representation of a Z-Wave Climate device.""" @@ -199,16 +207,18 @@ class ZWaveClimateEntity(ZWaveDeviceEntity, ClimateEntity): @property def temperature_unit(self): """Return the unit of measurement.""" - if self.values.temperature is not None and self.values.temperature.units == "F": - return TEMP_FAHRENHEIT - return TEMP_CELSIUS + return convert_units(self._current_mode_setpoint_values[0].units) @property def current_temperature(self): """Return the current temperature.""" if not self.values.temperature: return None - return self.values.temperature.value + return convert_temperature( + self.values.temperature.value, + convert_units(self._current_mode_setpoint_values[0].units), + self.temperature_unit, + ) @property def hvac_action(self): @@ -236,17 +246,29 @@ class ZWaveClimateEntity(ZWaveDeviceEntity, ClimateEntity): @property def target_temperature(self): """Return the temperature we try to reach.""" - return self._current_mode_setpoint_values[0].value + return convert_temperature( + self._current_mode_setpoint_values[0].value, + convert_units(self._current_mode_setpoint_values[0].units), + self.temperature_unit, + ) @property def target_temperature_low(self) -> Optional[float]: """Return the lowbound target temperature we try to reach.""" - return self._current_mode_setpoint_values[0].value + return convert_temperature( + self._current_mode_setpoint_values[0].value, + convert_units(self._current_mode_setpoint_values[0].units), + self.temperature_unit, + ) @property def target_temperature_high(self) -> Optional[float]: """Return the highbound target temperature we try to reach.""" - return self._current_mode_setpoint_values[1].value + return convert_temperature( + self._current_mode_setpoint_values[1].value, + convert_units(self._current_mode_setpoint_values[1].units), + self.temperature_unit, + ) async def async_set_temperature(self, **kwargs): """Set new target temperature. @@ -262,14 +284,29 @@ class ZWaveClimateEntity(ZWaveDeviceEntity, ClimateEntity): setpoint = self._current_mode_setpoint_values[0] target_temp = kwargs.get(ATTR_TEMPERATURE) if setpoint is not None and target_temp is not None: + target_temp = convert_temperature( + target_temp, + self.temperature_unit, + convert_units(setpoint.units), + ) setpoint.send_value(target_temp) elif len(self._current_mode_setpoint_values) == 2: (setpoint_low, setpoint_high) = self._current_mode_setpoint_values target_temp_low = kwargs.get(ATTR_TARGET_TEMP_LOW) target_temp_high = kwargs.get(ATTR_TARGET_TEMP_HIGH) if setpoint_low is not None and target_temp_low is not None: + target_temp_low = convert_temperature( + target_temp_low, + self.temperature_unit, + convert_units(setpoint_low.units), + ) setpoint_low.send_value(target_temp_low) if setpoint_high is not None and target_temp_high is not None: + target_temp_high = convert_temperature( + target_temp_high, + self.temperature_unit, + convert_units(setpoint_high.units), + ) setpoint_high.send_value(target_temp_high) async def async_set_fan_mode(self, fan_mode): diff --git a/tests/components/ozw/test_climate.py b/tests/components/ozw/test_climate.py index 3414e6c4832..e251a93c115 100644 --- a/tests/components/ozw/test_climate.py +++ b/tests/components/ozw/test_climate.py @@ -16,6 +16,8 @@ from homeassistant.components.climate.const import ( HVAC_MODE_HEAT_COOL, HVAC_MODE_OFF, ) +from homeassistant.components.ozw.climate import convert_units +from homeassistant.const import TEMP_FAHRENHEIT from .common import setup_ozw @@ -36,8 +38,8 @@ async def test_climate(hass, climate_data, sent_messages, climate_msg, caplog): HVAC_MODE_HEAT_COOL, ] assert state.attributes[ATTR_HVAC_ACTION] == CURRENT_HVAC_IDLE - assert state.attributes[ATTR_CURRENT_TEMPERATURE] == 23.1 - assert state.attributes[ATTR_TEMPERATURE] == 21.1 + assert state.attributes[ATTR_CURRENT_TEMPERATURE] == 73.5 + assert state.attributes[ATTR_TEMPERATURE] == 70.0 assert state.attributes.get(ATTR_TARGET_TEMP_LOW) is None assert state.attributes.get(ATTR_TARGET_TEMP_HIGH) is None assert state.attributes[ATTR_FAN_MODE] == "Auto Low" @@ -54,7 +56,7 @@ async def test_climate(hass, climate_data, sent_messages, climate_msg, caplog): msg = sent_messages[-1] assert msg["topic"] == "OpenZWave/1/command/setvalue/" # Celsius is converted to Fahrenheit here! - assert round(msg["payload"]["Value"], 2) == 78.98 + assert round(msg["payload"]["Value"], 2) == 26.1 assert msg["payload"]["ValueIDKey"] == 281475099443218 # Test hvac_mode with set_temperature @@ -72,7 +74,7 @@ async def test_climate(hass, climate_data, sent_messages, climate_msg, caplog): msg = sent_messages[-1] assert msg["topic"] == "OpenZWave/1/command/setvalue/" # Celsius is converted to Fahrenheit here! - assert round(msg["payload"]["Value"], 2) == 75.38 + assert round(msg["payload"]["Value"], 2) == 24.1 assert msg["payload"]["ValueIDKey"] == 281475099443218 # Test set mode @@ -127,8 +129,8 @@ async def test_climate(hass, climate_data, sent_messages, climate_msg, caplog): assert state is not None assert state.state == HVAC_MODE_HEAT_COOL assert state.attributes.get(ATTR_TEMPERATURE) is None - assert state.attributes[ATTR_TARGET_TEMP_LOW] == 21.1 - assert state.attributes[ATTR_TARGET_TEMP_HIGH] == 25.6 + assert state.attributes[ATTR_TARGET_TEMP_LOW] == 70.0 + assert state.attributes[ATTR_TARGET_TEMP_HIGH] == 78.0 # Test setting high/low temp on multiple setpoints await hass.services.async_call( @@ -144,11 +146,11 @@ async def test_climate(hass, climate_data, sent_messages, climate_msg, caplog): assert len(sent_messages) == 7 # 2 messages ! msg = sent_messages[-2] # low setpoint assert msg["topic"] == "OpenZWave/1/command/setvalue/" - assert round(msg["payload"]["Value"], 2) == 68.0 + assert round(msg["payload"]["Value"], 2) == 20.0 assert msg["payload"]["ValueIDKey"] == 281475099443218 msg = sent_messages[-1] # high setpoint assert msg["topic"] == "OpenZWave/1/command/setvalue/" - assert round(msg["payload"]["Value"], 2) == 77.0 + assert round(msg["payload"]["Value"], 2) == 25.0 assert msg["payload"]["ValueIDKey"] == 562950076153874 # Test basic/single-setpoint thermostat (node 16 in dump) @@ -325,3 +327,5 @@ async def test_climate(hass, climate_data, sent_messages, climate_msg, caplog): ) assert len(sent_messages) == 12 assert "does not support setting a mode" in caplog.text + + assert convert_units("F") == TEMP_FAHRENHEIT