diff --git a/homeassistant/components/knx/__init__.py b/homeassistant/components/knx/__init__.py index a52163cfca3..e98e598af1d 100644 --- a/homeassistant/components/knx/__init__.py +++ b/homeassistant/components/knx/__init__.py @@ -36,7 +36,6 @@ from homeassistant.helpers.typing import ConfigType from .const import DOMAIN, KNX_ADDRESS, SupportedPlatforms from .expose import KNXExposeSensor, KNXExposeTime, create_knx_exposure -from .factory import create_knx_device from .schema import ( BinarySensorSchema, ClimateSchema, @@ -229,19 +228,15 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool: ) for platform in SupportedPlatforms: - if platform.value in config[DOMAIN]: - for device_config in config[DOMAIN][platform.value]: - create_knx_device(platform, knx_module.xknx, device_config) - - # 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: + if platform.value not in config[DOMAIN]: + continue hass.async_create_task( discovery.async_load_platform( hass, platform.value, DOMAIN, { - "platform_config": config[DOMAIN].get(platform.value), + "platform_config": config[DOMAIN][platform.value], }, config, ) diff --git a/homeassistant/components/knx/factory.py b/homeassistant/components/knx/factory.py deleted file mode 100644 index 99885c8387a..00000000000 --- a/homeassistant/components/knx/factory.py +++ /dev/null @@ -1,35 +0,0 @@ -"""Factory function to initialize KNX devices from config.""" -from __future__ import annotations - -from xknx import XKNX -from xknx.devices import Device as XknxDevice, Sensor as XknxSensor - -from homeassistant.const import CONF_NAME, CONF_TYPE -from homeassistant.helpers.typing import ConfigType - -from .const import SupportedPlatforms -from .schema import SensorSchema - - -def create_knx_device( - platform: SupportedPlatforms, - knx_module: XKNX, - config: ConfigType, -) -> XknxDevice | None: - """Return the requested XKNX device.""" - if platform is SupportedPlatforms.SENSOR: - return _create_sensor(knx_module, config) - - return None - - -def _create_sensor(knx_module: XKNX, config: ConfigType) -> XknxSensor: - """Return a KNX sensor to be used within XKNX.""" - return XknxSensor( - knx_module, - name=config[CONF_NAME], - group_address_state=config[SensorSchema.CONF_STATE_ADDRESS], - sync_state=config[SensorSchema.CONF_SYNC_STATE], - always_callback=config[SensorSchema.CONF_ALWAYS_CALLBACK], - value_type=config[CONF_TYPE], - ) diff --git a/homeassistant/components/knx/sensor.py b/homeassistant/components/knx/sensor.py index fa4de79cb03..21586faf58c 100644 --- a/homeassistant/components/knx/sensor.py +++ b/homeassistant/components/knx/sensor.py @@ -3,9 +3,11 @@ from __future__ import annotations from typing import Any +from xknx import XKNX from xknx.devices import Sensor as XknxSensor from homeassistant.components.sensor import DEVICE_CLASSES, SensorEntity +from homeassistant.const import CONF_NAME, CONF_TYPE from homeassistant.core import HomeAssistant from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType, StateType @@ -13,6 +15,7 @@ from homeassistant.util import dt from .const import ATTR_LAST_KNX_UPDATE, ATTR_SOURCE, DOMAIN from .knx_entity import KnxEntity +from .schema import SensorSchema async def async_setup_platform( @@ -22,20 +25,38 @@ async def async_setup_platform( discovery_info: DiscoveryInfoType | None = None, ) -> None: """Set up sensor(s) 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, XknxSensor): - entities.append(KNXSensor(device)) + for entity_config in platform_config: + entities.append(KNXSensor(xknx, entity_config)) + async_add_entities(entities) +def _create_sensor(xknx: XKNX, config: ConfigType) -> XknxSensor: + """Return a KNX sensor to be used within XKNX.""" + return XknxSensor( + xknx, + name=config[CONF_NAME], + group_address_state=config[SensorSchema.CONF_STATE_ADDRESS], + sync_state=config[SensorSchema.CONF_SYNC_STATE], + always_callback=config[SensorSchema.CONF_ALWAYS_CALLBACK], + value_type=config[CONF_TYPE], + ) + + class KNXSensor(KnxEntity, SensorEntity): """Representation of a KNX sensor.""" - def __init__(self, device: XknxSensor) -> None: + def __init__(self, xknx: XKNX, config: ConfigType) -> None: """Initialize of a KNX sensor.""" self._device: XknxSensor - super().__init__(device) + super().__init__(_create_sensor(xknx, config)) self._unique_id = f"{self._device.sensor_value.group_address_state}" @property