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

Properly support switches in __init__.

Add logging around auto-detection.
Jason Rumney 5 лет назад
Родитель
Сommit
3c0ce2e2e0

+ 10 - 10
custom_components/tuya_local/__init__.py

@@ -23,12 +23,7 @@ from .const import (
     CONF_DEVICE_ID,
     CONF_DISPLAY_LIGHT,
     CONF_LOCAL_KEY,
-    CONF_TYPE,
-    CONF_TYPE_DEHUMIDIFIER,
-    CONF_TYPE_FAN,
-    CONF_TYPE_GPPH_HEATER,
-    SCAN_INTERVAL,
-    CONF_TYPE_AUTO,
+    CONF_SWITCH,
 )
 from .device import TuyaLocalDevice
 from .config_flow import ConfigFlowHandler
@@ -59,19 +54,22 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry):
     config = {**entry.data, **entry.options, "name": entry.title}
     setup_device(hass, config)
 
-    if config[CONF_CLIMATE] == True:
+    if config[CONF_CLIMATE] is True:
         hass.async_create_task(
             hass.config_entries.async_forward_entry_setup(entry, "climate")
         )
-    if config[CONF_DISPLAY_LIGHT] == True:
+    if config[CONF_DISPLAY_LIGHT] is True:
         hass.async_create_task(
             hass.config_entries.async_forward_entry_setup(entry, "light")
         )
-    if config[CONF_CHILD_LOCK] == True:
+    if config[CONF_CHILD_LOCK] is True:
         hass.async_create_task(
             hass.config_entries.async_forward_entry_setup(entry, "lock")
         )
-
+    if config[CONF_SWITCH] is True:
+        hass.async_create_task(
+            hass.config_entries.async_forward_entry_setup(entry, "switch")
+        )
     entry.add_update_listener(async_update_entry)
 
     return True
@@ -91,6 +89,8 @@ async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry):
         await hass.config_entries.async_forward_entry_unload(entry, "light")
     if CONF_CHILD_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")
 
     delete_device(hass, config)
     del hass.data[DOMAIN][config[CONF_DEVICE_ID]]

+ 2 - 0
custom_components/tuya_local/climate.py

@@ -45,6 +45,8 @@ async def async_setup_platform(hass, config, async_add_entities, discovery_info=
         data[CONF_CLIMATE] = GoldairGPCVHeater(device)
     elif discovery_info[CONF_TYPE] == CONF_TYPE_KOGAN_HEATER:
         data[CONF_CLIMATE] = KoganHeater(device)
+    else:
+        raise ValueError("This device does not support working as a climate device")
 
     if CONF_CLIMATE in data:
         async_add_entities([data[CONF_CLIMATE]])

+ 9 - 1
custom_components/tuya_local/device.py

@@ -5,7 +5,7 @@ API for Tuya Local devices.
 import json
 import logging
 from threading import Lock, Timer
-from time import time, sleep
+from time import time
 
 from homeassistant.const import TEMP_CELSIUS
 from homeassistant.core import HomeAssistant
@@ -89,19 +89,27 @@ class TuyaLocalDevice(object):
 
         _LOGGER.debug(f"Inferring device type from cached state: {cached_state}")
         if "1" not in cached_state:
+            _LOGGER.info(f"Detecting {self.name} as Kogan Heater")
             return CONF_TYPE_KOGAN_HEATER
         if "5" in cached_state and "3" not in cached_state and "103" in cached_state:
+            _LOGGER.info(f"Detecting {self.name} as Goldair Dehumidifier")
             return CONF_TYPE_DEHUMIDIFIER
         if "8" in cached_state:
+            _LOGGER.info(f"Detecting {self.name} as Goldair Fan")
             return CONF_TYPE_FAN
         if "5" in cached_state and "3" not in cached_state:
+            _LOGGER.info(f"Detecting {self.name} as Kogan Switch")
             return CONF_TYPE_KOGAN_SWITCH
         if "106" in cached_state:
+            _LOGGER.info(f"Detecting {self.name} as Goldair GPPH Heater")
             return CONF_TYPE_GPPH_HEATER
         if "7" in cached_state:
+            _LOGGER.info(f"Detecting {self.name} as Goldair GPCV Heater")
             return CONF_TYPE_GPCV_HEATER
         if "3" in cached_state:
+            _LOGGER.info(f"Detecting {self.name} as Goldair GECO Heater")
             return CONF_TYPE_GECO_HEATER
+        _LOGGER.warning(f"Detection for {self.name} failed")
         return None
 
     def set_fixed_properties(self, fixed_properties):

+ 2 - 9
custom_components/tuya_local/light.py

@@ -7,10 +7,7 @@ from .const import (
     CONF_TYPE,
     CONF_TYPE_DEHUMIDIFIER,
     CONF_TYPE_FAN,
-    CONF_TYPE_GECO_HEATER,
-    CONF_TYPE_GPCV_HEATER,
     CONF_TYPE_GPPH_HEATER,
-    CONF_TYPE_KOGAN_HEATER,
     CONF_DISPLAY_LIGHT,
     CONF_TYPE_AUTO,
 )
@@ -36,12 +33,8 @@ async def async_setup_platform(hass, config, async_add_entities, discovery_info=
         data[CONF_DISPLAY_LIGHT] = GoldairDehumidifierLedDisplayLight(device)
     elif discovery_info[CONF_TYPE] == CONF_TYPE_FAN:
         data[CONF_DISPLAY_LIGHT] = GoldairFanLedDisplayLight(device)
-    elif discovery_info[CONF_TYPE] == CONF_TYPE_GPCV_HEATER:
-        raise ValueError("Goldair GPCV Heaters do not support panel lighting control.")
-    elif discovery_info[CONF_TYPE] == CONF_TYPE_GECO_HEATER:
-        raise ValueError("Goldair GECO Heaters do not support panel lighting control.")
-    elif discovery_info[CONF_TYPE] == CONF_TYPE_KOGAN_HEATER:
-        raise ValueError("Kogan heaters do not support panel lighting control")
+    else:
+        raise ValueError("This device does not support panel lighting control.")
 
     if CONF_DISPLAY_LIGHT in data:
         async_add_entities([data[CONF_DISPLAY_LIGHT]])

+ 2 - 3
custom_components/tuya_local/lock.py

@@ -6,7 +6,6 @@ from .const import (
     CONF_DEVICE_ID,
     CONF_TYPE,
     CONF_TYPE_DEHUMIDIFIER,
-    CONF_TYPE_FAN,
     CONF_TYPE_GECO_HEATER,
     CONF_TYPE_GPCV_HEATER,
     CONF_TYPE_GPPH_HEATER,
@@ -46,8 +45,8 @@ async def async_setup_platform(hass, config, async_add_entities, discovery_info=
         data[CONF_CHILD_LOCK] = GoldairGPCVHeaterChildLock(device)
     elif discovery_info[CONF_TYPE] == CONF_TYPE_KOGAN_HEATER:
         data[CONF_CHILD_LOCK] = KoganHeaterChildLock(device)
-    elif discovery_info[CONF_TYPE] == CONF_TYPE_FAN:
-        raise ValueError("Goldair fans do not support child lock.")
+    else:
+        raise ValueError("This device does not support child lock.")
 
     if CONF_CHILD_LOCK in data:
         async_add_entities([data[CONF_CHILD_LOCK]])

+ 2 - 0
custom_components/tuya_local/switch.py

@@ -25,6 +25,8 @@ async def async_setup_platform(hass, config, async_add_entities, discovery_info=
 
     if discovery_info[CONF_TYPE] == CONF_TYPE_KOGAN_SWITCH:
         data[CONF_SWITCH] = KoganSocketSwitch(device)
+    else:
+        raise ValueError("This device does not support working as a switch")
 
     if CONF_SWITCH in data:
         async_add_entities([data[CONF_SWITCH]])