From 34a588d1ba2de83ba9163a54a86ac14e7a2de652 Mon Sep 17 00:00:00 2001 From: Shay Levy Date: Sun, 25 Apr 2021 07:47:18 +0300 Subject: [PATCH] Fix Shelly button first trigger (#49635) --- homeassistant/components/shelly/__init__.py | 12 ++++++++++++ homeassistant/components/shelly/const.py | 4 +++- homeassistant/components/shelly/device_trigger.py | 7 ++++--- homeassistant/components/shelly/utils.py | 9 +++++---- 4 files changed, 24 insertions(+), 8 deletions(-) diff --git a/homeassistant/components/shelly/__init__.py b/homeassistant/components/shelly/__init__.py index be87e2556eb..1e68ca78409 100644 --- a/homeassistant/components/shelly/__init__.py +++ b/homeassistant/components/shelly/__init__.py @@ -33,6 +33,7 @@ from .const import ( POLLING_TIMEOUT_SEC, REST, REST_SENSORS_UPDATE_INTERVAL, + SHBTN_MODELS, SLEEP_PERIOD_MULTIPLIER, UPDATE_PERIOD_MULTIPLIER, ) @@ -181,6 +182,17 @@ class ShellyDeviceWrapper(update_coordinator.DataUpdateCoordinator): if not self.device.initialized: return + # For buttons which are battery powered - set initial value for last_event_count + if self.model in SHBTN_MODELS and self._last_input_events_count.get(1) is None: + for block in self.device.blocks: + if block.type != "device": + continue + + if block.wakeupEvent[0] == "button": + self._last_input_events_count[1] = -1 + + break + # Check for input events for block in self.device.blocks: if ( diff --git a/homeassistant/components/shelly/const.py b/homeassistant/components/shelly/const.py index 4fda656e7b4..2609b7cd57f 100644 --- a/homeassistant/components/shelly/const.py +++ b/homeassistant/components/shelly/const.py @@ -49,7 +49,7 @@ BASIC_INPUTS_EVENTS_TYPES = { "long", } -SHBTN_1_INPUTS_EVENTS_TYPES = { +SHBTN_INPUTS_EVENTS_TYPES = { "single", "double", "triple", @@ -72,6 +72,8 @@ INPUTS_EVENTS_SUBTYPES = { "button3": 3, } +SHBTN_MODELS = ["SHBTN-1", "SHBTN-2"] + # Kelvin value for colorTemp KELVIN_MAX_VALUE = 6500 KELVIN_MIN_VALUE_WHITE = 2700 diff --git a/homeassistant/components/shelly/device_trigger.py b/homeassistant/components/shelly/device_trigger.py index 97938040543..05f806dd8e8 100644 --- a/homeassistant/components/shelly/device_trigger.py +++ b/homeassistant/components/shelly/device_trigger.py @@ -27,7 +27,8 @@ from .const import ( DOMAIN, EVENT_SHELLY_CLICK, INPUTS_EVENTS_SUBTYPES, - SHBTN_1_INPUTS_EVENTS_TYPES, + SHBTN_INPUTS_EVENTS_TYPES, + SHBTN_MODELS, SUPPORTED_INPUTS_EVENTS_TYPES, ) from .utils import get_device_wrapper, get_input_triggers @@ -69,8 +70,8 @@ async def async_get_triggers(hass: HomeAssistant, device_id: str) -> list[dict]: if not wrapper: raise InvalidDeviceAutomationConfig(f"Device not found: {device_id}") - if wrapper.model in ("SHBTN-1", "SHBTN-2"): - for trigger in SHBTN_1_INPUTS_EVENTS_TYPES: + if wrapper.model in SHBTN_MODELS: + for trigger in SHBTN_INPUTS_EVENTS_TYPES: triggers.append( { CONF_PLATFORM: "device", diff --git a/homeassistant/components/shelly/utils.py b/homeassistant/components/shelly/utils.py index 126491f65c1..39134957fb9 100644 --- a/homeassistant/components/shelly/utils.py +++ b/homeassistant/components/shelly/utils.py @@ -16,7 +16,8 @@ from .const import ( COAP, DATA_CONFIG_ENTRY, DOMAIN, - SHBTN_1_INPUTS_EVENTS_TYPES, + SHBTN_INPUTS_EVENTS_TYPES, + SHBTN_MODELS, SHIX3_1_INPUTS_EVENTS_TYPES, ) @@ -111,7 +112,7 @@ def get_device_channel_name( def is_momentary_input(settings: dict, block: aioshelly.Block) -> bool: """Return true if input button settings is set to a momentary type.""" # Shelly Button type is fixed to momentary and no btn_type - if settings["device"]["type"] in ("SHBTN-1", "SHBTN-2"): + if settings["device"]["type"] in SHBTN_MODELS: return True button = settings.get("relays") or settings.get("lights") or settings.get("inputs") @@ -158,8 +159,8 @@ def get_input_triggers( else: subtype = f"button{int(block.channel)+1}" - if device.settings["device"]["type"] in ("SHBTN-1", "SHBTN-2"): - trigger_types = SHBTN_1_INPUTS_EVENTS_TYPES + if device.settings["device"]["type"] in SHBTN_MODELS: + trigger_types = SHBTN_INPUTS_EVENTS_TYPES elif device.settings["device"]["type"] == "SHIX3-1": trigger_types = SHIX3_1_INPUTS_EVENTS_TYPES else: