Add switch flex button support. (#137524)

This commit is contained in:
Matrix 2025-02-19 02:23:27 +08:00 committed by GitHub
parent 8dd1e9d101
commit e6217efcd6
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 37 additions and 21 deletions

View File

@ -7,7 +7,7 @@ from dataclasses import dataclass
from datetime import timedelta from datetime import timedelta
from typing import Any from typing import Any
from yolink.const import ATTR_DEVICE_SMART_REMOTER from yolink.const import ATTR_DEVICE_SMART_REMOTER, ATTR_DEVICE_SWITCH
from yolink.device import YoLinkDevice from yolink.device import YoLinkDevice
from yolink.exception import YoLinkAuthFailError, YoLinkClientError from yolink.exception import YoLinkAuthFailError, YoLinkClientError
from yolink.home_manager import YoLinkHome from yolink.home_manager import YoLinkHome
@ -75,7 +75,8 @@ class YoLinkHomeMessageListener(MessageListener):
device_coordinator.async_set_updated_data(msg_data) device_coordinator.async_set_updated_data(msg_data)
# handling events # handling events
if ( if (
device_coordinator.device.device_type == ATTR_DEVICE_SMART_REMOTER device_coordinator.device.device_type
in [ATTR_DEVICE_SMART_REMOTER, ATTR_DEVICE_SWITCH]
and msg_data.get("event") is not None and msg_data.get("event") is not None
): ):
device_registry = dr.async_get(self._hass) device_registry = dr.async_get(self._hass)

View File

@ -33,3 +33,7 @@ DEV_MODEL_PLUG_YS6602_UC = "YS6602-UC"
DEV_MODEL_PLUG_YS6602_EC = "YS6602-EC" DEV_MODEL_PLUG_YS6602_EC = "YS6602-EC"
DEV_MODEL_PLUG_YS6803_UC = "YS6803-UC" DEV_MODEL_PLUG_YS6803_UC = "YS6803-UC"
DEV_MODEL_PLUG_YS6803_EC = "YS6803-EC" DEV_MODEL_PLUG_YS6803_EC = "YS6803-EC"
DEV_MODEL_SWITCH_YS5708_UC = "YS5708-UC"
DEV_MODEL_SWITCH_YS5708_EC = "YS5708-EC"
DEV_MODEL_SWITCH_YS5709_UC = "YS5709-UC"
DEV_MODEL_SWITCH_YS5709_EC = "YS5709-EC"

View File

@ -5,7 +5,7 @@ from __future__ import annotations
from typing import Any from typing import Any
import voluptuous as vol import voluptuous as vol
from yolink.const import ATTR_DEVICE_SMART_REMOTER from yolink.const import ATTR_DEVICE_SMART_REMOTER, ATTR_DEVICE_SWITCH
from homeassistant.components.device_automation import DEVICE_TRIGGER_BASE_SCHEMA from homeassistant.components.device_automation import DEVICE_TRIGGER_BASE_SCHEMA
from homeassistant.components.homeassistant.triggers import event as event_trigger from homeassistant.components.homeassistant.triggers import event as event_trigger
@ -21,6 +21,10 @@ from .const import (
DEV_MODEL_FLEX_FOB_YS3604_UC, DEV_MODEL_FLEX_FOB_YS3604_UC,
DEV_MODEL_FLEX_FOB_YS3614_EC, DEV_MODEL_FLEX_FOB_YS3614_EC,
DEV_MODEL_FLEX_FOB_YS3614_UC, DEV_MODEL_FLEX_FOB_YS3614_UC,
DEV_MODEL_SWITCH_YS5708_EC,
DEV_MODEL_SWITCH_YS5708_UC,
DEV_MODEL_SWITCH_YS5709_EC,
DEV_MODEL_SWITCH_YS5709_UC,
) )
CONF_BUTTON_1 = "button_1" CONF_BUTTON_1 = "button_1"
@ -30,7 +34,7 @@ CONF_BUTTON_4 = "button_4"
CONF_SHORT_PRESS = "short_press" CONF_SHORT_PRESS = "short_press"
CONF_LONG_PRESS = "long_press" CONF_LONG_PRESS = "long_press"
FLEX_FOB_4_BUTTONS = { FLEX_BUTTONS_4 = {
f"{CONF_BUTTON_1}_{CONF_SHORT_PRESS}", f"{CONF_BUTTON_1}_{CONF_SHORT_PRESS}",
f"{CONF_BUTTON_1}_{CONF_LONG_PRESS}", f"{CONF_BUTTON_1}_{CONF_LONG_PRESS}",
f"{CONF_BUTTON_2}_{CONF_SHORT_PRESS}", f"{CONF_BUTTON_2}_{CONF_SHORT_PRESS}",
@ -41,7 +45,7 @@ FLEX_FOB_4_BUTTONS = {
f"{CONF_BUTTON_4}_{CONF_LONG_PRESS}", f"{CONF_BUTTON_4}_{CONF_LONG_PRESS}",
} }
FLEX_FOB_2_BUTTONS = { FLEX_BUTTONS_2 = {
f"{CONF_BUTTON_1}_{CONF_SHORT_PRESS}", f"{CONF_BUTTON_1}_{CONF_SHORT_PRESS}",
f"{CONF_BUTTON_1}_{CONF_LONG_PRESS}", f"{CONF_BUTTON_1}_{CONF_LONG_PRESS}",
f"{CONF_BUTTON_2}_{CONF_SHORT_PRESS}", f"{CONF_BUTTON_2}_{CONF_SHORT_PRESS}",
@ -49,16 +53,19 @@ FLEX_FOB_2_BUTTONS = {
} }
TRIGGER_SCHEMA = DEVICE_TRIGGER_BASE_SCHEMA.extend( TRIGGER_SCHEMA = DEVICE_TRIGGER_BASE_SCHEMA.extend(
{vol.Required(CONF_TYPE): vol.In(FLEX_FOB_4_BUTTONS)} {vol.Required(CONF_TYPE): vol.In(FLEX_BUTTONS_4)}
) )
# YoLink Remotes YS3604/YS3614, Switch YS5708/YS5709
# YoLink Remotes YS3604/YS3614 TRIGGER_MAPPINGS: dict[str, set[str]] = {
FLEX_FOB_TRIGGER_TYPES: dict[str, set[str]] = { DEV_MODEL_FLEX_FOB_YS3604_EC: FLEX_BUTTONS_4,
DEV_MODEL_FLEX_FOB_YS3604_EC: FLEX_FOB_4_BUTTONS, DEV_MODEL_FLEX_FOB_YS3604_UC: FLEX_BUTTONS_4,
DEV_MODEL_FLEX_FOB_YS3604_UC: FLEX_FOB_4_BUTTONS, DEV_MODEL_FLEX_FOB_YS3614_UC: FLEX_BUTTONS_2,
DEV_MODEL_FLEX_FOB_YS3614_UC: FLEX_FOB_2_BUTTONS, DEV_MODEL_FLEX_FOB_YS3614_EC: FLEX_BUTTONS_2,
DEV_MODEL_FLEX_FOB_YS3614_EC: FLEX_FOB_2_BUTTONS, DEV_MODEL_SWITCH_YS5708_EC: FLEX_BUTTONS_2,
DEV_MODEL_SWITCH_YS5708_UC: FLEX_BUTTONS_2,
DEV_MODEL_SWITCH_YS5709_EC: FLEX_BUTTONS_2,
DEV_MODEL_SWITCH_YS5709_UC: FLEX_BUTTONS_2,
} }
@ -68,9 +75,12 @@ async def async_get_triggers(
"""List device triggers for YoLink devices.""" """List device triggers for YoLink devices."""
device_registry = dr.async_get(hass) device_registry = dr.async_get(hass)
registry_device = device_registry.async_get(device_id) registry_device = device_registry.async_get(device_id)
if not registry_device or registry_device.model != ATTR_DEVICE_SMART_REMOTER: if not registry_device or registry_device.model not in [
ATTR_DEVICE_SMART_REMOTER,
ATTR_DEVICE_SWITCH,
]:
return [] return []
if registry_device.model_id not in list(FLEX_FOB_TRIGGER_TYPES.keys()): if registry_device.model_id not in list(TRIGGER_MAPPINGS.keys()):
return [] return []
return [ return [
{ {
@ -79,7 +89,7 @@ async def async_get_triggers(
CONF_PLATFORM: "device", CONF_PLATFORM: "device",
CONF_TYPE: trigger, CONF_TYPE: trigger,
} }
for trigger in FLEX_FOB_TRIGGER_TYPES[registry_device.model_id] for trigger in TRIGGER_MAPPINGS[registry_device.model_id]
] ]

View File

@ -162,11 +162,12 @@ class YoLinkSwitchEntity(YoLinkEntity, SwitchEntity):
@callback @callback
def update_entity_state(self, state: dict[str, str | list[str]]) -> None: def update_entity_state(self, state: dict[str, str | list[str]]) -> None:
"""Update HA Entity State.""" """Update HA Entity State."""
self._attr_is_on = self._get_state( if (state_value := state.get("state")) is not None:
state.get("state"), self._attr_is_on = self._get_state(
self.entity_description.plug_index_fn(self.coordinator.device), state_value,
) self.entity_description.plug_index_fn(self.coordinator.device),
self.async_write_ha_state() )
self.async_write_ha_state()
async def call_state_change(self, state: str) -> None: async def call_state_change(self, state: str) -> None:
"""Call setState api to change switch state.""" """Call setState api to change switch state."""