Create KNX sensor entity directly from config (#49642)

* create sensor entities directly from config

* move staticmethod to module level

* remove factory call
pull/51088/head
Matthias Alphart 2021-05-25 20:16:54 +02:00 committed by GitHub
parent 9ec0b0a8da
commit 8b21a652ba
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 29 additions and 48 deletions

View File

@ -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,
)

View File

@ -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],
)

View File

@ -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