1 """Support for SLZB-06 buttons."""
3 from __future__
import annotations
5 from collections.abc
import Awaitable, Callable
6 from dataclasses
import dataclass
9 from pysmlight.web
import CmdWrapper
12 DOMAIN
as BUTTON_DOMAIN,
15 ButtonEntityDescription,
23 from .const
import DOMAIN
24 from .coordinator
import SmDataUpdateCoordinator
25 from .entity
import SmEntity
27 _LOGGER = logging.getLogger(__name__)
30 @dataclass(frozen=True, kw_only=True)
32 """Class to describe a Button entity."""
34 press_fn: Callable[[CmdWrapper], Awaitable[
None]]
37 BUTTONS: list[SmButtonDescription] = [
40 translation_key=
"core_restart",
41 device_class=ButtonDeviceClass.RESTART,
42 press_fn=
lambda cmd: cmd.reboot(),
46 translation_key=
"zigbee_restart",
47 device_class=ButtonDeviceClass.RESTART,
48 press_fn=
lambda cmd: cmd.zb_restart(),
51 key=
"zigbee_flash_mode",
52 translation_key=
"zigbee_flash_mode",
53 entity_registry_enabled_default=
False,
54 press_fn=
lambda cmd: cmd.zb_bootloader(),
59 key=
"reconnect_zigbee_router",
60 translation_key=
"reconnect_zigbee_router",
61 entity_registry_enabled_default=
False,
62 press_fn=
lambda cmd: cmd.zb_router(),
69 async_add_entities: AddEntitiesCallback,
71 """Set up SMLIGHT buttons based on a config entry."""
72 coordinator = entry.runtime_data.data
75 entity_created =
False
78 def _check_router(startup: bool =
False) ->
None:
79 nonlocal entity_created
81 if coordinator.data.info.zb_type == 1
and not entity_created:
84 elif coordinator.data.info.zb_type != 1
and (startup
or entity_created):
85 entity_registry = er.async_get(hass)
86 if entity_id := entity_registry.async_get_entity_id(
87 BUTTON_DOMAIN, DOMAIN, f
"{coordinator.unique_id}-{ROUTER.key}"
89 entity_registry.async_remove(entity_id)
91 coordinator.async_add_listener(_check_router)
92 _check_router(startup=
True)
96 """Defines a SLZB-06 button."""
98 coordinator: SmDataUpdateCoordinator
99 entity_description: SmButtonDescription
100 _attr_entity_category = EntityCategory.CONFIG
104 coordinator: SmDataUpdateCoordinator,
105 description: SmButtonDescription,
107 """Initialize SLZB-06 button entity."""
114 """Trigger button press."""
115 await self.
entity_descriptionentity_description.press_fn(self.coordinator.client.cmds)