Skip to content
This repository has been archived by the owner on Oct 7, 2021. It is now read-only.

add support device offline #2

Merged
merged 1 commit into from
Jun 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 15 additions & 6 deletions custom_components/tuya_custom/tuyaha/devices/cover.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,27 @@ class TuyaCover(TuyaDevice):

def open_cover(self):
"""Open the cover."""
self.api.device_control(self.obj_id, "turnOnOff", {"value": "1"})
self._update_data("state", True)
success, response = self.api.device_control(self.obj_id, "turnOnOff", {"value": "1"})
if success:
self._update_data("state", True)
else:
self._update_data("online", False)

def close_cover(self):
"""Close cover."""
self.api.device_control(self.obj_id, "turnOnOff", {"value": "0"})
self._update_data("state", False)
success, response = self.api.device_control(self.obj_id, "turnOnOff", {"value": "0"})
if success:
self._update_data("state", False)
else:
self._update_data("online", False)

def stop_cover(self):
"""Stop the cover."""
self.api.device_control(self.obj_id, "startStop", {"value": "0"})
self._update_data("state", False)
success, response = self.api.device_control(self.obj_id, "startStop", {"value": "0"})
if success:
self._update_data("state", False)
else:
self._update_data("online", False)

def support_stop(self):
support = self.data.get("support_stop")
Expand Down
37 changes: 26 additions & 11 deletions custom_components/tuya_custom/tuyaha/devices/light.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,18 +54,27 @@ def max_color_temp(self):
return 1000

def turn_on(self):
self.api.device_control(self.obj_id, "turnOnOff", {"value": "1"})
self._update_data("state", "true")
success, response = self.api.device_control(self.obj_id, "turnOnOff", {"value": "1"})
if success:
self._update_data("state", "true")
else:
self._update_data("online", False)

def turn_off(self):
self.api.device_control(self.obj_id, "turnOnOff", {"value": "0"})
self._update_data("state", "false")
success, response = self.api.device_control(self.obj_id, "turnOnOff", {"value": "0"})
if success:
self._update_data("state", "false")
else:
self._update_data("online", False)

def set_brightness(self, brightness):
"""Set the brightness(0-255) of light."""
value = int(brightness * 100 / 255)
self.api.device_control(self.obj_id, "brightnessSet", {"value": value})
self._update_data("brightness", value)
success, response = self.api.device_control(self.obj_id, "brightnessSet", {"value": value})
if success:
self._update_data("brightness", brightness)
else:
self._update_data("online", False)

def set_color(self, color):
"""Set the color of light."""
Expand All @@ -79,15 +88,21 @@ def set_color(self, color):
# color white
if hsv_color["saturation"] == 0:
hsv_color["hue"] = 0
self.api.device_control(self.obj_id, "colorSet", {"color": hsv_color})
self._update_data("color", hsv_color)
self._update_data("color_mode", "white" if hsv_color["saturation"] == 0 else "colour")
success, response = self.api.device_control(self.obj_id, "colorSet", {"color": hsv_color})
if success:
self._update_data("color", hsv_color)
self._update_data("color_mode", "white" if hsv_color["saturation"] == 0 else "colour")
else:
self._update_data("online", False)

def set_color_temp(self, color_temp):
self.api.device_control(
success, response = self.api.device_control(
self.obj_id, "colorTemperatureSet", {"value": color_temp}
)
self._update_data("color_temp", color_temp)
if success:
self._update_data("color_temp", color_temp)
else:
self._update_data("online", False)

def update(self):
return self._update(use_discovery=True)
14 changes: 10 additions & 4 deletions custom_components/tuya_custom/tuyaha/devices/switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,18 @@
class TuyaSwitch(TuyaDevice):

def turn_on(self):
self.api.device_control(self.obj_id, "turnOnOff", {"value": "1"})
self._update_data("state", True)
success, response = self.api.device_control(self.obj_id, "turnOnOff", {"value": "1"})
if success:
self._update_data("state", True)
else:
self._update_data("online", False)

def turn_off(self):
self.api.device_control(self.obj_id, "turnOnOff", {"value": "0"})
self._update_data("state", False)
success, response = self.api.device_control(self.obj_id, "turnOnOff", {"value": "0"})
if success:
self._update_data("state", False)
else:
self._update_data("online", False)

def update(self):
return self._update(use_discovery=True)