Skip to content

Commit 4583964

Browse files
committed
proper generics
1 parent ff10349 commit 4583964

File tree

16 files changed

+70
-127
lines changed

16 files changed

+70
-127
lines changed

zha/application/platforms/__init__.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from abc import abstractmethod
66
import asyncio
77
from contextlib import suppress
8+
import functools
89
from functools import cached_property
910
import logging
1011
from typing import TYPE_CHECKING, Any, Generic, Literal, TypeVar, final
@@ -29,7 +30,7 @@
2930

3031
if TYPE_CHECKING:
3132
from zha.zigbee.cluster_handlers import ClusterHandler
32-
from zha.zigbee.device import Device
33+
from zha.zigbee.device import Device, WebSocketClientDevice
3334
from zha.zigbee.endpoint import Endpoint
3435
from zha.zigbee.group import Group
3536

@@ -483,10 +484,13 @@ async def async_update(self, _: Any | None = None) -> None:
483484
class WebSocketClientEntity(BaseEntity, Generic[BaseEntityInfoType]):
484485
"""Entity repsentation for the websocket client."""
485486

486-
def __init__(self, entity_info: BaseEntityInfoType) -> None:
487+
def __init__(
488+
self, entity_info: BaseEntityInfoType, device: WebSocketClientDevice
489+
) -> None:
487490
"""Initialize the websocket client entity."""
488491
super().__init__(entity_info.unique_id)
489492
self.PLATFORM = entity_info.platform
493+
self._device: WebSocketClientDevice = device
490494
self._entity_info: BaseEntityInfoType = entity_info
491495
self._attr_enabled = self._entity_info.enabled
492496
self._attr_fallback_name = self._entity_info.fallback_name
@@ -498,6 +502,11 @@ def __init__(self, entity_info: BaseEntityInfoType) -> None:
498502
self._attr_device_class = self._entity_info.device_class
499503
self._attr_state_class = self._entity_info.state_class
500504

505+
@functools.cached_property
506+
def info_object(self) -> BaseEntityInfoType:
507+
"""Return a representation of the alarm control panel."""
508+
return self._entity_info
509+
501510
@property
502511
def state(self) -> dict[str, Any]:
503512
"""Return the arguments to use in the command."""
@@ -507,3 +516,8 @@ def state(self) -> dict[str, Any]:
507516
def state(self, value: dict[str, Any]) -> None:
508517
"""Set the state of the entity."""
509518
self._entity_info.state = value
519+
520+
async def async_update(self) -> None:
521+
"""Retrieve latest state."""
522+
self.debug("polling current state")
523+
await self._device.gateway.entities.refresh_state(self._entity_info)

zha/application/platforms/alarm_control_panel/__init__.py

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ async def async_alarm_trigger(self, code: str | None = None, **kwargs) -> None:
183183

184184

185185
class WebSocketClientAlarmControlPanel(
186-
WebSocketClientEntity, AlarmControlPanelEntityInterface
186+
WebSocketClientEntity[AlarmControlPanelEntityInfo], AlarmControlPanelEntityInterface
187187
):
188188
"""Alarm control panel entity for the WebSocket API."""
189189

@@ -194,13 +194,7 @@ def __init__(
194194
self, entity_info: AlarmControlPanelEntityInfo, device: WebSocketClientDevice
195195
) -> None:
196196
"""Initialize the ZHA alarm control device."""
197-
super().__init__(entity_info)
198-
self._device: WebSocketClientDevice = device
199-
200-
@functools.cached_property
201-
def info_object(self) -> AlarmControlPanelEntityInfo:
202-
"""Return a representation of the alarm control panel."""
203-
return self._entity_info
197+
super().__init__(entity_info, device)
204198

205199
@property
206200
def code_arm_required(self) -> bool:

zha/application/platforms/binary_sensor/__init__.py

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -404,7 +404,9 @@ class DanfossPreheatStatus(BinarySensor):
404404
_attr_entity_category = EntityCategory.DIAGNOSTIC
405405

406406

407-
class WebSocketClientBinarySensor(WebSocketClientEntity, BinarySensorEntityInterface):
407+
class WebSocketClientBinarySensor(
408+
WebSocketClientEntity[BinarySensorEntityInfo], BinarySensorEntityInterface
409+
):
408410
"""Base class for binary sensors that are updated via a websocket client."""
409411

410412
PLATFORM: Platform = Platform.BINARY_SENSOR
@@ -413,20 +415,9 @@ def __init__(
413415
self, entity_info: BinarySensorEntityInfo, device: WebSocketClientDevice
414416
) -> None:
415417
"""Initialize the ZHA alarm control device."""
416-
super().__init__(entity_info)
417-
self._device: WebSocketClientDevice = device
418-
419-
@functools.cached_property
420-
def info_object(self) -> BinarySensorEntityInfo:
421-
"""Return a representation of the binary sensor."""
422-
return self._entity_info
418+
super().__init__(entity_info, device)
423419

424420
@property
425421
def is_on(self) -> bool:
426422
"""Return True if the switch is on based on the state machine."""
427423
return self.info_object.state.state
428-
429-
async def async_update(self) -> None:
430-
"""Retrieve latest state."""
431-
self.debug("polling current state")
432-
await self._device.gateway.entities.refresh_state(self._entity_info)

zha/application/platforms/button/__init__.py

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,9 @@ class AqaraSelfTestButton(WriteAttributeButton):
234234
_attr_translation_key = "self_test"
235235

236236

237-
class WebSocketClientButtonEntity(WebSocketClientEntity, ButtonEntityInterface):
237+
class WebSocketClientButtonEntity(
238+
WebSocketClientEntity[ButtonEntityInfo], ButtonEntityInterface
239+
):
238240
"""Defines a ZHA button that is controlled via a websocket."""
239241

240242
PLATFORM = Platform.BUTTON
@@ -243,13 +245,7 @@ def __init__(
243245
self, entity_info: ButtonEntityInfo, device: WebSocketClientDevice
244246
) -> None:
245247
"""Initialize the ZHA alarm control device."""
246-
super().__init__(entity_info)
247-
self._device: WebSocketClientDevice = device
248-
249-
@functools.cached_property
250-
def info_object(self) -> ButtonEntityInfo:
251-
"""Return a representation of the button."""
252-
return self._entity_info
248+
super().__init__(entity_info, device)
253249

254250
@functools.cached_property
255251
def args(self) -> list[Any]:

zha/application/platforms/climate/__init__.py

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -962,7 +962,9 @@ async def async_preset_handler(self, preset: str, enable: bool = False) -> None:
962962
)
963963

964964

965-
class WebSocketClientThermostatEntity(WebSocketClientEntity, ClimateEntityInterface):
965+
class WebSocketClientThermostatEntity(
966+
WebSocketClientEntity[ThermostatEntityInfo], ClimateEntityInterface
967+
):
966968
"""Representation of a ZHA Thermostat device."""
967969

968970
PLATFORM: Platform = Platform.CLIMATE
@@ -971,13 +973,7 @@ def __init__(
971973
self, entity_info: ThermostatEntityInfo, device: WebSocketClientDevice
972974
) -> None:
973975
"""Initialize the ZHA climate entity."""
974-
super().__init__(entity_info)
975-
self._device: WebSocketClientDevice = device
976-
977-
@property
978-
def info_object(self) -> ThermostatEntityInfo:
979-
"""Return a representation of the thermostat."""
980-
return self._entity_info
976+
super().__init__(entity_info, device)
981977

982978
@property
983979
def current_temperature(self) -> float | None:

zha/application/platforms/cover/__init__.py

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -596,7 +596,9 @@ async def async_open_cover(self, **kwargs: Any) -> None:
596596
self.maybe_emit_state_changed_event()
597597

598598

599-
class WebSocketClientCoverEntity(WebSocketClientEntity, CoverEntityInterface):
599+
class WebSocketClientCoverEntity(
600+
WebSocketClientEntity[CoverEntityInfo], CoverEntityInterface
601+
):
600602
"""Representation of a ZHA cover."""
601603

602604
PLATFORM: Platform = Platform.COVER
@@ -605,13 +607,7 @@ def __init__(
605607
self, entity_info: CoverEntityInfo, device: WebSocketClientDevice
606608
) -> None:
607609
"""Initialize the ZHA fan entity."""
608-
super().__init__(entity_info)
609-
self._device: WebSocketClientDevice = device
610-
611-
@property
612-
def info_object(self) -> CoverEntityInfo:
613-
"""Return the info object for this entity."""
614-
return self._entity_info
610+
super().__init__(entity_info, device)
615611

616612
@property
617613
def supported_features(self) -> CoverEntityFeature:

zha/application/platforms/device_tracker/__init__.py

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ def handle_cluster_handler_attribute_updated(
160160

161161

162162
class WebSocketClientDeviceTrackerEntity(
163-
WebSocketClientEntity, DeviceTrackerEntityInterface
163+
WebSocketClientEntity[DeviceTrackerEntityInfo], DeviceTrackerEntityInterface
164164
):
165165
"""Device tracker entity for the WebSocket API."""
166166

@@ -170,13 +170,7 @@ def __init__(
170170
self, entity_info: DeviceTrackerEntityInfo, device: WebSocketClientDevice
171171
) -> None:
172172
"""Initialize the ZHA device tracker."""
173-
super().__init__(entity_info)
174-
self._device: WebSocketClientDevice = device
175-
176-
@property
177-
def info_object(self) -> DeviceTrackerEntityInfo:
178-
"""Return a representation of the device tracker."""
179-
return self._entity_info
173+
super().__init__(entity_info, device)
180174

181175
@property
182176
def is_connected(self) -> bool:

zha/application/platforms/fan/__init__.py

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -538,7 +538,9 @@ def preset_modes_to_name(self) -> dict[int, str]:
538538
return {6: PRESET_MODE_SMART}
539539

540540

541-
class WebSocketClientFanEntity(WebSocketClientEntity, FanEntityInterface):
541+
class WebSocketClientFanEntity(
542+
WebSocketClientEntity[FanEntityInfo], FanEntityInterface
543+
):
542544
"""Representation of a ZHA fan over WebSocket."""
543545

544546
PLATFORM: Platform = Platform.FAN
@@ -547,13 +549,7 @@ def __init__(
547549
self, entity_info: FanEntityInfo, device: WebSocketClientDevice
548550
) -> None:
549551
"""Initialize the ZHA fan entity."""
550-
super().__init__(entity_info)
551-
self._device: WebSocketClientDevice = device
552-
553-
@property
554-
def info_object(self) -> FanEntityInfo:
555-
"""Return the fan entity info."""
556-
return self._entity_info
552+
super().__init__(entity_info, device)
557553

558554
@property
559555
def preset_modes(self) -> list[str]:

zha/application/platforms/light/__init__.py

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1330,7 +1330,9 @@ def restore_external_state_attributes(
13301330
self._off_brightness = off_brightness
13311331

13321332

1333-
class WebSocketClientLightEntity(WebSocketClientEntity, LightEntityInterface):
1333+
class WebSocketClientLightEntity(
1334+
WebSocketClientEntity[LightEntityInfo], LightEntityInterface
1335+
):
13341336
"""Light entity that sends commands to a websocket client."""
13351337

13361338
PLATFORM: Platform = Platform.LIGHT
@@ -1339,13 +1341,7 @@ def __init__(
13391341
self, entity_info: LightEntityInfo, device: WebSocketClientDevice
13401342
) -> None:
13411343
"""Initialize the ZHA lock entity."""
1342-
super().__init__(entity_info)
1343-
self._device: WebSocketClientDevice = device
1344-
1345-
@property
1346-
def info_object(self) -> LightEntityInfo:
1347-
"""Return a representation of the select."""
1348-
return self._entity_info
1344+
super().__init__(entity_info, device)
13491345

13501346
@property
13511347
def xy_color(self) -> tuple[float, float] | None:

zha/application/platforms/lock/__init__.py

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,9 @@ def restore_external_state_attributes(
171171
self._state = state
172172

173173

174-
class WebSocketClientLockEntity(WebSocketClientEntity, LockEntityInterface):
174+
class WebSocketClientLockEntity(
175+
WebSocketClientEntity[LockEntityInfo], LockEntityInterface
176+
):
175177
"""Representation of a ZHA lock on the client side."""
176178

177179
PLATFORM: Platform = Platform.LOCK
@@ -180,13 +182,7 @@ def __init__(
180182
self, entity_info: LockEntityInfo, device: WebSocketClientDevice
181183
) -> None:
182184
"""Initialize the ZHA lock entity."""
183-
super().__init__(entity_info)
184-
self._device: WebSocketClientDevice = device
185-
186-
@property
187-
def info_object(self) -> LockEntityInfo:
188-
"""Return a representation of the lock."""
189-
return self._entity_info
185+
super().__init__(entity_info, device)
190186

191187
@property
192188
def is_locked(self) -> bool:

0 commit comments

Comments
 (0)