diff --git a/custom_components/localtuya/light.py b/custom_components/localtuya/light.py index a9dd6b6..d1a58ed 100644 --- a/custom_components/localtuya/light.py +++ b/custom_components/localtuya/light.py @@ -3,7 +3,7 @@ Simple platform to control LOCALLY Tuya switch devices. Sample config yaml -switch: +light: - platform: localtuya host: 192.168.0.1 local_key: 1234567891234567 @@ -25,13 +25,13 @@ from homeassistant.components.light import ( SUPPORT_BRIGHTNESS, SUPPORT_COLOR, SUPPORT_COLOR_TEMP, - Light, + LightEntity, PLATFORM_SCHEMA ) from homeassistant.util import color as colorutil import socket -REQUIREMENTS = ['pytuya==7.0.4'] +REQUIREMENTS = ['pytuya==7.0.8'] CONF_DEVICE_ID = 'device_id' CONF_LOCAL_KEY = 'local_key' @@ -49,6 +49,7 @@ PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({ vol.Required(CONF_DEVICE_ID): cv.string, vol.Required(CONF_LOCAL_KEY): cv.string, vol.Required(CONF_NAME): cv.string, + vol.Required(CONF_FRIENDLY_NAME): cv.string, vol.Required(CONF_PROTOCOL_VERSION, default=DEFAULT_PROTOCOL_VERSION): vol.Coerce(float), vol.Optional(CONF_ID, default=DEFAULT_ID): cv.string, }) @@ -69,6 +70,7 @@ def setup_platform(hass, config, add_devices, discovery_info=None): TuyaDevice( bulb_device, config.get(CONF_NAME), + config.get(CONF_FRIENDLY_NAME), config.get(CONF_ICON), config.get(CONF_ID) ) @@ -140,6 +142,8 @@ class TuyaCache: return self._device.brightness() except ConnectionError: pass + except KeyError: + return "999" except socket.timeout: pass log.warn( @@ -151,6 +155,8 @@ class TuyaCache: return self._device.colourtemp() except ConnectionError: pass + except KeyError: + return "999" except socket.timeout: pass log.warn( @@ -162,6 +168,8 @@ class TuyaCache: return self._device.set_brightness(brightness) except ConnectionError: pass + except KeyError: + pass except socket.timeout: pass log.warn( @@ -173,6 +181,8 @@ class TuyaCache: return self._device.set_colourtemp(color_temp) except ConnectionError: pass + except KeyError: + pass except socket.timeout: pass log.warn( @@ -187,13 +197,14 @@ class TuyaCache: def turn_off(self): self._device.turn_off(); -class TuyaDevice(Light): +class TuyaDevice(LightEntity): """Representation of a Tuya switch.""" - def __init__(self, device, name, icon, bulbid): + def __init__(self, device, name, friendly_name, icon, bulbid): """Initialize the Tuya switch.""" self._device = device - self._name = name + self.entity_id = ENTITY_ID_FORMAT.format(name) + self._name = friendly_name self._state = False self._brightness = 127 self._color_temp = 127 @@ -282,7 +293,7 @@ class TuyaDevice(Light): def supported_features(self): """Flag supported features.""" supports = SUPPORT_BRIGHTNESS - #if self._device.support_color(): - # supports = supports | SUPPORT_COLOR - supports = supports | SUPPORT_COLOR_TEMP + if self._device.color_temp() != "999": + supports = supports | SUPPORT_COLOR + #supports = supports | SUPPORT_COLOR_TEMP return supports diff --git a/custom_components/localtuya/manifest.json b/custom_components/localtuya/manifest.json index 5ff4d51..9b7297e 100644 --- a/custom_components/localtuya/manifest.json +++ b/custom_components/localtuya/manifest.json @@ -4,5 +4,5 @@ "documentation": "https://github.com/rospogrigio/localtuya-homeassistant/", "dependencies": [], "codeowners": ["@rospogrigio"], - "requirements": [] + "requirements": ["pycryptodome==3.9.8"] } \ No newline at end of file diff --git a/custom_components/localtuya/pytuya/__init__.py b/custom_components/localtuya/pytuya/__init__.py index 7caad0a..0dd085f 100644 --- a/custom_components/localtuya/pytuya/__init__.py +++ b/custom_components/localtuya/pytuya/__init__.py @@ -6,7 +6,7 @@ # This would not exist without the protocol reverse engineering from # https://github.com/codetheweb/tuyapi by codetheweb and blackrozes # -# Tested with Python 2.7 and Python 3.6.1 only +# Tested with Python 2.7 and Python 3.6.1 only () import base64