Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
ef5303a
MQTT Climate: Add support for setting the current humidity via MQTT
Stonos Dec 26, 2022
1375500
MQTT Climate: Add configuration constants related to setting the targ…
Stonos Dec 31, 2022
5f1b7b5
MQTT Climate: Add support for setting the humidity's state topic & te…
Stonos Dec 31, 2022
ff85c71
MQTT Climate: Add support for setting the initial humidity
Stonos Jan 1, 2023
dac37f1
MQTT Climate: Add support for setting the humidity's command topic & …
Stonos Jan 1, 2023
bf6c3f3
MQTT Climate: Add support for setting the min/max humidity
Stonos Jan 1, 2023
4096b88
MQTT Climate: Fix style & tests
Stonos Jan 1, 2023
1654500
MQTT Climate: Set the initial humidity to None
Stonos Jan 1, 2023
cade91e
MQTT Climate: Rename _set_mqtt_attribute to _set_climate_attribute an…
Stonos Jan 1, 2023
d41926c
MQTT Climate: Copy humidity range validation from MQTT Humidifier
Stonos Jan 1, 2023
39b0086
MQTT Climate: Remove CONF_HUMIDITY_INITIAL
Stonos Jan 2, 2023
925d87d
MQTT Climate: Only enable support for TARGET_HUMIDITY when the comman…
Stonos Jan 2, 2023
58d710a
MQTT Climate: Check if setting the target humidity is supported befor…
Stonos Jan 2, 2023
f29d2ca
MQTT Climate: Make sure that CONF_HUMIDITY_COMMAND_TOPIC has been con…
Stonos Jan 2, 2023
6edfe3b
MQTT Climate: Fix broken tests
Stonos Jan 2, 2023
c56806a
MQTT Climate: Add test for optimistically setting the target humidity
Stonos Jan 2, 2023
0051a2c
MQTT Climate: Remove references to "temperature" in handle_climate_at…
Stonos Jan 3, 2023
81ca8dc
MQTT Climate: Add additional humidity-related tests
Stonos Jan 3, 2023
f88efcf
MQTT Climate: Remove supported feature check in handle_target_humidit…
Stonos Jan 3, 2023
3410b31
MQTT Climate: Remove supported feature check in async_set_humidity
Stonos Jan 3, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions homeassistant/components/mqtt/abbreviations.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@
"cod_arm_req": "code_arm_required",
"cod_dis_req": "code_disarm_required",
"cod_trig_req": "code_trigger_required",
"curr_hum_t": "current_humidity_topic",
"curr_hum_tpl": "current_humidity_template",
"curr_temp_t": "current_temperature_topic",
"curr_temp_tpl": "current_temperature_template",
"dev": "device",
Expand Down
120 changes: 109 additions & 11 deletions homeassistant/components/mqtt/climate.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
ATTR_HVAC_MODE,
ATTR_TARGET_TEMP_HIGH,
ATTR_TARGET_TEMP_LOW,
DEFAULT_MAX_HUMIDITY,
DEFAULT_MAX_TEMP,
DEFAULT_MIN_HUMIDITY,
DEFAULT_MIN_TEMP,
FAN_AUTO,
FAN_HIGH,
Expand Down Expand Up @@ -85,6 +87,8 @@
CONF_AWAY_MODE_STATE_TEMPLATE = "away_mode_state_template"
CONF_AWAY_MODE_STATE_TOPIC = "away_mode_state_topic"

CONF_CURRENT_HUMIDITY_TEMPLATE = "current_humidity_template"
CONF_CURRENT_HUMIDITY_TOPIC = "current_humidity_topic"
CONF_CURRENT_TEMP_TEMPLATE = "current_temperature_template"
CONF_CURRENT_TEMP_TOPIC = "current_temperature_topic"
CONF_FAN_MODE_COMMAND_TEMPLATE = "fan_mode_command_template"
Expand All @@ -99,6 +103,12 @@
CONF_HOLD_STATE_TOPIC = "hold_state_topic"
CONF_HOLD_LIST = "hold_modes"

CONF_HUMIDITY_COMMAND_TEMPLATE = "target_humidity_command_template"
CONF_HUMIDITY_COMMAND_TOPIC = "target_humidity_command_topic"
CONF_HUMIDITY_STATE_TEMPLATE = "target_humidity_state_template"
CONF_HUMIDITY_STATE_TOPIC = "target_humidity_state_topic"
Comment thread
Stonos marked this conversation as resolved.
CONF_HUMIDITY_MAX = "max_humidity"
CONF_HUMIDITY_MIN = "min_humidity"
CONF_MODE_COMMAND_TEMPLATE = "mode_command_template"
CONF_MODE_COMMAND_TOPIC = "mode_command_topic"
CONF_MODE_LIST = "modes"
Expand Down Expand Up @@ -164,8 +174,10 @@

VALUE_TEMPLATE_KEYS = (
CONF_AUX_STATE_TEMPLATE,
CONF_CURRENT_HUMIDITY_TEMPLATE,
CONF_CURRENT_TEMP_TEMPLATE,
CONF_FAN_MODE_STATE_TEMPLATE,
CONF_HUMIDITY_STATE_TEMPLATE,
CONF_MODE_STATE_TEMPLATE,
CONF_POWER_STATE_TEMPLATE,
CONF_ACTION_TEMPLATE,
Expand All @@ -178,6 +190,7 @@

COMMAND_TEMPLATE_KEYS = {
CONF_FAN_MODE_COMMAND_TEMPLATE,
CONF_HUMIDITY_COMMAND_TEMPLATE,
CONF_MODE_COMMAND_TEMPLATE,
CONF_PRESET_MODE_COMMAND_TEMPLATE,
CONF_SWING_MODE_COMMAND_TEMPLATE,
Expand All @@ -191,9 +204,12 @@
CONF_ACTION_TOPIC,
CONF_AUX_COMMAND_TOPIC,
CONF_AUX_STATE_TOPIC,
CONF_CURRENT_HUMIDITY_TOPIC,
CONF_CURRENT_TEMP_TOPIC,
CONF_FAN_MODE_COMMAND_TOPIC,
CONF_FAN_MODE_STATE_TOPIC,
CONF_HUMIDITY_COMMAND_TOPIC,
CONF_HUMIDITY_STATE_TOPIC,
CONF_MODE_COMMAND_TOPIC,
CONF_MODE_STATE_TOPIC,
CONF_POWER_COMMAND_TOPIC,
Expand All @@ -218,11 +234,36 @@ def valid_preset_mode_configuration(config: ConfigType) -> ConfigType:
return config


def valid_humidity_range_configuration(config: ConfigType) -> ConfigType:
"""Validate that the target_humidity range configuration is valid, throws if it isn't."""
if config[CONF_HUMIDITY_MIN] >= config[CONF_HUMIDITY_MAX]:
raise ValueError("target_humidity_max must be > target_humidity_min")
if config[CONF_HUMIDITY_MAX] > 100:
raise ValueError("max_humidity must be <= 100")

return config


def valid_humidity_state_configuration(config: ConfigType) -> ConfigType:
"""Validate that if CONF_HUMIDITY_STATE_TOPIC is set then CONF_HUMIDITY_COMMAND_TOPIC is also set."""
if (
CONF_HUMIDITY_STATE_TOPIC in config
and CONF_HUMIDITY_COMMAND_TOPIC not in config
):
raise ValueError(
f"{CONF_HUMIDITY_STATE_TOPIC} cannot be used without {CONF_HUMIDITY_COMMAND_TOPIC}"
)

return config


_PLATFORM_SCHEMA_BASE = MQTT_BASE_SCHEMA.extend(
{
vol.Optional(CONF_AUX_COMMAND_TOPIC): valid_publish_topic,
vol.Optional(CONF_AUX_STATE_TEMPLATE): cv.template,
vol.Optional(CONF_AUX_STATE_TOPIC): valid_subscribe_topic,
vol.Optional(CONF_CURRENT_HUMIDITY_TEMPLATE): cv.template,
vol.Optional(CONF_CURRENT_HUMIDITY_TOPIC): valid_subscribe_topic,
Comment thread
Stonos marked this conversation as resolved.
vol.Optional(CONF_CURRENT_TEMP_TEMPLATE): cv.template,
vol.Optional(CONF_CURRENT_TEMP_TOPIC): valid_subscribe_topic,
vol.Optional(CONF_FAN_MODE_COMMAND_TEMPLATE): cv.template,
Expand All @@ -233,6 +274,16 @@ def valid_preset_mode_configuration(config: ConfigType) -> ConfigType:
): cv.ensure_list,
vol.Optional(CONF_FAN_MODE_STATE_TEMPLATE): cv.template,
vol.Optional(CONF_FAN_MODE_STATE_TOPIC): valid_subscribe_topic,
vol.Optional(CONF_HUMIDITY_COMMAND_TEMPLATE): cv.template,
vol.Optional(CONF_HUMIDITY_COMMAND_TOPIC): valid_publish_topic,
vol.Optional(CONF_HUMIDITY_MIN, default=DEFAULT_MIN_HUMIDITY): vol.Coerce(
float
),
vol.Optional(CONF_HUMIDITY_MAX, default=DEFAULT_MAX_HUMIDITY): vol.Coerce(
float
),
vol.Optional(CONF_HUMIDITY_STATE_TEMPLATE): cv.template,
vol.Optional(CONF_HUMIDITY_STATE_TOPIC): valid_subscribe_topic,
Comment thread
Stonos marked this conversation as resolved.
vol.Optional(CONF_MODE_COMMAND_TEMPLATE): cv.template,
vol.Optional(CONF_MODE_COMMAND_TOPIC): valid_publish_topic,
vol.Optional(
Expand Down Expand Up @@ -313,6 +364,8 @@ def valid_preset_mode_configuration(config: ConfigType) -> ConfigType:
cv.removed(CONF_HOLD_LIST),
_PLATFORM_SCHEMA_BASE,
valid_preset_mode_configuration,
valid_humidity_range_configuration,
valid_humidity_state_configuration,
)

# Configuring MQTT Climate under the climate platform key was deprecated in HA Core 2022.6
Expand All @@ -337,6 +390,8 @@ def valid_preset_mode_configuration(config: ConfigType) -> ConfigType:
cv.removed(CONF_HOLD_STATE_TOPIC),
cv.removed(CONF_HOLD_LIST),
valid_preset_mode_configuration,
valid_humidity_range_configuration,
valid_humidity_state_configuration,
)


Expand Down Expand Up @@ -396,6 +451,8 @@ def _setup_from_config(self, config: ConfigType) -> None:
self._attr_hvac_modes = config[CONF_MODE_LIST]
self._attr_min_temp = config[CONF_TEMP_MIN]
self._attr_max_temp = config[CONF_TEMP_MAX]
self._attr_min_humidity = config[CONF_HUMIDITY_MIN]
self._attr_max_humidity = config[CONF_HUMIDITY_MAX]
self._attr_precision = config.get(CONF_PRECISION, super().precision)
self._attr_fan_modes = config[CONF_FAN_MODE_LIST]
self._attr_swing_modes = config[CONF_SWING_MODE_LIST]
Expand Down Expand Up @@ -485,6 +542,9 @@ def _setup_from_config(self, config: ConfigType) -> None:
):
support |= ClimateEntityFeature.TARGET_TEMPERATURE_RANGE

if self._topic[CONF_HUMIDITY_COMMAND_TOPIC] is not None:
support |= ClimateEntityFeature.TARGET_HUMIDITY

if (self._topic[CONF_FAN_MODE_STATE_TOPIC] is not None) or (
self._topic[CONF_FAN_MODE_COMMAND_TOPIC] is not None
):
Expand Down Expand Up @@ -554,23 +614,23 @@ def handle_action_received(msg: ReceiveMessage) -> None:
add_subscription(topics, CONF_ACTION_TOPIC, handle_action_received)

@callback
def handle_temperature_received(
def handle_climate_attribute_received(
Comment thread
Stonos marked this conversation as resolved.
msg: ReceiveMessage, template_name: str, attr: str
) -> None:
"""Handle temperature coming via MQTT."""
"""Handle climate attributes coming via MQTT."""
payload = render_template(msg, template_name)

try:
setattr(self, attr, float(payload))
get_mqtt_data(self.hass).state_write_requests.write_state_request(self)
except ValueError:
_LOGGER.error("Could not parse temperature from %s", payload)
_LOGGER.error("Could not parse %s from %s", template_name, payload)

@callback
@log_messages(self.hass, self.entity_id)
def handle_current_temperature_received(msg: ReceiveMessage) -> None:
"""Handle current temperature coming via MQTT."""
handle_temperature_received(
handle_climate_attribute_received(
msg, CONF_CURRENT_TEMP_TEMPLATE, "_attr_current_temperature"
)

Expand All @@ -582,7 +642,7 @@ def handle_current_temperature_received(msg: ReceiveMessage) -> None:
@log_messages(self.hass, self.entity_id)
def handle_target_temperature_received(msg: ReceiveMessage) -> None:
"""Handle target temperature coming via MQTT."""
handle_temperature_received(
handle_climate_attribute_received(
msg, CONF_TEMP_STATE_TEMPLATE, "_attr_target_temperature"
)

Expand All @@ -594,7 +654,7 @@ def handle_target_temperature_received(msg: ReceiveMessage) -> None:
@log_messages(self.hass, self.entity_id)
def handle_temperature_low_received(msg: ReceiveMessage) -> None:
"""Handle target temperature low coming via MQTT."""
handle_temperature_received(
handle_climate_attribute_received(
msg, CONF_TEMP_LOW_STATE_TEMPLATE, "_attr_target_temperature_low"
)

Expand All @@ -606,14 +666,39 @@ def handle_temperature_low_received(msg: ReceiveMessage) -> None:
@log_messages(self.hass, self.entity_id)
def handle_temperature_high_received(msg: ReceiveMessage) -> None:
"""Handle target temperature high coming via MQTT."""
handle_temperature_received(
handle_climate_attribute_received(
msg, CONF_TEMP_HIGH_STATE_TEMPLATE, "_attr_target_temperature_high"
)

add_subscription(
topics, CONF_TEMP_HIGH_STATE_TOPIC, handle_temperature_high_received
)

@callback
@log_messages(self.hass, self.entity_id)
def handle_current_humidity_received(msg: ReceiveMessage) -> None:
"""Handle current humidity coming via MQTT."""
handle_climate_attribute_received(
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just for the record
It is okay to enable to setting the current humidity attribute, even if ClimateEntityFeature.TARGET_HUMIDITY is not set.

msg, CONF_CURRENT_HUMIDITY_TEMPLATE, "_attr_current_humidity"
)

add_subscription(
topics, CONF_CURRENT_HUMIDITY_TOPIC, handle_current_humidity_received
)

@callback
@log_messages(self.hass, self.entity_id)
def handle_target_humidity_received(msg: ReceiveMessage) -> None:
"""Handle target humidity coming via MQTT."""

handle_climate_attribute_received(
Comment thread
Stonos marked this conversation as resolved.
msg, CONF_HUMIDITY_STATE_TEMPLATE, "_attr_target_humidity"
)

add_subscription(
topics, CONF_HUMIDITY_STATE_TOPIC, handle_target_humidity_received
)

@callback
def handle_mode_received(
msg: ReceiveMessage, template_name: str, attr: str, mode_list: str
Expand Down Expand Up @@ -744,7 +829,7 @@ async def _publish(self, topic: str, payload: PublishPayloadType) -> None:
self._config[CONF_ENCODING],
)

async def _set_temperature(
async def _set_climate_attribute(
self,
temp: float | None,
cmnd_topic: str,
Expand All @@ -770,23 +855,23 @@ async def async_set_temperature(self, **kwargs: Any) -> None:
if (operation_mode := kwargs.get(ATTR_HVAC_MODE)) is not None:
await self.async_set_hvac_mode(operation_mode)

changed = await self._set_temperature(
changed = await self._set_climate_attribute(
kwargs.get(ATTR_TEMPERATURE),
CONF_TEMP_COMMAND_TOPIC,
CONF_TEMP_COMMAND_TEMPLATE,
CONF_TEMP_STATE_TOPIC,
"_attr_target_temperature",
)

changed |= await self._set_temperature(
changed |= await self._set_climate_attribute(
kwargs.get(ATTR_TARGET_TEMP_LOW),
CONF_TEMP_LOW_COMMAND_TOPIC,
CONF_TEMP_LOW_COMMAND_TEMPLATE,
CONF_TEMP_LOW_STATE_TOPIC,
"_attr_target_temperature_low",
)

changed |= await self._set_temperature(
changed |= await self._set_climate_attribute(
kwargs.get(ATTR_TARGET_TEMP_HIGH),
CONF_TEMP_HIGH_COMMAND_TOPIC,
CONF_TEMP_HIGH_COMMAND_TEMPLATE,
Expand All @@ -798,6 +883,19 @@ async def async_set_temperature(self, **kwargs: Any) -> None:
return
self.async_write_ha_state()

async def async_set_humidity(self, humidity: int) -> None:
"""Set new target humidity."""

await self._set_climate_attribute(
Comment thread
Stonos marked this conversation as resolved.
humidity,
CONF_HUMIDITY_COMMAND_TOPIC,
CONF_HUMIDITY_COMMAND_TEMPLATE,
CONF_HUMIDITY_STATE_TOPIC,
"_attr_target_humidity",
)

self.async_write_ha_state()

async def async_set_swing_mode(self, swing_mode: str) -> None:
"""Set new swing mode."""
payload = self._command_templates[CONF_SWING_MODE_COMMAND_TEMPLATE](swing_mode)
Expand Down
Loading