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 ( from homeassistant.components.climate import (
PLATFORM_SCHEMA as CLIMATE_PLATFORM_SCHEMA, PLATFORM_SCHEMA as CLIMATE_PLATFORM_SCHEMA,
ClimateEntity, ClimateEntity,
ClimateEntityFeature,
) )
from homeassistant.components.climate.const import ( from homeassistant.components.climate.const import (
ATTR_HVAC_MODE, ATTR_HVAC_MODE,
@ -23,14 +22,13 @@ from homeassistant.components.climate.const import (
FAN_HIGH, FAN_HIGH,
FAN_LOW, FAN_LOW,
FAN_MEDIUM, FAN_MEDIUM,
HVAC_MODE_AUTO,
HVAC_MODE_COOL,
HVAC_MODE_DRY,
HVAC_MODE_FAN_ONLY,
HVAC_MODE_HEAT,
HVAC_MODE_OFF,
PRESET_AWAY, PRESET_AWAY,
PRESET_NONE, PRESET_NONE,
SWING_OFF,
SWING_ON,
ClimateEntityFeature,
HVACAction,
HVACMode,
) )
from homeassistant.config_entries import ConfigEntry from homeassistant.config_entries import ConfigEntry
from homeassistant.const import ( from homeassistant.const import (
@ -43,7 +41,6 @@ from homeassistant.const import (
PRECISION_HALVES, PRECISION_HALVES,
PRECISION_TENTHS, PRECISION_TENTHS,
PRECISION_WHOLE, PRECISION_WHOLE,
STATE_ON,
) )
from homeassistant.core import HomeAssistant, callback from homeassistant.core import HomeAssistant, callback
import homeassistant.helpers.config_validation as cv import homeassistant.helpers.config_validation as cv
@ -272,12 +269,12 @@ _PLATFORM_SCHEMA_BASE = SCHEMA_BASE.extend(
vol.Optional( vol.Optional(
CONF_MODE_LIST, CONF_MODE_LIST,
default=[ default=[
HVAC_MODE_AUTO, HVACMode.AUTO,
HVAC_MODE_OFF, HVACMode.OFF,
HVAC_MODE_COOL, HVACMode.COOL,
HVAC_MODE_HEAT, HVACMode.HEAT,
HVAC_MODE_DRY, HVACMode.DRY,
HVAC_MODE_FAN_ONLY, HVACMode.FAN_ONLY,
], ],
): cv.ensure_list, ): cv.ensure_list,
vol.Optional(CONF_MODE_STATE_TEMPLATE): cv.template, 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_TEMPLATE): cv.template,
vol.Optional(CONF_SWING_MODE_COMMAND_TOPIC): mqtt.valid_publish_topic, vol.Optional(CONF_SWING_MODE_COMMAND_TOPIC): mqtt.valid_publish_topic,
vol.Optional( vol.Optional(
CONF_SWING_MODE_LIST, default=[STATE_ON, HVAC_MODE_OFF] CONF_SWING_MODE_LIST, default=[SWING_ON, SWING_OFF]
): cv.ensure_list, ): cv.ensure_list,
vol.Optional(CONF_SWING_MODE_STATE_TEMPLATE): cv.template, vol.Optional(CONF_SWING_MODE_STATE_TEMPLATE): cv.template,
vol.Optional(CONF_SWING_MODE_STATE_TOPIC): mqtt.valid_subscribe_topic, 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: if self._topic[CONF_FAN_MODE_STATE_TOPIC] is None:
self._current_fan_mode = FAN_LOW self._current_fan_mode = FAN_LOW
if self._topic[CONF_SWING_MODE_STATE_TOPIC] is None: 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: 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 self._feature_preset_mode = CONF_PRESET_MODE_COMMAND_TOPIC in config
if self._feature_preset_mode: if self._feature_preset_mode:
self._preset_modes = config[CONF_PRESET_MODES_LIST] self._preset_modes = config[CONF_PRESET_MODES_LIST]
@ -773,17 +770,17 @@ class MqttClimate(MqttEntity, ClimateEntity):
return self._target_temp_high return self._target_temp_high
@property @property
def hvac_action(self): def hvac_action(self) -> HVACAction | None:
"""Return the current running hvac operation if supported.""" """Return the current running hvac operation if supported."""
return self._action return self._action
@property @property
def hvac_mode(self): def hvac_mode(self) -> HVACMode:
"""Return current operation ie. heat, cool, idle.""" """Return current operation ie. heat, cool, idle."""
return self._current_operation return self._current_operation
@property @property
def hvac_modes(self): def hvac_modes(self) -> list[HVACMode]:
"""Return the list of available operation modes.""" """Return the list of available operation modes."""
return self._config[CONF_MODE_LIST] return self._config[CONF_MODE_LIST]
@ -859,7 +856,7 @@ class MqttClimate(MqttEntity, ClimateEntity):
setattr(self, attr, temp) setattr(self, attr, temp)
# CONF_SEND_IF_OFF is deprecated, support will be removed with release 2022.9 # 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) payload = self._command_templates[cmnd_template](temp)
await self._publish(cmnd_topic, payload) await self._publish(cmnd_topic, payload)
@ -899,7 +896,7 @@ class MqttClimate(MqttEntity, ClimateEntity):
async def async_set_swing_mode(self, swing_mode): async def async_set_swing_mode(self, swing_mode):
"""Set new swing mode.""" """Set new swing mode."""
# CONF_SEND_IF_OFF is deprecated, support will be removed with release 2022.9 # 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]( payload = self._command_templates[CONF_SWING_MODE_COMMAND_TEMPLATE](
swing_mode swing_mode
) )
@ -912,7 +909,7 @@ class MqttClimate(MqttEntity, ClimateEntity):
async def async_set_fan_mode(self, fan_mode): async def async_set_fan_mode(self, fan_mode):
"""Set new target temperature.""" """Set new target temperature."""
# CONF_SEND_IF_OFF is deprecated, support will be removed with release 2022.9 # 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) payload = self._command_templates[CONF_FAN_MODE_COMMAND_TEMPLATE](fan_mode)
await self._publish(CONF_FAN_MODE_COMMAND_TOPIC, payload) 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: async def async_set_hvac_mode(self, hvac_mode) -> None:
"""Set new operation mode.""" """Set new operation mode."""
if hvac_mode == HVAC_MODE_OFF: if hvac_mode == HVACMode.OFF:
await self._publish( await self._publish(
CONF_POWER_COMMAND_TOPIC, self._config[CONF_PAYLOAD_OFF] 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, ATTR_TARGET_TEMP_LOW,
CURRENT_HVAC_ACTIONS, CURRENT_HVAC_ACTIONS,
DOMAIN as CLIMATE_DOMAIN, DOMAIN as CLIMATE_DOMAIN,
HVAC_MODE_AUTO,
HVAC_MODE_COOL,
HVAC_MODE_DRY,
HVAC_MODE_FAN_ONLY,
HVAC_MODE_HEAT,
PRESET_AWAY, PRESET_AWAY,
PRESET_ECO, PRESET_ECO,
PRESET_NONE, PRESET_NONE,
SUPPORT_AUX_HEAT, ClimateEntityFeature,
SUPPORT_FAN_MODE, HVACMode,
SUPPORT_PRESET_MODE,
SUPPORT_SWING_MODE,
SUPPORT_TARGET_TEMPERATURE,
SUPPORT_TARGET_TEMPERATURE_RANGE,
) )
from homeassistant.components.mqtt.climate import MQTT_CLIMATE_ATTRIBUTES_BLOCKED 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 homeassistant.setup import async_setup_component
from .test_common import ( from .test_common import (
@ -171,12 +162,12 @@ async def test_supported_features(hass, mqtt_mock):
state = hass.states.get(ENTITY_CLIMATE) state = hass.states.get(ENTITY_CLIMATE)
support = ( support = (
SUPPORT_TARGET_TEMPERATURE ClimateEntityFeature.TARGET_TEMPERATURE
| SUPPORT_SWING_MODE | ClimateEntityFeature.SWING_MODE
| SUPPORT_FAN_MODE | ClimateEntityFeature.FAN_MODE
| SUPPORT_PRESET_MODE | ClimateEntityFeature.PRESET_MODE
| SUPPORT_AUX_HEAT | ClimateEntityFeature.AUX_HEAT
| SUPPORT_TARGET_TEMPERATURE_RANGE | ClimateEntityFeature.TARGET_TEMPERATURE_RANGE
) )
assert state.attributes.get("supported_features") == support 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) state = hass.states.get(ENTITY_CLIMATE)
modes = state.attributes.get("hvac_modes") modes = state.attributes.get("hvac_modes")
assert [ assert [
HVAC_MODE_AUTO, HVACMode.AUTO,
STATE_OFF, HVACMode.OFF,
HVAC_MODE_COOL, HVACMode.COOL,
HVAC_MODE_HEAT, HVACMode.HEAT,
HVAC_MODE_DRY, HVACMode.DRY,
HVAC_MODE_FAN_ONLY, HVACMode.FAN_ONLY,
] == modes ] == modes