diff --git a/homeassistant/components/shelly/utils.py b/homeassistant/components/shelly/utils.py index a824488327b0..a01b5de133a4 100644 --- a/homeassistant/components/shelly/utils.py +++ b/homeassistant/components/shelly/utils.py @@ -125,15 +125,22 @@ def get_block_channel_name(device: BlockDevice, block: Block | None) -> str: return f"{entity_name} channel {chr(int(block.channel)+base)}" -def is_block_momentary_input(settings: dict[str, Any], block: Block) -> bool: +def is_block_momentary_input( + settings: dict[str, Any], block: Block, include_detached: bool = False +) -> bool: """Return true if block input button settings is set to a momentary type.""" + momentary_types = ["momentary", "momentary_on_release"] + + if include_detached: + momentary_types.append("detached") + # Shelly Button type is fixed to momentary and no btn_type if settings["device"]["type"] in SHBTN_MODELS: return True if settings.get("mode") == "roller": button_type = settings["rollers"][0]["button_type"] - return button_type in ["momentary", "momentary_on_release"] + return button_type in momentary_types button = settings.get("relays") or settings.get("lights") or settings.get("inputs") if button is None: @@ -148,7 +155,7 @@ def is_block_momentary_input(settings: dict[str, Any], block: Block) -> bool: channel = min(int(block.channel or 0), len(button) - 1) button_type = button[channel].get("btn_type") - return button_type in ["momentary", "momentary_on_release"] + return button_type in momentary_types def get_device_uptime(uptime: float, last_uptime: datetime | None) -> datetime: @@ -171,7 +178,7 @@ def get_block_input_triggers( if "inputEvent" not in block.sensor_ids or "inputEventCnt" not in block.sensor_ids: return [] - if not is_block_momentary_input(device.settings, block): + if not is_block_momentary_input(device.settings, block, True): return [] triggers = [] diff --git a/tests/components/shelly/test_device_trigger.py b/tests/components/shelly/test_device_trigger.py index aee171eb46e9..0532fa5c82ca 100644 --- a/tests/components/shelly/test_device_trigger.py +++ b/tests/components/shelly/test_device_trigger.py @@ -29,25 +29,48 @@ from tests.common import ( ) -async def test_get_triggers_block_device(hass, coap_wrapper): +@pytest.mark.parametrize( + "button_type, is_valid", + [ + ("momentary", True), + ("momentary_on_release", True), + ("detached", True), + ("toggle", False), + ], +) +async def test_get_triggers_block_device( + hass, coap_wrapper, monkeypatch, button_type, is_valid +): """Test we get the expected triggers from a shelly block device.""" assert coap_wrapper - expected_triggers = [ - { - CONF_PLATFORM: "device", - CONF_DEVICE_ID: coap_wrapper.device_id, - CONF_DOMAIN: DOMAIN, - CONF_TYPE: "single", - CONF_SUBTYPE: "button1", - }, - { - CONF_PLATFORM: "device", - CONF_DEVICE_ID: coap_wrapper.device_id, - CONF_DOMAIN: DOMAIN, - CONF_TYPE: "long", - CONF_SUBTYPE: "button1", - }, - ] + + monkeypatch.setitem( + coap_wrapper.device.settings, + "relays", + [ + {"btn_type": button_type}, + {"btn_type": "toggle"}, + ], + ) + + expected_triggers = [] + if is_valid: + expected_triggers = [ + { + CONF_PLATFORM: "device", + CONF_DEVICE_ID: coap_wrapper.device_id, + CONF_DOMAIN: DOMAIN, + CONF_TYPE: "single", + CONF_SUBTYPE: "button1", + }, + { + CONF_PLATFORM: "device", + CONF_DEVICE_ID: coap_wrapper.device_id, + CONF_DOMAIN: DOMAIN, + CONF_TYPE: "long", + CONF_SUBTYPE: "button1", + }, + ] triggers = await async_get_device_automations( hass, DeviceAutomationType.TRIGGER, coap_wrapper.device_id