2019-02-14 04:35:12 +00:00
|
|
|
"""Support for KNX scenes."""
|
2021-03-19 09:16:27 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2021-05-04 12:50:06 +00:00
|
|
|
from typing import Any
|
2020-04-21 01:07:50 +00:00
|
|
|
|
2021-05-16 06:14:28 +00:00
|
|
|
from xknx import XKNX
|
2019-10-22 05:38:21 +00:00
|
|
|
from xknx.devices import Scene as XknxScene
|
2018-02-26 07:44:09 +00:00
|
|
|
|
2021-11-20 10:30:41 +00:00
|
|
|
from homeassistant import config_entries
|
2020-08-26 16:03:03 +00:00
|
|
|
from homeassistant.components.scene import Scene
|
2021-12-03 17:29:38 +00:00
|
|
|
from homeassistant.const import CONF_ENTITY_CATEGORY, CONF_NAME, Platform
|
2021-03-27 21:20:11 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
2021-05-04 12:50:06 +00:00
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
2021-11-20 10:30:41 +00:00
|
|
|
from homeassistant.helpers.typing import ConfigType
|
2018-02-26 07:44:09 +00:00
|
|
|
|
2021-12-03 17:29:38 +00:00
|
|
|
from .const import DATA_KNX_CONFIG, DOMAIN, KNX_ADDRESS
|
2020-09-21 16:08:35 +00:00
|
|
|
from .knx_entity import KnxEntity
|
2021-05-16 06:14:28 +00:00
|
|
|
from .schema import SceneSchema
|
2019-03-21 05:56:46 +00:00
|
|
|
|
2018-02-26 07:44:09 +00:00
|
|
|
|
2021-11-20 10:30:41 +00:00
|
|
|
async def async_setup_entry(
|
2021-03-27 21:20:11 +00:00
|
|
|
hass: HomeAssistant,
|
2021-11-20 10:30:41 +00:00
|
|
|
config_entry: config_entries.ConfigEntry,
|
2021-05-04 12:50:06 +00:00
|
|
|
async_add_entities: AddEntitiesCallback,
|
2021-03-19 09:16:27 +00:00
|
|
|
) -> None:
|
2021-11-20 10:30:41 +00:00
|
|
|
"""Set up scene(s) for KNX platform."""
|
2021-05-16 06:14:28 +00:00
|
|
|
xknx: XKNX = hass.data[DOMAIN].xknx
|
2021-12-03 17:29:38 +00:00
|
|
|
config: list[ConfigType] = hass.data[DATA_KNX_CONFIG][Platform.SCENE]
|
2021-05-16 06:14:28 +00:00
|
|
|
|
2021-11-20 10:30:41 +00:00
|
|
|
async_add_entities(KNXScene(xknx, entity_config) for entity_config in config)
|
2018-02-26 07:44:09 +00:00
|
|
|
|
|
|
|
|
2020-09-21 16:08:35 +00:00
|
|
|
class KNXScene(KnxEntity, Scene):
|
2018-02-26 07:44:09 +00:00
|
|
|
"""Representation of a KNX scene."""
|
|
|
|
|
2021-06-26 12:30:36 +00:00
|
|
|
_device: XknxScene
|
|
|
|
|
2021-05-16 06:14:28 +00:00
|
|
|
def __init__(self, xknx: XKNX, config: ConfigType) -> None:
|
2018-02-26 07:44:09 +00:00
|
|
|
"""Init KNX scene."""
|
2021-05-16 06:14:28 +00:00
|
|
|
super().__init__(
|
|
|
|
device=XknxScene(
|
|
|
|
xknx,
|
|
|
|
name=config[CONF_NAME],
|
|
|
|
group_address=config[KNX_ADDRESS],
|
|
|
|
scene_number=config[SceneSchema.CONF_SCENE_NUMBER],
|
|
|
|
)
|
|
|
|
)
|
2021-10-25 03:12:26 +00:00
|
|
|
self._attr_entity_category = config.get(CONF_ENTITY_CATEGORY)
|
2021-06-26 12:30:36 +00:00
|
|
|
self._attr_unique_id = (
|
2021-04-28 13:50:01 +00:00
|
|
|
f"{self._device.scene_value.group_address}_{self._device.scene_number}"
|
|
|
|
)
|
2018-02-26 07:44:09 +00:00
|
|
|
|
2020-04-21 01:07:50 +00:00
|
|
|
async def async_activate(self, **kwargs: Any) -> None:
|
2018-02-26 07:44:09 +00:00
|
|
|
"""Activate the scene."""
|
2020-09-21 16:08:35 +00:00
|
|
|
await self._device.run()
|