2021-11-10 19:34:35 +00:00
|
|
|
"""Support for KNX/IP buttons."""
|
2024-03-08 14:01:29 +00:00
|
|
|
|
2021-11-10 19:34:35 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
from xknx.devices import RawValue as XknxRawValue
|
|
|
|
|
2021-11-20 10:30:41 +00:00
|
|
|
from homeassistant import config_entries
|
2021-11-10 19:34:35 +00:00
|
|
|
from homeassistant.components.button import ButtonEntity
|
2023-12-13 16:05:37 +00:00
|
|
|
from homeassistant.const import CONF_ENTITY_CATEGORY, CONF_NAME, CONF_PAYLOAD, Platform
|
2021-11-10 19:34:35 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
2021-11-20 10:30:41 +00:00
|
|
|
from homeassistant.helpers.typing import ConfigType
|
|
|
|
|
2024-08-02 10:53:39 +00:00
|
|
|
from . import KNXModule
|
2023-12-13 16:05:37 +00:00
|
|
|
from .const import CONF_PAYLOAD_LENGTH, DATA_KNX_CONFIG, DOMAIN, KNX_ADDRESS
|
2024-08-05 10:34:48 +00:00
|
|
|
from .knx_entity import KnxYamlEntity
|
2021-11-10 19:34:35 +00:00
|
|
|
|
|
|
|
|
2021-11-20 10:30:41 +00:00
|
|
|
async def async_setup_entry(
|
2021-11-10 19:34:35 +00:00
|
|
|
hass: HomeAssistant,
|
2021-11-20 10:30:41 +00:00
|
|
|
config_entry: config_entries.ConfigEntry,
|
2021-11-10 19:34:35 +00:00
|
|
|
async_add_entities: AddEntitiesCallback,
|
|
|
|
) -> None:
|
2021-11-20 10:30:41 +00:00
|
|
|
"""Set up the KNX binary sensor platform."""
|
2024-08-02 10:53:39 +00:00
|
|
|
knx_module: KNXModule = hass.data[DOMAIN]
|
2021-11-20 10:30:41 +00:00
|
|
|
config: ConfigType = hass.data[DATA_KNX_CONFIG]
|
2021-11-10 19:34:35 +00:00
|
|
|
|
|
|
|
async_add_entities(
|
2024-08-02 10:53:39 +00:00
|
|
|
KNXButton(knx_module, entity_config)
|
|
|
|
for entity_config in config[Platform.BUTTON]
|
2021-11-10 19:34:35 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
2024-08-05 10:34:48 +00:00
|
|
|
class KNXButton(KnxYamlEntity, ButtonEntity):
|
2021-11-10 19:34:35 +00:00
|
|
|
"""Representation of a KNX button."""
|
|
|
|
|
|
|
|
_device: XknxRawValue
|
|
|
|
|
2024-08-02 10:53:39 +00:00
|
|
|
def __init__(self, knx_module: KNXModule, config: ConfigType) -> None:
|
2021-11-10 19:34:35 +00:00
|
|
|
"""Initialize a KNX button."""
|
|
|
|
super().__init__(
|
2024-08-02 10:53:39 +00:00
|
|
|
knx_module=knx_module,
|
2021-11-10 19:34:35 +00:00
|
|
|
device=XknxRawValue(
|
2024-08-02 10:53:39 +00:00
|
|
|
xknx=knx_module.xknx,
|
2021-11-10 19:34:35 +00:00
|
|
|
name=config[CONF_NAME],
|
|
|
|
payload_length=config[CONF_PAYLOAD_LENGTH],
|
|
|
|
group_address=config[KNX_ADDRESS],
|
2024-08-02 10:53:39 +00:00
|
|
|
),
|
2021-11-10 19:34:35 +00:00
|
|
|
)
|
|
|
|
self._payload = config[CONF_PAYLOAD]
|
|
|
|
self._attr_entity_category = config.get(CONF_ENTITY_CATEGORY)
|
|
|
|
self._attr_unique_id = (
|
|
|
|
f"{self._device.remote_value.group_address}_{self._payload}"
|
|
|
|
)
|
|
|
|
|
|
|
|
async def async_press(self) -> None:
|
|
|
|
"""Press the button."""
|
|
|
|
await self._device.set(self._payload)
|