Fixing device model compatibility issues. (#118686)

This commit is contained in:
Matrix 2024-06-03 21:56:42 +08:00 committed by GitHub
parent 771ed33b14
commit 595c9a2e01
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 24 additions and 13 deletions

View file

@ -16,3 +16,4 @@ YOLINK_EVENT = f"{DOMAIN}_event"
YOLINK_OFFLINE_TIME = 32400 YOLINK_OFFLINE_TIME = 32400
DEV_MODEL_WATER_METER_YS5007 = "YS5007" DEV_MODEL_WATER_METER_YS5007 = "YS5007"
DEV_MODEL_MULTI_OUTLET_YS6801 = "YS6801"

View file

@ -25,7 +25,7 @@ from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant, callback from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.entity_platform import AddEntitiesCallback
from .const import DOMAIN from .const import DEV_MODEL_MULTI_OUTLET_YS6801, DOMAIN
from .coordinator import YoLinkCoordinator from .coordinator import YoLinkCoordinator
from .entity import YoLinkEntity from .entity import YoLinkEntity
@ -35,7 +35,7 @@ class YoLinkSwitchEntityDescription(SwitchEntityDescription):
"""YoLink SwitchEntityDescription.""" """YoLink SwitchEntityDescription."""
exists_fn: Callable[[YoLinkDevice], bool] = lambda _: True exists_fn: Callable[[YoLinkDevice], bool] = lambda _: True
plug_index: int | None = None plug_index_fn: Callable[[YoLinkDevice], int | None] = lambda _: None
DEVICE_TYPES: tuple[YoLinkSwitchEntityDescription, ...] = ( DEVICE_TYPES: tuple[YoLinkSwitchEntityDescription, ...] = (
@ -61,36 +61,43 @@ DEVICE_TYPES: tuple[YoLinkSwitchEntityDescription, ...] = (
key="multi_outlet_usb_ports", key="multi_outlet_usb_ports",
translation_key="usb_ports", translation_key="usb_ports",
device_class=SwitchDeviceClass.OUTLET, device_class=SwitchDeviceClass.OUTLET,
exists_fn=lambda device: device.device_type == ATTR_DEVICE_MULTI_OUTLET, exists_fn=lambda device: device.device_type == ATTR_DEVICE_MULTI_OUTLET
plug_index=0, and device.device_model_name.startswith(DEV_MODEL_MULTI_OUTLET_YS6801),
plug_index_fn=lambda _: 0,
), ),
YoLinkSwitchEntityDescription( YoLinkSwitchEntityDescription(
key="multi_outlet_plug_1", key="multi_outlet_plug_1",
translation_key="plug_1", translation_key="plug_1",
device_class=SwitchDeviceClass.OUTLET, device_class=SwitchDeviceClass.OUTLET,
exists_fn=lambda device: device.device_type == ATTR_DEVICE_MULTI_OUTLET, exists_fn=lambda device: device.device_type == ATTR_DEVICE_MULTI_OUTLET,
plug_index=1, plug_index_fn=lambda device: 1
if device.device_model_name.startswith(DEV_MODEL_MULTI_OUTLET_YS6801)
else 0,
), ),
YoLinkSwitchEntityDescription( YoLinkSwitchEntityDescription(
key="multi_outlet_plug_2", key="multi_outlet_plug_2",
translation_key="plug_2", translation_key="plug_2",
device_class=SwitchDeviceClass.OUTLET, device_class=SwitchDeviceClass.OUTLET,
exists_fn=lambda device: device.device_type == ATTR_DEVICE_MULTI_OUTLET, exists_fn=lambda device: device.device_type == ATTR_DEVICE_MULTI_OUTLET,
plug_index=2, plug_index_fn=lambda device: 2
if device.device_model_name.startswith(DEV_MODEL_MULTI_OUTLET_YS6801)
else 1,
), ),
YoLinkSwitchEntityDescription( YoLinkSwitchEntityDescription(
key="multi_outlet_plug_3", key="multi_outlet_plug_3",
translation_key="plug_3", translation_key="plug_3",
device_class=SwitchDeviceClass.OUTLET, device_class=SwitchDeviceClass.OUTLET,
exists_fn=lambda device: device.device_type == ATTR_DEVICE_MULTI_OUTLET, exists_fn=lambda device: device.device_type == ATTR_DEVICE_MULTI_OUTLET
plug_index=3, and device.device_model_name.startswith(DEV_MODEL_MULTI_OUTLET_YS6801),
plug_index_fn=lambda _: 3,
), ),
YoLinkSwitchEntityDescription( YoLinkSwitchEntityDescription(
key="multi_outlet_plug_4", key="multi_outlet_plug_4",
translation_key="plug_4", translation_key="plug_4",
device_class=SwitchDeviceClass.OUTLET, device_class=SwitchDeviceClass.OUTLET,
exists_fn=lambda device: device.device_type == ATTR_DEVICE_MULTI_OUTLET, exists_fn=lambda device: device.device_type == ATTR_DEVICE_MULTI_OUTLET
plug_index=4, and device.device_model_name.startswith(DEV_MODEL_MULTI_OUTLET_YS6801),
plug_index_fn=lambda _: 4,
), ),
) )
@ -152,7 +159,8 @@ class YoLinkSwitchEntity(YoLinkEntity, SwitchEntity):
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( self._attr_is_on = self._get_state(
state.get("state"), self.entity_description.plug_index state.get("state"),
self.entity_description.plug_index_fn(self.coordinator.device),
) )
self.async_write_ha_state() self.async_write_ha_state()
@ -164,12 +172,14 @@ class YoLinkSwitchEntity(YoLinkEntity, SwitchEntity):
ATTR_DEVICE_MULTI_OUTLET, ATTR_DEVICE_MULTI_OUTLET,
]: ]:
client_request = OutletRequestBuilder.set_state_request( client_request = OutletRequestBuilder.set_state_request(
state, self.entity_description.plug_index state, self.entity_description.plug_index_fn(self.coordinator.device)
) )
else: else:
client_request = ClientRequest("setState", {"state": state}) client_request = ClientRequest("setState", {"state": state})
await self.call_device(client_request) await self.call_device(client_request)
self._attr_is_on = self._get_state(state, self.entity_description.plug_index) self._attr_is_on = self._get_state(
state, self.entity_description.plug_index_fn(self.coordinator.device)
)
self.async_write_ha_state() self.async_write_ha_state()
async def async_turn_on(self, **kwargs: Any) -> None: async def async_turn_on(self, **kwargs: Any) -> None: