2022-02-16 16:55:30 +00:00
|
|
|
"""Support for deCONZ buttons."""
|
|
|
|
|
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
from dataclasses import dataclass
|
|
|
|
|
2022-04-24 20:58:36 +00:00
|
|
|
from pydeconz.models.event import EventType
|
2022-04-24 08:09:58 +00:00
|
|
|
from pydeconz.models.scene import Scene as PydeconzScene
|
2022-02-16 16:55:30 +00:00
|
|
|
|
|
|
|
from homeassistant.components.button import (
|
|
|
|
DOMAIN,
|
|
|
|
ButtonEntity,
|
|
|
|
ButtonEntityDescription,
|
|
|
|
)
|
|
|
|
from homeassistant.config_entries import ConfigEntry
|
|
|
|
from homeassistant.core import HomeAssistant, callback
|
|
|
|
from homeassistant.helpers.entity import EntityCategory
|
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
|
|
|
|
|
|
|
from .deconz_device import DeconzSceneMixin
|
|
|
|
from .gateway import DeconzGateway, get_gateway_from_config_entry
|
|
|
|
|
|
|
|
|
|
|
|
@dataclass
|
|
|
|
class DeconzButtonDescriptionMixin:
|
|
|
|
"""Required values when describing deCONZ button entities."""
|
|
|
|
|
|
|
|
suffix: str
|
|
|
|
button_fn: str
|
|
|
|
|
|
|
|
|
|
|
|
@dataclass
|
|
|
|
class DeconzButtonDescription(ButtonEntityDescription, DeconzButtonDescriptionMixin):
|
|
|
|
"""Class describing deCONZ button entities."""
|
|
|
|
|
|
|
|
|
|
|
|
ENTITY_DESCRIPTIONS = {
|
|
|
|
PydeconzScene: [
|
|
|
|
DeconzButtonDescription(
|
|
|
|
key="store",
|
|
|
|
button_fn="store",
|
|
|
|
suffix="Store Current Scene",
|
|
|
|
icon="mdi:inbox-arrow-down",
|
|
|
|
entity_category=EntityCategory.CONFIG,
|
|
|
|
)
|
|
|
|
]
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
async def async_setup_entry(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
config_entry: ConfigEntry,
|
|
|
|
async_add_entities: AddEntitiesCallback,
|
|
|
|
) -> None:
|
|
|
|
"""Set up the deCONZ button entity."""
|
|
|
|
gateway = get_gateway_from_config_entry(hass, config_entry)
|
|
|
|
gateway.entities[DOMAIN] = set()
|
|
|
|
|
|
|
|
@callback
|
2022-04-24 20:58:36 +00:00
|
|
|
def async_add_scene(_: EventType, scene_id: str) -> None:
|
2022-02-16 16:55:30 +00:00
|
|
|
"""Add scene button from deCONZ."""
|
2022-04-24 20:58:36 +00:00
|
|
|
scene = gateway.api.scenes[scene_id]
|
|
|
|
async_add_entities(
|
|
|
|
DeconzButton(scene, gateway, description)
|
|
|
|
for description in ENTITY_DESCRIPTIONS.get(PydeconzScene, [])
|
|
|
|
)
|
2022-02-16 16:55:30 +00:00
|
|
|
|
|
|
|
config_entry.async_on_unload(
|
2022-04-24 20:58:36 +00:00
|
|
|
gateway.api.scenes.subscribe(
|
2022-02-16 16:55:30 +00:00
|
|
|
async_add_scene,
|
2022-04-24 20:58:36 +00:00
|
|
|
EventType.ADDED,
|
2022-02-16 16:55:30 +00:00
|
|
|
)
|
|
|
|
)
|
|
|
|
|
2022-04-24 20:58:36 +00:00
|
|
|
for scene_id in gateway.api.scenes:
|
|
|
|
async_add_scene(EventType.ADDED, scene_id)
|
2022-02-16 16:55:30 +00:00
|
|
|
|
|
|
|
|
|
|
|
class DeconzButton(DeconzSceneMixin, ButtonEntity):
|
|
|
|
"""Representation of a deCONZ button entity."""
|
|
|
|
|
|
|
|
TYPE = DOMAIN
|
|
|
|
|
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
device: PydeconzScene,
|
|
|
|
gateway: DeconzGateway,
|
|
|
|
description: DeconzButtonDescription,
|
|
|
|
) -> None:
|
|
|
|
"""Initialize deCONZ number entity."""
|
|
|
|
self.entity_description: DeconzButtonDescription = description
|
|
|
|
super().__init__(device, gateway)
|
|
|
|
|
|
|
|
self._attr_name = f"{self._attr_name} {description.suffix}"
|
|
|
|
|
|
|
|
async def async_press(self) -> None:
|
|
|
|
"""Store light states into scene."""
|
|
|
|
async_button_fn = getattr(self._device, self.entity_description.button_fn)
|
|
|
|
await async_button_fn()
|
|
|
|
|
|
|
|
def get_device_identifier(self) -> str:
|
|
|
|
"""Return a unique identifier for this scene."""
|
|
|
|
return f"{super().get_device_identifier()}-{self.entity_description.key}"
|