Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Only accept valid hvac actions sent via mqtt #59919

Merged
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 10 additions & 3 deletions homeassistant/components/mqtt/climate.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
ATTR_HVAC_MODE,
ATTR_TARGET_TEMP_HIGH,
ATTR_TARGET_TEMP_LOW,
CURRENT_HVAC_ACTIONS,
DEFAULT_MAX_TEMP,
DEFAULT_MIN_TEMP,
FAN_AUTO,
Expand Down Expand Up @@ -405,9 +406,15 @@ def render_template(msg, template_name):
def handle_action_received(msg):
"""Handle receiving action via MQTT."""
payload = render_template(msg, CONF_ACTION_TEMPLATE)

self._action = payload
self.async_write_ha_state()
if payload is None or payload in CURRENT_HVAC_ACTIONS:
Copy link
Contributor

Choose a reason for hiding this comment

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

First of all, it's not clear from the documentation that the action should be set to None.
Is that allowed?

We probably also need a special payload which will be interpreted as None when templates are not used, if it's allowed to set the action to None. This is used in MQTT fan to reset the percentage to None: https://www.home-assistant.io/integrations/fan.mqtt/#payload_reset_percentage

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I basically just added None there based on @bdraco's comment. Setting None via async_fire_mqtt_message doesn't work apparently(?).

Anyway, based on the doc, would it make sense to have the unit test run something along the lines of
assert all(elem in actions for elem in CURRENT_HVAC_ACTIONS) in order to catch future edits of CURRENT_HVAC_ACTIONS?

Copy link
Contributor

Choose a reason for hiding this comment

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

It's not possible to receive None directly over MQTT, an empty MQTT payload will be interpreted as the empty string.
If it's necessary to allow resetting the action to None, we should enable that functionality in the same way as for MQTT fan.
If it's not necessary, drop the None-check.

Your idea for a test seems reasonable.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

tbh I am not sure None is a valid action that can be set given the description of HVAC action. I suppose None really just makes sense as the initial value to be interpreted as "unknown" - which no further update should result in? Unless a device is paired/reset again maybe.
I removed the None check for now given your remarks and added the assert all

Copy link
Member

Choose a reason for hiding this comment

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

For clarity

The explicit None is a valid value hvac_action
The string "None" is not a valid value for hvac_action. (which is what was seen in the HomeKit PR)

Copy link
Contributor

Choose a reason for hiding this comment

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

In my mind, if we allow None as a HVAC action (or perhaps rather the absence of a HVAC action) , we should:

There's a somewhat related discussion about Fan, about no longer supporting None: #59688

Copy link
Contributor Author

Choose a reason for hiding this comment

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

For reference, https://www.home-assistant.io/integrations/climate.mqtt#action_topic currently only lists the 6 valid values checked in this PR.
Based on @bdraco's comment above, we could default to None (not "None") upon receiving an invalid value since there is some unknown state not handled by HA in place until further notice (similar to what the state within HA is upon startup, thus being a valid case?).
However, any side-effects of doing so are sorta unknown I guess? A warning should be logged in that case imho

Copy link
Member

Choose a reason for hiding this comment

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

If None isn't actually supported now there is no need to add it. It would also be perfectly reasonable to reject invalid values instead of setting them to None

Copy link
Contributor Author

Choose a reason for hiding this comment

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

So, how should we proceed? Take this PR as the bugfix that it is intended to be and not accept values not documented or extend it to allow some variation of None?

Copy link
Contributor

Choose a reason for hiding this comment

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

I think None is only valid as an initial state. There is no reason to reset it later e.g. if the device becomes unavailable. This is different from the MQTT fan where speed percentage and preset_mode state can become invalid, e.g. when a fan has one mode auto, and setting a fixed speed percentage will reset this mode.

Further: The value template will return None if a value not could be parsed, we should not use this for other means. I think this PR is fine, but I would log a warning, instead of an error.

self._action = payload
self.async_write_ha_state()
else:
_LOGGER.error(
Grennith marked this conversation as resolved.
Show resolved Hide resolved
"Invalid %s action: %s",
CURRENT_HVAC_ACTIONS,
payload,
)

add_subscription(topics, CONF_ACTION_TOPIC, handle_action_received)

Expand Down
41 changes: 24 additions & 17 deletions tests/components/mqtt/test_climate.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

from homeassistant.components.climate import DEFAULT_MAX_TEMP, DEFAULT_MIN_TEMP
from homeassistant.components.climate.const import (
ATTR_HVAC_ACTION,
DOMAIN as CLIMATE_DOMAIN,
HVAC_MODE_AUTO,
HVAC_MODE_COOL,
Expand Down Expand Up @@ -432,6 +433,27 @@ async def test_receive_mqtt_temperature(hass, mqtt_mock):
assert state.attributes.get("current_temperature") == 47


async def test_handle_action_received(hass, mqtt_mock):
"""Test getting the action received via MQTT."""
config = copy.deepcopy(DEFAULT_CONFIG)
config["climate"]["action_topic"] = "action"
assert await async_setup_component(hass, CLIMATE_DOMAIN, config)
await hass.async_block_till_done()

# Cycle through valid modes and also check for wrong input such as "None" (str(None))
async_fire_mqtt_message(hass, "action", "None")
state = hass.states.get(ENTITY_CLIMATE)
hvac_action = state.attributes.get(ATTR_HVAC_ACTION)
assert hvac_action is None
# Redefine actions according to https://developers.home-assistant.io/docs/core/entity/climate/#hvac-action
actions = ["off", "heating", "cooling", "drying", "idle", "fan"]
for action in actions:
async_fire_mqtt_message(hass, "action", action)
state = hass.states.get(ENTITY_CLIMATE)
hvac_action = state.attributes.get(ATTR_HVAC_ACTION)
assert hvac_action == action


async def test_set_away_mode_pessimistic(hass, mqtt_mock):
"""Test setting of the away mode."""
config = copy.deepcopy(DEFAULT_CONFIG)
Expand Down Expand Up @@ -492,21 +514,6 @@ async def test_set_away_mode(hass, mqtt_mock):
assert state.attributes.get("preset_mode") == "away"


async def test_set_hvac_action(hass, mqtt_mock):
"""Test setting of the HVAC action."""
config = copy.deepcopy(DEFAULT_CONFIG)
config["climate"]["action_topic"] = "action"
assert await async_setup_component(hass, CLIMATE_DOMAIN, config)
await hass.async_block_till_done()

state = hass.states.get(ENTITY_CLIMATE)
assert state.attributes.get("hvac_action") is None

async_fire_mqtt_message(hass, "action", "cool")
state = hass.states.get(ENTITY_CLIMATE)
assert state.attributes.get("hvac_action") == "cool"


async def test_set_hold_pessimistic(hass, mqtt_mock):
"""Test setting the hold mode in pessimistic mode."""
config = copy.deepcopy(DEFAULT_CONFIG)
Expand Down Expand Up @@ -779,9 +786,9 @@ async def test_get_with_templates(hass, mqtt_mock, caplog):
assert state.attributes.get("current_temperature") == 74656

# Action
async_fire_mqtt_message(hass, "action", '"cool"')
async_fire_mqtt_message(hass, "action", '"cooling"')
state = hass.states.get(ENTITY_CLIMATE)
assert state.attributes.get("hvac_action") == "cool"
assert state.attributes.get("hvac_action") == "cooling"


async def test_set_with_templates(hass, mqtt_mock, caplog):
Expand Down