mirror of
https://github.com/home-assistant/core.git
synced 2025-07-25 22:27:07 +00:00
Create KNX scene entities directly from config (#50686)
This commit is contained in:
parent
a92acdb528
commit
222336a1db
@ -11,7 +11,6 @@ from xknx.devices import (
|
|||||||
Fan as XknxFan,
|
Fan as XknxFan,
|
||||||
Light as XknxLight,
|
Light as XknxLight,
|
||||||
Notification as XknxNotification,
|
Notification as XknxNotification,
|
||||||
Scene as XknxScene,
|
|
||||||
Sensor as XknxSensor,
|
Sensor as XknxSensor,
|
||||||
Weather as XknxWeather,
|
Weather as XknxWeather,
|
||||||
)
|
)
|
||||||
@ -26,7 +25,6 @@ from .schema import (
|
|||||||
CoverSchema,
|
CoverSchema,
|
||||||
FanSchema,
|
FanSchema,
|
||||||
LightSchema,
|
LightSchema,
|
||||||
SceneSchema,
|
|
||||||
SensorSchema,
|
SensorSchema,
|
||||||
WeatherSchema,
|
WeatherSchema,
|
||||||
)
|
)
|
||||||
@ -53,9 +51,6 @@ def create_knx_device(
|
|||||||
if platform is SupportedPlatforms.NOTIFY:
|
if platform is SupportedPlatforms.NOTIFY:
|
||||||
return _create_notify(knx_module, config)
|
return _create_notify(knx_module, config)
|
||||||
|
|
||||||
if platform is SupportedPlatforms.SCENE:
|
|
||||||
return _create_scene(knx_module, config)
|
|
||||||
|
|
||||||
if platform is SupportedPlatforms.BINARY_SENSOR:
|
if platform is SupportedPlatforms.BINARY_SENSOR:
|
||||||
return _create_binary_sensor(knx_module, config)
|
return _create_binary_sensor(knx_module, config)
|
||||||
|
|
||||||
@ -288,16 +283,6 @@ def _create_notify(knx_module: XKNX, config: ConfigType) -> XknxNotification:
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
def _create_scene(knx_module: XKNX, config: ConfigType) -> XknxScene:
|
|
||||||
"""Return a KNX scene to be used within XKNX."""
|
|
||||||
return XknxScene(
|
|
||||||
knx_module,
|
|
||||||
name=config[CONF_NAME],
|
|
||||||
group_address=config[KNX_ADDRESS],
|
|
||||||
scene_number=config[SceneSchema.CONF_SCENE_NUMBER],
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
def _create_binary_sensor(knx_module: XKNX, config: ConfigType) -> XknxBinarySensor:
|
def _create_binary_sensor(knx_module: XKNX, config: ConfigType) -> XknxBinarySensor:
|
||||||
"""Return a KNX binary sensor to be used within XKNX."""
|
"""Return a KNX binary sensor to be used within XKNX."""
|
||||||
device_name = config[CONF_NAME]
|
device_name = config[CONF_NAME]
|
||||||
|
@ -3,15 +3,18 @@ from __future__ import annotations
|
|||||||
|
|
||||||
from typing import Any
|
from typing import Any
|
||||||
|
|
||||||
|
from xknx import XKNX
|
||||||
from xknx.devices import Scene as XknxScene
|
from xknx.devices import Scene as XknxScene
|
||||||
|
|
||||||
from homeassistant.components.scene import Scene
|
from homeassistant.components.scene import Scene
|
||||||
|
from homeassistant.const import CONF_NAME
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||||
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
|
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
|
||||||
|
|
||||||
from .const import DOMAIN
|
from .const import DOMAIN, KNX_ADDRESS
|
||||||
from .knx_entity import KnxEntity
|
from .knx_entity import KnxEntity
|
||||||
|
from .schema import SceneSchema
|
||||||
|
|
||||||
|
|
||||||
async def async_setup_platform(
|
async def async_setup_platform(
|
||||||
@ -21,20 +24,33 @@ async def async_setup_platform(
|
|||||||
discovery_info: DiscoveryInfoType | None = None,
|
discovery_info: DiscoveryInfoType | None = None,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Set up the scenes for KNX platform."""
|
"""Set up the scenes for KNX platform."""
|
||||||
|
if not discovery_info or not discovery_info["platform_config"]:
|
||||||
|
return
|
||||||
|
|
||||||
|
platform_config = discovery_info["platform_config"]
|
||||||
|
xknx: XKNX = hass.data[DOMAIN].xknx
|
||||||
|
|
||||||
entities = []
|
entities = []
|
||||||
for device in hass.data[DOMAIN].xknx.devices:
|
for entity_config in platform_config:
|
||||||
if isinstance(device, XknxScene):
|
entities.append(KNXScene(xknx, entity_config))
|
||||||
entities.append(KNXScene(device))
|
|
||||||
async_add_entities(entities)
|
async_add_entities(entities)
|
||||||
|
|
||||||
|
|
||||||
class KNXScene(KnxEntity, Scene):
|
class KNXScene(KnxEntity, Scene):
|
||||||
"""Representation of a KNX scene."""
|
"""Representation of a KNX scene."""
|
||||||
|
|
||||||
def __init__(self, device: XknxScene) -> None:
|
def __init__(self, xknx: XKNX, config: ConfigType) -> None:
|
||||||
"""Init KNX scene."""
|
"""Init KNX scene."""
|
||||||
self._device: XknxScene
|
self._device: XknxScene
|
||||||
super().__init__(device)
|
super().__init__(
|
||||||
|
device=XknxScene(
|
||||||
|
xknx,
|
||||||
|
name=config[CONF_NAME],
|
||||||
|
group_address=config[KNX_ADDRESS],
|
||||||
|
scene_number=config[SceneSchema.CONF_SCENE_NUMBER],
|
||||||
|
)
|
||||||
|
)
|
||||||
self._unique_id = (
|
self._unique_id = (
|
||||||
f"{self._device.scene_value.group_address}_{self._device.scene_number}"
|
f"{self._device.scene_value.group_address}_{self._device.scene_number}"
|
||||||
)
|
)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user