From 3baa7b679db0df6bd398fcfb7314d7e5ae9ad019 Mon Sep 17 00:00:00 2001 From: Marvin Wichmann Date: Fri, 3 Dec 2021 18:29:38 +0100 Subject: [PATCH] Use new Platform enum in KNX (#60902) --- homeassistant/components/knx/__init__.py | 15 ++++---- homeassistant/components/knx/binary_sensor.py | 11 ++---- homeassistant/components/knx/button.py | 6 ++-- homeassistant/components/knx/climate.py | 6 ++-- homeassistant/components/knx/const.py | 33 +++++++++--------- homeassistant/components/knx/cover.py | 13 ++++--- homeassistant/components/knx/fan.py | 6 ++-- homeassistant/components/knx/light.py | 14 ++------ homeassistant/components/knx/number.py | 6 ++-- homeassistant/components/knx/scene.py | 8 ++--- homeassistant/components/knx/schema.py | 34 +++++++++---------- homeassistant/components/knx/select.py | 6 ++-- homeassistant/components/knx/sensor.py | 8 ++--- homeassistant/components/knx/switch.py | 13 ++----- homeassistant/components/knx/weather.py | 8 ++--- tests/components/knx/test_binary_sensor.py | 14 ++++---- tests/components/knx/test_button.py | 6 ++-- tests/components/knx/test_fan.py | 6 ++-- tests/components/knx/test_light.py | 28 ++++++++------- tests/components/knx/test_number.py | 4 +-- tests/components/knx/test_scene.py | 2 +- tests/components/knx/test_select.py | 6 ++-- tests/components/knx/test_sensor.py | 4 +-- tests/components/knx/test_switch.py | 6 ++-- tests/components/knx/test_weather.py | 2 +- 25 files changed, 118 insertions(+), 147 deletions(-) diff --git a/homeassistant/components/knx/__init__.py b/homeassistant/components/knx/__init__.py index 786c1264248..3e75c614f1e 100644 --- a/homeassistant/components/knx/__init__.py +++ b/homeassistant/components/knx/__init__.py @@ -47,7 +47,7 @@ from .const import ( DATA_KNX_CONFIG, DOMAIN, KNX_ADDRESS, - SupportedPlatforms, + SUPPORTED_PLATFORMS, ) from .expose import KNXExposeSensor, KNXExposeTime, create_knx_exposure from .schema import ( @@ -251,16 +251,15 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: ) hass.config_entries.async_setup_platforms( - entry, - [platform.value for platform in SupportedPlatforms if platform.value in config], + entry, [platform for platform in SUPPORTED_PLATFORMS if platform in config] ) # set up notify platform, no entry support for notify component yet, # have to use discovery to load platform. - if NotifySchema.PLATFORM_NAME in conf: + if NotifySchema.PLATFORM in conf: hass.async_create_task( discovery.async_load_platform( - hass, "notify", DOMAIN, conf[NotifySchema.PLATFORM_NAME], config + hass, "notify", DOMAIN, conf[NotifySchema.PLATFORM], config ) ) @@ -310,9 +309,9 @@ async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: unload_ok = await hass.config_entries.async_unload_platforms( entry, [ - platform.value - for platform in SupportedPlatforms - if platform.value in hass.data[DATA_KNX_CONFIG] + platform + for platform in SUPPORTED_PLATFORMS + if platform in hass.data[DATA_KNX_CONFIG] ], ) if unload_ok: diff --git a/homeassistant/components/knx/binary_sensor.py b/homeassistant/components/knx/binary_sensor.py index b3dbdc0db12..9005ca707b9 100644 --- a/homeassistant/components/knx/binary_sensor.py +++ b/homeassistant/components/knx/binary_sensor.py @@ -15,19 +15,14 @@ from homeassistant.const import ( STATE_ON, STATE_UNAVAILABLE, STATE_UNKNOWN, + Platform, ) from homeassistant.core import HomeAssistant from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.restore_state import RestoreEntity from homeassistant.helpers.typing import ConfigType -from .const import ( - ATTR_COUNTER, - ATTR_SOURCE, - DATA_KNX_CONFIG, - DOMAIN, - SupportedPlatforms, -) +from .const import ATTR_COUNTER, ATTR_SOURCE, DATA_KNX_CONFIG, DOMAIN from .knx_entity import KnxEntity from .schema import BinarySensorSchema @@ -43,7 +38,7 @@ async def async_setup_entry( async_add_entities( KNXBinarySensor(xknx, entity_config) - for entity_config in config[SupportedPlatforms.BINARY_SENSOR.value] + for entity_config in config[Platform.BINARY_SENSOR] ) diff --git a/homeassistant/components/knx/button.py b/homeassistant/components/knx/button.py index 457e1d6d43d..274ced80146 100644 --- a/homeassistant/components/knx/button.py +++ b/homeassistant/components/knx/button.py @@ -6,7 +6,7 @@ from xknx.devices import RawValue as XknxRawValue from homeassistant import config_entries from homeassistant.components.button import ButtonEntity -from homeassistant.const import CONF_ENTITY_CATEGORY, CONF_NAME +from homeassistant.const import CONF_ENTITY_CATEGORY, CONF_NAME, Platform from homeassistant.core import HomeAssistant from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.typing import ConfigType @@ -17,7 +17,6 @@ from .const import ( DATA_KNX_CONFIG, DOMAIN, KNX_ADDRESS, - SupportedPlatforms, ) from .knx_entity import KnxEntity @@ -32,8 +31,7 @@ async def async_setup_entry( config: ConfigType = hass.data[DATA_KNX_CONFIG] async_add_entities( - KNXButton(xknx, entity_config) - for entity_config in config[SupportedPlatforms.BUTTON.value] + KNXButton(xknx, entity_config) for entity_config in config[Platform.BUTTON] ) diff --git a/homeassistant/components/knx/climate.py b/homeassistant/components/knx/climate.py index 8517eed7ace..63fbb170ca7 100644 --- a/homeassistant/components/knx/climate.py +++ b/homeassistant/components/knx/climate.py @@ -22,6 +22,7 @@ from homeassistant.const import ( CONF_ENTITY_CATEGORY, CONF_NAME, TEMP_CELSIUS, + Platform, ) from homeassistant.core import HomeAssistant from homeassistant.helpers.entity_platform import AddEntitiesCallback @@ -33,7 +34,6 @@ from .const import ( DATA_KNX_CONFIG, DOMAIN, PRESET_MODES, - SupportedPlatforms, ) from .knx_entity import KnxEntity from .schema import ClimateSchema @@ -50,9 +50,7 @@ async def async_setup_entry( ) -> None: """Set up climate(s) for KNX platform.""" xknx: XKNX = hass.data[DOMAIN].xknx - config: list[ConfigType] = hass.data[DATA_KNX_CONFIG][ - SupportedPlatforms.CLIMATE.value - ] + config: list[ConfigType] = hass.data[DATA_KNX_CONFIG][Platform.CLIMATE] async_add_entities(KNXClimate(xknx, entity_config) for entity_config in config) diff --git a/homeassistant/components/knx/const.py b/homeassistant/components/knx/const.py index e12d9594795..950deff95c1 100644 --- a/homeassistant/components/knx/const.py +++ b/homeassistant/components/knx/const.py @@ -20,6 +20,7 @@ from homeassistant.components.climate.const import ( PRESET_NONE, PRESET_SLEEP, ) +from homeassistant.const import Platform DOMAIN: Final = "knx" @@ -54,23 +55,21 @@ class ColorTempModes(Enum): RELATIVE = "DPT-5.001" -class SupportedPlatforms(Enum): - """Supported platforms.""" - - BINARY_SENSOR = "binary_sensor" - BUTTON = "button" - CLIMATE = "climate" - COVER = "cover" - FAN = "fan" - LIGHT = "light" - NOTIFY = "notify" - NUMBER = "number" - SCENE = "scene" - SELECT = "select" - SENSOR = "sensor" - SWITCH = "switch" - WEATHER = "weather" - +SUPPORTED_PLATFORMS: Final = [ + Platform.BINARY_SENSOR, + Platform.BUTTON, + Platform.CLIMATE, + Platform.COVER, + Platform.FAN, + Platform.LIGHT, + Platform.NOTIFY, + Platform.NUMBER, + Platform.SCENE, + Platform.SELECT, + Platform.SENSOR, + Platform.SWITCH, + Platform.WEATHER, +] # Map KNX controller modes to HA modes. This list might not be complete. CONTROLLER_MODES: Final = { diff --git a/homeassistant/components/knx/cover.py b/homeassistant/components/knx/cover.py index 8b00f6232f3..96996e0ef27 100644 --- a/homeassistant/components/knx/cover.py +++ b/homeassistant/components/knx/cover.py @@ -23,13 +23,18 @@ from homeassistant.components.cover import ( SUPPORT_STOP_TILT, CoverEntity, ) -from homeassistant.const import CONF_DEVICE_CLASS, CONF_ENTITY_CATEGORY, CONF_NAME +from homeassistant.const import ( + CONF_DEVICE_CLASS, + CONF_ENTITY_CATEGORY, + CONF_NAME, + Platform, +) from homeassistant.core import HomeAssistant, callback from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.event import async_track_utc_time_change from homeassistant.helpers.typing import ConfigType -from .const import DATA_KNX_CONFIG, DOMAIN, SupportedPlatforms +from .const import DATA_KNX_CONFIG, DOMAIN from .knx_entity import KnxEntity from .schema import CoverSchema @@ -41,9 +46,7 @@ async def async_setup_entry( ) -> None: """Set up cover(s) for KNX platform.""" xknx: XKNX = hass.data[DOMAIN].xknx - config: list[ConfigType] = hass.data[DATA_KNX_CONFIG][ - SupportedPlatforms.COVER.value - ] + config: list[ConfigType] = hass.data[DATA_KNX_CONFIG][Platform.COVER] async_add_entities(KNXCover(xknx, entity_config) for entity_config in config) diff --git a/homeassistant/components/knx/fan.py b/homeassistant/components/knx/fan.py index bdb0bbf9dcc..38c90aa149d 100644 --- a/homeassistant/components/knx/fan.py +++ b/homeassistant/components/knx/fan.py @@ -9,7 +9,7 @@ from xknx.devices import Fan as XknxFan from homeassistant import config_entries from homeassistant.components.fan import SUPPORT_OSCILLATE, SUPPORT_SET_SPEED, FanEntity -from homeassistant.const import CONF_ENTITY_CATEGORY, CONF_NAME +from homeassistant.const import CONF_ENTITY_CATEGORY, CONF_NAME, Platform from homeassistant.core import HomeAssistant from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.typing import ConfigType @@ -19,7 +19,7 @@ from homeassistant.util.percentage import ( ranged_value_to_percentage, ) -from .const import DATA_KNX_CONFIG, DOMAIN, KNX_ADDRESS, SupportedPlatforms +from .const import DATA_KNX_CONFIG, DOMAIN, KNX_ADDRESS from .knx_entity import KnxEntity from .schema import FanSchema @@ -33,7 +33,7 @@ async def async_setup_entry( ) -> None: """Set up fan(s) for KNX platform.""" xknx: XKNX = hass.data[DOMAIN].xknx - config: list[ConfigType] = hass.data[DATA_KNX_CONFIG][SupportedPlatforms.FAN.value] + config: list[ConfigType] = hass.data[DATA_KNX_CONFIG][Platform.FAN] async_add_entities(KNXFan(xknx, entity_config) for entity_config in config) diff --git a/homeassistant/components/knx/light.py b/homeassistant/components/knx/light.py index 580293a15d8..0f8f2fe89af 100644 --- a/homeassistant/components/knx/light.py +++ b/homeassistant/components/knx/light.py @@ -23,19 +23,13 @@ from homeassistant.components.light import ( COLOR_MODE_XY, LightEntity, ) -from homeassistant.const import CONF_ENTITY_CATEGORY, CONF_NAME +from homeassistant.const import CONF_ENTITY_CATEGORY, CONF_NAME, Platform from homeassistant.core import HomeAssistant from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.typing import ConfigType import homeassistant.util.color as color_util -from .const import ( - DATA_KNX_CONFIG, - DOMAIN, - KNX_ADDRESS, - ColorTempModes, - SupportedPlatforms, -) +from .const import DATA_KNX_CONFIG, DOMAIN, KNX_ADDRESS, ColorTempModes from .knx_entity import KnxEntity from .schema import LightSchema @@ -47,9 +41,7 @@ async def async_setup_entry( ) -> None: """Set up light(s) for KNX platform.""" xknx: XKNX = hass.data[DOMAIN].xknx - config: list[ConfigType] = hass.data[DATA_KNX_CONFIG][ - SupportedPlatforms.LIGHT.value - ] + config: list[ConfigType] = hass.data[DATA_KNX_CONFIG][Platform.LIGHT] async_add_entities(KNXLight(xknx, entity_config) for entity_config in config) diff --git a/homeassistant/components/knx/number.py b/homeassistant/components/knx/number.py index 7d4ea8a717b..9f12aa4ce24 100644 --- a/homeassistant/components/knx/number.py +++ b/homeassistant/components/knx/number.py @@ -15,6 +15,7 @@ from homeassistant.const import ( CONF_TYPE, STATE_UNAVAILABLE, STATE_UNKNOWN, + Platform, ) from homeassistant.core import HomeAssistant from homeassistant.helpers.entity_platform import AddEntitiesCallback @@ -27,7 +28,6 @@ from .const import ( DATA_KNX_CONFIG, DOMAIN, KNX_ADDRESS, - SupportedPlatforms, ) from .knx_entity import KnxEntity from .schema import NumberSchema @@ -40,9 +40,7 @@ async def async_setup_entry( ) -> None: """Set up number(s) for KNX platform.""" xknx: XKNX = hass.data[DOMAIN].xknx - config: list[ConfigType] = hass.data[DATA_KNX_CONFIG][ - SupportedPlatforms.NUMBER.value - ] + config: list[ConfigType] = hass.data[DATA_KNX_CONFIG][Platform.NUMBER] async_add_entities(KNXNumber(xknx, entity_config) for entity_config in config) diff --git a/homeassistant/components/knx/scene.py b/homeassistant/components/knx/scene.py index 658c6d6d298..a028cebc8f7 100644 --- a/homeassistant/components/knx/scene.py +++ b/homeassistant/components/knx/scene.py @@ -8,12 +8,12 @@ from xknx.devices import Scene as XknxScene from homeassistant import config_entries from homeassistant.components.scene import Scene -from homeassistant.const import CONF_ENTITY_CATEGORY, CONF_NAME +from homeassistant.const import CONF_ENTITY_CATEGORY, CONF_NAME, Platform from homeassistant.core import HomeAssistant from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.typing import ConfigType -from .const import DATA_KNX_CONFIG, DOMAIN, KNX_ADDRESS, SupportedPlatforms +from .const import DATA_KNX_CONFIG, DOMAIN, KNX_ADDRESS from .knx_entity import KnxEntity from .schema import SceneSchema @@ -25,9 +25,7 @@ async def async_setup_entry( ) -> None: """Set up scene(s) for KNX platform.""" xknx: XKNX = hass.data[DOMAIN].xknx - config: list[ConfigType] = hass.data[DATA_KNX_CONFIG][ - SupportedPlatforms.SCENE.value - ] + config: list[ConfigType] = hass.data[DATA_KNX_CONFIG][Platform.SCENE] async_add_entities(KNXScene(xknx, entity_config) for entity_config in config) diff --git a/homeassistant/components/knx/schema.py b/homeassistant/components/knx/schema.py index 218a84b2485..4b7105f15f1 100644 --- a/homeassistant/components/knx/schema.py +++ b/homeassistant/components/knx/schema.py @@ -30,6 +30,7 @@ from homeassistant.const import ( CONF_NAME, CONF_PORT, CONF_TYPE, + Platform, ) import homeassistant.helpers.config_validation as cv from homeassistant.helpers.entity import ENTITY_CATEGORIES_SCHEMA @@ -50,7 +51,6 @@ from .const import ( KNX_ADDRESS, PRESET_MODES, ColorTempModes, - SupportedPlatforms, ) ################## @@ -275,14 +275,14 @@ class EventSchema: class KNXPlatformSchema(ABC): """Voluptuous schema for KNX platform entity configuration.""" - PLATFORM_NAME: ClassVar[str] + PLATFORM: ClassVar[Platform | str] ENTITY_SCHEMA: ClassVar[vol.Schema] @classmethod def platform_node(cls) -> dict[vol.Optional, vol.All]: """Return a schema node for the platform.""" return { - vol.Optional(cls.PLATFORM_NAME): vol.All( + vol.Optional(str(cls.PLATFORM)): vol.All( cv.ensure_list, [cls.ENTITY_SCHEMA] ) } @@ -291,7 +291,7 @@ class KNXPlatformSchema(ABC): class BinarySensorSchema(KNXPlatformSchema): """Voluptuous schema for KNX binary sensors.""" - PLATFORM_NAME = SupportedPlatforms.BINARY_SENSOR.value + PLATFORM = Platform.BINARY_SENSOR CONF_STATE_ADDRESS = CONF_STATE_ADDRESS CONF_SYNC_STATE = CONF_SYNC_STATE @@ -327,7 +327,7 @@ class BinarySensorSchema(KNXPlatformSchema): class ButtonSchema(KNXPlatformSchema): """Voluptuous schema for KNX buttons.""" - PLATFORM_NAME = SupportedPlatforms.BUTTON.value + PLATFORM = Platform.BUTTON CONF_VALUE = "value" DEFAULT_NAME = "KNX Button" @@ -388,7 +388,7 @@ class ButtonSchema(KNXPlatformSchema): class ClimateSchema(KNXPlatformSchema): """Voluptuous schema for KNX climate devices.""" - PLATFORM_NAME = SupportedPlatforms.CLIMATE.value + PLATFORM = Platform.CLIMATE CONF_ACTIVE_STATE_ADDRESS = "active_state_address" CONF_SETPOINT_SHIFT_ADDRESS = "setpoint_shift_address" @@ -507,7 +507,7 @@ class ClimateSchema(KNXPlatformSchema): class CoverSchema(KNXPlatformSchema): """Voluptuous schema for KNX covers.""" - PLATFORM_NAME = SupportedPlatforms.COVER.value + PLATFORM = Platform.COVER CONF_MOVE_LONG_ADDRESS = "move_long_address" CONF_MOVE_SHORT_ADDRESS = "move_short_address" @@ -562,7 +562,7 @@ class CoverSchema(KNXPlatformSchema): class ExposeSchema(KNXPlatformSchema): """Voluptuous schema for KNX exposures.""" - PLATFORM_NAME = CONF_KNX_EXPOSE + PLATFORM = CONF_KNX_EXPOSE CONF_KNX_EXPOSE_TYPE = CONF_TYPE CONF_KNX_EXPOSE_ATTRIBUTE = "attribute" @@ -599,7 +599,7 @@ class ExposeSchema(KNXPlatformSchema): class FanSchema(KNXPlatformSchema): """Voluptuous schema for KNX fans.""" - PLATFORM_NAME = SupportedPlatforms.FAN.value + PLATFORM = Platform.FAN CONF_STATE_ADDRESS = CONF_STATE_ADDRESS CONF_OSCILLATION_ADDRESS = "oscillation_address" @@ -624,7 +624,7 @@ class FanSchema(KNXPlatformSchema): class LightSchema(KNXPlatformSchema): """Voluptuous schema for KNX lights.""" - PLATFORM_NAME = SupportedPlatforms.LIGHT.value + PLATFORM = Platform.LIGHT CONF_STATE_ADDRESS = CONF_STATE_ADDRESS CONF_BRIGHTNESS_ADDRESS = "brightness_address" @@ -764,7 +764,7 @@ class LightSchema(KNXPlatformSchema): class NotifySchema(KNXPlatformSchema): """Voluptuous schema for KNX notifications.""" - PLATFORM_NAME = SupportedPlatforms.NOTIFY.value + PLATFORM = Platform.NOTIFY DEFAULT_NAME = "KNX Notify" @@ -779,7 +779,7 @@ class NotifySchema(KNXPlatformSchema): class NumberSchema(KNXPlatformSchema): """Voluptuous schema for KNX numbers.""" - PLATFORM_NAME = SupportedPlatforms.NUMBER.value + PLATFORM = Platform.NUMBER CONF_MAX = "max" CONF_MIN = "min" @@ -810,7 +810,7 @@ class NumberSchema(KNXPlatformSchema): class SceneSchema(KNXPlatformSchema): """Voluptuous schema for KNX scenes.""" - PLATFORM_NAME = SupportedPlatforms.SCENE.value + PLATFORM = Platform.SCENE CONF_SCENE_NUMBER = "scene_number" @@ -830,7 +830,7 @@ class SceneSchema(KNXPlatformSchema): class SelectSchema(KNXPlatformSchema): """Voluptuous schema for KNX selects.""" - PLATFORM_NAME = SupportedPlatforms.SELECT.value + PLATFORM = Platform.SELECT CONF_OPTION = "option" CONF_OPTIONS = "options" @@ -863,7 +863,7 @@ class SelectSchema(KNXPlatformSchema): class SensorSchema(KNXPlatformSchema): """Voluptuous schema for KNX sensors.""" - PLATFORM_NAME = SupportedPlatforms.SENSOR.value + PLATFORM = Platform.SENSOR CONF_ALWAYS_CALLBACK = "always_callback" CONF_STATE_ADDRESS = CONF_STATE_ADDRESS @@ -886,7 +886,7 @@ class SensorSchema(KNXPlatformSchema): class SwitchSchema(KNXPlatformSchema): """Voluptuous schema for KNX switches.""" - PLATFORM_NAME = SupportedPlatforms.SWITCH.value + PLATFORM = Platform.SWITCH CONF_INVERT = CONF_INVERT CONF_STATE_ADDRESS = CONF_STATE_ADDRESS @@ -907,7 +907,7 @@ class SwitchSchema(KNXPlatformSchema): class WeatherSchema(KNXPlatformSchema): """Voluptuous schema for KNX weather station.""" - PLATFORM_NAME = SupportedPlatforms.WEATHER.value + PLATFORM = Platform.WEATHER CONF_SYNC_STATE = CONF_SYNC_STATE CONF_KNX_TEMPERATURE_ADDRESS = "address_temperature" diff --git a/homeassistant/components/knx/select.py b/homeassistant/components/knx/select.py index aefa4749e88..5baa068eaa6 100644 --- a/homeassistant/components/knx/select.py +++ b/homeassistant/components/knx/select.py @@ -11,6 +11,7 @@ from homeassistant.const import ( CONF_NAME, STATE_UNAVAILABLE, STATE_UNKNOWN, + Platform, ) from homeassistant.core import HomeAssistant from homeassistant.helpers.entity_platform import AddEntitiesCallback @@ -26,7 +27,6 @@ from .const import ( DATA_KNX_CONFIG, DOMAIN, KNX_ADDRESS, - SupportedPlatforms, ) from .knx_entity import KnxEntity from .schema import SelectSchema @@ -39,9 +39,7 @@ async def async_setup_entry( ) -> None: """Set up select(s) for KNX platform.""" xknx: XKNX = hass.data[DOMAIN].xknx - config: list[ConfigType] = hass.data[DATA_KNX_CONFIG][ - SupportedPlatforms.SELECT.value - ] + config: list[ConfigType] = hass.data[DATA_KNX_CONFIG][Platform.SELECT] async_add_entities(KNXSelect(xknx, entity_config) for entity_config in config) diff --git a/homeassistant/components/knx/sensor.py b/homeassistant/components/knx/sensor.py index a9a1feca9e3..ceb9f435d83 100644 --- a/homeassistant/components/knx/sensor.py +++ b/homeassistant/components/knx/sensor.py @@ -12,12 +12,12 @@ from homeassistant.components.sensor import ( DEVICE_CLASSES, SensorEntity, ) -from homeassistant.const import CONF_ENTITY_CATEGORY, CONF_NAME, CONF_TYPE +from homeassistant.const import CONF_ENTITY_CATEGORY, CONF_NAME, CONF_TYPE, Platform from homeassistant.core import HomeAssistant from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.typing import ConfigType, StateType -from .const import ATTR_SOURCE, DATA_KNX_CONFIG, DOMAIN, SupportedPlatforms +from .const import ATTR_SOURCE, DATA_KNX_CONFIG, DOMAIN from .knx_entity import KnxEntity from .schema import SensorSchema @@ -29,9 +29,7 @@ async def async_setup_entry( ) -> None: """Set up sensor(s) for KNX platform.""" xknx: XKNX = hass.data[DOMAIN].xknx - config: list[ConfigType] = hass.data[DATA_KNX_CONFIG][ - SupportedPlatforms.SENSOR.value - ] + config: list[ConfigType] = hass.data[DATA_KNX_CONFIG][Platform.SENSOR] async_add_entities(KNXSensor(xknx, entity_config) for entity_config in config) diff --git a/homeassistant/components/knx/switch.py b/homeassistant/components/knx/switch.py index 3bbb419a22b..9f4eb6fc632 100644 --- a/homeassistant/components/knx/switch.py +++ b/homeassistant/components/knx/switch.py @@ -14,19 +14,14 @@ from homeassistant.const import ( STATE_ON, STATE_UNAVAILABLE, STATE_UNKNOWN, + Platform, ) from homeassistant.core import HomeAssistant from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.restore_state import RestoreEntity from homeassistant.helpers.typing import ConfigType -from .const import ( - CONF_RESPOND_TO_READ, - DATA_KNX_CONFIG, - DOMAIN, - KNX_ADDRESS, - SupportedPlatforms, -) +from .const import CONF_RESPOND_TO_READ, DATA_KNX_CONFIG, DOMAIN, KNX_ADDRESS from .knx_entity import KnxEntity from .schema import SwitchSchema @@ -38,9 +33,7 @@ async def async_setup_entry( ) -> None: """Set up switch(es) for KNX platform.""" xknx: XKNX = hass.data[DOMAIN].xknx - config: list[ConfigType] = hass.data[DATA_KNX_CONFIG][ - SupportedPlatforms.SWITCH.value - ] + config: list[ConfigType] = hass.data[DATA_KNX_CONFIG][Platform.SWITCH] async_add_entities(KNXSwitch(xknx, entity_config) for entity_config in config) diff --git a/homeassistant/components/knx/weather.py b/homeassistant/components/knx/weather.py index b52ee644b39..6e71c09501f 100644 --- a/homeassistant/components/knx/weather.py +++ b/homeassistant/components/knx/weather.py @@ -6,12 +6,12 @@ from xknx.devices import Weather as XknxWeather from homeassistant import config_entries from homeassistant.components.weather import WeatherEntity -from homeassistant.const import CONF_ENTITY_CATEGORY, CONF_NAME, TEMP_CELSIUS +from homeassistant.const import CONF_ENTITY_CATEGORY, CONF_NAME, TEMP_CELSIUS, Platform from homeassistant.core import HomeAssistant from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.typing import ConfigType -from .const import DATA_KNX_CONFIG, DOMAIN, SupportedPlatforms +from .const import DATA_KNX_CONFIG, DOMAIN from .knx_entity import KnxEntity from .schema import WeatherSchema @@ -23,9 +23,7 @@ async def async_setup_entry( ) -> None: """Set up switch(es) for KNX platform.""" xknx: XKNX = hass.data[DOMAIN].xknx - config: list[ConfigType] = hass.data[DATA_KNX_CONFIG][ - SupportedPlatforms.WEATHER.value - ] + config: list[ConfigType] = hass.data[DATA_KNX_CONFIG][Platform.WEATHER] async_add_entities(KNXWeather(xknx, entity_config) for entity_config in config) diff --git a/tests/components/knx/test_binary_sensor.py b/tests/components/knx/test_binary_sensor.py index 9adc7205543..5513cefcbb4 100644 --- a/tests/components/knx/test_binary_sensor.py +++ b/tests/components/knx/test_binary_sensor.py @@ -26,7 +26,7 @@ async def test_binary_sensor_entity_category(hass: HomeAssistant, knx: KNXTestKi """Test KNX binary sensor entity category.""" await knx.setup_integration( { - BinarySensorSchema.PLATFORM_NAME: [ + BinarySensorSchema.PLATFORM: [ { CONF_NAME: "test_normal", CONF_STATE_ADDRESS: "1/1/1", @@ -49,7 +49,7 @@ async def test_binary_sensor(hass: HomeAssistant, knx: KNXTestKit): """Test KNX binary sensor and inverted binary_sensor.""" await knx.setup_integration( { - BinarySensorSchema.PLATFORM_NAME: [ + BinarySensorSchema.PLATFORM: [ { CONF_NAME: "test_normal", CONF_STATE_ADDRESS: "1/1/1", @@ -104,7 +104,7 @@ async def test_binary_sensor_ignore_internal_state( await knx.setup_integration( { - BinarySensorSchema.PLATFORM_NAME: [ + BinarySensorSchema.PLATFORM: [ { CONF_NAME: "test_normal", CONF_STATE_ADDRESS: "1/1/1", @@ -156,7 +156,7 @@ async def test_binary_sensor_counter(hass: HomeAssistant, knx: KNXTestKit): await knx.setup_integration( { - BinarySensorSchema.PLATFORM_NAME: [ + BinarySensorSchema.PLATFORM: [ { CONF_NAME: "test", CONF_STATE_ADDRESS: "2/2/2", @@ -223,7 +223,7 @@ async def test_binary_sensor_reset(hass: HomeAssistant, knx: KNXTestKit): await knx.setup_integration( { - BinarySensorSchema.PLATFORM_NAME: [ + BinarySensorSchema.PLATFORM: [ { CONF_NAME: "test", CONF_STATE_ADDRESS: "2/2/2", @@ -259,7 +259,7 @@ async def test_binary_sensor_restore_and_respond(hass, knx): ): await knx.setup_integration( { - BinarySensorSchema.PLATFORM_NAME: [ + BinarySensorSchema.PLATFORM: [ { CONF_NAME: "test", CONF_STATE_ADDRESS: _ADDRESS, @@ -291,7 +291,7 @@ async def test_binary_sensor_restore_invert(hass, knx): ): await knx.setup_integration( { - BinarySensorSchema.PLATFORM_NAME: [ + BinarySensorSchema.PLATFORM: [ { CONF_NAME: "test", CONF_STATE_ADDRESS: _ADDRESS, diff --git a/tests/components/knx/test_button.py b/tests/components/knx/test_button.py index 0e5f40670f7..eff81a3c6b6 100644 --- a/tests/components/knx/test_button.py +++ b/tests/components/knx/test_button.py @@ -21,7 +21,7 @@ async def test_button_simple(hass: HomeAssistant, knx: KNXTestKit): events = async_capture_events(hass, "state_changed") await knx.setup_integration( { - ButtonSchema.PLATFORM_NAME: { + ButtonSchema.PLATFORM: { CONF_NAME: "test", KNX_ADDRESS: "1/2/3", } @@ -57,7 +57,7 @@ async def test_button_raw(hass: HomeAssistant, knx: KNXTestKit): """Test KNX button with raw payload.""" await knx.setup_integration( { - ButtonSchema.PLATFORM_NAME: { + ButtonSchema.PLATFORM: { CONF_NAME: "test", KNX_ADDRESS: "1/2/3", CONF_PAYLOAD: False, @@ -76,7 +76,7 @@ async def test_button_type(hass: HomeAssistant, knx: KNXTestKit): """Test KNX button with encoded payload.""" await knx.setup_integration( { - ButtonSchema.PLATFORM_NAME: { + ButtonSchema.PLATFORM: { CONF_NAME: "test", KNX_ADDRESS: "1/2/3", ButtonSchema.CONF_VALUE: 21.5, diff --git a/tests/components/knx/test_fan.py b/tests/components/knx/test_fan.py index cc2365888f0..37a69911e51 100644 --- a/tests/components/knx/test_fan.py +++ b/tests/components/knx/test_fan.py @@ -11,7 +11,7 @@ async def test_fan_percent(hass: HomeAssistant, knx: KNXTestKit): """Test KNX fan with percentage speed.""" await knx.setup_integration( { - FanSchema.PLATFORM_NAME: { + FanSchema.PLATFORM: { CONF_NAME: "test", KNX_ADDRESS: "1/2/3", } @@ -56,7 +56,7 @@ async def test_fan_step(hass: HomeAssistant, knx: KNXTestKit): """Test KNX fan with speed steps.""" await knx.setup_integration( { - FanSchema.PLATFORM_NAME: { + FanSchema.PLATFORM: { CONF_NAME: "test", KNX_ADDRESS: "1/2/3", FanSchema.CONF_MAX_STEP: 4, @@ -109,7 +109,7 @@ async def test_fan_oscillation(hass: HomeAssistant, knx: KNXTestKit): """Test KNX fan oscillation.""" await knx.setup_integration( { - FanSchema.PLATFORM_NAME: { + FanSchema.PLATFORM: { CONF_NAME: "test", KNX_ADDRESS: "1/1/1", FanSchema.CONF_OSCILLATION_ADDRESS: "2/2/2", diff --git a/tests/components/knx/test_light.py b/tests/components/knx/test_light.py index 2988bf189d3..9c7ef0e91ec 100644 --- a/tests/components/knx/test_light.py +++ b/tests/components/knx/test_light.py @@ -3,6 +3,7 @@ from __future__ import annotations from datetime import timedelta +from xknx.core import XknxConnectionState from xknx.devices.light import Light as XknxLight from homeassistant.components.knx.const import CONF_STATE_ADDRESS, KNX_ADDRESS @@ -35,7 +36,7 @@ async def test_light_simple(hass: HomeAssistant, knx: KNXTestKit): test_address = "1/1/1" await knx.setup_integration( { - LightSchema.PLATFORM_NAME: { + LightSchema.PLATFORM: { CONF_NAME: "test", KNX_ADDRESS: test_address, } @@ -86,7 +87,7 @@ async def test_light_brightness(hass: HomeAssistant, knx: KNXTestKit): test_brightness_state = "1/1/3" await knx.setup_integration( { - LightSchema.PLATFORM_NAME: { + LightSchema.PLATFORM: { CONF_NAME: "test", KNX_ADDRESS: test_address, LightSchema.CONF_BRIGHTNESS_ADDRESS: test_brightness, @@ -96,6 +97,9 @@ async def test_light_brightness(hass: HomeAssistant, knx: KNXTestKit): ) # StateUpdater initialize state await knx.assert_read(test_brightness_state) + await knx.xknx.connection_manager.connection_state_changed( + XknxConnectionState.CONNECTED + ) # turn on light via brightness await hass.services.async_call( "light", @@ -139,7 +143,7 @@ async def test_light_color_temp_absolute(hass: HomeAssistant, knx: KNXTestKit): test_ct_state = "1/1/6" await knx.setup_integration( { - LightSchema.PLATFORM_NAME: [ + LightSchema.PLATFORM: [ { CONF_NAME: "test", KNX_ADDRESS: test_address, @@ -193,7 +197,7 @@ async def test_light_color_temp_relative(hass: HomeAssistant, knx: KNXTestKit): test_ct_state = "1/1/6" await knx.setup_integration( { - LightSchema.PLATFORM_NAME: [ + LightSchema.PLATFORM: [ { CONF_NAME: "test", KNX_ADDRESS: test_address, @@ -251,7 +255,7 @@ async def test_light_hs_color(hass: HomeAssistant, knx: KNXTestKit): test_sat_state = "1/1/8" await knx.setup_integration( { - LightSchema.PLATFORM_NAME: [ + LightSchema.PLATFORM: [ { CONF_NAME: "test", KNX_ADDRESS: test_address, @@ -335,7 +339,7 @@ async def test_light_xyy_color(hass: HomeAssistant, knx: KNXTestKit): test_xyy_state = "1/1/6" await knx.setup_integration( { - LightSchema.PLATFORM_NAME: [ + LightSchema.PLATFORM: [ { CONF_NAME: "test", KNX_ADDRESS: test_address, @@ -410,7 +414,7 @@ async def test_light_xyy_color_with_brightness(hass: HomeAssistant, knx: KNXTest test_xyy_state = "1/1/6" await knx.setup_integration( { - LightSchema.PLATFORM_NAME: [ + LightSchema.PLATFORM: [ { CONF_NAME: "test", KNX_ADDRESS: test_address, @@ -488,7 +492,7 @@ async def test_light_rgb_individual(hass: HomeAssistant, knx: KNXTestKit): test_blue_state = "1/1/8" await knx.setup_integration( { - LightSchema.PLATFORM_NAME: [ + LightSchema.PLATFORM: [ { CONF_NAME: "test", LightSchema.CONF_INDIVIDUAL_COLORS: { @@ -636,7 +640,7 @@ async def test_light_rgbw_individual(hass: HomeAssistant, knx: KNXTestKit): test_white_state = "1/1/10" await knx.setup_integration( { - LightSchema.PLATFORM_NAME: [ + LightSchema.PLATFORM: [ { CONF_NAME: "test", LightSchema.CONF_INDIVIDUAL_COLORS: { @@ -810,7 +814,7 @@ async def test_light_rgb(hass: HomeAssistant, knx: KNXTestKit): test_rgb_state = "1/1/6" await knx.setup_integration( { - LightSchema.PLATFORM_NAME: [ + LightSchema.PLATFORM: [ { CONF_NAME: "test", KNX_ADDRESS: test_address, @@ -918,7 +922,7 @@ async def test_light_rgbw(hass: HomeAssistant, knx: KNXTestKit): test_rgbw_state = "1/1/6" await knx.setup_integration( { - LightSchema.PLATFORM_NAME: [ + LightSchema.PLATFORM: [ { CONF_NAME: "test", KNX_ADDRESS: test_address, @@ -1031,7 +1035,7 @@ async def test_light_rgbw_brightness(hass: HomeAssistant, knx: KNXTestKit): test_rgbw_state = "1/1/6" await knx.setup_integration( { - LightSchema.PLATFORM_NAME: [ + LightSchema.PLATFORM: [ { CONF_NAME: "test", KNX_ADDRESS: test_address, diff --git a/tests/components/knx/test_number.py b/tests/components/knx/test_number.py index d14f01ee5fe..668b046df74 100644 --- a/tests/components/knx/test_number.py +++ b/tests/components/knx/test_number.py @@ -16,7 +16,7 @@ async def test_number_set_value(hass: HomeAssistant, knx: KNXTestKit): test_address = "1/1/1" await knx.setup_integration( { - NumberSchema.PLATFORM_NAME: { + NumberSchema.PLATFORM: { CONF_NAME: "test", KNX_ADDRESS: test_address, CONF_TYPE: "percent", @@ -72,7 +72,7 @@ async def test_number_restore_and_respond(hass: HomeAssistant, knx: KNXTestKit): ): await knx.setup_integration( { - NumberSchema.PLATFORM_NAME: { + NumberSchema.PLATFORM: { CONF_NAME: "test", KNX_ADDRESS: [test_address, test_passive_address], CONF_RESPOND_TO_READ: True, diff --git a/tests/components/knx/test_scene.py b/tests/components/knx/test_scene.py index 452e5347932..c2f15df6f6c 100644 --- a/tests/components/knx/test_scene.py +++ b/tests/components/knx/test_scene.py @@ -19,7 +19,7 @@ async def test_activate_knx_scene(hass: HomeAssistant, knx: KNXTestKit): """Test KNX scene.""" await knx.setup_integration( { - SceneSchema.PLATFORM_NAME: [ + SceneSchema.PLATFORM: [ { CONF_NAME: "test", SceneSchema.CONF_SCENE_NUMBER: 24, diff --git a/tests/components/knx/test_select.py b/tests/components/knx/test_select.py index f7f80c2ebf3..c8db3625c05 100644 --- a/tests/components/knx/test_select.py +++ b/tests/components/knx/test_select.py @@ -28,7 +28,7 @@ async def test_select_dpt_2_simple(hass: HomeAssistant, knx: KNXTestKit): test_address = "1/1/1" await knx.setup_integration( { - SelectSchema.PLATFORM_NAME: { + SelectSchema.PLATFORM: { CONF_NAME: "test", KNX_ADDRESS: test_address, CONF_SYNC_STATE: False, @@ -105,7 +105,7 @@ async def test_select_dpt_2_restore(hass: HomeAssistant, knx: KNXTestKit): ): await knx.setup_integration( { - SelectSchema.PLATFORM_NAME: { + SelectSchema.PLATFORM: { CONF_NAME: "test", KNX_ADDRESS: [test_address, test_passive_address], CONF_RESPOND_TO_READ: True, @@ -143,7 +143,7 @@ async def test_select_dpt_20_103_all_options(hass: HomeAssistant, knx: KNXTestKi await knx.setup_integration( { - SelectSchema.PLATFORM_NAME: { + SelectSchema.PLATFORM: { CONF_NAME: "test", KNX_ADDRESS: [test_address, test_passive_address], CONF_STATE_ADDRESS: test_state_address, diff --git a/tests/components/knx/test_sensor.py b/tests/components/knx/test_sensor.py index 16ea5e8d385..8deb4629d8b 100644 --- a/tests/components/knx/test_sensor.py +++ b/tests/components/knx/test_sensor.py @@ -14,7 +14,7 @@ async def test_sensor(hass: HomeAssistant, knx: KNXTestKit): await knx.setup_integration( { - SensorSchema.PLATFORM_NAME: { + SensorSchema.PLATFORM: { CONF_NAME: "test", CONF_STATE_ADDRESS: "1/1/1", CONF_TYPE: "current", # 2 byte unsigned int @@ -47,7 +47,7 @@ async def test_always_callback(hass: HomeAssistant, knx: KNXTestKit): events = async_capture_events(hass, "state_changed") await knx.setup_integration( { - SensorSchema.PLATFORM_NAME: [ + SensorSchema.PLATFORM: [ { CONF_NAME: "test_normal", CONF_STATE_ADDRESS: "1/1/1", diff --git a/tests/components/knx/test_switch.py b/tests/components/knx/test_switch.py index eff34243ca8..07fe8793fd8 100644 --- a/tests/components/knx/test_switch.py +++ b/tests/components/knx/test_switch.py @@ -17,7 +17,7 @@ async def test_switch_simple(hass: HomeAssistant, knx: KNXTestKit): """Test simple KNX switch.""" await knx.setup_integration( { - SwitchSchema.PLATFORM_NAME: { + SwitchSchema.PLATFORM: { CONF_NAME: "test", KNX_ADDRESS: "1/2/3", } @@ -59,7 +59,7 @@ async def test_switch_state(hass: HomeAssistant, knx: KNXTestKit): await knx.setup_integration( { - SwitchSchema.PLATFORM_NAME: { + SwitchSchema.PLATFORM: { CONF_NAME: "test", KNX_ADDRESS: _ADDRESS, CONF_STATE_ADDRESS: _STATE_ADDRESS, @@ -122,7 +122,7 @@ async def test_switch_restore_and_respond(hass, knx): ): await knx.setup_integration( { - SwitchSchema.PLATFORM_NAME: { + SwitchSchema.PLATFORM: { CONF_NAME: "test", KNX_ADDRESS: _ADDRESS, CONF_RESPOND_TO_READ: True, diff --git a/tests/components/knx/test_weather.py b/tests/components/knx/test_weather.py index 0fc1255bf26..21d80248b97 100644 --- a/tests/components/knx/test_weather.py +++ b/tests/components/knx/test_weather.py @@ -17,7 +17,7 @@ async def test_weather(hass: HomeAssistant, knx: KNXTestKit): await knx.setup_integration( { - WeatherSchema.PLATFORM_NAME: { + WeatherSchema.PLATFORM: { CONF_NAME: "test", WeatherSchema.CONF_KNX_WIND_ALARM_ADDRESS: "1/1/1", WeatherSchema.CONF_KNX_RAIN_ALARM_ADDRESS: "1/1/2",