1
mirror of https://github.com/home-assistant/core synced 2024-07-12 07:21:24 +02:00

Fix Shelly detached switches automation triggers (#65059)

This commit is contained in:
Shay Levy 2022-01-28 18:30:44 +02:00 committed by GitHub
parent cf6b3fc810
commit efbbef5922
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 51 additions and 21 deletions

View File

@ -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 = []

View File

@ -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