Skip to content

Commit df5d818

Browse files
sdb9696cdce8p
andauthored
Make ring device generic in RingEntity (#115406)
Co-authored-by: Marc Mueller <[email protected]>
1 parent 5308e02 commit df5d818

File tree

7 files changed

+27
-34
lines changed

7 files changed

+27
-34
lines changed

homeassistant/components/ring/button.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,9 @@ async def async_setup_entry(
3535
)
3636

3737

38-
class RingDoorButton(RingEntity, ButtonEntity):
38+
class RingDoorButton(RingEntity[RingOther], ButtonEntity):
3939
"""Creates a button to open the ring intercom door."""
4040

41-
_device: RingOther
42-
4341
def __init__(
4442
self,
4543
device: RingOther,

homeassistant/components/ring/camera.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,10 @@ async def async_setup_entry(
4848
async_add_entities(cams)
4949

5050

51-
class RingCam(RingEntity, Camera):
51+
class RingCam(RingEntity[RingDoorBell], Camera):
5252
"""An implementation of a Ring Door Bell camera."""
5353

5454
_attr_name = None
55-
_device: RingDoorBell
5655

5756
def __init__(
5857
self,

homeassistant/components/ring/entity.py

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"""Base class for Ring entity."""
22

33
from collections.abc import Callable
4-
from typing import Any, Concatenate, ParamSpec, TypeVar
4+
from typing import Any, Concatenate, Generic, ParamSpec, cast
55

66
from ring_doorbell import (
77
AuthenticationError,
@@ -10,6 +10,7 @@
1010
RingGeneric,
1111
RingTimeout,
1212
)
13+
from typing_extensions import TypeVar
1314

1415
from homeassistant.core import callback
1516
from homeassistant.exceptions import HomeAssistantError
@@ -19,11 +20,13 @@
1920
from .const import ATTRIBUTION, DOMAIN
2021
from .coordinator import RingDataCoordinator, RingNotificationsCoordinator
2122

23+
RingDeviceT = TypeVar("RingDeviceT", bound=RingGeneric, default=RingGeneric)
24+
2225
_RingCoordinatorT = TypeVar(
2326
"_RingCoordinatorT",
2427
bound=(RingDataCoordinator | RingNotificationsCoordinator),
2528
)
26-
_RingBaseEntityT = TypeVar("_RingBaseEntityT", bound="RingBaseEntity[Any]")
29+
_RingBaseEntityT = TypeVar("_RingBaseEntityT", bound="RingBaseEntity[Any, Any]")
2730
_R = TypeVar("_R")
2831
_P = ParamSpec("_P")
2932

@@ -53,7 +56,9 @@ def _wrap(self: _RingBaseEntityT, *args: _P.args, **kwargs: _P.kwargs) -> _R:
5356
return _wrap
5457

5558

56-
class RingBaseEntity(CoordinatorEntity[_RingCoordinatorT]):
59+
class RingBaseEntity(
60+
CoordinatorEntity[_RingCoordinatorT], Generic[_RingCoordinatorT, RingDeviceT]
61+
):
5762
"""Base implementation for Ring device."""
5863

5964
_attr_attribution = ATTRIBUTION
@@ -62,7 +67,7 @@ class RingBaseEntity(CoordinatorEntity[_RingCoordinatorT]):
6267

6368
def __init__(
6469
self,
65-
device: RingGeneric,
70+
device: RingDeviceT,
6671
coordinator: _RingCoordinatorT,
6772
) -> None:
6873
"""Initialize a sensor for Ring device."""
@@ -77,15 +82,16 @@ def __init__(
7782
)
7883

7984

80-
class RingEntity(RingBaseEntity[RingDataCoordinator]):
85+
class RingEntity(RingBaseEntity[RingDataCoordinator, RingDeviceT]):
8186
"""Implementation for Ring devices."""
8287

8388
def _get_coordinator_data(self) -> RingDevices:
8489
return self.coordinator.data
8590

8691
@callback
8792
def _handle_coordinator_update(self) -> None:
88-
self._device = self._get_coordinator_data().get_device(
89-
self._device.device_api_id
93+
self._device = cast(
94+
RingDeviceT,
95+
self._get_coordinator_data().get_device(self._device.device_api_id),
9096
)
9197
super()._handle_coordinator_update()

homeassistant/components/ring/light.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,15 +52,13 @@ async def async_setup_entry(
5252
)
5353

5454

55-
class RingLight(RingEntity, LightEntity):
55+
class RingLight(RingEntity[RingStickUpCam], LightEntity):
5656
"""Creates a switch to turn the ring cameras light on and off."""
5757

5858
_attr_color_mode = ColorMode.ONOFF
5959
_attr_supported_color_modes = {ColorMode.ONOFF}
6060
_attr_translation_key = "light"
6161

62-
_device: RingStickUpCam
63-
6462
def __init__(
6563
self, device: RingStickUpCam, coordinator: RingDataCoordinator
6664
) -> None:

homeassistant/components/ring/sensor.py

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
RingGeneric,
1515
RingOther,
1616
)
17-
from typing_extensions import TypeVar
1817

1918
from homeassistant.components.sensor import (
2019
SensorDeviceClass,
@@ -35,9 +34,7 @@
3534
from . import RingData
3635
from .const import DOMAIN
3736
from .coordinator import RingDataCoordinator
38-
from .entity import RingEntity
39-
40-
_RingDeviceT = TypeVar("_RingDeviceT", bound=RingGeneric, default=RingGeneric)
37+
from .entity import RingDeviceT, RingEntity
4138

4239

4340
async def async_setup_entry(
@@ -59,17 +56,16 @@ async def async_setup_entry(
5956
async_add_entities(entities)
6057

6158

62-
class RingSensor(RingEntity, SensorEntity, Generic[_RingDeviceT]):
59+
class RingSensor(RingEntity[RingDeviceT], SensorEntity):
6360
"""A sensor implementation for Ring device."""
6461

65-
entity_description: RingSensorEntityDescription[_RingDeviceT]
66-
_device: _RingDeviceT
62+
entity_description: RingSensorEntityDescription[RingDeviceT]
6763

6864
def __init__(
6965
self,
70-
device: RingGeneric,
66+
device: RingDeviceT,
7167
coordinator: RingDataCoordinator,
72-
description: RingSensorEntityDescription[_RingDeviceT],
68+
description: RingSensorEntityDescription[RingDeviceT],
7369
) -> None:
7470
"""Initialize a sensor for Ring device."""
7571
super().__init__(device, coordinator)
@@ -85,7 +81,7 @@ def _handle_coordinator_update(self) -> None:
8581
"""Call update method."""
8682

8783
self._device = cast(
88-
_RingDeviceT,
84+
RingDeviceT,
8985
self._get_coordinator_data().get_device(self._device.device_api_id),
9086
)
9187
# History values can drop off the last 10 events so only update
@@ -126,12 +122,12 @@ def _get_last_event_attrs(
126122

127123

128124
@dataclass(frozen=True, kw_only=True)
129-
class RingSensorEntityDescription(SensorEntityDescription, Generic[_RingDeviceT]):
125+
class RingSensorEntityDescription(SensorEntityDescription, Generic[RingDeviceT]):
130126
"""Describes Ring sensor entity."""
131127

132-
value_fn: Callable[[_RingDeviceT], StateType] = lambda _: True
128+
value_fn: Callable[[RingDeviceT], StateType] = lambda _: True
133129
exists_fn: Callable[[RingGeneric], bool] = lambda _: True
134-
extra_state_attributes_fn: Callable[[_RingDeviceT], dict[str, Any] | None] = (
130+
extra_state_attributes_fn: Callable[[RingDeviceT], dict[str, Any] | None] = (
135131
lambda _: None
136132
)
137133

homeassistant/components/ring/siren.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,13 @@ async def async_setup_entry(
3333
)
3434

3535

36-
class RingChimeSiren(RingEntity, SirenEntity):
36+
class RingChimeSiren(RingEntity[RingChime], SirenEntity):
3737
"""Creates a siren to play the test chimes of a Chime device."""
3838

3939
_attr_available_tones = [RingEventKind.DING.value, RingEventKind.MOTION.value]
4040
_attr_supported_features = SirenEntityFeature.TURN_ON | SirenEntityFeature.TONES
4141
_attr_translation_key = "siren"
4242

43-
_device: RingChime
44-
4543
def __init__(self, device: RingChime, coordinator: RingDataCoordinator) -> None:
4644
"""Initialize a Ring Chime siren."""
4745
super().__init__(device, coordinator)

homeassistant/components/ring/switch.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,9 @@ async def async_setup_entry(
4444
)
4545

4646

47-
class BaseRingSwitch(RingEntity, SwitchEntity):
47+
class BaseRingSwitch(RingEntity[RingStickUpCam], SwitchEntity):
4848
"""Represents a switch for controlling an aspect of a ring device."""
4949

50-
_device: RingStickUpCam
51-
5250
def __init__(
5351
self, device: RingStickUpCam, coordinator: RingDataCoordinator, device_type: str
5452
) -> None:

0 commit comments

Comments
 (0)