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"
@dataclasses.dataclass(frozen=True)
@dataclasses.dataclass(frozen=True, kw_only=True)
class ProtectBinaryEntityDescription(
ProtectRequiredKeysMixin, BinarySensorEntityDescription
):
"""Describes UniFi Protect Binary Sensor entity."""
@dataclasses.dataclass(frozen=True)
@dataclasses.dataclass(frozen=True, kw_only=True)
class ProtectBinaryEventEntityDescription(
ProtectEventMixin, BinarySensorEntityDescription
):

View file

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

View file

@ -5,7 +5,7 @@ from collections.abc import Callable, Coroutine
from dataclasses import dataclass
from enum import Enum
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
@ -35,7 +35,7 @@ class PermRequired(int, Enum):
DELETE = 3
@dataclass(frozen=True)
@dataclass(frozen=True, kw_only=True)
class ProtectRequiredKeysMixin(EntityDescription, Generic[T]):
"""Mixin for required keys."""
@ -100,7 +100,7 @@ class ProtectRequiredKeysMixin(EntityDescription, Generic[T]):
return bool(get_nested_attr(obj, ufp_required_field))
@dataclass(frozen=True)
@dataclass(frozen=True, kw_only=True)
class ProtectEventMixin(ProtectRequiredKeysMixin[T]):
"""Mixin for events."""
@ -110,7 +110,8 @@ class ProtectEventMixin(ProtectRequiredKeysMixin[T]):
"""Return value from UniFi Protect device."""
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
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)
@dataclass(frozen=True)
@dataclass(frozen=True, kw_only=True)
class ProtectSetableKeysMixin(ProtectRequiredKeysMixin[T]):
"""Mixin for settable values."""

View file

@ -30,22 +30,17 @@ from .utils import async_dispatch_id as _ufpd
_LOGGER = logging.getLogger(__name__)
@dataclass(frozen=True)
class NumberKeysMixin:
"""Mixin for required keys."""
@dataclass(frozen=True, kw_only=True)
class ProtectNumberEntityDescription(
ProtectSetableKeysMixin[T], NumberEntityDescription
):
"""Describes UniFi Protect Number entity."""
ufp_max: int | float
ufp_min: 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:
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"
@dataclass(frozen=True)
@dataclass(frozen=True, kw_only=True)
class ProtectSelectEntityDescription(
ProtectSetableKeysMixin[T], SelectEntityDescription
):

View file

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

View file

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

View file

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