Migrate unifiprotect descriptions to be kw_only (#107832)

This commit is contained in:
J. Nick Koston 2024-01-11 22:33:33 -10:00 committed by GitHub
parent b6dfa1fa7c
commit bef596d0dd
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 21 additions and 26 deletions

View file

@ -43,14 +43,14 @@ _LOGGER = logging.getLogger(__name__)
_KEY_DOOR = "door" _KEY_DOOR = "door"
@dataclasses.dataclass(frozen=True) @dataclasses.dataclass(frozen=True, kw_only=True)
class ProtectBinaryEntityDescription( class ProtectBinaryEntityDescription(
ProtectRequiredKeysMixin, BinarySensorEntityDescription ProtectRequiredKeysMixin, BinarySensorEntityDescription
): ):
"""Describes UniFi Protect Binary Sensor entity.""" """Describes UniFi Protect Binary Sensor entity."""
@dataclasses.dataclass(frozen=True) @dataclasses.dataclass(frozen=True, kw_only=True)
class ProtectBinaryEventEntityDescription( class ProtectBinaryEventEntityDescription(
ProtectEventMixin, BinarySensorEntityDescription ProtectEventMixin, BinarySensorEntityDescription
): ):

View file

@ -28,7 +28,7 @@ from .utils import async_dispatch_id as _ufpd
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
@dataclass(frozen=True) @dataclass(frozen=True, kw_only=True)
class ProtectButtonEntityDescription( class ProtectButtonEntityDescription(
ProtectSetableKeysMixin[T], ButtonEntityDescription ProtectSetableKeysMixin[T], ButtonEntityDescription
): ):

View file

@ -5,7 +5,7 @@ from collections.abc import Callable, Coroutine
from dataclasses import dataclass from dataclasses import dataclass
from enum import Enum from enum import Enum
import logging import logging
from typing import TYPE_CHECKING, Any, Generic, TypeVar, cast from typing import TYPE_CHECKING, Any, Generic, TypeVar
from pyunifiprotect.data import NVR, Event, ProtectAdoptableDeviceModel from pyunifiprotect.data import NVR, Event, ProtectAdoptableDeviceModel
@ -35,7 +35,7 @@ class PermRequired(int, Enum):
DELETE = 3 DELETE = 3
@dataclass(frozen=True) @dataclass(frozen=True, kw_only=True)
class ProtectRequiredKeysMixin(EntityDescription, Generic[T]): class ProtectRequiredKeysMixin(EntityDescription, Generic[T]):
"""Mixin for required keys.""" """Mixin for required keys."""
@ -100,7 +100,7 @@ class ProtectRequiredKeysMixin(EntityDescription, Generic[T]):
return bool(get_nested_attr(obj, ufp_required_field)) return bool(get_nested_attr(obj, ufp_required_field))
@dataclass(frozen=True) @dataclass(frozen=True, kw_only=True)
class ProtectEventMixin(ProtectRequiredKeysMixin[T]): class ProtectEventMixin(ProtectRequiredKeysMixin[T]):
"""Mixin for events.""" """Mixin for events."""
@ -110,7 +110,8 @@ class ProtectEventMixin(ProtectRequiredKeysMixin[T]):
"""Return value from UniFi Protect device.""" """Return value from UniFi Protect device."""
if self.ufp_event_obj is not None: if self.ufp_event_obj is not None:
return cast(Event, getattr(obj, self.ufp_event_obj, None)) event: Event | None = getattr(obj, self.ufp_event_obj, None)
return event
return None return None
def get_is_on(self, obj: T, event: Event | None) -> bool: def get_is_on(self, obj: T, event: Event | None) -> bool:
@ -119,7 +120,7 @@ class ProtectEventMixin(ProtectRequiredKeysMixin[T]):
return event is not None and self.get_ufp_value(obj) return event is not None and self.get_ufp_value(obj)
@dataclass(frozen=True) @dataclass(frozen=True, kw_only=True)
class ProtectSetableKeysMixin(ProtectRequiredKeysMixin[T]): class ProtectSetableKeysMixin(ProtectRequiredKeysMixin[T]):
"""Mixin for settable values.""" """Mixin for settable values."""

View file

@ -30,22 +30,17 @@ from .utils import async_dispatch_id as _ufpd
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
@dataclass(frozen=True) @dataclass(frozen=True, kw_only=True)
class NumberKeysMixin: class ProtectNumberEntityDescription(
"""Mixin for required keys.""" ProtectSetableKeysMixin[T], NumberEntityDescription
):
"""Describes UniFi Protect Number entity."""
ufp_max: int | float ufp_max: int | float
ufp_min: int | float ufp_min: int | float
ufp_step: int | float ufp_step: int | float
@dataclass(frozen=True)
class ProtectNumberEntityDescription(
ProtectSetableKeysMixin[T], NumberEntityDescription, NumberKeysMixin
):
"""Describes UniFi Protect Number entity."""
def _get_pir_duration(obj: Light) -> int: def _get_pir_duration(obj: Light) -> int:
return int(obj.light_device_settings.pir_duration.total_seconds()) return int(obj.light_device_settings.pir_duration.total_seconds())

View file

@ -92,7 +92,7 @@ DEVICE_RECORDING_MODES = [
DEVICE_CLASS_LCD_MESSAGE: Final = "unifiprotect__lcd_message" DEVICE_CLASS_LCD_MESSAGE: Final = "unifiprotect__lcd_message"
@dataclass(frozen=True) @dataclass(frozen=True, kw_only=True)
class ProtectSelectEntityDescription( class ProtectSelectEntityDescription(
ProtectSetableKeysMixin[T], SelectEntityDescription ProtectSetableKeysMixin[T], SelectEntityDescription
): ):

View file

@ -54,7 +54,7 @@ _LOGGER = logging.getLogger(__name__)
OBJECT_TYPE_NONE = "none" OBJECT_TYPE_NONE = "none"
@dataclass(frozen=True) @dataclass(frozen=True, kw_only=True)
class ProtectSensorEntityDescription( class ProtectSensorEntityDescription(
ProtectRequiredKeysMixin[T], SensorEntityDescription ProtectRequiredKeysMixin[T], SensorEntityDescription
): ):
@ -65,13 +65,12 @@ class ProtectSensorEntityDescription(
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."""
value = super().get_ufp_value(obj) value = super().get_ufp_value(obj)
if self.precision and value is not None:
if isinstance(value, float) and self.precision: return round(value, self.precision)
value = round(value, self.precision)
return value return value
@dataclass(frozen=True) @dataclass(frozen=True, kw_only=True)
class ProtectSensorEventEntityDescription( class ProtectSensorEventEntityDescription(
ProtectEventMixin[T], SensorEntityDescription ProtectEventMixin[T], SensorEntityDescription
): ):

View file

@ -33,7 +33,7 @@ ATTR_PREV_MIC = "prev_mic_level"
ATTR_PREV_RECORD = "prev_record_mode" ATTR_PREV_RECORD = "prev_record_mode"
@dataclass(frozen=True) @dataclass(frozen=True, kw_only=True)
class ProtectSwitchEntityDescription( class ProtectSwitchEntityDescription(
ProtectSetableKeysMixin[T], SwitchEntityDescription ProtectSetableKeysMixin[T], SwitchEntityDescription
): ):

View file

@ -25,7 +25,7 @@ from .models import PermRequired, ProtectSetableKeysMixin, T
from .utils import async_dispatch_id as _ufpd from .utils import async_dispatch_id as _ufpd
@dataclass(frozen=True) @dataclass(frozen=True, kw_only=True)
class ProtectTextEntityDescription(ProtectSetableKeysMixin[T], TextEntityDescription): class ProtectTextEntityDescription(ProtectSetableKeysMixin[T], TextEntityDescription):
"""Describes UniFi Protect Text entity.""" """Describes UniFi Protect Text entity."""