Avoid mutating entity descriptions in unifiprotect (#105976)

pull/106037/head
Erik Montnemery 2023-12-18 20:58:32 +01:00 committed by GitHub
parent 5175737b60
commit 27f81b3f63
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 11 deletions

View File

@ -1,8 +1,7 @@
"""Component providing binary sensors for UniFi Protect."""
from __future__ import annotations
from copy import copy
from dataclasses import dataclass
import dataclasses
import logging
from pyunifiprotect.data import (
@ -43,14 +42,14 @@ _LOGGER = logging.getLogger(__name__)
_KEY_DOOR = "door"
@dataclass
@dataclasses.dataclass
class ProtectBinaryEntityDescription(
ProtectRequiredKeysMixin, BinarySensorEntityDescription
):
"""Describes UniFi Protect Binary Sensor entity."""
@dataclass
@dataclasses.dataclass
class ProtectBinaryEventEntityDescription(
ProtectEventMixin, BinarySensorEntityDescription
):
@ -561,9 +560,11 @@ class ProtectDeviceBinarySensor(ProtectDeviceEntity, BinarySensorEntity):
self._attr_is_on = entity_description.get_ufp_value(updated_device)
# UP Sense can be any of the 3 contact sensor device classes
if entity_description.key == _KEY_DOOR and isinstance(updated_device, Sensor):
entity_description.device_class = MOUNT_DEVICE_CLASS_MAP.get(
self._attr_device_class = MOUNT_DEVICE_CLASS_MAP.get(
updated_device.mount_type, BinarySensorDeviceClass.DOOR
)
else:
self._attr_device_class = self.entity_description.device_class
class ProtectDiskBinarySensor(ProtectNVREntity, BinarySensorEntity):
@ -584,9 +585,11 @@ class ProtectDiskBinarySensor(ProtectNVREntity, BinarySensorEntity):
# backwards compat with old unique IDs
index = self._disk.slot - 1
description = copy(description)
description.key = f"{description.key}_{index}"
description.name = f"{disk.type} {disk.slot}"
description = dataclasses.replace(
description,
key=f"{description.key}_{index}",
name=f"{disk.type} {disk.slot}",
)
super().__init__(data, device, description)
@callback

View File

@ -52,9 +52,11 @@ class ProtectRequiredKeysMixin(EntityDescription, Generic[T]):
def __post_init__(self) -> None:
"""Pre-convert strings to tuples for faster get_nested_attr."""
self.ufp_required_field = split_tuple(self.ufp_required_field)
self.ufp_value = split_tuple(self.ufp_value)
self.ufp_enabled = split_tuple(self.ufp_enabled)
object.__setattr__(
self, "ufp_required_field", split_tuple(self.ufp_required_field)
)
object.__setattr__(self, "ufp_value", split_tuple(self.ufp_value))
object.__setattr__(self, "ufp_enabled", split_tuple(self.ufp_enabled))
def get_ufp_value(self, obj: T) -> Any:
"""Return value from UniFi Protect device."""