Updated to support lights with dimmable and colour_temp settings
This commit is contained in:
@@ -3,7 +3,7 @@ Simple platform to control LOCALLY Tuya switch devices.
|
|||||||
|
|
||||||
Sample config yaml
|
Sample config yaml
|
||||||
|
|
||||||
switch:
|
light:
|
||||||
- platform: localtuya
|
- platform: localtuya
|
||||||
host: 192.168.0.1
|
host: 192.168.0.1
|
||||||
local_key: 1234567891234567
|
local_key: 1234567891234567
|
||||||
@@ -25,13 +25,13 @@ from homeassistant.components.light import (
|
|||||||
SUPPORT_BRIGHTNESS,
|
SUPPORT_BRIGHTNESS,
|
||||||
SUPPORT_COLOR,
|
SUPPORT_COLOR,
|
||||||
SUPPORT_COLOR_TEMP,
|
SUPPORT_COLOR_TEMP,
|
||||||
Light,
|
LightEntity,
|
||||||
PLATFORM_SCHEMA
|
PLATFORM_SCHEMA
|
||||||
)
|
)
|
||||||
from homeassistant.util import color as colorutil
|
from homeassistant.util import color as colorutil
|
||||||
import socket
|
import socket
|
||||||
|
|
||||||
REQUIREMENTS = ['pytuya==7.0.4']
|
REQUIREMENTS = ['pytuya==7.0.8']
|
||||||
|
|
||||||
CONF_DEVICE_ID = 'device_id'
|
CONF_DEVICE_ID = 'device_id'
|
||||||
CONF_LOCAL_KEY = 'local_key'
|
CONF_LOCAL_KEY = 'local_key'
|
||||||
@@ -49,6 +49,7 @@ PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
|
|||||||
vol.Required(CONF_DEVICE_ID): cv.string,
|
vol.Required(CONF_DEVICE_ID): cv.string,
|
||||||
vol.Required(CONF_LOCAL_KEY): cv.string,
|
vol.Required(CONF_LOCAL_KEY): cv.string,
|
||||||
vol.Required(CONF_NAME): 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.Required(CONF_PROTOCOL_VERSION, default=DEFAULT_PROTOCOL_VERSION): vol.Coerce(float),
|
||||||
vol.Optional(CONF_ID, default=DEFAULT_ID): cv.string,
|
vol.Optional(CONF_ID, default=DEFAULT_ID): cv.string,
|
||||||
})
|
})
|
||||||
@@ -69,6 +70,7 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
|
|||||||
TuyaDevice(
|
TuyaDevice(
|
||||||
bulb_device,
|
bulb_device,
|
||||||
config.get(CONF_NAME),
|
config.get(CONF_NAME),
|
||||||
|
config.get(CONF_FRIENDLY_NAME),
|
||||||
config.get(CONF_ICON),
|
config.get(CONF_ICON),
|
||||||
config.get(CONF_ID)
|
config.get(CONF_ID)
|
||||||
)
|
)
|
||||||
@@ -140,6 +142,8 @@ class TuyaCache:
|
|||||||
return self._device.brightness()
|
return self._device.brightness()
|
||||||
except ConnectionError:
|
except ConnectionError:
|
||||||
pass
|
pass
|
||||||
|
except KeyError:
|
||||||
|
return "999"
|
||||||
except socket.timeout:
|
except socket.timeout:
|
||||||
pass
|
pass
|
||||||
log.warn(
|
log.warn(
|
||||||
@@ -151,6 +155,8 @@ class TuyaCache:
|
|||||||
return self._device.colourtemp()
|
return self._device.colourtemp()
|
||||||
except ConnectionError:
|
except ConnectionError:
|
||||||
pass
|
pass
|
||||||
|
except KeyError:
|
||||||
|
return "999"
|
||||||
except socket.timeout:
|
except socket.timeout:
|
||||||
pass
|
pass
|
||||||
log.warn(
|
log.warn(
|
||||||
@@ -162,6 +168,8 @@ class TuyaCache:
|
|||||||
return self._device.set_brightness(brightness)
|
return self._device.set_brightness(brightness)
|
||||||
except ConnectionError:
|
except ConnectionError:
|
||||||
pass
|
pass
|
||||||
|
except KeyError:
|
||||||
|
pass
|
||||||
except socket.timeout:
|
except socket.timeout:
|
||||||
pass
|
pass
|
||||||
log.warn(
|
log.warn(
|
||||||
@@ -173,6 +181,8 @@ class TuyaCache:
|
|||||||
return self._device.set_colourtemp(color_temp)
|
return self._device.set_colourtemp(color_temp)
|
||||||
except ConnectionError:
|
except ConnectionError:
|
||||||
pass
|
pass
|
||||||
|
except KeyError:
|
||||||
|
pass
|
||||||
except socket.timeout:
|
except socket.timeout:
|
||||||
pass
|
pass
|
||||||
log.warn(
|
log.warn(
|
||||||
@@ -187,13 +197,14 @@ class TuyaCache:
|
|||||||
def turn_off(self):
|
def turn_off(self):
|
||||||
self._device.turn_off();
|
self._device.turn_off();
|
||||||
|
|
||||||
class TuyaDevice(Light):
|
class TuyaDevice(LightEntity):
|
||||||
"""Representation of a Tuya switch."""
|
"""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."""
|
"""Initialize the Tuya switch."""
|
||||||
self._device = device
|
self._device = device
|
||||||
self._name = name
|
self.entity_id = ENTITY_ID_FORMAT.format(name)
|
||||||
|
self._name = friendly_name
|
||||||
self._state = False
|
self._state = False
|
||||||
self._brightness = 127
|
self._brightness = 127
|
||||||
self._color_temp = 127
|
self._color_temp = 127
|
||||||
@@ -282,7 +293,7 @@ class TuyaDevice(Light):
|
|||||||
def supported_features(self):
|
def supported_features(self):
|
||||||
"""Flag supported features."""
|
"""Flag supported features."""
|
||||||
supports = SUPPORT_BRIGHTNESS
|
supports = SUPPORT_BRIGHTNESS
|
||||||
#if self._device.support_color():
|
if self._device.color_temp() != "999":
|
||||||
# supports = supports | SUPPORT_COLOR
|
supports = supports | SUPPORT_COLOR
|
||||||
supports = supports | SUPPORT_COLOR_TEMP
|
#supports = supports | SUPPORT_COLOR_TEMP
|
||||||
return supports
|
return supports
|
||||||
|
@@ -4,5 +4,5 @@
|
|||||||
"documentation": "https://github.com/rospogrigio/localtuya-homeassistant/",
|
"documentation": "https://github.com/rospogrigio/localtuya-homeassistant/",
|
||||||
"dependencies": [],
|
"dependencies": [],
|
||||||
"codeowners": ["@rospogrigio"],
|
"codeowners": ["@rospogrigio"],
|
||||||
"requirements": []
|
"requirements": ["pycryptodome==3.9.8"]
|
||||||
}
|
}
|
@@ -6,7 +6,7 @@
|
|||||||
# This would not exist without the protocol reverse engineering from
|
# This would not exist without the protocol reverse engineering from
|
||||||
# https://github.com/codetheweb/tuyapi by codetheweb and blackrozes
|
# 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
|
import base64
|
||||||
|
Reference in New Issue
Block a user