From d6f48683a3951ff50a6af389a293f7bcb023c835 Mon Sep 17 00:00:00 2001 From: Marc Mueller <30130371+cdce8p@users.noreply.github.com> Date: Mon, 13 Dec 2021 14:09:49 +0100 Subject: [PATCH] Use platform enum (8) [Misc] (#61013) --- homeassistant/components/axis/const.py | 7 ++-- homeassistant/components/firmata/const.py | 9 ++--- homeassistant/components/hive/const.py | 30 ++++++++-------- .../components/home_plus_control/__init__.py | 4 +-- homeassistant/components/hyperion/__init__.py | 7 ++-- .../components/iaqualink/__init__.py | 12 +++---- homeassistant/components/insteon/const.py | 14 ++++---- homeassistant/components/mysensors/const.py | 24 +++++++------ .../components/panasonic_viera/__init__.py | 13 ++++--- homeassistant/components/risco/__init__.py | 3 +- .../components/switcher_kis/__init__.py | 4 +-- homeassistant/components/tasmota/const.py | 14 ++++---- homeassistant/components/vera/__init__.py | 34 +++++++++---------- homeassistant/components/vera/common.py | 11 +++--- homeassistant/components/vesync/__init__.py | 8 ++--- 15 files changed, 102 insertions(+), 92 deletions(-) diff --git a/homeassistant/components/axis/const.py b/homeassistant/components/axis/const.py index a1ce77f099b..c9267568707 100644 --- a/homeassistant/components/axis/const.py +++ b/homeassistant/components/axis/const.py @@ -1,10 +1,7 @@ """Constants for the Axis component.""" import logging -from homeassistant.components.binary_sensor import DOMAIN as BINARY_SENSOR_DOMAIN -from homeassistant.components.camera import DOMAIN as CAMERA_DOMAIN -from homeassistant.components.light import DOMAIN as LIGHT_DOMAIN -from homeassistant.components.switch import DOMAIN as SWITCH_DOMAIN +from homeassistant.const import Platform LOGGER = logging.getLogger(__package__) @@ -22,4 +19,4 @@ DEFAULT_STREAM_PROFILE = "No stream profile" DEFAULT_TRIGGER_TIME = 0 DEFAULT_VIDEO_SOURCE = "No video source" -PLATFORMS = [BINARY_SENSOR_DOMAIN, CAMERA_DOMAIN, LIGHT_DOMAIN, SWITCH_DOMAIN] +PLATFORMS = [Platform.BINARY_SENSOR, Platform.CAMERA, Platform.LIGHT, Platform.SWITCH] diff --git a/homeassistant/components/firmata/const.py b/homeassistant/components/firmata/const.py index 0d859363e2b..091d724229c 100644 --- a/homeassistant/components/firmata/const.py +++ b/homeassistant/components/firmata/const.py @@ -4,6 +4,7 @@ from homeassistant.const import ( CONF_LIGHTS, CONF_SENSORS, CONF_SWITCHES, + Platform, ) CONF_ARDUINO_INSTANCE_ID = "arduino_instance_id" @@ -27,8 +28,8 @@ CONF_SLEEP_TUNE = "sleep_tune" DOMAIN = "firmata" FIRMATA_MANUFACTURER = "Firmata" CONF_PLATFORM_MAP = { - CONF_BINARY_SENSORS: "binary_sensor", - CONF_LIGHTS: "light", - CONF_SENSORS: "sensor", - CONF_SWITCHES: "switch", + CONF_BINARY_SENSORS: Platform.BINARY_SENSOR, + CONF_LIGHTS: Platform.LIGHT, + CONF_SENSORS: Platform.SENSOR, + CONF_SWITCHES: Platform.SWITCH, } diff --git a/homeassistant/components/hive/const.py b/homeassistant/components/hive/const.py index f24ed0f7b24..82c07761eef 100644 --- a/homeassistant/components/hive/const.py +++ b/homeassistant/components/hive/const.py @@ -1,4 +1,6 @@ """Constants for Hive.""" +from homeassistant.const import Platform + ATTR_MODE = "mode" ATTR_TIME_PERIOD = "time_period" ATTR_ONOFF = "on_off" @@ -7,22 +9,22 @@ CONFIG_ENTRY_VERSION = 1 DEFAULT_NAME = "Hive" DOMAIN = "hive" PLATFORMS = [ - "alarm_control_panel", - "binary_sensor", - "climate", - "light", - "sensor", - "switch", - "water_heater", + Platform.ALARM_CONTROL_PANEL, + Platform.BINARY_SENSOR, + Platform.CLIMATE, + Platform.LIGHT, + Platform.SENSOR, + Platform.SWITCH, + Platform.WATER_HEATER, ] PLATFORM_LOOKUP = { - "alarm_control_panel": "alarm_control_panel", - "binary_sensor": "binary_sensor", - "climate": "climate", - "light": "light", - "sensor": "sensor", - "switch": "switch", - "water_heater": "water_heater", + Platform.ALARM_CONTROL_PANEL: "alarm_control_panel", + Platform.BINARY_SENSOR: "binary_sensor", + Platform.CLIMATE: "climate", + Platform.LIGHT: "light", + Platform.SENSOR: "sensor", + Platform.SWITCH: "switch", + Platform.WATER_HEATER: "water_heater", } SERVICE_BOOST_HOT_WATER = "boost_hot_water" SERVICE_BOOST_HEATING_ON = "boost_heating_on" diff --git a/homeassistant/components/home_plus_control/__init__.py b/homeassistant/components/home_plus_control/__init__.py index ffb055e6324..78e31c83caa 100644 --- a/homeassistant/components/home_plus_control/__init__.py +++ b/homeassistant/components/home_plus_control/__init__.py @@ -8,7 +8,7 @@ from homepluscontrol.homeplusapi import HomePlusControlApiError import voluptuous as vol from homeassistant.config_entries import ConfigEntry -from homeassistant.const import CONF_CLIENT_ID, CONF_CLIENT_SECRET +from homeassistant.const import CONF_CLIENT_ID, CONF_CLIENT_SECRET, Platform from homeassistant.core import HomeAssistant from homeassistant.helpers import ( config_entry_oauth2_flow, @@ -46,7 +46,7 @@ CONFIG_SCHEMA = vol.Schema( ) # The Legrand Home+ Control platform is currently limited to "switch" entities -PLATFORMS = ["switch"] +PLATFORMS = [Platform.SWITCH] _LOGGER = logging.getLogger(__name__) diff --git a/homeassistant/components/hyperion/__init__.py b/homeassistant/components/hyperion/__init__.py index b43b25ca5ac..292c426ba19 100644 --- a/homeassistant/components/hyperion/__init__.py +++ b/homeassistant/components/hyperion/__init__.py @@ -10,11 +10,8 @@ from typing import Any, cast from awesomeversion import AwesomeVersion from hyperion import client, const as hyperion_const -from homeassistant.components.camera.const import DOMAIN as CAMERA_DOMAIN -from homeassistant.components.light import DOMAIN as LIGHT_DOMAIN -from homeassistant.components.switch import DOMAIN as SWITCH_DOMAIN from homeassistant.config_entries import ConfigEntry -from homeassistant.const import CONF_HOST, CONF_PORT, CONF_TOKEN +from homeassistant.const import CONF_HOST, CONF_PORT, CONF_TOKEN, Platform from homeassistant.core import HomeAssistant, callback from homeassistant.exceptions import ConfigEntryAuthFailed, ConfigEntryNotReady from homeassistant.helpers import device_registry as dr @@ -36,7 +33,7 @@ from .const import ( SIGNAL_INSTANCE_REMOVE, ) -PLATFORMS = [LIGHT_DOMAIN, SWITCH_DOMAIN, CAMERA_DOMAIN] +PLATFORMS = [Platform.LIGHT, Platform.SWITCH, Platform.CAMERA] _LOGGER = logging.getLogger(__name__) diff --git a/homeassistant/components/iaqualink/__init__.py b/homeassistant/components/iaqualink/__init__.py index 856674f1345..61f2c906c17 100644 --- a/homeassistant/components/iaqualink/__init__.py +++ b/homeassistant/components/iaqualink/__init__.py @@ -25,7 +25,7 @@ from homeassistant.components.light import DOMAIN as LIGHT_DOMAIN from homeassistant.components.sensor import DOMAIN as SENSOR_DOMAIN from homeassistant.components.switch import DOMAIN as SWITCH_DOMAIN from homeassistant.config_entries import ConfigEntry -from homeassistant.const import CONF_PASSWORD, CONF_USERNAME +from homeassistant.const import CONF_PASSWORD, CONF_USERNAME, Platform from homeassistant.core import HomeAssistant from homeassistant.exceptions import ConfigEntryNotReady from homeassistant.helpers.aiohttp_client import async_get_clientsession @@ -135,19 +135,19 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: forward_setup = hass.config_entries.async_forward_entry_setup if binary_sensors: _LOGGER.debug("Got %s binary sensors: %s", len(binary_sensors), binary_sensors) - hass.async_create_task(forward_setup(entry, BINARY_SENSOR_DOMAIN)) + hass.async_create_task(forward_setup(entry, Platform.BINARY_SENSOR)) if climates: _LOGGER.debug("Got %s climates: %s", len(climates), climates) - hass.async_create_task(forward_setup(entry, CLIMATE_DOMAIN)) + hass.async_create_task(forward_setup(entry, Platform.CLIMATE)) if lights: _LOGGER.debug("Got %s lights: %s", len(lights), lights) - hass.async_create_task(forward_setup(entry, LIGHT_DOMAIN)) + hass.async_create_task(forward_setup(entry, Platform.LIGHT)) if sensors: _LOGGER.debug("Got %s sensors: %s", len(sensors), sensors) - hass.async_create_task(forward_setup(entry, SENSOR_DOMAIN)) + hass.async_create_task(forward_setup(entry, Platform.SENSOR)) if switches: _LOGGER.debug("Got %s switches: %s", len(switches), switches) - hass.async_create_task(forward_setup(entry, SWITCH_DOMAIN)) + hass.async_create_task(forward_setup(entry, Platform.SWITCH)) async def _async_systems_update(now): """Refresh internal state for all systems.""" diff --git a/homeassistant/components/insteon/const.py b/homeassistant/components/insteon/const.py index dca53d20369..d93d03847a1 100644 --- a/homeassistant/components/insteon/const.py +++ b/homeassistant/components/insteon/const.py @@ -34,15 +34,17 @@ from pyinsteon.groups import ( TEST_SENSOR, ) +from homeassistant.const import Platform + DOMAIN = "insteon" INSTEON_PLATFORMS = [ - "binary_sensor", - "climate", - "cover", - "fan", - "light", - "switch", + Platform.BINARY_SENSOR, + Platform.CLIMATE, + Platform.COVER, + Platform.FAN, + Platform.LIGHT, + Platform.SWITCH, ] X10_PLATFORMS = [ diff --git a/homeassistant/components/mysensors/const.py b/homeassistant/components/mysensors/const.py index f8e157e3622..9feca1e6369 100644 --- a/homeassistant/components/mysensors/const.py +++ b/homeassistant/components/mysensors/const.py @@ -4,6 +4,8 @@ from __future__ import annotations from collections import defaultdict from typing import Final, Literal, Tuple, TypedDict +from homeassistant.const import Platform + ATTR_DEVICES: Final = "devices" ATTR_GATEWAY_ID: Final = "gateway_id" @@ -144,15 +146,15 @@ SWITCH_TYPES: dict[SensorType, set[ValueType]] = { } -PLATFORM_TYPES: dict[str, dict[SensorType, set[ValueType]]] = { - "binary_sensor": BINARY_SENSOR_TYPES, - "climate": CLIMATE_TYPES, - "cover": COVER_TYPES, - "device_tracker": DEVICE_TRACKER_TYPES, - "light": LIGHT_TYPES, - "notify": NOTIFY_TYPES, - "sensor": SENSOR_TYPES, - "switch": SWITCH_TYPES, +PLATFORM_TYPES: dict[Platform, dict[SensorType, set[ValueType]]] = { + Platform.BINARY_SENSOR: BINARY_SENSOR_TYPES, + Platform.CLIMATE: CLIMATE_TYPES, + Platform.COVER: COVER_TYPES, + Platform.DEVICE_TRACKER: DEVICE_TRACKER_TYPES, + Platform.LIGHT: LIGHT_TYPES, + Platform.NOTIFY: NOTIFY_TYPES, + Platform.SENSOR: SENSOR_TYPES, + Platform.SWITCH: SWITCH_TYPES, } FLAT_PLATFORM_TYPES: dict[tuple[str, SensorType], set[ValueType]] = { @@ -168,6 +170,6 @@ for platform, platform_types in PLATFORM_TYPES.items(): TYPE_TO_PLATFORMS[s_type_name].append(platform) PLATFORMS_WITH_ENTRY_SUPPORT = set(PLATFORM_TYPES.keys()) - { - "notify", - "device_tracker", + Platform.NOTIFY, + Platform.DEVICE_TRACKER, } diff --git a/homeassistant/components/panasonic_viera/__init__.py b/homeassistant/components/panasonic_viera/__init__.py index 419181da2d9..7448097ea2c 100644 --- a/homeassistant/components/panasonic_viera/__init__.py +++ b/homeassistant/components/panasonic_viera/__init__.py @@ -6,10 +6,15 @@ from urllib.error import HTTPError, URLError from panasonic_viera import EncryptionRequired, Keys, RemoteControl, SOAPError import voluptuous as vol -from homeassistant.components.media_player.const import DOMAIN as MEDIA_PLAYER_DOMAIN -from homeassistant.components.remote import DOMAIN as REMOTE_DOMAIN from homeassistant.config_entries import SOURCE_IMPORT -from homeassistant.const import CONF_HOST, CONF_NAME, CONF_PORT, STATE_OFF, STATE_ON +from homeassistant.const import ( + CONF_HOST, + CONF_NAME, + CONF_PORT, + STATE_OFF, + STATE_ON, + Platform, +) import homeassistant.helpers.config_validation as cv from homeassistant.helpers.script import Script @@ -46,7 +51,7 @@ CONFIG_SCHEMA = vol.Schema( extra=vol.ALLOW_EXTRA, ) -PLATFORMS = [MEDIA_PLAYER_DOMAIN, REMOTE_DOMAIN] +PLATFORMS = [Platform.MEDIA_PLAYER, Platform.REMOTE] async def async_setup(hass, config): diff --git a/homeassistant/components/risco/__init__.py b/homeassistant/components/risco/__init__.py index 0e61d6c7aeb..2e37499b10f 100644 --- a/homeassistant/components/risco/__init__.py +++ b/homeassistant/components/risco/__init__.py @@ -11,6 +11,7 @@ from homeassistant.const import ( CONF_PIN, CONF_SCAN_INTERVAL, CONF_USERNAME, + Platform, ) from homeassistant.core import HomeAssistant from homeassistant.exceptions import ConfigEntryNotReady @@ -20,7 +21,7 @@ from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, Upda from .const import DATA_COORDINATOR, DEFAULT_SCAN_INTERVAL, DOMAIN, EVENTS_COORDINATOR -PLATFORMS = ["alarm_control_panel", "binary_sensor", "sensor"] +PLATFORMS = [Platform.ALARM_CONTROL_PANEL, Platform.BINARY_SENSOR, Platform.SENSOR] UNDO_UPDATE_LISTENER = "undo_update_listener" LAST_EVENT_STORAGE_VERSION = 1 LAST_EVENT_TIMESTAMP_KEY = "last_event_timestamp" diff --git a/homeassistant/components/switcher_kis/__init__.py b/homeassistant/components/switcher_kis/__init__.py index 7be726388f0..d4779e8e748 100644 --- a/homeassistant/components/switcher_kis/__init__.py +++ b/homeassistant/components/switcher_kis/__init__.py @@ -9,7 +9,7 @@ from aioswitcher.device import SwitcherBase import voluptuous as vol from homeassistant.config_entries import SOURCE_IMPORT, ConfigEntry -from homeassistant.const import CONF_DEVICE_ID, EVENT_HOMEASSISTANT_STOP +from homeassistant.const import CONF_DEVICE_ID, EVENT_HOMEASSISTANT_STOP, Platform from homeassistant.core import Event, HomeAssistant, callback from homeassistant.helpers import ( config_validation as cv, @@ -30,7 +30,7 @@ from .const import ( ) from .utils import async_start_bridge, async_stop_bridge -PLATFORMS = ["switch", "sensor"] +PLATFORMS = [Platform.SWITCH, Platform.SENSOR] _LOGGER = logging.getLogger(__name__) diff --git a/homeassistant/components/tasmota/const.py b/homeassistant/components/tasmota/const.py index 48026f3e93d..2e38284e43d 100644 --- a/homeassistant/components/tasmota/const.py +++ b/homeassistant/components/tasmota/const.py @@ -1,4 +1,6 @@ """Constants used by multiple Tasmota modules.""" +from homeassistant.const import Platform + CONF_DISCOVERY_PREFIX = "discovery_prefix" DATA_REMOVE_DISCOVER_COMPONENT = "tasmota_discover_{}" @@ -9,12 +11,12 @@ DEFAULT_PREFIX = "tasmota/discovery" DOMAIN = "tasmota" PLATFORMS = [ - "binary_sensor", - "cover", - "fan", - "light", - "sensor", - "switch", + Platform.BINARY_SENSOR, + Platform.COVER, + Platform.FAN, + Platform.LIGHT, + Platform.SENSOR, + Platform.SWITCH, ] TASMOTA_EVENT = "tasmota_event" diff --git a/homeassistant/components/vera/__init__.py b/homeassistant/components/vera/__init__.py index 3b7c11b943e..bbc6902007a 100644 --- a/homeassistant/components/vera/__init__.py +++ b/homeassistant/components/vera/__init__.py @@ -20,6 +20,7 @@ from homeassistant.const import ( CONF_EXCLUDE, CONF_LIGHTS, EVENT_HOMEASSISTANT_STOP, + Platform, ) from homeassistant.core import HomeAssistant from homeassistant.exceptions import ConfigEntryNotReady @@ -124,7 +125,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: # Exclude devices unwanted by user. devices = [device for device in all_devices if device.device_id not in exclude_ids] - vera_devices = defaultdict(list) + vera_devices: defaultdict[Platform, list[veraApi.VeraDevice]] = defaultdict(list) for device in devices: device_type = map_vera_device(device, light_ids) if device_type is not None: @@ -144,10 +145,9 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: set_controller_data(hass, entry, controller_data) # Forward the config data to the necessary platforms. - for platform in get_configured_platforms(controller_data): - hass.async_create_task( - hass.config_entries.async_forward_entry_setup(entry, platform) - ) + hass.config_entries.async_setup_platforms( + entry, platforms=get_configured_platforms(controller_data) + ) def stop_subscription(event): """Stop SubscriptionRegistry updates.""" @@ -181,24 +181,24 @@ async def _async_update_listener(hass: HomeAssistant, entry: ConfigEntry): await hass.config_entries.async_reload(entry.entry_id) -def map_vera_device(vera_device: veraApi.VeraDevice, remap: list[int]) -> str: +def map_vera_device(vera_device: veraApi.VeraDevice, remap: list[int]) -> Platform: """Map vera classes to Home Assistant types.""" type_map = { - veraApi.VeraDimmer: "light", - veraApi.VeraBinarySensor: "binary_sensor", - veraApi.VeraSensor: "sensor", - veraApi.VeraArmableDevice: "switch", - veraApi.VeraLock: "lock", - veraApi.VeraThermostat: "climate", - veraApi.VeraCurtain: "cover", - veraApi.VeraSceneController: "sensor", - veraApi.VeraSwitch: "switch", + veraApi.VeraDimmer: Platform.LIGHT, + veraApi.VeraBinarySensor: Platform.BINARY_SENSOR, + veraApi.VeraSensor: Platform.SENSOR, + veraApi.VeraArmableDevice: Platform.SWITCH, + veraApi.VeraLock: Platform.LOCK, + veraApi.VeraThermostat: Platform.CLIMATE, + veraApi.VeraCurtain: Platform.COVER, + veraApi.VeraSceneController: Platform.SENSOR, + veraApi.VeraSwitch: Platform.SWITCH, } - def map_special_case(instance_class: type, entity_type: str) -> str: + def map_special_case(instance_class: type, entity_type: Platform) -> Platform: if instance_class is veraApi.VeraSwitch and vera_device.device_id in remap: - return "light" + return Platform.LIGHT return entity_type return next( diff --git a/homeassistant/components/vera/common.py b/homeassistant/components/vera/common.py index 243ee4d7594..d352c162098 100644 --- a/homeassistant/components/vera/common.py +++ b/homeassistant/components/vera/common.py @@ -2,12 +2,13 @@ from __future__ import annotations from collections import defaultdict +from collections.abc import Sequence from typing import NamedTuple import pyvera as pv -from homeassistant.components.scene import DOMAIN as SCENE_DOMAIN from homeassistant.config_entries import ConfigEntry +from homeassistant.const import Platform from homeassistant.core import HomeAssistant from homeassistant.helpers.event import call_later @@ -18,19 +19,19 @@ class ControllerData(NamedTuple): """Controller data.""" controller: pv.VeraController - devices: defaultdict[str, list[pv.VeraDevice]] + devices: defaultdict[Platform, list[pv.VeraDevice]] scenes: list[pv.VeraScene] config_entry: ConfigEntry -def get_configured_platforms(controller_data: ControllerData) -> set[str]: +def get_configured_platforms(controller_data: ControllerData) -> set[Platform]: """Get configured platforms for a controller.""" - platforms = [] + platforms: Sequence[Platform] = [] for platform in controller_data.devices: platforms.append(platform) if controller_data.scenes: - platforms.append(SCENE_DOMAIN) + platforms.append(Platform.SCENE) return set(platforms) diff --git a/homeassistant/components/vesync/__init__.py b/homeassistant/components/vesync/__init__.py index 48a7a577313..95370b01409 100644 --- a/homeassistant/components/vesync/__init__.py +++ b/homeassistant/components/vesync/__init__.py @@ -3,7 +3,7 @@ import logging from pyvesync import VeSync -from homeassistant.const import CONF_PASSWORD, CONF_USERNAME +from homeassistant.const import CONF_PASSWORD, CONF_USERNAME, Platform from homeassistant.helpers import config_validation as cv from homeassistant.helpers.dispatcher import async_dispatcher_send @@ -56,15 +56,15 @@ async def async_setup_entry(hass, config_entry): if device_dict[VS_SWITCHES]: switches.extend(device_dict[VS_SWITCHES]) - hass.async_create_task(forward_setup(config_entry, "switch")) + hass.async_create_task(forward_setup(config_entry, Platform.SWITCH)) if device_dict[VS_FANS]: fans.extend(device_dict[VS_FANS]) - hass.async_create_task(forward_setup(config_entry, "fan")) + hass.async_create_task(forward_setup(config_entry, Platform.FAN)) if device_dict[VS_LIGHTS]: lights.extend(device_dict[VS_LIGHTS]) - hass.async_create_task(forward_setup(config_entry, "light")) + hass.async_create_task(forward_setup(config_entry, Platform.LIGHT)) async def async_new_device_discovery(service): """Discover if new devices should be added."""