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 <tboyce021@gmail.com>
This commit is contained in:
Christopher Gozdziewski 2021-02-04 01:32:43 -06:00 committed by GitHub
parent 14d914e300
commit 1b6ee8301a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 56 additions and 15 deletions

View file

@ -28,6 +28,7 @@ from homeassistant.components.climate.const import (
from homeassistant.const import ATTR_TEMPERATURE, TEMP_CELSIUS, TEMP_FAHRENHEIT from homeassistant.const import ATTR_TEMPERATURE, TEMP_CELSIUS, TEMP_FAHRENHEIT
from homeassistant.core import callback from homeassistant.core import callback
from homeassistant.helpers.dispatcher import async_dispatcher_connect from homeassistant.helpers.dispatcher import async_dispatcher_connect
from homeassistant.util.temperature import convert as convert_temperature
from .const import DATA_UNSUBSCRIBE, DOMAIN from .const import DATA_UNSUBSCRIBE, DOMAIN
from .entity import ZWaveDeviceEntity 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): class ZWaveClimateEntity(ZWaveDeviceEntity, ClimateEntity):
"""Representation of a Z-Wave Climate device.""" """Representation of a Z-Wave Climate device."""
@ -199,16 +207,18 @@ class ZWaveClimateEntity(ZWaveDeviceEntity, ClimateEntity):
@property @property
def temperature_unit(self): def temperature_unit(self):
"""Return the unit of measurement.""" """Return the unit of measurement."""
if self.values.temperature is not None and self.values.temperature.units == "F": return convert_units(self._current_mode_setpoint_values[0].units)
return TEMP_FAHRENHEIT
return TEMP_CELSIUS
@property @property
def current_temperature(self): def current_temperature(self):
"""Return the current temperature.""" """Return the current temperature."""
if not self.values.temperature: if not self.values.temperature:
return None 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 @property
def hvac_action(self): def hvac_action(self):
@ -236,17 +246,29 @@ class ZWaveClimateEntity(ZWaveDeviceEntity, ClimateEntity):
@property @property
def target_temperature(self): def target_temperature(self):
"""Return the temperature we try to reach.""" """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 @property
def target_temperature_low(self) -> Optional[float]: def target_temperature_low(self) -> Optional[float]:
"""Return the lowbound target temperature we try to reach.""" """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 @property
def target_temperature_high(self) -> Optional[float]: def target_temperature_high(self) -> Optional[float]:
"""Return the highbound target temperature we try to reach.""" """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): async def async_set_temperature(self, **kwargs):
"""Set new target temperature. """Set new target temperature.
@ -262,14 +284,29 @@ class ZWaveClimateEntity(ZWaveDeviceEntity, ClimateEntity):
setpoint = self._current_mode_setpoint_values[0] setpoint = self._current_mode_setpoint_values[0]
target_temp = kwargs.get(ATTR_TEMPERATURE) target_temp = kwargs.get(ATTR_TEMPERATURE)
if setpoint is not None and target_temp is not None: 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) setpoint.send_value(target_temp)
elif len(self._current_mode_setpoint_values) == 2: elif len(self._current_mode_setpoint_values) == 2:
(setpoint_low, setpoint_high) = self._current_mode_setpoint_values (setpoint_low, setpoint_high) = self._current_mode_setpoint_values
target_temp_low = kwargs.get(ATTR_TARGET_TEMP_LOW) target_temp_low = kwargs.get(ATTR_TARGET_TEMP_LOW)
target_temp_high = kwargs.get(ATTR_TARGET_TEMP_HIGH) target_temp_high = kwargs.get(ATTR_TARGET_TEMP_HIGH)
if setpoint_low is not None and target_temp_low is not None: 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) setpoint_low.send_value(target_temp_low)
if setpoint_high is not None and target_temp_high is not None: 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) setpoint_high.send_value(target_temp_high)
async def async_set_fan_mode(self, fan_mode): async def async_set_fan_mode(self, fan_mode):

View file

@ -16,6 +16,8 @@ from homeassistant.components.climate.const import (
HVAC_MODE_HEAT_COOL, HVAC_MODE_HEAT_COOL,
HVAC_MODE_OFF, HVAC_MODE_OFF,
) )
from homeassistant.components.ozw.climate import convert_units
from homeassistant.const import TEMP_FAHRENHEIT
from .common import setup_ozw 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, HVAC_MODE_HEAT_COOL,
] ]
assert state.attributes[ATTR_HVAC_ACTION] == CURRENT_HVAC_IDLE assert state.attributes[ATTR_HVAC_ACTION] == CURRENT_HVAC_IDLE
assert state.attributes[ATTR_CURRENT_TEMPERATURE] == 23.1 assert state.attributes[ATTR_CURRENT_TEMPERATURE] == 73.5
assert state.attributes[ATTR_TEMPERATURE] == 21.1 assert state.attributes[ATTR_TEMPERATURE] == 70.0
assert state.attributes.get(ATTR_TARGET_TEMP_LOW) is None assert state.attributes.get(ATTR_TARGET_TEMP_LOW) is None
assert state.attributes.get(ATTR_TARGET_TEMP_HIGH) is None assert state.attributes.get(ATTR_TARGET_TEMP_HIGH) is None
assert state.attributes[ATTR_FAN_MODE] == "Auto Low" 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] msg = sent_messages[-1]
assert msg["topic"] == "OpenZWave/1/command/setvalue/" assert msg["topic"] == "OpenZWave/1/command/setvalue/"
# Celsius is converted to Fahrenheit here! # 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 assert msg["payload"]["ValueIDKey"] == 281475099443218
# Test hvac_mode with set_temperature # 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] msg = sent_messages[-1]
assert msg["topic"] == "OpenZWave/1/command/setvalue/" assert msg["topic"] == "OpenZWave/1/command/setvalue/"
# Celsius is converted to Fahrenheit here! # 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 assert msg["payload"]["ValueIDKey"] == 281475099443218
# Test set mode # 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 is not None
assert state.state == HVAC_MODE_HEAT_COOL assert state.state == HVAC_MODE_HEAT_COOL
assert state.attributes.get(ATTR_TEMPERATURE) is None assert state.attributes.get(ATTR_TEMPERATURE) is None
assert state.attributes[ATTR_TARGET_TEMP_LOW] == 21.1 assert state.attributes[ATTR_TARGET_TEMP_LOW] == 70.0
assert state.attributes[ATTR_TARGET_TEMP_HIGH] == 25.6 assert state.attributes[ATTR_TARGET_TEMP_HIGH] == 78.0
# Test setting high/low temp on multiple setpoints # Test setting high/low temp on multiple setpoints
await hass.services.async_call( 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 ! assert len(sent_messages) == 7 # 2 messages !
msg = sent_messages[-2] # low setpoint msg = sent_messages[-2] # low setpoint
assert msg["topic"] == "OpenZWave/1/command/setvalue/" 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 assert msg["payload"]["ValueIDKey"] == 281475099443218
msg = sent_messages[-1] # high setpoint msg = sent_messages[-1] # high setpoint
assert msg["topic"] == "OpenZWave/1/command/setvalue/" 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 assert msg["payload"]["ValueIDKey"] == 562950076153874
# Test basic/single-setpoint thermostat (node 16 in dump) # 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 len(sent_messages) == 12
assert "does not support setting a mode" in caplog.text assert "does not support setting a mode" in caplog.text
assert convert_units("F") == TEMP_FAHRENHEIT