Use climate enums in venstar (#70749)

This commit is contained in:
epenet 2022-04-26 09:17:19 +02:00 committed by GitHub
parent 9009b1ef7d
commit 20f7e2f4d2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 29 additions and 45 deletions

View file

@ -3,27 +3,18 @@ from __future__ import annotations
import voluptuous as vol
from homeassistant.components.climate import (
PLATFORM_SCHEMA,
ClimateEntity,
ClimateEntityFeature,
)
from homeassistant.components.climate import PLATFORM_SCHEMA, ClimateEntity
from homeassistant.components.climate.const import (
ATTR_HVAC_MODE,
ATTR_TARGET_TEMP_HIGH,
ATTR_TARGET_TEMP_LOW,
CURRENT_HVAC_COOL,
CURRENT_HVAC_HEAT,
CURRENT_HVAC_IDLE,
CURRENT_HVAC_OFF,
FAN_AUTO,
FAN_ON,
HVAC_MODE_AUTO,
HVAC_MODE_COOL,
HVAC_MODE_HEAT,
HVAC_MODE_OFF,
PRESET_AWAY,
PRESET_NONE,
ClimateEntityFeature,
HVACAction,
HVACMode,
)
from homeassistant.config_entries import SOURCE_IMPORT, ConfigEntry
from homeassistant.const import (
@ -115,6 +106,8 @@ async def async_setup_platform(
class VenstarThermostat(VenstarEntity, ClimateEntity):
"""Representation of a Venstar thermostat."""
_attr_hvac_modes = VALID_THERMOSTAT_MODES
def __init__(
self,
venstar_data_coordinator: VenstarDataUpdateCoordinator,
@ -123,9 +116,9 @@ class VenstarThermostat(VenstarEntity, ClimateEntity):
"""Initialize the thermostat."""
super().__init__(venstar_data_coordinator, config)
self._mode_map = {
HVAC_MODE_HEAT: self._client.MODE_HEAT,
HVAC_MODE_COOL: self._client.MODE_COOL,
HVAC_MODE_AUTO: self._client.MODE_AUTO,
HVACMode.HEAT: self._client.MODE_HEAT,
HVACMode.COOL: self._client.MODE_COOL,
HVACMode.AUTO: self._client.MODE_AUTO,
}
self._attr_unique_id = config.entry_id
self._attr_name = self._client.name
@ -168,11 +161,6 @@ class VenstarThermostat(VenstarEntity, ClimateEntity):
"""Return the list of available fan modes."""
return VALID_FAN_STATES
@property
def hvac_modes(self):
"""Return the list of available operation modes."""
return VALID_THERMOSTAT_MODES
@property
def current_temperature(self):
"""Return the current temperature."""
@ -184,26 +172,26 @@ class VenstarThermostat(VenstarEntity, ClimateEntity):
return self._client.get_indoor_humidity()
@property
def hvac_mode(self):
def hvac_mode(self) -> HVACMode:
"""Return current operation mode ie. heat, cool, auto."""
if self._client.mode == self._client.MODE_HEAT:
return HVAC_MODE_HEAT
return HVACMode.HEAT
if self._client.mode == self._client.MODE_COOL:
return HVAC_MODE_COOL
return HVACMode.COOL
if self._client.mode == self._client.MODE_AUTO:
return HVAC_MODE_AUTO
return HVAC_MODE_OFF
return HVACMode.AUTO
return HVACMode.OFF
@property
def hvac_action(self):
def hvac_action(self) -> HVACAction:
"""Return current operation mode ie. heat, cool, auto."""
if self._client.state == self._client.STATE_IDLE:
return CURRENT_HVAC_IDLE
return HVACAction.IDLE
if self._client.state == self._client.STATE_HEATING:
return CURRENT_HVAC_HEAT
return HVACAction.HEATING
if self._client.state == self._client.STATE_COOLING:
return CURRENT_HVAC_COOL
return CURRENT_HVAC_OFF
return HVACAction.COOLING
return HVACAction.OFF
@property
def fan_mode(self):
@ -272,13 +260,13 @@ class VenstarThermostat(VenstarEntity, ClimateEntity):
"""Return valid preset modes."""
return [PRESET_NONE, PRESET_AWAY, HOLD_MODE_TEMPERATURE]
def _set_operation_mode(self, operation_mode):
def _set_operation_mode(self, operation_mode: HVACMode):
"""Change the operation mode (internal)."""
if operation_mode == HVAC_MODE_HEAT:
if operation_mode == HVACMode.HEAT:
success = self._client.set_mode(self._client.MODE_HEAT)
elif operation_mode == HVAC_MODE_COOL:
elif operation_mode == HVACMode.COOL:
success = self._client.set_mode(self._client.MODE_COOL)
elif operation_mode == HVAC_MODE_AUTO:
elif operation_mode == HVACMode.AUTO:
success = self._client.set_mode(self._client.MODE_AUTO)
else:
success = self._client.set_mode(self._client.MODE_OFF)
@ -337,7 +325,7 @@ class VenstarThermostat(VenstarEntity, ClimateEntity):
_LOGGER.error("Failed to change the fan mode")
self.schedule_update_ha_state()
def set_hvac_mode(self, hvac_mode):
def set_hvac_mode(self, hvac_mode: HVACMode) -> None:
"""Set new target operation mode."""
self._set_operation_mode(hvac_mode)
self.schedule_update_ha_state()

View file

@ -1,18 +1,14 @@
"""The climate tests for the venstar integration."""
from unittest.mock import patch
from homeassistant.components.climate.const import (
SUPPORT_FAN_MODE,
SUPPORT_PRESET_MODE,
SUPPORT_TARGET_HUMIDITY,
SUPPORT_TARGET_TEMPERATURE,
)
from homeassistant.components.climate.const import ClimateEntityFeature
from .util import async_init_integration, mock_venstar_devices
EXPECTED_BASE_SUPPORTED_FEATURES = (
SUPPORT_TARGET_TEMPERATURE | SUPPORT_FAN_MODE | SUPPORT_PRESET_MODE
ClimateEntityFeature.TARGET_TEMPERATURE
| ClimateEntityFeature.FAN_MODE
| ClimateEntityFeature.PRESET_MODE
)
@ -45,7 +41,7 @@ async def test_colortouch(hass):
"hvac_mode": 0,
"friendly_name": "COLORTOUCH",
"supported_features": EXPECTED_BASE_SUPPORTED_FEATURES
| SUPPORT_TARGET_HUMIDITY,
| ClimateEntityFeature.TARGET_HUMIDITY,
}
# Only test for a subset of attributes in case
# HA changes the implementation and a new one appears