1 """Base class for Ring entity."""
3 from collections.abc
import Awaitable, Callable, Coroutine
4 from dataclasses
import dataclass
5 from typing
import Any, Concatenate, Generic, cast
7 from ring_doorbell
import (
14 from typing_extensions
import TypeVar
26 BaseCoordinatorEntity,
30 from .const
import ATTRIBUTION, DOMAIN
31 from .coordinator
import RingDataCoordinator, RingListenCoordinator
33 RingDeviceT = TypeVar(
"RingDeviceT", bound=RingGeneric, default=RingGeneric)
35 _RingCoordinatorT = TypeVar(
37 bound=(RingDataCoordinator | RingListenCoordinator),
41 @dataclass(slots=True)
43 """Class to define deprecation info for deprecated entities."""
45 new_platform: Platform
46 breaks_in_ha_version: str
49 @dataclass(frozen=True, kw_only=True)
51 """Base class for a ring entity description."""
53 deprecated_info: DeprecatedInfo |
None =
None
56 def exception_wrap[_RingBaseEntityT: RingBaseEntity[Any, Any], **_P, _R](
57 async_func: Callable[Concatenate[_RingBaseEntityT, _P], Coroutine[Any, Any, _R]],
58 ) -> Callable[Concatenate[_RingBaseEntityT, _P], Coroutine[Any, Any, _R]]:
59 """Define a wrapper to catch exceptions and raise HomeAssistant errors."""
61 async
def _wrap(self: _RingBaseEntityT, *args: _P.args, **kwargs: _P.kwargs) -> _R:
63 return await async_func(self, *args, **kwargs)
64 except AuthenticationError
as err:
65 self.coordinator.config_entry.async_start_reauth(self.hass)
67 except RingTimeout
as err:
69 f
"Timeout communicating with API {async_func}: {err}"
71 except RingError
as err:
73 f
"Error communicating with API{async_func}: {err}"
79 def refresh_after[_RingEntityT: RingEntity[Any], **_P](
80 func: Callable[Concatenate[_RingEntityT, _P], Awaitable[
None]],
81 ) -> Callable[Concatenate[_RingEntityT, _P], Coroutine[Any, Any,
None]]:
82 """Define a wrapper to handle api call errors or refresh after success."""
85 async
def _wrap(self: _RingEntityT, *args: _P.args, **kwargs: _P.kwargs) ->
None:
86 await func(self, *args, **kwargs)
87 await self.coordinator.async_request_refresh()
96 entity_description: RingEntityDescription,
98 """Return true if the entitty should be created based on the deprecated_info.
100 If deprecated_info is not defined will return true.
101 If entity not yet created will return false.
102 If entity disabled will delete it and return false.
103 Otherwise will return true and create issues for scripts or automations.
105 if not entity_description.deprecated_info:
108 ent_reg = er.async_get(hass)
109 entity_id = ent_reg.async_get_entity_id(
117 entity_entry = ent_reg.async_get(entity_id)
119 if entity_entry.disabled:
122 ent_reg.async_remove(entity_id)
128 if entity_automations
or entity_scripts:
129 deprecated_info = entity_description.deprecated_info
130 for item
in entity_automations + entity_scripts:
134 f
"deprecated_entity_{entity_id}_{item}",
135 breaks_in_ha_version=deprecated_info.breaks_in_ha_version,
138 severity=IssueSeverity.WARNING,
139 translation_key=
"deprecated_entity",
140 translation_placeholders={
143 "platform": platform,
144 "new_platform": deprecated_info.new_platform,
151 BaseCoordinatorEntity[_RingCoordinatorT], Generic[_RingCoordinatorT, RingDeviceT]
153 """Base implementation for Ring device."""
155 _attr_attribution = ATTRIBUTION
156 _attr_should_poll =
False
157 _attr_has_entity_name =
True
162 coordinator: _RingCoordinatorT,
164 """Initialize a sensor for Ring device."""
165 super().
__init__(coordinator, context=device.id)
169 identifiers={(DOMAIN, device.device_id)},
177 """Implementation for Ring devices."""
180 return self.coordinator.data
_attr_extra_state_attributes
None __init__(self, RingDeviceT device, _RingCoordinatorT coordinator)
RingDevices _get_coordinator_data(self)
None _handle_coordinator_update(self)
list[str] automations_with_entity(HomeAssistant hass, str entity_id)
DeviceEntry get_device(HomeAssistant hass, str unique_id)
None async_create_issue(HomeAssistant hass, str entry_id)
bool async_check_create_deprecated(HomeAssistant hass, Platform platform, str unique_id, RingEntityDescription entity_description)
list[str] scripts_with_entity(HomeAssistant hass, str entity_id)