hass-core/homeassistant/components/knx/factory.py
Matthias Alphart 3e1f51883e
Create KNX weather entity directly from config (#49640)
* create climate entities directly from config

* deprecate create_sensors

* move create staticmethod to module level

* add comment for deprecation date
2021-05-21 01:30:37 +02:00

35 lines
1.1 KiB
Python

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