Use climate enums in blebox (#70628)

* Use climate enums in blebox

* Adjust tests
This commit is contained in:
epenet 2022-04-25 13:17:46 +02:00 committed by GitHub
parent a75daab33f
commit e51ed7a11b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 27 additions and 32 deletions

View file

@ -1,11 +1,9 @@
"""BleBox climate entity.""" """BleBox climate entity."""
from homeassistant.components.climate import ClimateEntity, ClimateEntityFeature from homeassistant.components.climate import ClimateEntity
from homeassistant.components.climate.const import ( from homeassistant.components.climate.const import (
CURRENT_HVAC_HEAT, ClimateEntityFeature,
CURRENT_HVAC_IDLE, HVACAction,
CURRENT_HVAC_OFF, HVACMode,
HVAC_MODE_HEAT,
HVAC_MODE_OFF,
) )
from homeassistant.config_entries import ConfigEntry from homeassistant.config_entries import ConfigEntry
from homeassistant.const import ATTR_TEMPERATURE, TEMP_CELSIUS from homeassistant.const import ATTR_TEMPERATURE, TEMP_CELSIUS
@ -31,7 +29,7 @@ class BleBoxClimateEntity(BleBoxEntity, ClimateEntity):
"""Representation of a BleBox climate feature (saunaBox).""" """Representation of a BleBox climate feature (saunaBox)."""
_attr_supported_features = ClimateEntityFeature.TARGET_TEMPERATURE _attr_supported_features = ClimateEntityFeature.TARGET_TEMPERATURE
_attr_hvac_modes = [HVAC_MODE_OFF, HVAC_MODE_HEAT] _attr_hvac_modes = [HVACMode.OFF, HVACMode.HEAT]
_attr_temperature_unit = TEMP_CELSIUS _attr_temperature_unit = TEMP_CELSIUS
@property @property
@ -40,16 +38,16 @@ class BleBoxClimateEntity(BleBoxEntity, ClimateEntity):
if self._feature.is_on is None: if self._feature.is_on is None:
return None return None
return HVAC_MODE_HEAT if self._feature.is_on else HVAC_MODE_OFF return HVACMode.HEAT if self._feature.is_on else HVACMode.OFF
@property @property
def hvac_action(self): def hvac_action(self):
"""Return the actual current HVAC action.""" """Return the actual current HVAC action."""
if not (is_on := self._feature.is_on): if not (is_on := self._feature.is_on):
return None if is_on is None else CURRENT_HVAC_OFF return None if is_on is None else HVACAction.OFF
# NOTE: In practice, there's no need to handle case when is_heating is None # NOTE: In practice, there's no need to handle case when is_heating is None
return CURRENT_HVAC_HEAT if self._feature.is_heating else CURRENT_HVAC_IDLE return HVACAction.HEATING if self._feature.is_heating else HVACAction.IDLE
@property @property
def max_temp(self): def max_temp(self):
@ -73,7 +71,7 @@ class BleBoxClimateEntity(BleBoxEntity, ClimateEntity):
async def async_set_hvac_mode(self, hvac_mode): async def async_set_hvac_mode(self, hvac_mode):
"""Set the climate entity mode.""" """Set the climate entity mode."""
if hvac_mode == HVAC_MODE_HEAT: if hvac_mode == HVACMode.HEAT:
await self._feature.async_on() await self._feature.async_on()
return return

View file

@ -12,14 +12,11 @@ from homeassistant.components.climate.const import (
ATTR_HVAC_MODES, ATTR_HVAC_MODES,
ATTR_MAX_TEMP, ATTR_MAX_TEMP,
ATTR_MIN_TEMP, ATTR_MIN_TEMP,
CURRENT_HVAC_HEAT,
CURRENT_HVAC_IDLE,
CURRENT_HVAC_OFF,
HVAC_MODE_HEAT,
HVAC_MODE_OFF,
SERVICE_SET_HVAC_MODE, SERVICE_SET_HVAC_MODE,
SERVICE_SET_TEMPERATURE, SERVICE_SET_TEMPERATURE,
SUPPORT_TARGET_TEMPERATURE, ClimateEntityFeature,
HVACAction,
HVACMode,
) )
from homeassistant.const import ( from homeassistant.const import (
ATTR_DEVICE_CLASS, ATTR_DEVICE_CLASS,
@ -64,9 +61,9 @@ async def test_init(saunabox, hass, config):
assert state.name == "saunaBox-thermostat" assert state.name == "saunaBox-thermostat"
supported_features = state.attributes[ATTR_SUPPORTED_FEATURES] supported_features = state.attributes[ATTR_SUPPORTED_FEATURES]
assert supported_features & SUPPORT_TARGET_TEMPERATURE assert supported_features & ClimateEntityFeature.TARGET_TEMPERATURE
assert state.attributes[ATTR_HVAC_MODES] == [HVAC_MODE_OFF, HVAC_MODE_HEAT] assert state.attributes[ATTR_HVAC_MODES] == [HVACMode.OFF, HVACMode.HEAT]
assert ATTR_DEVICE_CLASS not in state.attributes assert ATTR_DEVICE_CLASS not in state.attributes
assert ATTR_HVAC_MODE not in state.attributes assert ATTR_HVAC_MODE not in state.attributes
@ -103,10 +100,10 @@ async def test_update(saunabox, hass, config):
await async_setup_entity(hass, config, entity_id) await async_setup_entity(hass, config, entity_id)
state = hass.states.get(entity_id) state = hass.states.get(entity_id)
assert state.attributes[ATTR_HVAC_ACTION] == CURRENT_HVAC_OFF assert state.attributes[ATTR_HVAC_ACTION] == HVACAction.OFF
assert state.attributes[ATTR_TEMPERATURE] == 64.3 assert state.attributes[ATTR_TEMPERATURE] == 64.3
assert state.attributes[ATTR_CURRENT_TEMPERATURE] == 40.9 assert state.attributes[ATTR_CURRENT_TEMPERATURE] == 40.9
assert state.state == HVAC_MODE_OFF assert state.state == HVACMode.OFF
async def test_on_when_below_desired(saunabox, hass, config): async def test_on_when_below_desired(saunabox, hass, config):
@ -131,16 +128,16 @@ async def test_on_when_below_desired(saunabox, hass, config):
await hass.services.async_call( await hass.services.async_call(
"climate", "climate",
SERVICE_SET_HVAC_MODE, SERVICE_SET_HVAC_MODE,
{"entity_id": entity_id, ATTR_HVAC_MODE: HVAC_MODE_HEAT}, {"entity_id": entity_id, ATTR_HVAC_MODE: HVACMode.HEAT},
blocking=True, blocking=True,
) )
feature_mock.async_off.assert_not_called() feature_mock.async_off.assert_not_called()
state = hass.states.get(entity_id) state = hass.states.get(entity_id)
assert state.attributes[ATTR_HVAC_ACTION] == CURRENT_HVAC_HEAT assert state.attributes[ATTR_HVAC_ACTION] == HVACAction.HEATING
assert state.attributes[ATTR_TEMPERATURE] == 64.8 assert state.attributes[ATTR_TEMPERATURE] == 64.8
assert state.attributes[ATTR_CURRENT_TEMPERATURE] == 25.7 assert state.attributes[ATTR_CURRENT_TEMPERATURE] == 25.7
assert state.state == HVAC_MODE_HEAT assert state.state == HVACMode.HEAT
async def test_on_when_above_desired(saunabox, hass, config): async def test_on_when_above_desired(saunabox, hass, config):
@ -166,7 +163,7 @@ async def test_on_when_above_desired(saunabox, hass, config):
await hass.services.async_call( await hass.services.async_call(
"climate", "climate",
SERVICE_SET_HVAC_MODE, SERVICE_SET_HVAC_MODE,
{"entity_id": entity_id, ATTR_HVAC_MODE: HVAC_MODE_HEAT}, {"entity_id": entity_id, ATTR_HVAC_MODE: HVACMode.HEAT},
blocking=True, blocking=True,
) )
feature_mock.async_off.assert_not_called() feature_mock.async_off.assert_not_called()
@ -174,8 +171,8 @@ async def test_on_when_above_desired(saunabox, hass, config):
assert state.attributes[ATTR_TEMPERATURE] == 23.4 assert state.attributes[ATTR_TEMPERATURE] == 23.4
assert state.attributes[ATTR_CURRENT_TEMPERATURE] == 28.7 assert state.attributes[ATTR_CURRENT_TEMPERATURE] == 28.7
assert state.attributes[ATTR_HVAC_ACTION] == CURRENT_HVAC_IDLE assert state.attributes[ATTR_HVAC_ACTION] == HVACAction.IDLE
assert state.state == HVAC_MODE_HEAT assert state.state == HVACMode.HEAT
async def test_off(saunabox, hass, config): async def test_off(saunabox, hass, config):
@ -201,16 +198,16 @@ async def test_off(saunabox, hass, config):
await hass.services.async_call( await hass.services.async_call(
"climate", "climate",
SERVICE_SET_HVAC_MODE, SERVICE_SET_HVAC_MODE,
{"entity_id": entity_id, ATTR_HVAC_MODE: HVAC_MODE_OFF}, {"entity_id": entity_id, ATTR_HVAC_MODE: HVACMode.OFF},
blocking=True, blocking=True,
) )
feature_mock.async_on.assert_not_called() feature_mock.async_on.assert_not_called()
state = hass.states.get(entity_id) state = hass.states.get(entity_id)
assert state.attributes[ATTR_HVAC_ACTION] == CURRENT_HVAC_OFF assert state.attributes[ATTR_HVAC_ACTION] == HVACAction.OFF
assert state.attributes[ATTR_TEMPERATURE] == 29.8 assert state.attributes[ATTR_TEMPERATURE] == 29.8
assert state.attributes[ATTR_CURRENT_TEMPERATURE] == 22.7 assert state.attributes[ATTR_CURRENT_TEMPERATURE] == 22.7
assert state.state == HVAC_MODE_OFF assert state.state == HVACMode.OFF
async def test_set_thermo(saunabox, hass, config): async def test_set_thermo(saunabox, hass, config):
@ -243,8 +240,8 @@ async def test_set_thermo(saunabox, hass, config):
assert state.attributes[ATTR_TEMPERATURE] == 29.2 assert state.attributes[ATTR_TEMPERATURE] == 29.2
assert state.attributes[ATTR_CURRENT_TEMPERATURE] == 29.1 assert state.attributes[ATTR_CURRENT_TEMPERATURE] == 29.1
assert state.attributes[ATTR_HVAC_ACTION] == CURRENT_HVAC_HEAT assert state.attributes[ATTR_HVAC_ACTION] == HVACAction.HEATING
assert state.state == HVAC_MODE_HEAT assert state.state == HVACMode.HEAT
async def test_update_failure(saunabox, hass, config, caplog): async def test_update_failure(saunabox, hass, config, caplog):