From 3e1f51883ed01e90c2e98f3885cbcb99d4e61b56 Mon Sep 17 00:00:00 2001 From: Matthias Alphart Date: Fri, 21 May 2021 01:30:37 +0200 Subject: [PATCH] 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 --- homeassistant/components/knx/factory.py | 48 +-------------------- homeassistant/components/knx/schema.py | 42 ++++++++++--------- homeassistant/components/knx/weather.py | 56 ++++++++++++++++++++++--- 3 files changed, 74 insertions(+), 72 deletions(-) diff --git a/homeassistant/components/knx/factory.py b/homeassistant/components/knx/factory.py index 7b5b19dcdd2..99885c8387a 100644 --- a/homeassistant/components/knx/factory.py +++ b/homeassistant/components/knx/factory.py @@ -2,17 +2,13 @@ from __future__ import annotations from xknx import XKNX -from xknx.devices import ( - Device as XknxDevice, - Sensor as XknxSensor, - Weather as XknxWeather, -) +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, WeatherSchema +from .schema import SensorSchema def create_knx_device( @@ -24,9 +20,6 @@ def create_knx_device( if platform is SupportedPlatforms.SENSOR: return _create_sensor(knx_module, config) - if platform is SupportedPlatforms.WEATHER: - return _create_weather(knx_module, config) - return None @@ -40,40 +33,3 @@ def _create_sensor(knx_module: XKNX, config: ConfigType) -> XknxSensor: always_callback=config[SensorSchema.CONF_ALWAYS_CALLBACK], value_type=config[CONF_TYPE], ) - - -def _create_weather(knx_module: XKNX, config: ConfigType) -> XknxWeather: - """Return a KNX weather device to be used within XKNX.""" - return XknxWeather( - knx_module, - name=config[CONF_NAME], - sync_state=config[WeatherSchema.CONF_SYNC_STATE], - create_sensors=config[WeatherSchema.CONF_KNX_CREATE_SENSORS], - group_address_temperature=config[WeatherSchema.CONF_KNX_TEMPERATURE_ADDRESS], - group_address_brightness_south=config.get( - WeatherSchema.CONF_KNX_BRIGHTNESS_SOUTH_ADDRESS - ), - group_address_brightness_east=config.get( - WeatherSchema.CONF_KNX_BRIGHTNESS_EAST_ADDRESS - ), - group_address_brightness_west=config.get( - WeatherSchema.CONF_KNX_BRIGHTNESS_WEST_ADDRESS - ), - group_address_brightness_north=config.get( - WeatherSchema.CONF_KNX_BRIGHTNESS_NORTH_ADDRESS - ), - group_address_wind_speed=config.get(WeatherSchema.CONF_KNX_WIND_SPEED_ADDRESS), - group_address_wind_bearing=config.get( - WeatherSchema.CONF_KNX_WIND_BEARING_ADDRESS - ), - group_address_rain_alarm=config.get(WeatherSchema.CONF_KNX_RAIN_ALARM_ADDRESS), - group_address_frost_alarm=config.get( - WeatherSchema.CONF_KNX_FROST_ALARM_ADDRESS - ), - group_address_wind_alarm=config.get(WeatherSchema.CONF_KNX_WIND_ALARM_ADDRESS), - group_address_day_night=config.get(WeatherSchema.CONF_KNX_DAY_NIGHT_ADDRESS), - group_address_air_pressure=config.get( - WeatherSchema.CONF_KNX_AIR_PRESSURE_ADDRESS - ), - group_address_humidity=config.get(WeatherSchema.CONF_KNX_HUMIDITY_ADDRESS), - ) diff --git a/homeassistant/components/knx/schema.py b/homeassistant/components/knx/schema.py index 2dde2fd7160..4fd0aaaa3b9 100644 --- a/homeassistant/components/knx/schema.py +++ b/homeassistant/components/knx/schema.py @@ -521,27 +521,29 @@ class WeatherSchema: CONF_KNX_DAY_NIGHT_ADDRESS = "address_day_night" CONF_KNX_AIR_PRESSURE_ADDRESS = "address_air_pressure" CONF_KNX_HUMIDITY_ADDRESS = "address_humidity" - CONF_KNX_CREATE_SENSORS = "create_sensors" DEFAULT_NAME = "KNX Weather Station" - SCHEMA = vol.Schema( - { - vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string, - vol.Optional(CONF_SYNC_STATE, default=True): sync_state_validator, - vol.Optional(CONF_KNX_CREATE_SENSORS, default=False): cv.boolean, - vol.Required(CONF_KNX_TEMPERATURE_ADDRESS): ga_list_validator, - vol.Optional(CONF_KNX_BRIGHTNESS_SOUTH_ADDRESS): ga_list_validator, - vol.Optional(CONF_KNX_BRIGHTNESS_EAST_ADDRESS): ga_list_validator, - vol.Optional(CONF_KNX_BRIGHTNESS_WEST_ADDRESS): ga_list_validator, - vol.Optional(CONF_KNX_BRIGHTNESS_NORTH_ADDRESS): ga_list_validator, - vol.Optional(CONF_KNX_WIND_SPEED_ADDRESS): ga_list_validator, - vol.Optional(CONF_KNX_WIND_BEARING_ADDRESS): ga_list_validator, - vol.Optional(CONF_KNX_RAIN_ALARM_ADDRESS): ga_list_validator, - vol.Optional(CONF_KNX_FROST_ALARM_ADDRESS): ga_list_validator, - vol.Optional(CONF_KNX_WIND_ALARM_ADDRESS): ga_list_validator, - vol.Optional(CONF_KNX_DAY_NIGHT_ADDRESS): ga_list_validator, - vol.Optional(CONF_KNX_AIR_PRESSURE_ADDRESS): ga_list_validator, - vol.Optional(CONF_KNX_HUMIDITY_ADDRESS): ga_list_validator, - } + SCHEMA = vol.All( + # deprecated since 2021.6 + cv.deprecated("create_sensors"), + vol.Schema( + { + vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string, + vol.Optional(CONF_SYNC_STATE, default=True): sync_state_validator, + vol.Required(CONF_KNX_TEMPERATURE_ADDRESS): ga_list_validator, + vol.Optional(CONF_KNX_BRIGHTNESS_SOUTH_ADDRESS): ga_list_validator, + vol.Optional(CONF_KNX_BRIGHTNESS_EAST_ADDRESS): ga_list_validator, + vol.Optional(CONF_KNX_BRIGHTNESS_WEST_ADDRESS): ga_list_validator, + vol.Optional(CONF_KNX_BRIGHTNESS_NORTH_ADDRESS): ga_list_validator, + vol.Optional(CONF_KNX_WIND_SPEED_ADDRESS): ga_list_validator, + vol.Optional(CONF_KNX_WIND_BEARING_ADDRESS): ga_list_validator, + vol.Optional(CONF_KNX_RAIN_ALARM_ADDRESS): ga_list_validator, + vol.Optional(CONF_KNX_FROST_ALARM_ADDRESS): ga_list_validator, + vol.Optional(CONF_KNX_WIND_ALARM_ADDRESS): ga_list_validator, + vol.Optional(CONF_KNX_DAY_NIGHT_ADDRESS): ga_list_validator, + vol.Optional(CONF_KNX_AIR_PRESSURE_ADDRESS): ga_list_validator, + vol.Optional(CONF_KNX_HUMIDITY_ADDRESS): ga_list_validator, + } + ), ) diff --git a/homeassistant/components/knx/weather.py b/homeassistant/components/knx/weather.py index 18cb217105c..b396142e387 100644 --- a/homeassistant/components/knx/weather.py +++ b/homeassistant/components/knx/weather.py @@ -1,16 +1,18 @@ """Support for KNX/IP weather station.""" from __future__ import annotations +from xknx import XKNX from xknx.devices import Weather as XknxWeather from homeassistant.components.weather import WeatherEntity -from homeassistant.const import TEMP_CELSIUS +from homeassistant.const import CONF_NAME, TEMP_CELSIUS from homeassistant.core import HomeAssistant from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType from .const import DOMAIN from .knx_entity import KnxEntity +from .schema import WeatherSchema async def async_setup_platform( @@ -20,20 +22,62 @@ async def async_setup_platform( discovery_info: DiscoveryInfoType | None = None, ) -> None: """Set up weather entities 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, XknxWeather): - entities.append(KNXWeather(device)) + for entity_config in platform_config: + entities.append(KNXWeather(xknx, entity_config)) + async_add_entities(entities) +def _create_weather(xknx: XKNX, config: ConfigType) -> XknxWeather: + """Return a KNX weather device to be used within XKNX.""" + return XknxWeather( + xknx, + name=config[CONF_NAME], + sync_state=config[WeatherSchema.CONF_SYNC_STATE], + group_address_temperature=config[WeatherSchema.CONF_KNX_TEMPERATURE_ADDRESS], + group_address_brightness_south=config.get( + WeatherSchema.CONF_KNX_BRIGHTNESS_SOUTH_ADDRESS + ), + group_address_brightness_east=config.get( + WeatherSchema.CONF_KNX_BRIGHTNESS_EAST_ADDRESS + ), + group_address_brightness_west=config.get( + WeatherSchema.CONF_KNX_BRIGHTNESS_WEST_ADDRESS + ), + group_address_brightness_north=config.get( + WeatherSchema.CONF_KNX_BRIGHTNESS_NORTH_ADDRESS + ), + group_address_wind_speed=config.get(WeatherSchema.CONF_KNX_WIND_SPEED_ADDRESS), + group_address_wind_bearing=config.get( + WeatherSchema.CONF_KNX_WIND_BEARING_ADDRESS + ), + group_address_rain_alarm=config.get(WeatherSchema.CONF_KNX_RAIN_ALARM_ADDRESS), + group_address_frost_alarm=config.get( + WeatherSchema.CONF_KNX_FROST_ALARM_ADDRESS + ), + group_address_wind_alarm=config.get(WeatherSchema.CONF_KNX_WIND_ALARM_ADDRESS), + group_address_day_night=config.get(WeatherSchema.CONF_KNX_DAY_NIGHT_ADDRESS), + group_address_air_pressure=config.get( + WeatherSchema.CONF_KNX_AIR_PRESSURE_ADDRESS + ), + group_address_humidity=config.get(WeatherSchema.CONF_KNX_HUMIDITY_ADDRESS), + ) + + class KNXWeather(KnxEntity, WeatherEntity): """Representation of a KNX weather device.""" - def __init__(self, device: XknxWeather) -> None: + def __init__(self, xknx: XKNX, config: ConfigType) -> None: """Initialize of a KNX sensor.""" self._device: XknxWeather - super().__init__(device) + super().__init__(_create_weather(xknx, config)) self._unique_id = f"{self._device._temperature.group_address_state}" @property