-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbutton.py
89 lines (69 loc) · 2.66 KB
/
button.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
"""Button entities for the MotionBlinds RS485 integration."""
import logging
from homeassistant.components.button import (
ButtonEntity,
ButtonEntityDescription,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import EntityCategory
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from .const import ATTR_START, ATTR_STOP, DOMAIN, ICON_START, ICON_STOP
from .select import SceneSelect
from collections.abc import Callable
from dataclasses import dataclass
_LOGGER = logging.getLogger(__name__)
PARALLEL_UPDATES = 0
@dataclass
class CommandButtonEntityDescription(ButtonEntityDescription):
command_callback: Callable[[SceneSelect], None] | None = None
async def command_start(scene_select: SceneSelect) -> None:
await scene_select.start()
async def command_stop(scene_select: SceneSelect) -> None:
await scene_select.stop()
BUTTON_TYPES: dict[str, CommandButtonEntityDescription] = {
ATTR_START: CommandButtonEntityDescription(
key=ATTR_START,
translation_key=ATTR_START,
icon=ICON_START,
entity_category=EntityCategory.CONFIG,
has_entity_name=True,
command_callback=command_start,
),
ATTR_STOP: CommandButtonEntityDescription(
key=ATTR_STOP,
translation_key=ATTR_STOP,
icon=ICON_STOP,
entity_category=EntityCategory.CONFIG,
has_entity_name=True,
command_callback=command_stop,
),
}
async def async_setup_entry(
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
) -> None:
"""Set up buttons based on a config entry."""
scene_select: SceneSelect = hass.data[DOMAIN][entry.entry_id]
_LOGGER.info("Setting up buttons")
async_add_entities(
[
GenericCommandButton(scene_select, entity_description)
for entity_description in BUTTON_TYPES.values()
]
)
class GenericCommandButton(ButtonEntity):
"""Representation of a command button."""
def __init__(
self,
scene_select: SceneSelect,
entity_description: CommandButtonEntityDescription,
) -> None:
"""Initialize the command button."""
_LOGGER.info(f"Setting up {entity_description.key} button")
self.entity_description = entity_description
self._scene_select = scene_select
self._attr_unique_id = f"{scene_select.unique_id}_{entity_description.key}"
self._attr_device_info = scene_select.device_info
async def async_press(self) -> None:
"""Handle the button press."""
await self.entity_description.command_callback(self._scene_select)