From bef596d0dd04f2dc5600fd7c02ea68c0a556164b Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Thu, 11 Jan 2024 22:33:33 -1000 Subject: [PATCH] Migrate unifiprotect descriptions to be kw_only (#107832) --- .../components/unifiprotect/binary_sensor.py | 4 ++-- homeassistant/components/unifiprotect/button.py | 2 +- homeassistant/components/unifiprotect/models.py | 11 ++++++----- homeassistant/components/unifiprotect/number.py | 15 +++++---------- homeassistant/components/unifiprotect/select.py | 2 +- homeassistant/components/unifiprotect/sensor.py | 9 ++++----- homeassistant/components/unifiprotect/switch.py | 2 +- homeassistant/components/unifiprotect/text.py | 2 +- 8 files changed, 21 insertions(+), 26 deletions(-) diff --git a/homeassistant/components/unifiprotect/binary_sensor.py b/homeassistant/components/unifiprotect/binary_sensor.py index d5baaa3b5bf..66767224de2 100644 --- a/homeassistant/components/unifiprotect/binary_sensor.py +++ b/homeassistant/components/unifiprotect/binary_sensor.py @@ -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 ): diff --git a/homeassistant/components/unifiprotect/button.py b/homeassistant/components/unifiprotect/button.py index c0872e03f03..cee4280507d 100644 --- a/homeassistant/components/unifiprotect/button.py +++ b/homeassistant/components/unifiprotect/button.py @@ -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 ): diff --git a/homeassistant/components/unifiprotect/models.py b/homeassistant/components/unifiprotect/models.py index 08f5c2075e6..f7da2f781ff 100644 --- a/homeassistant/components/unifiprotect/models.py +++ b/homeassistant/components/unifiprotect/models.py @@ -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.""" diff --git a/homeassistant/components/unifiprotect/number.py b/homeassistant/components/unifiprotect/number.py index 04f779ecbd7..90201da98d8 100644 --- a/homeassistant/components/unifiprotect/number.py +++ b/homeassistant/components/unifiprotect/number.py @@ -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()) diff --git a/homeassistant/components/unifiprotect/select.py b/homeassistant/components/unifiprotect/select.py index a3688916959..eed49ac87e7 100644 --- a/homeassistant/components/unifiprotect/select.py +++ b/homeassistant/components/unifiprotect/select.py @@ -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 ): diff --git a/homeassistant/components/unifiprotect/sensor.py b/homeassistant/components/unifiprotect/sensor.py index 212c0d5245b..5a52b45b62d 100644 --- a/homeassistant/components/unifiprotect/sensor.py +++ b/homeassistant/components/unifiprotect/sensor.py @@ -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 ): diff --git a/homeassistant/components/unifiprotect/switch.py b/homeassistant/components/unifiprotect/switch.py index 57089157169..ace769d7c43 100644 --- a/homeassistant/components/unifiprotect/switch.py +++ b/homeassistant/components/unifiprotect/switch.py @@ -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 ): diff --git a/homeassistant/components/unifiprotect/text.py b/homeassistant/components/unifiprotect/text.py index 7fb66d7c8e3..cfc4ad5702f 100644 --- a/homeassistant/components/unifiprotect/text.py +++ b/homeassistant/components/unifiprotect/text.py @@ -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."""