Improve Unifi switch entity unique ID naming function (#104370)

pull/103513/head^2
Aarni Koskela 2023-11-22 21:02:16 +02:00 committed by GitHub
parent 41626ed500
commit ae4552eb3e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 8 additions and 2 deletions

View File

@ -199,6 +199,12 @@ class UnifiSwitchEntityDescription(
only_event_for_state_change: bool = False
def _make_unique_id(obj_id: str, type_name: str) -> str:
"""Split an object id by the first underscore and interpose the given type."""
prefix, _, suffix = obj_id.partition("_")
return f"{prefix}-{type_name}-{suffix}"
ENTITY_DESCRIPTIONS: tuple[UnifiSwitchEntityDescription, ...] = (
UnifiSwitchEntityDescription[Clients, Client](
key="Block client",
@ -256,7 +262,7 @@ ENTITY_DESCRIPTIONS: tuple[UnifiSwitchEntityDescription, ...] = (
object_fn=lambda api, obj_id: api.outlets[obj_id],
should_poll=False,
supported_fn=async_outlet_supports_switching_fn,
unique_id_fn=lambda controller, obj_id: f"{obj_id.split('_', 1)[0]}-outlet-{obj_id.split('_', 1)[1]}",
unique_id_fn=lambda controller, obj_id: _make_unique_id(obj_id, "outlet"),
),
UnifiSwitchEntityDescription[PortForwarding, PortForward](
key="Port forward control",
@ -297,7 +303,7 @@ ENTITY_DESCRIPTIONS: tuple[UnifiSwitchEntityDescription, ...] = (
object_fn=lambda api, obj_id: api.ports[obj_id],
should_poll=False,
supported_fn=lambda controller, obj_id: controller.api.ports[obj_id].port_poe,
unique_id_fn=lambda controller, obj_id: f"{obj_id.split('_', 1)[0]}-poe-{obj_id.split('_', 1)[1]}",
unique_id_fn=lambda controller, obj_id: _make_unique_id(obj_id, "poe"),
),
UnifiSwitchEntityDescription[Wlans, Wlan](
key="WLAN control",