1
mirror of https://github.com/home-assistant/core synced 2024-10-07 10:13:38 +02:00

Use Platform enum in load_platform (#63903)

Co-authored-by: epenet <epenet@users.noreply.github.com>
This commit is contained in:
epenet 2022-01-13 10:46:16 +01:00 committed by GitHub
parent ed5e1af10a
commit 04d2c6e0bf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 69 additions and 54 deletions

View File

@ -10,6 +10,7 @@ from homeassistant.const import (
CONF_PASSWORD, CONF_PASSWORD,
CONF_TYPE, CONF_TYPE,
CONF_USERNAME, CONF_USERNAME,
Platform,
) )
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.helpers import discovery from homeassistant.helpers import discovery
@ -42,14 +43,14 @@ CONFIG_SCHEMA = vol.Schema(
extra=vol.ALLOW_EXTRA, extra=vol.ALLOW_EXTRA,
) )
EUFY_DISPATCH = { PLATFORMS = {
"T1011": "light", "T1011": Platform.LIGHT,
"T1012": "light", "T1012": Platform.LIGHT,
"T1013": "light", "T1013": Platform.LIGHT,
"T1201": "switch", "T1201": Platform.SWITCH,
"T1202": "switch", "T1202": Platform.SWITCH,
"T1203": "switch", "T1203": Platform.SWITCH,
"T1211": "switch", "T1211": Platform.SWITCH,
} }
@ -62,19 +63,19 @@ def setup(hass: HomeAssistant, config: ConfigType) -> bool:
) )
for device in data: for device in data:
kind = device["type"] kind = device["type"]
if kind not in EUFY_DISPATCH: if kind not in PLATFORMS:
continue continue
discovery.load_platform(hass, EUFY_DISPATCH[kind], DOMAIN, device, config) discovery.load_platform(hass, PLATFORMS[kind], DOMAIN, device, config)
for device_info in config[DOMAIN][CONF_DEVICES]: for device_info in config[DOMAIN][CONF_DEVICES]:
kind = device_info["type"] kind = device_info["type"]
if kind not in EUFY_DISPATCH: if kind not in PLATFORMS:
continue continue
device = {} device = {}
device["address"] = device_info["address"] device["address"] = device_info["address"]
device["code"] = device_info["access_token"] device["code"] = device_info["access_token"]
device["type"] = device_info["type"] device["type"] = device_info["type"]
device["name"] = device_info["name"] device["name"] = device_info["name"]
discovery.load_platform(hass, EUFY_DISPATCH[kind], DOMAIN, device, config) discovery.load_platform(hass, PLATFORMS[kind], DOMAIN, device, config)
return True return True

View File

@ -18,6 +18,7 @@ from homeassistant.const import (
CONF_TOKEN, CONF_TOKEN,
CONF_USERNAME, CONF_USERNAME,
TEMP_CELSIUS, TEMP_CELSIUS,
Platform,
) )
from homeassistant.core import HomeAssistant, ServiceCall, callback from homeassistant.core import HomeAssistant, ServiceCall, callback
from homeassistant.helpers import config_validation as cv from homeassistant.helpers import config_validation as cv
@ -95,6 +96,14 @@ SET_ZONE_OVERRIDE_SCHEMA = vol.Schema(
} }
) )
PLATFORMS = (
Platform.CLIMATE,
Platform.WATER_HEATER,
Platform.SENSOR,
Platform.BINARY_SENSOR,
Platform.SWITCH,
)
async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool: async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
"""Create a Genius Hub system.""" """Create a Genius Hub system."""
@ -120,7 +129,7 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
async_track_time_interval(hass, broker.async_update, SCAN_INTERVAL) async_track_time_interval(hass, broker.async_update, SCAN_INTERVAL)
for platform in ("climate", "water_heater", "sensor", "binary_sensor", "switch"): for platform in PLATFORMS:
hass.async_create_task(async_load_platform(hass, platform, DOMAIN, {}, config)) hass.async_create_task(async_load_platform(hass, platform, DOMAIN, {}, config))
setup_service_functions(hass, broker) setup_service_functions(hass, broker)

View File

@ -7,7 +7,7 @@ from aiohttp import ClientResponseError
from incomfortclient import Gateway as InComfortGateway from incomfortclient import Gateway as InComfortGateway
import voluptuous as vol import voluptuous as vol
from homeassistant.const import CONF_HOST, CONF_PASSWORD, CONF_USERNAME from homeassistant.const import CONF_HOST, CONF_PASSWORD, CONF_USERNAME, Platform
from homeassistant.core import HomeAssistant, callback from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers import config_validation as cv from homeassistant.helpers import config_validation as cv
from homeassistant.helpers.aiohttp_client import async_get_clientsession from homeassistant.helpers.aiohttp_client import async_get_clientsession
@ -33,6 +33,13 @@ CONFIG_SCHEMA = vol.Schema(
extra=vol.ALLOW_EXTRA, extra=vol.ALLOW_EXTRA,
) )
PLATFORMS = (
Platform.WATER_HEATER,
Platform.BINARY_SENSOR,
Platform.SENSOR,
Platform.CLIMATE,
)
async def async_setup(hass: HomeAssistant, hass_config: ConfigType) -> bool: async def async_setup(hass: HomeAssistant, hass_config: ConfigType) -> bool:
"""Create an Intergas InComfort/Intouch system.""" """Create an Intergas InComfort/Intouch system."""
@ -54,7 +61,7 @@ async def async_setup(hass: HomeAssistant, hass_config: ConfigType) -> bool:
for heater in heaters: for heater in heaters:
await heater.update() await heater.update()
for platform in ("water_heater", "binary_sensor", "sensor", "climate"): for platform in PLATFORMS:
hass.async_create_task( hass.async_create_task(
async_load_platform(hass, platform, DOMAIN, {}, hass_config) async_load_platform(hass, platform, DOMAIN, {}, hass_config)
) )

View File

@ -5,7 +5,7 @@ import logging
from keba_kecontact.connection import KebaKeContact from keba_kecontact.connection import KebaKeContact
import voluptuous as vol import voluptuous as vol
from homeassistant.const import CONF_HOST from homeassistant.const import CONF_HOST, Platform
from homeassistant.core import HomeAssistant, ServiceCall from homeassistant.core import HomeAssistant, ServiceCall
from homeassistant.helpers import discovery from homeassistant.helpers import discovery
import homeassistant.helpers.config_validation as cv import homeassistant.helpers.config_validation as cv
@ -14,7 +14,7 @@ from homeassistant.helpers.typing import ConfigType
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
DOMAIN = "keba" DOMAIN = "keba"
SUPPORTED_COMPONENTS = ["binary_sensor", "sensor", "lock", "notify"] PLATFORMS = (Platform.BINARY_SENSOR, Platform.SENSOR, Platform.LOCK, Platform.NOTIFY)
CONF_RFID = "rfid" CONF_RFID = "rfid"
CONF_FS = "failsafe" CONF_FS = "failsafe"
@ -93,9 +93,9 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
hass.services.async_register(DOMAIN, service, execute_service) hass.services.async_register(DOMAIN, service, execute_service)
# Load components # Load components
for domain in SUPPORTED_COMPONENTS: for platform in PLATFORMS:
hass.async_create_task( hass.async_create_task(
discovery.async_load_platform(hass, domain, DOMAIN, {}, config) discovery.async_load_platform(hass, platform, DOMAIN, {}, config)
) )
# Start periodic polling of charging station data # Start periodic polling of charging station data

View File

@ -2,8 +2,6 @@
from lightwave.lightwave import LWLink from lightwave.lightwave import LWLink
import voluptuous as vol import voluptuous as vol
from homeassistant.components.climate import DOMAIN as CLIMATE_DOMAIN
from homeassistant.components.sensor import DOMAIN as SENSOR_DOMAIN
from homeassistant.const import ( from homeassistant.const import (
CONF_HOST, CONF_HOST,
CONF_LIGHTS, CONF_LIGHTS,
@ -65,6 +63,8 @@ CONFIG_SCHEMA = vol.Schema(
extra=vol.ALLOW_EXTRA, extra=vol.ALLOW_EXTRA,
) )
PLATFORMS = (Platform.CLIMATE, Platform.SENSOR)
async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool: async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
"""Try to start embedded Lightwave broker.""" """Try to start embedded Lightwave broker."""
@ -88,8 +88,7 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
proxy_port = trv[CONF_PROXY_PORT] proxy_port = trv[CONF_PROXY_PORT]
lwlink.set_trv_proxy(proxy_ip, proxy_port) lwlink.set_trv_proxy(proxy_ip, proxy_port)
platforms = [CLIMATE_DOMAIN, SENSOR_DOMAIN] for platform in PLATFORMS:
for platform in platforms:
hass.async_create_task( hass.async_create_task(
async_load_platform(hass, platform, DOMAIN, trvs, config) async_load_platform(hass, platform, DOMAIN, trvs, config)
) )

View File

@ -1,19 +1,13 @@
"""Constants used in modbus integration.""" """Constants used in modbus integration."""
from enum import Enum from enum import Enum
from homeassistant.components.binary_sensor import DOMAIN as BINARY_SENSOR_DOMAIN
from homeassistant.components.climate.const import DOMAIN as CLIMATE_DOMAIN
from homeassistant.components.cover import DOMAIN as COVER_DOMAIN
from homeassistant.components.fan import DOMAIN as FAN_DOMAIN
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.const import ( from homeassistant.const import (
CONF_BINARY_SENSORS, CONF_BINARY_SENSORS,
CONF_COVERS, CONF_COVERS,
CONF_LIGHTS, CONF_LIGHTS,
CONF_SENSORS, CONF_SENSORS,
CONF_SWITCHES, CONF_SWITCHES,
Platform,
) )
# configuration names # configuration names
@ -131,11 +125,11 @@ MODBUS_DOMAIN = "modbus"
ACTIVE_SCAN_INTERVAL = 2 # limit to force an extra update ACTIVE_SCAN_INTERVAL = 2 # limit to force an extra update
PLATFORMS = ( PLATFORMS = (
(BINARY_SENSOR_DOMAIN, CONF_BINARY_SENSORS), (Platform.BINARY_SENSOR, CONF_BINARY_SENSORS),
(CLIMATE_DOMAIN, CONF_CLIMATES), (Platform.CLIMATE, CONF_CLIMATES),
(COVER_DOMAIN, CONF_COVERS), (Platform.COVER, CONF_COVERS),
(LIGHT_DOMAIN, CONF_LIGHTS), (Platform.LIGHT, CONF_LIGHTS),
(FAN_DOMAIN, CONF_FANS), (Platform.FAN, CONF_FANS),
(SENSOR_DOMAIN, CONF_SENSORS), (Platform.SENSOR, CONF_SENSORS),
(SWITCH_DOMAIN, CONF_SWITCHES), (Platform.SWITCH, CONF_SWITCHES),
) )

View File

@ -15,6 +15,7 @@ from homeassistant.const import (
CONF_URL, CONF_URL,
EVENT_HOMEASSISTANT_START, EVENT_HOMEASSISTANT_START,
EVENT_HOMEASSISTANT_STOP, EVENT_HOMEASSISTANT_STOP,
Platform,
) )
from homeassistant.core import HomeAssistant, callback from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers.aiohttp_client import async_get_clientsession from homeassistant.helpers.aiohttp_client import async_get_clientsession
@ -166,11 +167,11 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
hass.data[DOMAIN] = qsusb hass.data[DOMAIN] = qsusb
comps: dict[str, list] = { comps: dict[Platform, list] = {
"switch": [], Platform.SWITCH: [],
"light": [], Platform.LIGHT: [],
"sensor": [], Platform.SENSOR: [],
"binary_sensor": [], Platform.BINARY_SENSOR: [],
} }
sensor_ids = [] sensor_ids = []
@ -179,9 +180,9 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
_, _type = SENSORS[sens["type"]] _, _type = SENSORS[sens["type"]]
sensor_ids.append(sens["id"]) sensor_ids.append(sens["id"])
if _type is bool: if _type is bool:
comps["binary_sensor"].append(sens) comps[Platform.BINARY_SENSOR].append(sens)
continue continue
comps["sensor"].append(sens) comps[Platform.SENSOR].append(sens)
for _key in ("invert", "class"): for _key in ("invert", "class"):
if _key in sens: if _key in sens:
_LOGGER.warning( _LOGGER.warning(
@ -199,9 +200,9 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
if dev.qstype != QSType.relay: if dev.qstype != QSType.relay:
_LOGGER.warning("You specified a switch that is not a relay %s", qsid) _LOGGER.warning("You specified a switch that is not a relay %s", qsid)
continue continue
comps["switch"].append(qsid) comps[Platform.SWITCH].append(qsid)
elif dev.qstype in (QSType.relay, QSType.dimmer): elif dev.qstype in (QSType.relay, QSType.dimmer):
comps["light"].append(qsid) comps[Platform.LIGHT].append(qsid)
else: else:
_LOGGER.warning("Ignored unknown QSUSB device: %s", dev) _LOGGER.warning("Ignored unknown QSUSB device: %s", dev)
continue continue

View File

@ -22,7 +22,7 @@ from homeassistant.helpers.typing import ConfigType
from .const import ( from .const import (
API, API,
COMPONENT, COMPONENT_PLATFORM,
CONF_INVERT_POSITION, CONF_INVERT_POSITION,
DEFAULT_OFFSET, DEFAULT_OFFSET,
DEFAULT_RETRY, DEFAULT_RETRY,
@ -167,7 +167,9 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
await update_slides() await update_slides()
hass.async_create_task(async_load_platform(hass, COMPONENT, DOMAIN, {}, config)) hass.async_create_task(
async_load_platform(hass, COMPONENT_PLATFORM, DOMAIN, {}, config)
)
async_track_time_interval(hass, update_slides, scaninterval) async_track_time_interval(hass, update_slides, scaninterval)

View File

@ -1,7 +1,9 @@
"""Define constants for the Slide component.""" """Define constants for the Slide component."""
from homeassistant.const import Platform
API = "api" API = "api"
COMPONENT = "cover" COMPONENT_PLATFORM = Platform.COVER
CONF_INVERT_POSITION = "invert_position" CONF_INVERT_POSITION = "invert_position"
DOMAIN = "slide" DOMAIN = "slide"
SLIDES = "slides" SLIDES = "slides"

View File

@ -8,7 +8,7 @@ import async_timeout
from asyncpysupla import SuplaAPI from asyncpysupla import SuplaAPI
import voluptuous as vol import voluptuous as vol
from homeassistant.const import CONF_ACCESS_TOKEN from homeassistant.const import CONF_ACCESS_TOKEN, Platform
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.helpers.aiohttp_client import async_get_clientsession from homeassistant.helpers.aiohttp_client import async_get_clientsession
import homeassistant.helpers.config_validation as cv import homeassistant.helpers.config_validation as cv
@ -28,9 +28,9 @@ CONF_SERVERS = "servers"
SCAN_INTERVAL = timedelta(seconds=10) SCAN_INTERVAL = timedelta(seconds=10)
SUPLA_FUNCTION_HA_CMP_MAP = { SUPLA_FUNCTION_HA_CMP_MAP = {
"CONTROLLINGTHEROLLERSHUTTER": "cover", "CONTROLLINGTHEROLLERSHUTTER": Platform.COVER,
"CONTROLLINGTHEGATE": "cover", "CONTROLLINGTHEGATE": Platform.COVER,
"LIGHTSWITCH": "switch", "LIGHTSWITCH": Platform.SWITCH,
} }
SUPLA_FUNCTION_NONE = "NONE" SUPLA_FUNCTION_NONE = "NONE"
SUPLA_SERVERS = "supla_servers" SUPLA_SERVERS = "supla_servers"
@ -98,7 +98,7 @@ async def discover_devices(hass, hass_config):
Currently it is only run at startup. Currently it is only run at startup.
""" """
component_configs = {} component_configs: dict[Platform, list[dict]] = {}
for server_name, server in hass.data[DOMAIN][SUPLA_SERVERS].items(): for server_name, server in hass.data[DOMAIN][SUPLA_SERVERS].items():