From 9a5dc848c9ab772821b521dd4f89e2b63b21abc7 Mon Sep 17 00:00:00 2001 From: SukramJ Date: Mon, 25 Nov 2019 20:12:01 +0100 Subject: [PATCH] Fix climate device actions (#28660) * limit climate device actions * update test * use supported_features for device_action * Fix tests * user support_features for device_condition --- .../components/climate/device_action.py | 17 +++++----- .../components/climate/device_condition.py | 2 +- .../components/climate/test_device_action.py | 24 ++++++++++++++ .../climate/test_device_condition.py | 33 +++++++++++++++++++ 4 files changed, 67 insertions(+), 9 deletions(-) diff --git a/homeassistant/components/climate/device_action.py b/homeassistant/components/climate/device_action.py index b53109f69cb..836e2277461 100644 --- a/homeassistant/components/climate/device_action.py +++ b/homeassistant/components/climate/device_action.py @@ -59,14 +59,15 @@ async def async_get_actions(hass: HomeAssistant, device_id: str) -> List[dict]: CONF_TYPE: "set_hvac_mode", } ) - actions.append( - { - CONF_DEVICE_ID: device_id, - CONF_DOMAIN: DOMAIN, - CONF_ENTITY_ID: entry.entity_id, - CONF_TYPE: "set_preset_mode", - } - ) + if state.attributes["supported_features"] & const.SUPPORT_PRESET_MODE: + actions.append( + { + CONF_DEVICE_ID: device_id, + CONF_DOMAIN: DOMAIN, + CONF_ENTITY_ID: entry.entity_id, + CONF_TYPE: "set_preset_mode", + } + ) return actions diff --git a/homeassistant/components/climate/device_condition.py b/homeassistant/components/climate/device_condition.py index c923f3123f1..3a075233942 100644 --- a/homeassistant/components/climate/device_condition.py +++ b/homeassistant/components/climate/device_condition.py @@ -61,7 +61,7 @@ async def async_get_conditions( } ) - if state and const.ATTR_PRESET_MODES in state.attributes: + if state and state.attributes["supported_features"] & const.SUPPORT_PRESET_MODE: conditions.append( { CONF_CONDITION: "device", diff --git a/tests/components/climate/test_device_action.py b/tests/components/climate/test_device_action.py index 3eb1f38ec41..46e8b3395c4 100644 --- a/tests/components/climate/test_device_action.py +++ b/tests/components/climate/test_device_action.py @@ -39,6 +39,7 @@ async def test_get_actions(hass, device_reg, entity_reg): ) entity_reg.async_get_or_create(DOMAIN, "test", "5678", device_id=device_entry.id) hass.states.async_set("climate.test_5678", const.HVAC_MODE_COOL, {}) + hass.states.async_set("climate.test_5678", "attributes", {"supported_features": 17}) expected_actions = [ { "domain": DOMAIN, @@ -57,6 +58,29 @@ async def test_get_actions(hass, device_reg, entity_reg): assert_lists_same(actions, expected_actions) +async def test_get_action_hvac_only(hass, device_reg, entity_reg): + """Test we get the expected actions from a climate.""" + config_entry = MockConfigEntry(domain="test", data={}) + config_entry.add_to_hass(hass) + device_entry = device_reg.async_get_or_create( + config_entry_id=config_entry.entry_id, + connections={(device_registry.CONNECTION_NETWORK_MAC, "12:34:56:AB:CD:EF")}, + ) + entity_reg.async_get_or_create(DOMAIN, "test", "5678", device_id=device_entry.id) + hass.states.async_set("climate.test_5678", const.HVAC_MODE_COOL, {}) + hass.states.async_set("climate.test_5678", "attributes", {"supported_features": 1}) + expected_actions = [ + { + "domain": DOMAIN, + "type": "set_hvac_mode", + "device_id": device_entry.id, + "entity_id": "climate.test_5678", + }, + ] + actions = await async_get_device_automations(hass, "action", device_entry.id) + assert_lists_same(actions, expected_actions) + + async def test_action(hass): """Test for actions.""" hass.states.async_set( diff --git a/tests/components/climate/test_device_condition.py b/tests/components/climate/test_device_condition.py index 82b6f595fb0..b0a9c6c283a 100644 --- a/tests/components/climate/test_device_condition.py +++ b/tests/components/climate/test_device_condition.py @@ -53,6 +53,7 @@ async def test_get_conditions(hass, device_reg, entity_reg): const.ATTR_PRESET_MODES: [const.PRESET_HOME, const.PRESET_AWAY], }, ) + hass.states.async_set("climate.test_5678", "attributes", {"supported_features": 17}) expected_conditions = [ { "condition": "device", @@ -73,6 +74,38 @@ async def test_get_conditions(hass, device_reg, entity_reg): assert_lists_same(conditions, expected_conditions) +async def test_get_conditions_hvac_only(hass, device_reg, entity_reg): + """Test we get the expected conditions from a climate.""" + config_entry = MockConfigEntry(domain="test", data={}) + config_entry.add_to_hass(hass) + device_entry = device_reg.async_get_or_create( + config_entry_id=config_entry.entry_id, + connections={(device_registry.CONNECTION_NETWORK_MAC, "12:34:56:AB:CD:EF")}, + ) + entity_reg.async_get_or_create(DOMAIN, "test", "5678", device_id=device_entry.id) + hass.states.async_set( + f"{DOMAIN}.test_5678", + const.HVAC_MODE_COOL, + { + const.ATTR_HVAC_MODE: const.HVAC_MODE_COOL, + const.ATTR_PRESET_MODE: const.PRESET_AWAY, + const.ATTR_PRESET_MODES: [const.PRESET_HOME, const.PRESET_AWAY], + }, + ) + hass.states.async_set("climate.test_5678", "attributes", {"supported_features": 1}) + expected_conditions = [ + { + "condition": "device", + "domain": DOMAIN, + "type": "is_hvac_mode", + "device_id": device_entry.id, + "entity_id": f"{DOMAIN}.test_5678", + } + ] + conditions = await async_get_device_automations(hass, "condition", device_entry.id) + assert_lists_same(conditions, expected_conditions) + + async def test_if_state(hass, calls): """Test for turn_on and turn_off conditions.""" hass.states.async_set(