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鈥檒l 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 all 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 in CURRENT_HVAC_ACTIONS:
self._action = payload
self.async_write_ha_state()
else:
_LOGGER.warning(
"Invalid %s action: %s",
CURRENT_HVAC_ACTIONS,
payload,
)

add_subscription(topics, CONF_ACTION_TOPIC, handle_action_received)

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

from homeassistant.components.climate import DEFAULT_MAX_TEMP, DEFAULT_MIN_TEMP
from homeassistant.components.climate.const import (
ATTR_HVAC_ACTION,
CURRENT_HVAC_ACTIONS,
DOMAIN as CLIMATE_DOMAIN,
HVAC_MODE_AUTO,
HVAC_MODE_COOL,
Expand Down Expand Up @@ -432,6 +434,28 @@ 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"]
assert all(elem in actions for elem in CURRENT_HVAC_ACTIONS)
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 +516,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 +788,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