1 """Define Guardian-specific utilities."""
3 from __future__
import annotations
5 from collections.abc
import Callable, Coroutine, Iterable
6 from dataclasses
import dataclass
7 from datetime
import timedelta
8 from functools
import wraps
9 from typing
import TYPE_CHECKING, Any, Concatenate
11 from aioguardian.errors
import GuardianError
18 from .const
import LOGGER
21 from .entity
import GuardianEntity
25 SIGNAL_REBOOT_REQUESTED =
"guardian_reboot_requested_{0}"
30 """Define an entity replacement."""
40 entity_replacement_strategies: Iterable[EntityDomainReplacementStrategy],
42 """Remove old entities and create a repairs issue with info on their replacement."""
43 ent_reg = er.async_get(hass)
44 for strategy
in entity_replacement_strategies:
48 for registry_entry
in er.async_entries_for_config_entry(
49 ent_reg, entry.entry_id
51 if registry_entry.domain == strategy.old_domain
52 and registry_entry.unique_id == strategy.old_unique_id
57 old_entity_id = registry_entry.entity_id
58 LOGGER.debug(
'Removing old entity: "%s"', old_entity_id)
59 ent_reg.async_remove(old_entity_id)
63 def convert_exceptions_to_homeassistant_error[_GuardianEntityT: GuardianEntity, **_P](
64 func: Callable[Concatenate[_GuardianEntityT, _P], Coroutine[Any, Any, Any]],
65 ) -> Callable[Concatenate[_GuardianEntityT, _P], Coroutine[Any, Any,
None]]:
66 """Decorate to handle exceptions from the Guardian API."""
70 entity: _GuardianEntityT, *args: _P.args, **kwargs: _P.kwargs
72 """Wrap the provided function."""
74 await func(entity, *args, **kwargs)
75 except GuardianError
as err:
77 f
"Error while calling {func.__name__}: {err}"
None async_finish_entity_domain_replacements(HomeAssistant hass, ConfigEntry entry, Iterable[EntityDomainReplacementStrategy] entity_replacement_strategies)