From c65a50bd2e40af76b01669c9d38c3916df59f0bd Mon Sep 17 00:00:00 2001 From: Shay Levy Date: Fri, 31 Dec 2021 19:47:03 +0200 Subject: [PATCH] Fix Shelly error fetching device triggers for sleeping devices (#63103) --- .../components/shelly/device_trigger.py | 3 ++ .../components/shelly/test_device_trigger.py | 36 +++++++++++++++++++ 2 files changed, 39 insertions(+) diff --git a/homeassistant/components/shelly/device_trigger.py b/homeassistant/components/shelly/device_trigger.py index f5abf76e8f2..3e839507127 100644 --- a/homeassistant/components/shelly/device_trigger.py +++ b/homeassistant/components/shelly/device_trigger.py @@ -123,6 +123,9 @@ async def async_get_triggers( append_input_triggers(triggers, input_triggers, device_id) return triggers + if not block_wrapper.device.initialized: + return triggers + assert block_wrapper.device.blocks for block in block_wrapper.device.blocks: diff --git a/tests/components/shelly/test_device_trigger.py b/tests/components/shelly/test_device_trigger.py index 32fff324b55..aee171eb46e 100644 --- a/tests/components/shelly/test_device_trigger.py +++ b/tests/components/shelly/test_device_trigger.py @@ -169,6 +169,42 @@ async def test_get_triggers_button(hass): assert_lists_same(triggers, expected_triggers) +async def test_get_triggers_non_initialized_devices(hass): + """Test we get the empty triggers for non-initialized devices.""" + await async_setup_component(hass, "shelly", {}) + + config_entry = MockConfigEntry( + domain=DOMAIN, + data={"sleep_period": 43200, "model": "SHDW-2", "host": "1.2.3.4"}, + unique_id="12345678", + ) + config_entry.add_to_hass(hass) + + device = Mock( + blocks=None, + settings=None, + shelly=None, + update=AsyncMock(), + initialized=False, + ) + + hass.data[DOMAIN] = {DATA_CONFIG_ENTRY: {}} + hass.data[DOMAIN][DATA_CONFIG_ENTRY][config_entry.entry_id] = {} + coap_wrapper = hass.data[DOMAIN][DATA_CONFIG_ENTRY][config_entry.entry_id][ + BLOCK + ] = BlockDeviceWrapper(hass, config_entry, device) + + coap_wrapper.async_setup() + + expected_triggers = [] + + triggers = await async_get_device_automations( + hass, DeviceAutomationType.TRIGGER, coap_wrapper.device_id + ) + + assert_lists_same(triggers, expected_triggers) + + async def test_get_triggers_for_invalid_device_id(hass, device_reg, coap_wrapper): """Test error raised for invalid shelly device_id.""" assert coap_wrapper