Просмотр исходного кода

Use config file to work out which entities should be supported.

New property config_id allows for config ids that are not the entity type
in future, so multiple entities of the same type may be configured for the same
device.  Further work on entity implementations and probably unique_id assignment
is needed before this split can be implemented though.
Jason Rumney 4 лет назад
Родитель
Сommit
195bea6015

+ 30 - 41
custom_components/tuya_local/__init__.py

@@ -13,14 +13,10 @@ from homeassistant.const import CONF_HOST
 from homeassistant.core import HomeAssistant
 
 from .const import (
-    CONF_CLIMATE,
     CONF_DEVICE_ID,
-    CONF_FAN,
-    CONF_HUMIDIFIER,
     CONF_LIGHT,
     CONF_LOCAL_KEY,
     CONF_LOCK,
-    CONF_SWITCH,
     CONF_TYPE,
     DOMAIN,
 )
@@ -112,31 +108,22 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry):
     _LOGGER.debug(f"Setting up entry for device: {entry.data[CONF_DEVICE_ID]}")
     config = {**entry.data, **entry.options, "name": entry.title}
     setup_device(hass, config)
+    device_conf = get_config(config[CONF_TYPE])
+    if device_conf is None:
+        _LOGGER.error(f"COnfiguration file for {config[CONF_TYPE]} not found.")
+        return False
+
+    entities = {}
+    e = device_conf.primary_entity
+    if config.get(e.config_id, False):
+        entities[e.entity] = True
+    for e in device_conf.secondary_entities():
+        if config.get(e.config_id, False):
+            entities[e.entity] = True
+
+    for e in entities:
+        hass.async_create_task(hass.config_entries.async_forward_entry_setup(entry, e))
 
-    if config.get(CONF_CLIMATE, False) is True:
-        hass.async_create_task(
-            hass.config_entries.async_forward_entry_setup(entry, "climate")
-        )
-    if config.get(CONF_LIGHT, False) is True:
-        hass.async_create_task(
-            hass.config_entries.async_forward_entry_setup(entry, "light")
-        )
-    if config.get(CONF_LOCK, False) is True:
-        hass.async_create_task(
-            hass.config_entries.async_forward_entry_setup(entry, "lock")
-        )
-    if config.get(CONF_SWITCH, False) is True:
-        hass.async_create_task(
-            hass.config_entries.async_forward_entry_setup(entry, "switch")
-        )
-    if config.get(CONF_HUMIDIFIER, False) is True:
-        hass.async_create_task(
-            hass.config_entries.async_forward_entry_setup(entry, "humidifier")
-        )
-    if config.get(CONF_FAN, False) is True:
-        hass.async_create_task(
-            hass.config_entries.async_forward_entry_setup(entry, "fan")
-        )
     entry.add_update_listener(async_update_entry)
 
     return True
@@ -146,19 +133,21 @@ async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry):
     _LOGGER.debug(f"Unloading entry for device: {entry.data[CONF_DEVICE_ID]}")
     config = entry.data
     data = hass.data[DOMAIN][config[CONF_DEVICE_ID]]
-
-    if CONF_CLIMATE in data:
-        await hass.config_entries.async_forward_entry_unload(entry, "climate")
-    if CONF_LIGHT in data:
-        await hass.config_entries.async_forward_entry_unload(entry, "light")
-    if CONF_LOCK in data:
-        await hass.config_entries.async_forward_entry_unload(entry, "lock")
-    if CONF_SWITCH in data:
-        await hass.config_entries.async_forward_entry_unload(entry, "switch")
-    if CONF_HUMIDIFIER in data:
-        await hass.config_entries.async_forward_entry_unload(entry, "humidifier")
-    if CONF_FAN in data:
-        await hass.config_entries.async_forward_entry_unload(entry, "fan")
+    device_conf = get_config(config[CONF_TYPE])
+    if device_conf is None:
+        _LOGGER.error(f"Configuration file for {config[CONF_TYPE]} not found.")
+        return False
+
+    entities = {}
+    e = device_conf.primary_entity
+    if e.config_id in data:
+        entities[e.entity] = True
+    for e in device_conf.secondary_entities():
+        if e.config_id in data:
+            entities[e.entity] = True
+
+    for e in entities:
+        await hass.config_entries.async_forward_entry_unload(entry, e)
 
     delete_device(hass, config)
     del hass.data[DOMAIN][config[CONF_DEVICE_ID]]

+ 5 - 0
custom_components/tuya_local/helpers/device_config.py

@@ -173,6 +173,11 @@ class TuyaEntityConfig:
         """The entity type of this entity."""
         return self._config["entity"]
 
+    @property
+    def config_id(self):
+        """The identifier for this entitiy in the config."""
+        return self.entity
+
     @property
     def device_class(self):
         """The device class of this entity."""

+ 1 - 1
custom_components/tuya_local/manifest.json

@@ -2,7 +2,7 @@
     "domain": "tuya_local",
     "iot_class": "local_polling",
     "name": "Tuya Local",
-    "version": "0.8.9",
+    "version": "0.9.0",
     "documentation": "https://github.com/make-all/tuya-local",
     "issue_tracker": "https://github.com/make-all/tuya-local/issues",
     "dependencies": [],

+ 1 - 1
tests/test_config_flow.py

@@ -470,7 +470,7 @@ async def test_options_flow_fails_when_config_is_missing(mock_test, hass):
     )
     config_entry.add_to_hass(hass)
 
-    assert await hass.config_entries.async_setup(config_entry.entry_id)
+    await hass.config_entries.async_setup(config_entry.entry_id)
     await hass.async_block_till_done()
     # show initial form
     result = await hass.config_entries.options.async_init(config_entry.entry_id)