Skip to content

Commit

Permalink
Move timings properly to backend
Browse files Browse the repository at this point in the history
  • Loading branch information
hcoohb committed Oct 3, 2020
1 parent bc2ddc5 commit 584b010
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 10 deletions.
5 changes: 1 addition & 4 deletions custom_components/yeelight_bt/light.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
""" light platform """

import logging
import time

import homeassistant.helpers.config_validation as cv
import voluptuous as vol
Expand Down Expand Up @@ -233,11 +232,9 @@ def turn_on(self, **kwargs):
brightness = self._brightness
brightness_dev = int(round(brightness * 1.0 / 255 * 100))

# ATTR cannot be set while light is off, so turn it on first:
# Add a timer of 1 sec as the light will not register com while turning on
# ATTR cannot be set while light is off, so turn it on first
if not self._is_on:
self._dev.turn_on()
time.sleep(1)
self._is_on = True

if ATTR_HS_COLOR in kwargs:
Expand Down
17 changes: 11 additions & 6 deletions custom_components/yeelight_bt/yeelightbt.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ def __init__(self, mac_address):
self._conn_time = 0
self._conn_max_time = 60 # minutes before reconnection
self._write_time = 0
self._wait_next_cmd = 0
self._paired = True
self.versions = None
self._model = "Unknown"
Expand Down Expand Up @@ -263,7 +264,9 @@ def writeCharacteristic(
_LOGGER.error(msg)
return False

def send_cmd(self, bits, withResponse=True, wait_notif: float = 1):
def send_cmd(
self, bits, withResponse=True, wait_notif: float = 1, wait_before_next_cmd=0.5
):
"""
Send a control command to the lamp, checking for response and waiting for notif.
The lamp takes some time to transition to new state. If another command is
Expand All @@ -274,13 +277,16 @@ def send_cmd(self, bits, withResponse=True, wait_notif: float = 1):

# if last command less than xx, we wait
sec_since_write = time.time() - self._write_time
if sec_since_write < 0.4:
if sec_since_write < self._wait_next_cmd:
_LOGGER.debug("WAITING before next command")
time.sleep(0.4 - sec_since_write) # allow lamp transition to finish
# allow lamp transition to finish:
time.sleep(self._wait_next_cmd - sec_since_write)
ret = self.writeCharacteristic(
self._handle_control, bits, withResponse, wait_notif
)
self._wait_next_cmd = wait_before_next_cmd

# reconnect the lamp every x hrs to maintain connection
if ret and (time.time() - self._conn_time > 60 * self._conn_max_time):
_LOGGER.debug(
f"Connection is {self._conn_max_time}min old - reconnecting..."
Expand Down Expand Up @@ -337,19 +343,18 @@ def turn_on(self):
"""Turn the lamp on. (send back state through notif) """
_LOGGER.debug("Turn_on")
bits = struct.pack("BBB15x", COMMAND_STX, CMD_POWER, CMD_POWER_ON)
return self.send_cmd(bits)
return self.send_cmd(bits, wait_before_next_cmd=1)

def turn_off(self):
"""Turn the lamp off. (send back state through notif) """
_LOGGER.debug("Turn_off")
bits = struct.pack("BBB15x", COMMAND_STX, CMD_POWER, CMD_POWER_OFF)
return self.send_cmd(bits)
return self.send_cmd(bits, wait_before_next_cmd=1)

# set_brightness/temperature/color do NOT send a notification back.
# However, the lamp takes time to transition to new state
# and if another command (including get_state) is sent during that time,
# it stops the transition where it is...
# HA sends get_state straight away, so better put a little timer after sending
def set_brightness(self, brightness: int):
""" Set the brightness [1-100] (no notif)"""
brightness = min(100, max(0, int(brightness)))
Expand Down

0 comments on commit 584b010

Please sign in to comment.