Use climate enums in mqtt (#70696)

This commit is contained in:
epenet 2022-05-09 08:15:11 +02:00 committed by GitHub
parent 24d7a464e1
commit 1be2438ef6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 36 additions and 48 deletions

View File

@ -10,7 +10,6 @@ from homeassistant.components import climate
from homeassistant.components.climate import (
PLATFORM_SCHEMA as CLIMATE_PLATFORM_SCHEMA,
ClimateEntity,
ClimateEntityFeature,
)
from homeassistant.components.climate.const import (
ATTR_HVAC_MODE,
@ -23,14 +22,13 @@ from homeassistant.components.climate.const import (
FAN_HIGH,
FAN_LOW,
FAN_MEDIUM,
HVAC_MODE_AUTO,
HVAC_MODE_COOL,
HVAC_MODE_DRY,
HVAC_MODE_FAN_ONLY,
HVAC_MODE_HEAT,
HVAC_MODE_OFF,
PRESET_AWAY,
PRESET_NONE,
SWING_OFF,
SWING_ON,
ClimateEntityFeature,
HVACAction,
HVACMode,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import (
@ -43,7 +41,6 @@ from homeassistant.const import (
PRECISION_HALVES,
PRECISION_TENTHS,
PRECISION_WHOLE,
STATE_ON,
)
from homeassistant.core import HomeAssistant, callback
import homeassistant.helpers.config_validation as cv
@ -272,12 +269,12 @@ _PLATFORM_SCHEMA_BASE = SCHEMA_BASE.extend(
vol.Optional(
CONF_MODE_LIST,
default=[
HVAC_MODE_AUTO,
HVAC_MODE_OFF,
HVAC_MODE_COOL,
HVAC_MODE_HEAT,
HVAC_MODE_DRY,
HVAC_MODE_FAN_ONLY,
HVACMode.AUTO,
HVACMode.OFF,
HVACMode.COOL,
HVACMode.HEAT,
HVACMode.DRY,
HVACMode.FAN_ONLY,
],
): cv.ensure_list,
vol.Optional(CONF_MODE_STATE_TEMPLATE): cv.template,
@ -309,7 +306,7 @@ _PLATFORM_SCHEMA_BASE = SCHEMA_BASE.extend(
vol.Optional(CONF_SWING_MODE_COMMAND_TEMPLATE): cv.template,
vol.Optional(CONF_SWING_MODE_COMMAND_TOPIC): mqtt.valid_publish_topic,
vol.Optional(
CONF_SWING_MODE_LIST, default=[STATE_ON, HVAC_MODE_OFF]
CONF_SWING_MODE_LIST, default=[SWING_ON, SWING_OFF]
): cv.ensure_list,
vol.Optional(CONF_SWING_MODE_STATE_TEMPLATE): cv.template,
vol.Optional(CONF_SWING_MODE_STATE_TOPIC): mqtt.valid_subscribe_topic,
@ -460,9 +457,9 @@ class MqttClimate(MqttEntity, ClimateEntity):
if self._topic[CONF_FAN_MODE_STATE_TOPIC] is None:
self._current_fan_mode = FAN_LOW
if self._topic[CONF_SWING_MODE_STATE_TOPIC] is None:
self._current_swing_mode = HVAC_MODE_OFF
self._current_swing_mode = SWING_OFF
if self._topic[CONF_MODE_STATE_TOPIC] is None:
self._current_operation = HVAC_MODE_OFF
self._current_operation = HVACMode.OFF
self._feature_preset_mode = CONF_PRESET_MODE_COMMAND_TOPIC in config
if self._feature_preset_mode:
self._preset_modes = config[CONF_PRESET_MODES_LIST]
@ -773,17 +770,17 @@ class MqttClimate(MqttEntity, ClimateEntity):
return self._target_temp_high
@property
def hvac_action(self):
def hvac_action(self) -> HVACAction | None:
"""Return the current running hvac operation if supported."""
return self._action
@property
def hvac_mode(self):
def hvac_mode(self) -> HVACMode:
"""Return current operation ie. heat, cool, idle."""
return self._current_operation
@property
def hvac_modes(self):
def hvac_modes(self) -> list[HVACMode]:
"""Return the list of available operation modes."""
return self._config[CONF_MODE_LIST]
@ -859,7 +856,7 @@ class MqttClimate(MqttEntity, ClimateEntity):
setattr(self, attr, temp)
# CONF_SEND_IF_OFF is deprecated, support will be removed with release 2022.9
if self._send_if_off or self._current_operation != HVAC_MODE_OFF:
if self._send_if_off or self._current_operation != HVACMode.OFF:
payload = self._command_templates[cmnd_template](temp)
await self._publish(cmnd_topic, payload)
@ -899,7 +896,7 @@ class MqttClimate(MqttEntity, ClimateEntity):
async def async_set_swing_mode(self, swing_mode):
"""Set new swing mode."""
# CONF_SEND_IF_OFF is deprecated, support will be removed with release 2022.9
if self._send_if_off or self._current_operation != HVAC_MODE_OFF:
if self._send_if_off or self._current_operation != HVACMode.OFF:
payload = self._command_templates[CONF_SWING_MODE_COMMAND_TEMPLATE](
swing_mode
)
@ -912,7 +909,7 @@ class MqttClimate(MqttEntity, ClimateEntity):
async def async_set_fan_mode(self, fan_mode):
"""Set new target temperature."""
# CONF_SEND_IF_OFF is deprecated, support will be removed with release 2022.9
if self._send_if_off or self._current_operation != HVAC_MODE_OFF:
if self._send_if_off or self._current_operation != HVACMode.OFF:
payload = self._command_templates[CONF_FAN_MODE_COMMAND_TEMPLATE](fan_mode)
await self._publish(CONF_FAN_MODE_COMMAND_TOPIC, payload)
@ -922,7 +919,7 @@ class MqttClimate(MqttEntity, ClimateEntity):
async def async_set_hvac_mode(self, hvac_mode) -> None:
"""Set new operation mode."""
if hvac_mode == HVAC_MODE_OFF:
if hvac_mode == HVACMode.OFF:
await self._publish(
CONF_POWER_COMMAND_TOPIC, self._config[CONF_PAYLOAD_OFF]
)

View File

@ -19,23 +19,14 @@ from homeassistant.components.climate.const import (
ATTR_TARGET_TEMP_LOW,
CURRENT_HVAC_ACTIONS,
DOMAIN as CLIMATE_DOMAIN,
HVAC_MODE_AUTO,
HVAC_MODE_COOL,
HVAC_MODE_DRY,
HVAC_MODE_FAN_ONLY,
HVAC_MODE_HEAT,
PRESET_AWAY,
PRESET_ECO,
PRESET_NONE,
SUPPORT_AUX_HEAT,
SUPPORT_FAN_MODE,
SUPPORT_PRESET_MODE,
SUPPORT_SWING_MODE,
SUPPORT_TARGET_TEMPERATURE,
SUPPORT_TARGET_TEMPERATURE_RANGE,
ClimateEntityFeature,
HVACMode,
)
from homeassistant.components.mqtt.climate import MQTT_CLIMATE_ATTRIBUTES_BLOCKED
from homeassistant.const import ATTR_TEMPERATURE, STATE_OFF
from homeassistant.const import ATTR_TEMPERATURE
from homeassistant.setup import async_setup_component
from .test_common import (
@ -171,12 +162,12 @@ async def test_supported_features(hass, mqtt_mock):
state = hass.states.get(ENTITY_CLIMATE)
support = (
SUPPORT_TARGET_TEMPERATURE
| SUPPORT_SWING_MODE
| SUPPORT_FAN_MODE
| SUPPORT_PRESET_MODE
| SUPPORT_AUX_HEAT
| SUPPORT_TARGET_TEMPERATURE_RANGE
ClimateEntityFeature.TARGET_TEMPERATURE
| ClimateEntityFeature.SWING_MODE
| ClimateEntityFeature.FAN_MODE
| ClimateEntityFeature.PRESET_MODE
| ClimateEntityFeature.AUX_HEAT
| ClimateEntityFeature.TARGET_TEMPERATURE_RANGE
)
assert state.attributes.get("supported_features") == support
@ -190,12 +181,12 @@ async def test_get_hvac_modes(hass, mqtt_mock):
state = hass.states.get(ENTITY_CLIMATE)
modes = state.attributes.get("hvac_modes")
assert [
HVAC_MODE_AUTO,
STATE_OFF,
HVAC_MODE_COOL,
HVAC_MODE_HEAT,
HVAC_MODE_DRY,
HVAC_MODE_FAN_ONLY,
HVACMode.AUTO,
HVACMode.OFF,
HVACMode.COOL,
HVACMode.HEAT,
HVACMode.DRY,
HVACMode.FAN_ONLY,
] == modes