mirror of
https://github.com/home-assistant/core.git
synced 2025-07-22 20:57:21 +00:00
Avoid mutating entity descriptions in unifiprotect (#105976)
This commit is contained in:
parent
5175737b60
commit
27f81b3f63
@ -1,8 +1,7 @@
|
|||||||
"""Component providing binary sensors for UniFi Protect."""
|
"""Component providing binary sensors for UniFi Protect."""
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
from copy import copy
|
import dataclasses
|
||||||
from dataclasses import dataclass
|
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
from pyunifiprotect.data import (
|
from pyunifiprotect.data import (
|
||||||
@ -43,14 +42,14 @@ _LOGGER = logging.getLogger(__name__)
|
|||||||
_KEY_DOOR = "door"
|
_KEY_DOOR = "door"
|
||||||
|
|
||||||
|
|
||||||
@dataclass
|
@dataclasses.dataclass
|
||||||
class ProtectBinaryEntityDescription(
|
class ProtectBinaryEntityDescription(
|
||||||
ProtectRequiredKeysMixin, BinarySensorEntityDescription
|
ProtectRequiredKeysMixin, BinarySensorEntityDescription
|
||||||
):
|
):
|
||||||
"""Describes UniFi Protect Binary Sensor entity."""
|
"""Describes UniFi Protect Binary Sensor entity."""
|
||||||
|
|
||||||
|
|
||||||
@dataclass
|
@dataclasses.dataclass
|
||||||
class ProtectBinaryEventEntityDescription(
|
class ProtectBinaryEventEntityDescription(
|
||||||
ProtectEventMixin, BinarySensorEntityDescription
|
ProtectEventMixin, BinarySensorEntityDescription
|
||||||
):
|
):
|
||||||
@ -561,9 +560,11 @@ class ProtectDeviceBinarySensor(ProtectDeviceEntity, BinarySensorEntity):
|
|||||||
self._attr_is_on = entity_description.get_ufp_value(updated_device)
|
self._attr_is_on = entity_description.get_ufp_value(updated_device)
|
||||||
# UP Sense can be any of the 3 contact sensor device classes
|
# UP Sense can be any of the 3 contact sensor device classes
|
||||||
if entity_description.key == _KEY_DOOR and isinstance(updated_device, Sensor):
|
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
|
updated_device.mount_type, BinarySensorDeviceClass.DOOR
|
||||||
)
|
)
|
||||||
|
else:
|
||||||
|
self._attr_device_class = self.entity_description.device_class
|
||||||
|
|
||||||
|
|
||||||
class ProtectDiskBinarySensor(ProtectNVREntity, BinarySensorEntity):
|
class ProtectDiskBinarySensor(ProtectNVREntity, BinarySensorEntity):
|
||||||
@ -584,9 +585,11 @@ class ProtectDiskBinarySensor(ProtectNVREntity, BinarySensorEntity):
|
|||||||
# backwards compat with old unique IDs
|
# backwards compat with old unique IDs
|
||||||
index = self._disk.slot - 1
|
index = self._disk.slot - 1
|
||||||
|
|
||||||
description = copy(description)
|
description = dataclasses.replace(
|
||||||
description.key = f"{description.key}_{index}"
|
description,
|
||||||
description.name = f"{disk.type} {disk.slot}"
|
key=f"{description.key}_{index}",
|
||||||
|
name=f"{disk.type} {disk.slot}",
|
||||||
|
)
|
||||||
super().__init__(data, device, description)
|
super().__init__(data, device, description)
|
||||||
|
|
||||||
@callback
|
@callback
|
||||||
|
@ -52,9 +52,11 @@ class ProtectRequiredKeysMixin(EntityDescription, Generic[T]):
|
|||||||
|
|
||||||
def __post_init__(self) -> None:
|
def __post_init__(self) -> None:
|
||||||
"""Pre-convert strings to tuples for faster get_nested_attr."""
|
"""Pre-convert strings to tuples for faster get_nested_attr."""
|
||||||
self.ufp_required_field = split_tuple(self.ufp_required_field)
|
object.__setattr__(
|
||||||
self.ufp_value = split_tuple(self.ufp_value)
|
self, "ufp_required_field", split_tuple(self.ufp_required_field)
|
||||||
self.ufp_enabled = split_tuple(self.ufp_enabled)
|
)
|
||||||
|
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:
|
def get_ufp_value(self, obj: T) -> Any:
|
||||||
"""Return value from UniFi Protect device."""
|
"""Return value from UniFi Protect device."""
|
||||||
|
Loading…
x
Reference in New Issue
Block a user