Fixing device model compatibility issues. (#118686)

pull/113434/head^2
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
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.helpers.entity_platform import AddEntitiesCallback
from .const import DOMAIN
from .const import DEV_MODEL_MULTI_OUTLET_YS6801, DOMAIN
from .coordinator import YoLinkCoordinator
from .entity import YoLinkEntity
@ -35,7 +35,7 @@ class YoLinkSwitchEntityDescription(SwitchEntityDescription):
"""YoLink SwitchEntityDescription."""
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, ...] = (
@ -61,36 +61,43 @@ DEVICE_TYPES: tuple[YoLinkSwitchEntityDescription, ...] = (
key="multi_outlet_usb_ports",
translation_key="usb_ports",
device_class=SwitchDeviceClass.OUTLET,
exists_fn=lambda device: device.device_type == ATTR_DEVICE_MULTI_OUTLET,
plug_index=0,
exists_fn=lambda device: device.device_type == ATTR_DEVICE_MULTI_OUTLET
and device.device_model_name.startswith(DEV_MODEL_MULTI_OUTLET_YS6801),
plug_index_fn=lambda _: 0,
),
YoLinkSwitchEntityDescription(
key="multi_outlet_plug_1",
translation_key="plug_1",
device_class=SwitchDeviceClass.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(
key="multi_outlet_plug_2",
translation_key="plug_2",
device_class=SwitchDeviceClass.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(
key="multi_outlet_plug_3",
translation_key="plug_3",
device_class=SwitchDeviceClass.OUTLET,
exists_fn=lambda device: device.device_type == ATTR_DEVICE_MULTI_OUTLET,
plug_index=3,
exists_fn=lambda device: device.device_type == ATTR_DEVICE_MULTI_OUTLET
and device.device_model_name.startswith(DEV_MODEL_MULTI_OUTLET_YS6801),
plug_index_fn=lambda _: 3,
),
YoLinkSwitchEntityDescription(
key="multi_outlet_plug_4",
translation_key="plug_4",
device_class=SwitchDeviceClass.OUTLET,
exists_fn=lambda device: device.device_type == ATTR_DEVICE_MULTI_OUTLET,
plug_index=4,
exists_fn=lambda device: device.device_type == ATTR_DEVICE_MULTI_OUTLET
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:
"""Update HA Entity 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()
@ -164,12 +172,14 @@ class YoLinkSwitchEntity(YoLinkEntity, SwitchEntity):
ATTR_DEVICE_MULTI_OUTLET,
]:
client_request = OutletRequestBuilder.set_state_request(
state, self.entity_description.plug_index
state, self.entity_description.plug_index_fn(self.coordinator.device)
)
else:
client_request = ClientRequest("setState", {"state": state})
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()
async def async_turn_on(self, **kwargs: Any) -> None: