Jelajahi Sumber

Refactor common initialisation code into a helper.

Jason Rumney 3 tahun lalu
induk
melakukan
0c9b48e530

+ 8 - 41
custom_components/tuya_local/binary_sensor.py

@@ -1,52 +1,19 @@
 """
 Setup for different kinds of Tuya Binary sensors
 """
-import logging
-
-from . import DOMAIN
-from .const import (
-    CONF_DEVICE_ID,
-    CONF_TYPE,
-)
 from .generic.binary_sensor import TuyaLocalBinarySensor
-from .helpers.device_config import get_config
-
-_LOGGER = logging.getLogger(__name__)
+from .helpers.config import async_tuya_setup_platform
 
 
 async def async_setup_platform(hass, config, async_add_entities, discovery_info=None):
     """Set up the sensor device according to it's type."""
-    data = hass.data[DOMAIN][discovery_info[CONF_DEVICE_ID]]
-    device = data["device"]
-    sensors = []
-
-    cfg = get_config(discovery_info[CONF_TYPE])
-    if cfg is None:
-        raise ValueError(f"No device config found for {discovery_info}")
-    ecfg = cfg.primary_entity
-    if ecfg.entity == "binary_sensor" and (
-        discovery_info.get(ecfg.config_id, False) or not ecfg.deprecated
-    ):
-        data[ecfg.config_id] = TuyaLocalBinarySensor(device, ecfg)
-        sensors.append(data[ecfg.config_id])
-        if ecfg.deprecated:
-            _LOGGER.warning(ecfg.deprecation_message)
-        _LOGGER.debug(f"Adding binary_sensor for {ecfg.config_id}")
-
-    for ecfg in cfg.secondary_entities():
-        if ecfg.entity == "binary_sensor" and (
-            discovery_info.get(ecfg.config_id, False) or not ecfg.deprecated
-        ):
-            data[ecfg.config_id] = TuyaLocalBinarySensor(device, ecfg)
-            sensors.append(data[ecfg.config_id])
-            if ecfg.deprecated:
-                _LOGGER.warning(ecfg.deprecation_message)
-            _LOGGER.debug(f"Adding binary_sensor for {ecfg.config_id}")
-    if not sensors:
-        raise ValueError(
-            f"{device.name} does not support use as a binary_sensor device."
-        )
-    async_add_entities(sensors)
+    await async_tuya_setup_platform(
+        hass,
+        async_add_entities,
+        discovery_info,
+        "binary_sensor",
+        TuyaLocalBinarySensor,
+    )
 
 
 async def async_setup_entry(hass, config_entry, async_add_entities):

+ 8 - 41
custom_components/tuya_local/climate.py

@@ -1,52 +1,19 @@
 """
 Setup for different kinds of Tuya climate devices
 """
-import logging
-
-from . import DOMAIN
-from .const import (
-    CONF_DEVICE_ID,
-    CONF_TYPE,
-)
 from .generic.climate import TuyaLocalClimate
-from .helpers.device_config import get_config
-
-_LOGGER = logging.getLogger(__name__)
+from .helpers.config import async_tuya_setup_platform
 
 
 async def async_setup_platform(hass, config, async_add_entities, discovery_info=None):
     """Set up the Tuya device according to its type."""
-    data = hass.data[DOMAIN][discovery_info[CONF_DEVICE_ID]]
-    device = data["device"]
-    climates = []
-
-    cfg = get_config(discovery_info[CONF_TYPE])
-    if cfg is None:
-        raise ValueError(f"No device config found for {discovery_info}")
-    ecfg = cfg.primary_entity
-    if ecfg.entity == "climate" and (
-        discovery_info.get(ecfg.config_id, False) or not ecfg.deprecated
-    ):
-        data[ecfg.config_id] = TuyaLocalClimate(device, ecfg)
-        climates.append(data[ecfg.config_id])
-        if ecfg.deprecated:
-            _LOGGER.warning(ecfg.deprecation_message)
-        _LOGGER.debug(f"Adding climate for {ecfg.name}")
-
-    for ecfg in cfg.secondary_entities():
-        if ecfg.entity == "climate" and (
-            discovery_info.get(ecfg.config_id, False) or not ecfg.deprecated
-        ):
-            data[ecfg.config_id] = TuyaLocalClimate(device, ecfg)
-            climates.append(data[ecfg.config_id])
-            if ecfg.deprecated:
-                _LOGGER.warning(ecfg.deprecation_message)
-            _LOGGER.debug(f"Adding climate for {ecfg.name}")
-
-    if not climates:
-        raise ValueError(f"{device.name} does not support use as a climate device.")
-
-    async_add_entities(climates)
+    await async_tuya_setup_platform(
+        hass,
+        async_add_entities,
+        discovery_info,
+        "climate",
+        TuyaLocalClimate,
+    )
 
 
 async def async_setup_entry(hass, config_entry, async_add_entities):

+ 8 - 38
custom_components/tuya_local/cover.py

@@ -1,49 +1,19 @@
 """
 Setup for different kinds of Tuya cover devices
 """
-import logging
-
-from . import DOMAIN
-from .const import (
-    CONF_DEVICE_ID,
-    CONF_TYPE,
-)
 from .generic.cover import TuyaLocalCover
-from .helpers.device_config import get_config
-
-_LOGGER = logging.getLogger(__name__)
+from .helpers.config import async_tuya_setup_platform
 
 
 async def async_setup_platform(hass, config, async_add_entities, discovery_info=None):
     """Set up the Tuya device according to its type."""
-    data = hass.data[DOMAIN][discovery_info[CONF_DEVICE_ID]]
-    device = data["device"]
-    covers = []
-
-    cfg = get_config(discovery_info[CONF_TYPE])
-    if cfg is None:
-        raise ValueError(f"No device config found for {discovery_info}")
-    ecfg = cfg.primary_entity
-    if ecfg.entity == "cover" and (
-        discovery_info.get(ecfg.config_id, False) or not ecfg.deprecated
-    ):
-        data[ecfg.config_id] = TuyaLocalCover(device, ecfg)
-        covers.append(data[ecfg.config_id])
-        if ecfg.deprecated:
-            _LOGGER.warning(ecfg.deprecation_message)
-        _LOGGER.debug(f"Adding cover for {ecfg.name}")
-        if ecfg.entity == "cover" and (
-            discovery_info.get(ecfg.config_id, False) or not ecfg.deprecated
-        ):
-            data[ecfg.config_id] = TuyaLocalCover(device, ecfg)
-            covers.append(data[ecfg.config_id])
-            if ecfg.deprecated:
-                _LOGGER.warning(ecfg.deprecation_message)
-            _LOGGER.debug(f"Adding cover for {ecfg.name}")
-
-    if not covers:
-        raise ValueError(f"{device.name} does not support use as a cover device.")
-    async_add_entities(covers)
+    await async_tuya_setup_platform(
+        hass,
+        async_add_entities,
+        discovery_info,
+        "cover",
+        TuyaLocalCover,
+    )
 
 
 async def async_setup_entry(hass, config_entry, async_add_entities):

+ 8 - 41
custom_components/tuya_local/fan.py

@@ -1,52 +1,19 @@
 """
 Setup for different kinds of Tuya fan devices
 """
-import logging
-
-from . import DOMAIN
-from .const import (
-    CONF_DEVICE_ID,
-    CONF_TYPE,
-)
 from .generic.fan import TuyaLocalFan
-from .helpers.device_config import get_config
-
-_LOGGER = logging.getLogger(__name__)
+from .helpers.config import async_tuya_setup_platform
 
 
 async def async_setup_platform(hass, config, async_add_entities, discovery_info=None):
     """Set up the Tuya device according to its type."""
-    data = hass.data[DOMAIN][discovery_info[CONF_DEVICE_ID]]
-    device = data["device"]
-    fans = []
-
-    cfg = get_config(discovery_info[CONF_TYPE])
-    if cfg is None:
-        raise ValueError(f"No device config found for {discovery_info}")
-    ecfg = cfg.primary_entity
-    if ecfg.entity == "fan" and (
-        discovery_info.get(ecfg.config_id, False) or not ecfg.deprecated
-    ):
-        data[ecfg.config_id] = TuyaLocalFan(device, ecfg)
-        fans.append(data[ecfg.config_id])
-        if ecfg.deprecated:
-            _LOGGER.warning(ecfg.deprecation_message)
-        _LOGGER.debug(f"Adding fan for {ecfg.name}")
-
-    for ecfg in cfg.secondary_entities():
-        if ecfg.entity == "fan" and (
-            discovery_info.get(ecfg.config_id, False) or not ecfg.deprecated
-        ):
-            data[ecfg.config_id] = TuyaLocalFan(device, ecfg)
-            fans.append(data[ecfg.config_id])
-            if ecfg.deprecated:
-                _LOGGER.warning(ecfg.deprecation_message)
-            _LOGGER.debug(f"Adding fan for {ecfg.name}")
-
-    if not fans:
-        raise ValueError(f"{device.name} does not support use as a fan device.")
-
-    async_add_entities(fans)
+    await async_tuya_setup_platform(
+        hass,
+        async_add_entities,
+        discovery_info,
+        "fan",
+        TuyaLocalFan,
+    )
 
 
 async def async_setup_entry(hass, config_entry, async_add_entities):

+ 45 - 0
custom_components/tuya_local/helpers/config.py

@@ -0,0 +1,45 @@
+"""
+Helper for general config
+"""
+import logging
+
+from .. import DOMAIN
+from ..const import CONF_DEVICE_ID, CONF_TYPE
+from .device_config import get_config
+
+_LOGGER = logging.getLogger(__name__)
+
+
+async def async_tuya_setup_platform(
+    hass, async_add_entities, discovery_info, platform, entity_class
+):
+    """Common functions for async_setup_platform for each entity platform."""
+    data = hass.data[DOMAIN][discovery_info[CONF_DEVICE_ID]]
+    device = data["device"]
+    entities = []
+
+    cfg = get_config(discovery_info[CONF_TYPE])
+    if cfg is None:
+        raise ValueError(f"No device config found for {discovery_info}")
+    ecfg = cfg.primary_entity
+    if ecfg.entity == platform and (
+        discovery_info.get(ecfg.config_id, False) or not ecfg.deprecated
+    ):
+        data[ecfg.config_id] = entity_class(device, ecfg)
+        entities.append(data[ecfg.config_id])
+        if ecfg.deprecated:
+            _LOGGER.warning(ecfg.deprecation_message)
+        _LOGGER.debug(f"Adding {platform} for {ecfg.config_id}")
+
+    for ecfg in cfg.secondary_entities():
+        if ecfg.entity == platform and (
+            discovery_info.get(ecfg.config_id, False) or not ecfg.deprecated
+        ):
+            data[ecfg.config_id] = entity_class(device, ecfg)
+            entities.append(data[ecfg.config_id])
+            if ecfg.deprecated:
+                _LOGGER.warning(ecfg.deprecation_message)
+            _LOGGER.debug(f"Adding {platform} for {ecfg.config_id}")
+    if not entities:
+        raise ValueError(f"{device.name} does not support use as a {platform} device.")
+    async_add_entities(entities)

+ 8 - 41
custom_components/tuya_local/humidifier.py

@@ -1,52 +1,19 @@
 """
 Setup for different kinds of Tuya humidifier devices
 """
-import logging
-
-from . import DOMAIN
-from .const import (
-    CONF_DEVICE_ID,
-    CONF_TYPE,
-)
 from .generic.humidifier import TuyaLocalHumidifier
-from .helpers.device_config import get_config
-
-_LOGGER = logging.getLogger(__name__)
+from .helpers.config import async_tuya_setup_platform
 
 
 async def async_setup_platform(hass, config, async_add_entities, discovery_info=None):
     """Set up the Tuya device according to its type."""
-    data = hass.data[DOMAIN][discovery_info[CONF_DEVICE_ID]]
-    device = data["device"]
-    humidifiers = []
-
-    cfg = get_config(discovery_info[CONF_TYPE])
-    if cfg is None:
-        raise ValueError(f"No device config found for {discovery_info}")
-    ecfg = cfg.primary_entity
-    if ecfg.entity == "humidifier" and (
-        discovery_info.get(ecfg.config_id, False) or not ecfg.deprecated
-    ):
-        data[ecfg.config_id] = TuyaLocalHumidifier(device, ecfg)
-        humidifiers.append(data[ecfg.config_id])
-        if ecfg.deprecated:
-            _LOGGER.warning(ecfg.deprecation_message)
-        _LOGGER.debug(f"Adding humidifier for {ecfg.name}")
-
-    for ecfg in cfg.secondary_entities():
-        if ecfg.entity == "humidifier" and (
-            discovery_info.get(ecfg.config_id, False) or not ecfg.deprecated
-        ):
-            data[ecfg.config_id] = TuyaLocalHumidifier(device, ecfg)
-            humidifiers.append(data[ecfg.config_id])
-            if ecfg.deprecated:
-                _LOGGER.warning(ecfg.deprecation_message)
-            _LOGGER.debug(f"Adding humidifier for {ecfg.name}")
-
-    if not humidifiers:
-        raise ValueError(f"{device.name} does not support use as a humidifier device.")
-
-    async_add_entities(humidifiers)
+    await async_tuya_setup_platform(
+        hass,
+        async_add_entities,
+        discovery_info,
+        "humidifier",
+        TuyaLocalHumidifier,
+    )
 
 
 async def async_setup_entry(hass, config_entry, async_add_entities):

+ 8 - 41
custom_components/tuya_local/light.py

@@ -1,52 +1,19 @@
 """
 Setup for different kinds of Tuya light devices
 """
-import logging
-
-from . import DOMAIN
-from .const import (
-    CONF_DEVICE_ID,
-    CONF_TYPE,
-)
 from .generic.light import TuyaLocalLight
-from .helpers.device_config import get_config
-
-_LOGGER = logging.getLogger(__name__)
+from .helpers.config import async_tuya_setup_platform
 
 
 async def async_setup_platform(hass, config, async_add_entities, discovery_info=None):
     """Set up the light device according to its type."""
-    data = hass.data[DOMAIN][discovery_info[CONF_DEVICE_ID]]
-    device = data["device"]
-    lights = []
-
-    cfg = get_config(discovery_info[CONF_TYPE])
-    if cfg is None:
-        raise ValueError(f"No device config found for {discovery_info}")
-    ecfg = cfg.primary_entity
-    if ecfg.entity == "light" and (
-        discovery_info.get(ecfg.config_id, False) or not ecfg.deprecated
-    ):
-        data[ecfg.config_id] = TuyaLocalLight(device, ecfg)
-        lights.append(data[ecfg.config_id])
-        if ecfg.deprecated:
-            _LOGGER.warning(ecfg.deprecation_message)
-        _LOGGER.debug(f"Adding light for {device.name}/{ecfg.name}")
-
-    for ecfg in cfg.secondary_entities():
-        if ecfg.entity == "light" and (
-            discovery_info.get(ecfg.config_id, False) or not ecfg.deprecated
-        ):
-            data[ecfg.config_id] = TuyaLocalLight(device, ecfg)
-            lights.append(data[ecfg.config_id])
-            if ecfg.deprecated:
-                _LOGGER.warning(ecfg.deprecation_message)
-            _LOGGER.debug(f"Adding light for {ecfg.name}")
-
-    if not lights:
-        raise ValueError(f"{device.name} does not support use as a light device.")
-
-    async_add_entities(lights)
+    await async_tuya_setup_platform(
+        hass,
+        async_add_entities,
+        discovery_info,
+        "light",
+        TuyaLocalLight,
+    )
 
 
 async def async_setup_entry(hass, config_entry, async_add_entities):

+ 8 - 41
custom_components/tuya_local/lock.py

@@ -1,52 +1,19 @@
 """
 Setup for different kinds of Tuya lock devices
 """
-import logging
-
-from . import DOMAIN
-from .const import (
-    CONF_DEVICE_ID,
-    CONF_TYPE,
-)
 from .generic.lock import TuyaLocalLock
-from .helpers.device_config import get_config
-
-_LOGGER = logging.getLogger(__name__)
+from .helpers.config import async_tuya_setup_platform
 
 
 async def async_setup_platform(hass, config, async_add_entities, discovery_info=None):
     """Set up the lock device according to its type."""
-    _LOGGER.debug(f"Domain data: {hass.data[DOMAIN]}")
-    data = hass.data[DOMAIN][discovery_info[CONF_DEVICE_ID]]
-    device = data["device"]
-    locks = []
-
-    cfg = get_config(discovery_info[CONF_TYPE])
-    if cfg is None:
-        raise ValueError(f"No device config found for {discovery_info}")
-    ecfg = cfg.primary_entity
-    if ecfg.entity == "lock" and (
-        discovery_info.get(ecfg.config_id, False) or not ecfg.deprecated
-    ):
-        data[ecfg.config_id] = TuyaLocalLock(device, ecfg)
-        locks.append(data[ecfg.config_id])
-        if ecfg.deprecated:
-            _LOGGER.warning(ecfg.deprecation_message)
-        _LOGGER.debug(f"Adding lock for {ecfg.name}")
-
-    for ecfg in cfg.secondary_entities():
-        if ecfg.entity == "lock" and (
-            discovery_info.get(ecfg.config_id, False) or not ecfg.deprecated
-        ):
-            data[ecfg.config_id] = TuyaLocalLock(device, ecfg)
-            locks.append(data[ecfg.config_id])
-            if ecfg.deprecated:
-                _LOGGER.warning(ecfg.deprecation_message)
-            _LOGGER.debug(f"Adding lock for {ecfg.name}")
-
-    if not locks:
-        raise ValueError(f"{device.name} does not support use as a lock device.")
-    async_add_entities(locks)
+    await async_tuya_setup_platform(
+        hass,
+        async_add_entities,
+        discovery_info,
+        "lock",
+        TuyaLocalLock,
+    )
 
 
 async def async_setup_entry(hass, config_entry, async_add_entities):

+ 8 - 40
custom_components/tuya_local/number.py

@@ -1,51 +1,19 @@
 """
 Setup for different kinds of Tuya numbers
 """
-import logging
-
-from . import DOMAIN
-from .const import (
-    CONF_DEVICE_ID,
-    CONF_TYPE,
-)
 from .generic.number import TuyaLocalNumber
-from .helpers.device_config import get_config
-
-_LOGGER = logging.getLogger(__name__)
+from .helpers.config import async_tuya_setup_platform
 
 
 async def async_setup_platform(hass, config, async_add_entities, discovery_info=None):
     """Set up the number entity according to it's type."""
-    data = hass.data[DOMAIN][discovery_info[CONF_DEVICE_ID]]
-    device = data["device"]
-    numbers = []
-
-    cfg = get_config(discovery_info[CONF_TYPE])
-    if cfg is None:
-        raise ValueError(f"No device config found for {discovery_info}")
-    ecfg = cfg.primary_entity
-    if ecfg.entity == "number" and (
-        discovery_info.get(ecfg.config_id, False) or not ecfg.deprecated
-    ):
-        data[ecfg.config_id] = TuyaLocalNumber(device, ecfg)
-        numbers.append(data[ecfg.config_id])
-        if ecfg.deprecated:
-            _LOGGER.warning(ecfg.deprecation_message)
-        _LOGGER.debug(f"Adding number for {discovery_info[ecfg.config_id]}")
-
-    for ecfg in cfg.secondary_entities():
-        if ecfg.entity == "number" and (
-            discovery_info.get(ecfg.config_id, False) or not ecfg.deprecated
-        ):
-            data[ecfg.config_id] = TuyaLocalNumber(device, ecfg)
-            numbers.append(data[ecfg.config_id])
-            if ecfg.deprecated:
-                _LOGGER.warning(ecfg.deprecation_message)
-            _LOGGER.debug(f"Adding number for {discovery_info[ecfg.config_id]}")
-
-    if not numbers:
-        raise ValueError(f"{device.name} does not support use as a number device.")
-    async_add_entities(numbers)
+    await async_tuya_setup_platform(
+        hass,
+        async_add_entities,
+        discovery_info,
+        "number",
+        TuyaLocalNumber,
+    )
 
 
 async def async_setup_entry(hass, config_entry, async_add_entities):

+ 8 - 40
custom_components/tuya_local/select.py

@@ -1,51 +1,19 @@
 """
 Setup for different kinds of Tuya selects
 """
-import logging
-
-from . import DOMAIN
-from .const import (
-    CONF_DEVICE_ID,
-    CONF_TYPE,
-)
 from .generic.select import TuyaLocalSelect
-from .helpers.device_config import get_config
-
-_LOGGER = logging.getLogger(__name__)
+from .helpers.config import async_tuya_setup_platform
 
 
 async def async_setup_platform(hass, config, async_add_entities, discovery_info=None):
     """Set up the select entity according to it's type."""
-    data = hass.data[DOMAIN][discovery_info[CONF_DEVICE_ID]]
-    device = data["device"]
-    selects = []
-
-    cfg = get_config(discovery_info[CONF_TYPE])
-    if cfg is None:
-        raise ValueError(f"No device config found for {discovery_info}")
-    ecfg = cfg.primary_entity
-    if ecfg.entity == "select" and (
-        discovery_info.get(ecfg.config_id, False) or not ecfg.deprecated
-    ):
-        data[ecfg.config_id] = TuyaLocalSelect(device, ecfg)
-        selects.append(data[ecfg.config_id])
-        if ecfg.deprecated:
-            _LOGGER.warning(ecfg.deprecation_message)
-        _LOGGER.debug(f"Adding select for {discovery_info[ecfg.config_id]}")
-
-    for ecfg in cfg.secondary_entities():
-        if ecfg.entity == "select" and (
-            discovery_info.get(ecfg.config_id, False) or not ecfg.deprecated
-        ):
-            data[ecfg.config_id] = TuyaLocalSelect(device, ecfg)
-            selects.append(data[ecfg.config_id])
-            if ecfg.deprecated:
-                _LOGGER.warning(ecfg.deprecation_message)
-            _LOGGER.debug(f"Adding select for {discovery_info[ecfg.config_id]}")
-
-    if not selects:
-        raise ValueError(f"{device.name} does not support use as a select device.")
-    async_add_entities(selects)
+    await async_tuya_setup_platform(
+        hass,
+        async_add_entities,
+        discovery_info,
+        "select",
+        TuyaLocalSelect,
+    )
 
 
 async def async_setup_entry(hass, config_entry, async_add_entities):

+ 8 - 40
custom_components/tuya_local/sensor.py

@@ -1,51 +1,19 @@
 """
 Setup for different kinds of Tuya sensors
 """
-import logging
-
-from . import DOMAIN
-from .const import (
-    CONF_DEVICE_ID,
-    CONF_TYPE,
-)
 from .generic.sensor import TuyaLocalSensor
-from .helpers.device_config import get_config
-
-_LOGGER = logging.getLogger(__name__)
+from .helpers.config import async_tuya_setup_platform
 
 
 async def async_setup_platform(hass, config, async_add_entities, discovery_info=None):
     """Set up the sensor device according to it's type."""
-    data = hass.data[DOMAIN][discovery_info[CONF_DEVICE_ID]]
-    device = data["device"]
-    sensors = []
-
-    cfg = get_config(discovery_info[CONF_TYPE])
-    if cfg is None:
-        raise ValueError(f"No device config found for {discovery_info}")
-    ecfg = cfg.primary_entity
-    if ecfg.entity == "sensor" and (
-        discovery_info.get(ecfg.config_id, False) or not ecfg.deprecated
-    ):
-        data[ecfg.config_id] = TuyaLocalSensor(device, ecfg)
-        sensors.append(data[ecfg.config_id])
-        if ecfg.deprecated:
-            _LOGGER.warning(ecfg.deprecation_message)
-        _LOGGER.debug(f"Adding sensor for {discovery_info[ecfg.config_id]}")
-
-    for ecfg in cfg.secondary_entities():
-        if ecfg.entity == "sensor" and (
-            discovery_info.get(ecfg.config_id, False) or not ecfg.deprecated
-        ):
-            data[ecfg.config_id] = TuyaLocalSensor(device, ecfg)
-            sensors.append(data[ecfg.config_id])
-            if ecfg.deprecated:
-                _LOGGER.warning(ecfg.deprecation_message)
-            _LOGGER.debug(f"Adding sensor for {discovery_info[ecfg.config_id]}")
-
-    if not sensors:
-        raise ValueError(f"{device.name} does not support use as a sensor device.")
-    async_add_entities(sensors)
+    await async_tuya_setup_platform(
+        hass,
+        async_add_entities,
+        discovery_info,
+        "sensor",
+        TuyaLocalSensor,
+    )
 
 
 async def async_setup_entry(hass, config_entry, async_add_entities):

+ 8 - 41
custom_components/tuya_local/switch.py

@@ -1,52 +1,19 @@
 """
 Setup for different kinds of Tuya switch devices
 """
-import logging
-
-from . import DOMAIN
-from .const import (
-    CONF_DEVICE_ID,
-    CONF_TYPE,
-)
 from .generic.switch import TuyaLocalSwitch
-from .helpers.device_config import get_config
-
-_LOGGER = logging.getLogger(__name__)
+from .helpers.config import async_tuya_setup_platform
 
 
 async def async_setup_platform(hass, config, async_add_entities, discovery_info=None):
     """Set up the switch device according to its type."""
-    data = hass.data[DOMAIN][discovery_info[CONF_DEVICE_ID]]
-    device = data["device"]
-    switches = []
-
-    cfg = get_config(discovery_info[CONF_TYPE])
-    if cfg is None:
-        raise ValueError(f"No device config found for {discovery_info}")
-    ecfg = cfg.primary_entity
-    if ecfg.entity == "switch" and (
-        discovery_info.get(ecfg.config_id, False) or not ecfg.deprecated
-    ):
-        data[ecfg.config_id] = TuyaLocalSwitch(device, ecfg)
-        switches.append(data[ecfg.config_id])
-        if ecfg.deprecated:
-            _LOGGER.warning(ecfg.deprecation_message)
-        _LOGGER.debug(f"Adding switch for {discovery_info[ecfg.config_id]}")
-
-    for ecfg in cfg.secondary_entities():
-        if ecfg.entity == "switch" and (
-            discovery_info.get(ecfg.config_id, False) or not ecfg.deprecated
-        ):
-            data[ecfg.config_id] = TuyaLocalSwitch(device, ecfg)
-            switches.append(data[ecfg.config_id])
-            if ecfg.deprecated:
-                _LOGGER.warning(ecfg.deprecation_message)
-            _LOGGER.debug(f"Adding switch for {discovery_info[ecfg.config_id]}")
-
-    if not switches:
-        raise ValueError(f"{device.name} does not support use as a switch device.")
-
-    async_add_entities(switches)
+    await async_tuya_setup_platform(
+        hass,
+        async_add_entities,
+        discovery_info,
+        "switch",
+        TuyaLocalSwitch,
+    )
 
 
 async def async_setup_entry(hass, config_entry, async_add_entities):

+ 8 - 40
custom_components/tuya_local/vacuum.py

@@ -1,51 +1,19 @@
 """
 Setup for different kinds of Tuya vacuum cleaners
 """
-import logging
-
-from . import DOMAIN
-from .const import (
-    CONF_DEVICE_ID,
-    CONF_TYPE,
-)
 from .generic.vacuum import TuyaLocalVacuum
-from .helpers.device_config import get_config
-
-_LOGGER = logging.getLogger(__name__)
+from .helpers.config import async_tuya_setup_platform
 
 
 async def async_setup_platform(hass, config, async_add_entities, discovery_info=None):
     """Set up the vacuum device according to its type."""
-    data = hass.data[DOMAIN][discovery_info[CONF_DEVICE_ID]]
-    device = data["device"]
-    vacs = []
-
-    cfg = get_config(discovery_info[CONF_TYPE])
-    if cfg is None:
-        raise ValueError(f"No device config found for {discovery_info}")
-    ecfg = cfg.primary_entity
-    if ecfg.entity == "vacuum" and (
-        discovery_info.get(ecfg.config_id, False) or not ecfg.deprecated
-    ):
-        data[ecfg.config_id] = TuyaLocalVacuum(device, ecfg)
-        vacs.append(data[ecfg.config_id])
-        if ecfg.deprecated:
-            _LOGGER.warning(ecfg.deprecation_message)
-        _LOGGER.debug(f"Adding vacuum for {device.name}/{ecfg.name}")
-
-    for ecfg in cfg.secondary_entities():
-        if ecfg.entity == "vacuum" and (
-            discovery_info.get(ecfg.config_id, False) or not ecfg.deprecated
-        ):
-            data[ecfg.config_id] = TuyaLocalVacuum(device, ecfg)
-            vacs.append(data[ecfg.config_id])
-            if ecfg.deprecated:
-                _LOGGER.warning(ecfg.deprecation_message)
-            _LOGGER.debug(f"Adding vacuum for {ecfg.name}")
-
-    if not vacs:
-        raise ValueError(f"{device.name} does not support use as a vacuum device.")
-    async_add_entities(vacs)
+    await async_tuya_setup_platform(
+        hass,
+        async_add_entities,
+        discovery_info,
+        "vacuum",
+        TuyaLocalVacuum,
+    )
 
 
 async def async_setup_entry(hass, config_entry, async_add_entities):