From c1f5ead61df1d4272b8550fe1bfade392e747e44 Mon Sep 17 00:00:00 2001 From: huangyupeng Date: Tue, 24 Jul 2018 16:29:43 +0800 Subject: [PATCH] Add Tuya cover and scene platform (#15587) * Add Tuya cover platform * Add Tuya cover and scene * fix description * remove scene default method --- homeassistant/components/cover/tuya.py | 60 ++++++++++++++++++++++++++ homeassistant/components/scene/tuya.py | 40 +++++++++++++++++ homeassistant/components/tuya.py | 2 + 3 files changed, 102 insertions(+) create mode 100644 homeassistant/components/cover/tuya.py create mode 100644 homeassistant/components/scene/tuya.py diff --git a/homeassistant/components/cover/tuya.py b/homeassistant/components/cover/tuya.py new file mode 100644 index 00000000000..7b5fefee58a --- /dev/null +++ b/homeassistant/components/cover/tuya.py @@ -0,0 +1,60 @@ +""" +Support for Tuya cover. + +For more details about this platform, please refer to the documentation at +https://home-assistant.io/components/cover.tuya/ +""" +from homeassistant.components.cover import ( + CoverDevice, ENTITY_ID_FORMAT, SUPPORT_OPEN, SUPPORT_CLOSE, SUPPORT_STOP) +from homeassistant.components.tuya import DATA_TUYA, TuyaDevice + +DEPENDENCIES = ['tuya'] + + +def setup_platform(hass, config, add_devices, discovery_info=None): + """Set up Tuya cover devices.""" + if discovery_info is None: + return + tuya = hass.data[DATA_TUYA] + dev_ids = discovery_info.get('dev_ids') + devices = [] + for dev_id in dev_ids: + device = tuya.get_device_by_id(dev_id) + if device is None: + continue + devices.append(TuyaCover(device)) + add_devices(devices) + + +class TuyaCover(TuyaDevice, CoverDevice): + """Tuya cover devices.""" + + def __init__(self, tuya): + """Init tuya cover device.""" + super().__init__(tuya) + self.entity_id = ENTITY_ID_FORMAT.format(tuya.object_id()) + + @property + def supported_features(self): + """Flag supported features.""" + supported_features = SUPPORT_OPEN | SUPPORT_CLOSE + if self.tuya.support_stop(): + supported_features |= SUPPORT_STOP + return supported_features + + @property + def is_closed(self): + """Return if the cover is closed or not.""" + return None + + def open_cover(self, **kwargs): + """Open the cover.""" + self.tuya.open_cover() + + def close_cover(self, **kwargs): + """Close cover.""" + self.tuya.close_cover() + + def stop_cover(self, **kwargs): + """Stop the cover.""" + self.tuya.stop_cover() diff --git a/homeassistant/components/scene/tuya.py b/homeassistant/components/scene/tuya.py new file mode 100644 index 00000000000..3990a7da206 --- /dev/null +++ b/homeassistant/components/scene/tuya.py @@ -0,0 +1,40 @@ +""" +Support for the Tuya scene. + +For more details about this platform, please refer to the documentation at +https://home-assistant.io/components/scene.tuya/ +""" +from homeassistant.components.scene import Scene, DOMAIN +from homeassistant.components.tuya import DATA_TUYA, TuyaDevice + +DEPENDENCIES = ['tuya'] + +ENTITY_ID_FORMAT = DOMAIN + '.{}' + + +def setup_platform(hass, config, add_devices, discovery_info=None): + """Set up Tuya scenes.""" + if discovery_info is None: + return + tuya = hass.data[DATA_TUYA] + dev_ids = discovery_info.get('dev_ids') + devices = [] + for dev_id in dev_ids: + device = tuya.get_device_by_id(dev_id) + if device is None: + continue + devices.append(TuyaScene(device)) + add_devices(devices) + + +class TuyaScene(TuyaDevice, Scene): + """Tuya Scene.""" + + def __init__(self, tuya): + """Init Tuya scene.""" + super().__init__(tuya) + self.entity_id = ENTITY_ID_FORMAT.format(tuya.object_id()) + + def activate(self): + """Activate the scene.""" + self.tuya.activate() diff --git a/homeassistant/components/tuya.py b/homeassistant/components/tuya.py index f55fe7a03b3..490c11baad7 100644 --- a/homeassistant/components/tuya.py +++ b/homeassistant/components/tuya.py @@ -34,8 +34,10 @@ SERVICE_PULL_DEVICES = 'pull_devices' TUYA_TYPE_TO_HA = { 'climate': 'climate', + 'cover': 'cover', 'fan': 'fan', 'light': 'light', + 'scene': 'scene', 'switch': 'switch', }