Create KNX switch entity directly from config (#49238)
parent
e234fc6e7e
commit
985b4a581a
homeassistant/components/knx
|
@ -235,7 +235,15 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
|
|||
# We need to wait until all entities are loaded into the device list since they could also be created from other platforms
|
||||
for platform in SupportedPlatforms:
|
||||
hass.async_create_task(
|
||||
discovery.async_load_platform(hass, platform.value, DOMAIN, {}, config)
|
||||
discovery.async_load_platform(
|
||||
hass,
|
||||
platform.value,
|
||||
DOMAIN,
|
||||
{
|
||||
"platform_config": config[DOMAIN].get(platform.value),
|
||||
},
|
||||
config,
|
||||
)
|
||||
)
|
||||
|
||||
hass.services.async_register(
|
||||
|
|
|
@ -13,7 +13,6 @@ from xknx.devices import (
|
|||
Notification as XknxNotification,
|
||||
Scene as XknxScene,
|
||||
Sensor as XknxSensor,
|
||||
Switch as XknxSwitch,
|
||||
Weather as XknxWeather,
|
||||
)
|
||||
|
||||
|
@ -29,7 +28,6 @@ from .schema import (
|
|||
LightSchema,
|
||||
SceneSchema,
|
||||
SensorSchema,
|
||||
SwitchSchema,
|
||||
WeatherSchema,
|
||||
)
|
||||
|
||||
|
@ -38,7 +36,7 @@ def create_knx_device(
|
|||
platform: SupportedPlatforms,
|
||||
knx_module: XKNX,
|
||||
config: ConfigType,
|
||||
) -> XknxDevice:
|
||||
) -> XknxDevice | None:
|
||||
"""Return the requested XKNX device."""
|
||||
if platform is SupportedPlatforms.LIGHT:
|
||||
return _create_light(knx_module, config)
|
||||
|
@ -49,9 +47,6 @@ def create_knx_device(
|
|||
if platform is SupportedPlatforms.CLIMATE:
|
||||
return _create_climate(knx_module, config)
|
||||
|
||||
if platform is SupportedPlatforms.SWITCH:
|
||||
return _create_switch(knx_module, config)
|
||||
|
||||
if platform is SupportedPlatforms.SENSOR:
|
||||
return _create_sensor(knx_module, config)
|
||||
|
||||
|
@ -70,6 +65,8 @@ def create_knx_device(
|
|||
if platform is SupportedPlatforms.FAN:
|
||||
return _create_fan(knx_module, config)
|
||||
|
||||
return None
|
||||
|
||||
|
||||
def _create_cover(knx_module: XKNX, config: ConfigType) -> XknxCover:
|
||||
"""Return a KNX Cover device to be used within XKNX."""
|
||||
|
@ -270,17 +267,6 @@ def _create_climate(knx_module: XKNX, config: ConfigType) -> XknxClimate:
|
|||
)
|
||||
|
||||
|
||||
def _create_switch(knx_module: XKNX, config: ConfigType) -> XknxSwitch:
|
||||
"""Return a KNX switch to be used within XKNX."""
|
||||
return XknxSwitch(
|
||||
knx_module,
|
||||
name=config[CONF_NAME],
|
||||
group_address=config[KNX_ADDRESS],
|
||||
group_address_state=config.get(SwitchSchema.CONF_STATE_ADDRESS),
|
||||
invert=config[SwitchSchema.CONF_INVERT],
|
||||
)
|
||||
|
||||
|
||||
def _create_sensor(knx_module: XKNX, config: ConfigType) -> XknxSensor:
|
||||
"""Return a KNX sensor to be used within XKNX."""
|
||||
return XknxSensor(
|
||||
|
|
|
@ -3,15 +3,18 @@ from __future__ import annotations
|
|||
|
||||
from typing import Any, Callable, Iterable
|
||||
|
||||
from xknx import XKNX
|
||||
from xknx.devices import Switch as XknxSwitch
|
||||
|
||||
from homeassistant.components.switch import SwitchEntity
|
||||
from homeassistant.const import CONF_NAME
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers.entity import Entity
|
||||
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
|
||||
|
||||
from .const import DOMAIN
|
||||
from .const import DOMAIN, KNX_ADDRESS
|
||||
from .knx_entity import KnxEntity
|
||||
from .schema import SwitchSchema
|
||||
|
||||
|
||||
async def async_setup_platform(
|
||||
|
@ -21,20 +24,34 @@ async def async_setup_platform(
|
|||
discovery_info: DiscoveryInfoType | None = None,
|
||||
) -> None:
|
||||
"""Set up switch(es) 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 = []
|
||||
for device in hass.data[DOMAIN].xknx.devices:
|
||||
if isinstance(device, XknxSwitch):
|
||||
entities.append(KNXSwitch(device))
|
||||
for entity_config in platform_config:
|
||||
entities.append(KNXSwitch(xknx, entity_config))
|
||||
|
||||
async_add_entities(entities)
|
||||
|
||||
|
||||
class KNXSwitch(KnxEntity, SwitchEntity):
|
||||
"""Representation of a KNX switch."""
|
||||
|
||||
def __init__(self, device: XknxSwitch) -> None:
|
||||
def __init__(self, xknx: XKNX, config: ConfigType) -> None:
|
||||
"""Initialize of KNX switch."""
|
||||
self._device: XknxSwitch
|
||||
super().__init__(device)
|
||||
super().__init__(
|
||||
device=XknxSwitch(
|
||||
xknx,
|
||||
name=config[CONF_NAME],
|
||||
group_address=config[KNX_ADDRESS],
|
||||
group_address_state=config.get(SwitchSchema.CONF_STATE_ADDRESS),
|
||||
invert=config[SwitchSchema.CONF_INVERT],
|
||||
)
|
||||
)
|
||||
|
||||
@property
|
||||
def is_on(self) -> bool:
|
||||
|
|
Loading…
Reference in New Issue