mirror of
https://github.com/home-assistant/core.git
synced 2025-07-19 19:27:45 +00:00
Create KNX switch entity directly from config (#49238)
This commit is contained in:
parent
e234fc6e7e
commit
985b4a581a
@ -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
|
# 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:
|
for platform in SupportedPlatforms:
|
||||||
hass.async_create_task(
|
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(
|
hass.services.async_register(
|
||||||
|
@ -13,7 +13,6 @@ from xknx.devices import (
|
|||||||
Notification as XknxNotification,
|
Notification as XknxNotification,
|
||||||
Scene as XknxScene,
|
Scene as XknxScene,
|
||||||
Sensor as XknxSensor,
|
Sensor as XknxSensor,
|
||||||
Switch as XknxSwitch,
|
|
||||||
Weather as XknxWeather,
|
Weather as XknxWeather,
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -29,7 +28,6 @@ from .schema import (
|
|||||||
LightSchema,
|
LightSchema,
|
||||||
SceneSchema,
|
SceneSchema,
|
||||||
SensorSchema,
|
SensorSchema,
|
||||||
SwitchSchema,
|
|
||||||
WeatherSchema,
|
WeatherSchema,
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -38,7 +36,7 @@ def create_knx_device(
|
|||||||
platform: SupportedPlatforms,
|
platform: SupportedPlatforms,
|
||||||
knx_module: XKNX,
|
knx_module: XKNX,
|
||||||
config: ConfigType,
|
config: ConfigType,
|
||||||
) -> XknxDevice:
|
) -> XknxDevice | None:
|
||||||
"""Return the requested XKNX device."""
|
"""Return the requested XKNX device."""
|
||||||
if platform is SupportedPlatforms.LIGHT:
|
if platform is SupportedPlatforms.LIGHT:
|
||||||
return _create_light(knx_module, config)
|
return _create_light(knx_module, config)
|
||||||
@ -49,9 +47,6 @@ def create_knx_device(
|
|||||||
if platform is SupportedPlatforms.CLIMATE:
|
if platform is SupportedPlatforms.CLIMATE:
|
||||||
return _create_climate(knx_module, config)
|
return _create_climate(knx_module, config)
|
||||||
|
|
||||||
if platform is SupportedPlatforms.SWITCH:
|
|
||||||
return _create_switch(knx_module, config)
|
|
||||||
|
|
||||||
if platform is SupportedPlatforms.SENSOR:
|
if platform is SupportedPlatforms.SENSOR:
|
||||||
return _create_sensor(knx_module, config)
|
return _create_sensor(knx_module, config)
|
||||||
|
|
||||||
@ -70,6 +65,8 @@ def create_knx_device(
|
|||||||
if platform is SupportedPlatforms.FAN:
|
if platform is SupportedPlatforms.FAN:
|
||||||
return _create_fan(knx_module, config)
|
return _create_fan(knx_module, config)
|
||||||
|
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
def _create_cover(knx_module: XKNX, config: ConfigType) -> XknxCover:
|
def _create_cover(knx_module: XKNX, config: ConfigType) -> XknxCover:
|
||||||
"""Return a KNX Cover device to be used within XKNX."""
|
"""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:
|
def _create_sensor(knx_module: XKNX, config: ConfigType) -> XknxSensor:
|
||||||
"""Return a KNX sensor to be used within XKNX."""
|
"""Return a KNX sensor to be used within XKNX."""
|
||||||
return XknxSensor(
|
return XknxSensor(
|
||||||
|
@ -3,15 +3,18 @@ from __future__ import annotations
|
|||||||
|
|
||||||
from typing import Any, Callable, Iterable
|
from typing import Any, Callable, Iterable
|
||||||
|
|
||||||
|
from xknx import XKNX
|
||||||
from xknx.devices import Switch as XknxSwitch
|
from xknx.devices import Switch as XknxSwitch
|
||||||
|
|
||||||
from homeassistant.components.switch import SwitchEntity
|
from homeassistant.components.switch import SwitchEntity
|
||||||
|
from homeassistant.const import CONF_NAME
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
from homeassistant.helpers.entity import Entity
|
from homeassistant.helpers.entity import Entity
|
||||||
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 SwitchSchema
|
||||||
|
|
||||||
|
|
||||||
async def async_setup_platform(
|
async def async_setup_platform(
|
||||||
@ -21,20 +24,34 @@ async def async_setup_platform(
|
|||||||
discovery_info: DiscoveryInfoType | None = None,
|
discovery_info: DiscoveryInfoType | None = None,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Set up switch(es) for KNX platform."""
|
"""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 = []
|
entities = []
|
||||||
for device in hass.data[DOMAIN].xknx.devices:
|
for entity_config in platform_config:
|
||||||
if isinstance(device, XknxSwitch):
|
entities.append(KNXSwitch(xknx, entity_config))
|
||||||
entities.append(KNXSwitch(device))
|
|
||||||
async_add_entities(entities)
|
async_add_entities(entities)
|
||||||
|
|
||||||
|
|
||||||
class KNXSwitch(KnxEntity, SwitchEntity):
|
class KNXSwitch(KnxEntity, SwitchEntity):
|
||||||
"""Representation of a KNX switch."""
|
"""Representation of a KNX switch."""
|
||||||
|
|
||||||
def __init__(self, device: XknxSwitch) -> None:
|
def __init__(self, xknx: XKNX, config: ConfigType) -> None:
|
||||||
"""Initialize of KNX switch."""
|
"""Initialize of KNX switch."""
|
||||||
self._device: XknxSwitch
|
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
|
@property
|
||||||
def is_on(self) -> bool:
|
def is_on(self) -> bool:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user