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

Rearrange to try to avoid init error.

_CONFIG_DIR being defined at top level of device_config helper seems to be the issue, as that requires the devices directory to be imported.
Jason Rumney 4 лет назад
Родитель
Сommit
869cd8a387

+ 1 - 26
custom_components/tuya_local/__init__.py

@@ -11,9 +11,7 @@ import logging
 import homeassistant.helpers.config_validation as cv
 import homeassistant.helpers.config_validation as cv
 import voluptuous as vol
 import voluptuous as vol
 from homeassistant.config_entries import SOURCE_IMPORT, ConfigEntry
 from homeassistant.config_entries import SOURCE_IMPORT, ConfigEntry
-from homeassistant.const import CONF_HOST, CONF_NAME
 from homeassistant.core import HomeAssistant
 from homeassistant.core import HomeAssistant
-from homeassistant.helpers.discovery import async_load_platform
 
 
 from .configuration import individual_config_schema
 from .configuration import individual_config_schema
 from .const import (
 from .const import (
@@ -21,10 +19,10 @@ from .const import (
     CONF_CLIMATE,
     CONF_CLIMATE,
     CONF_DEVICE_ID,
     CONF_DEVICE_ID,
     CONF_DISPLAY_LIGHT,
     CONF_DISPLAY_LIGHT,
-    CONF_LOCAL_KEY,
     CONF_SWITCH,
     CONF_SWITCH,
     DOMAIN,
     DOMAIN,
 )
 )
+from .device import setup_device, delete_device
 
 
 _LOGGER = logging.getLogger(__name__)
 _LOGGER = logging.getLogger(__name__)
 
 
@@ -101,26 +99,3 @@ async def async_update_entry(hass: HomeAssistant, entry: ConfigEntry):
     _LOGGER.debug(f"Updating entry for device: {entry.data[CONF_DEVICE_ID]}")
     _LOGGER.debug(f"Updating entry for device: {entry.data[CONF_DEVICE_ID]}")
     await async_unload_entry(hass, entry)
     await async_unload_entry(hass, entry)
     await async_setup_entry(hass, entry)
     await async_setup_entry(hass, entry)
-
-
-def setup_device(hass: HomeAssistant, config: dict):
-    """Setup a tuya device based on passed in config."""
-    from .device import TuyaLocalDevice
-
-    _LOGGER.debug(f"Creating device: {config[CONF_DEVICE_ID]}")
-    hass.data[DOMAIN] = hass.data.get(DOMAIN, {})
-    device = TuyaLocalDevice(
-        config[CONF_NAME],
-        config[CONF_DEVICE_ID],
-        config[CONF_HOST],
-        config[CONF_LOCAL_KEY],
-        hass,
-    )
-    hass.data[DOMAIN][config[CONF_DEVICE_ID]] = {"device": device}
-
-    return device
-
-
-def delete_device(hass: HomeAssistant, config: dict):
-    _LOGGER.debug(f"Deleting device: {config[CONF_DEVICE_ID]}")
-    del hass.data[DOMAIN][config[CONF_DEVICE_ID]]["device"]

+ 27 - 3
custom_components/tuya_local/device.py

@@ -9,13 +9,17 @@ from threading import Lock, Timer
 from time import time
 from time import time
 
 
 
 
-from homeassistant.const import TEMP_CELSIUS
+from homeassistant.const import CONF_HOST, CONF_NAME, TEMP_CELSIUS
 from homeassistant.core import HomeAssistant
 from homeassistant.core import HomeAssistant
 
 
 from .const import (
 from .const import (
     API_PROTOCOL_VERSIONS,
     API_PROTOCOL_VERSIONS,
+    CONF_DEVICE_ID,
+    CONF_LOCAL_KEY,
     DOMAIN,
     DOMAIN,
 )
 )
+from .helpers.device_config import possible_matches
+
 
 
 _LOGGER = logging.getLogger(__name__)
 _LOGGER = logging.getLogger(__name__)
 
 
@@ -78,8 +82,6 @@ class TuyaLocalDevice(object):
 
 
     async def async_inferred_type(self):
     async def async_inferred_type(self):
 
 
-        from .helpers.device_config import possible_matches
-
         cached_state = self._get_cached_state()
         cached_state = self._get_cached_state()
         if "1" not in cached_state and "3" not in cached_state:
         if "1" not in cached_state and "3" not in cached_state:
             await self.async_refresh()
             await self.async_refresh()
@@ -246,3 +248,25 @@ class TuyaLocalDevice(object):
         keys = list(obj.keys())
         keys = list(obj.keys())
         values = list(obj.values())
         values = list(obj.values())
         return keys[values.index(value)] if value in values else fallback
         return keys[values.index(value)] if value in values else fallback
+
+
+def setup_device(hass: HomeAssistant, config: dict):
+    """Setup a tuya device based on passed in config."""
+
+    _LOGGER.debug(f"Creating device: {config[CONF_DEVICE_ID]}")
+    hass.data[DOMAIN] = hass.data.get(DOMAIN, {})
+    device = TuyaLocalDevice(
+        config[CONF_NAME],
+        config[CONF_DEVICE_ID],
+        config[CONF_HOST],
+        config[CONF_LOCAL_KEY],
+        hass,
+    )
+    hass.data[DOMAIN][config[CONF_DEVICE_ID]] = {"device": device}
+
+    return device
+
+
+def delete_device(hass: HomeAssistant, config: dict):
+    _LOGGER.debug(f"Deleting device: {config[CONF_DEVICE_ID]}")
+    del hass.data[DOMAIN][config[CONF_DEVICE_ID]]["device"]

+ 4 - 2
custom_components/tuya_local/helpers/device_config.py

@@ -8,9 +8,8 @@ from os.path import join, dirname
 from fnmatch import fnmatch
 from fnmatch import fnmatch
 from homeassistant.util.yaml import load_yaml
 from homeassistant.util.yaml import load_yaml
 
 
-import custom_components.tuya_local.devices
+import custom_components.tuya_local.devices as config_dir
 
 
-_CONFIG_DIR = dirname(custom_components.tuya_local.devices.__file__)
 _LOGGER = logging.getLogger("tuya_local")
 _LOGGER = logging.getLogger("tuya_local")
 
 
 
 
@@ -39,6 +38,7 @@ class TuyaDeviceConfig:
         """Initialize the device config.
         """Initialize the device config.
         Args:
         Args:
             fname (string): The filename of the yaml config to load."""
             fname (string): The filename of the yaml config to load."""
+        _CONFIG_DIR = dirname(config_dir.__file__)
         self.__fname = fname
         self.__fname = fname
         filename = join(_CONFIG_DIR, fname)
         filename = join(_CONFIG_DIR, fname)
         self.__config = load_yaml(filename)
         self.__config = load_yaml(filename)
@@ -192,6 +192,8 @@ class TuyaDpsConfig:
 
 
 def available_configs():
 def available_configs():
     """List the available config files."""
     """List the available config files."""
+    _CONFIG_DIR = dirname(config_dir.__file__)
+
     for (path, dirs, files) in walk(_CONFIG_DIR):
     for (path, dirs, files) in walk(_CONFIG_DIR):
         for basename in sorted(files):
         for basename in sorted(files):
             if fnmatch(basename, "*.yaml"):
             if fnmatch(basename, "*.yaml"):