1 """SFR Box button platform."""
3 from __future__
import annotations
5 from collections.abc
import Awaitable, Callable, Coroutine
6 from dataclasses
import dataclass
7 from functools
import wraps
8 from typing
import TYPE_CHECKING, Any, Concatenate
10 from sfrbox_api.bridge
import SFRBox
11 from sfrbox_api.exceptions
import SFRBoxError
12 from sfrbox_api.models
import SystemInfo
17 ButtonEntityDescription,
26 from .const
import DOMAIN
27 from .models
import DomainData
30 def with_error_wrapping[**_P, _R](
31 func: Callable[Concatenate[SFRBoxButton, _P], Awaitable[_R]],
32 ) -> Callable[Concatenate[SFRBoxButton, _P], Coroutine[Any, Any, _R]]:
33 """Catch SFR errors."""
41 """Catch SFRBoxError errors and raise HomeAssistantError."""
43 return await func(self, *args, **kwargs)
44 except SFRBoxError
as err:
50 @dataclass(frozen=True, kw_only=True)
52 """Description for SFR Box buttons."""
54 async_press: Callable[[SFRBox], Coroutine[
None,
None,
None]]
57 BUTTON_TYPES: tuple[SFRBoxButtonEntityDescription, ...] = (
59 async_press=
lambda x: x.system_reboot(),
60 device_class=ButtonDeviceClass.RESTART,
61 entity_category=EntityCategory.CONFIG,
68 hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
70 """Set up the buttons."""
71 data: DomainData = hass.data[DOMAIN][entry.entry_id]
72 system_info = data.system.data
74 assert system_info
is not None
77 SFRBoxButton(data.box, description, system_info)
for description
in BUTTON_TYPES
83 """Mixin for button specific attributes."""
85 entity_description: SFRBoxButtonEntityDescription
86 _attr_has_entity_name =
True
91 description: SFRBoxButtonEntityDescription,
92 system_info: SystemInfo,
94 """Initialize the sensor."""
99 identifiers={(DOMAIN, system_info.mac_addr)},
104 """Process the button press."""