Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support more recent construct releases #28

Merged
merged 11 commits into from
Feb 26, 2019
11 changes: 10 additions & 1 deletion yeelightbt/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

@click.pass_context
def paired_cb(ctx, data):
data = data.payload
if data.pairing_status == "PairRequest":
click.echo("Waiting for pairing, please push the button/change the brightness")
time.sleep(5)
Expand All @@ -27,6 +28,7 @@ def paired_cb(ctx, data):

@click.pass_context
def notification_cb(ctx, data):
print("Got notif: %s" % data)
if DEBUG:
click.echo("Got notification: %s" % data)

Expand Down Expand Up @@ -115,6 +117,11 @@ def off(dev):
""" Turns the lamp off. """
dev.turn_off()

@cli.command()
@pass_dev
def wait_for_notifications(dev):
"""Wait for notifications."""
dev.wait_for_notifications()

@cli.command()
@click.argument("brightness", type=int, default=None, required=False)
Expand Down Expand Up @@ -186,12 +193,14 @@ def sleep(dev: Lamp, time):
@pass_dev
def state(dev):
""" Requests the state from the device. """
click.echo("MAC: %s" % dev.mac)
click.echo(click.style("MAC: %s" % dev.mac, bold=dev.is_on))
click.echo(" Mode: %s" % dev.mode)
click.echo(" Color: %s" % (dev.color,))
click.echo(" Temperature: %s" % dev.temperature)
click.echo(" Brightness: %s" % dev.brightness)

dev._conn.wait(60)


@cli.command()
@click.argument('temperature', type=int, default=None, required=False)
Expand Down
22 changes: 14 additions & 8 deletions yeelightbt/lamp.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,23 @@ def _wrap(self, *args, **kwargs):
if "wait" in params:
wait = params["wait"]
del params["wait"]
query.update(params)
query["payload"] = params

_LOGGER.debug(">> %s (wait: %s)", query, wait)
_ex = None
try_count = 3
while try_count > 0:
try:
res = self.control_char.write(Request.build(query),
request_bytes = Request.build(query)
res = self.control_char.write(request_bytes,
withResponse=True)
self._conn.wait(wait)

return res
except Exception as ex:
_LOGGER.error("got exception on %s, tries left %s: %s",
query, try_count, ex)
raise
_ex = ex
try_count -= 1
self.connect()
Expand Down Expand Up @@ -102,6 +104,10 @@ def connect(self):
timeout=None)
self.pair()

def wait_for_notifications(self):
while True:
self._conn.wait(1)

def disconnect(self):
self._conn.disconnect()

Expand Down Expand Up @@ -231,13 +237,13 @@ def __str__(self):
def handle_notification(self, data):
_LOGGER.debug("<< %s", codecs.encode(data, 'hex'))
res = Response.parse(data)
print(res)
payload = res.payload
if res.type == "StateResult":
self._is_on = res.state
self._mode = res.mode
self._rgb = (res.red, res.green, res.blue, res.white)
self._brightness = res.brightness
self._temperature = res.temperature
self._is_on = payload.state
self._mode = payload.mode
self._rgb = (payload.red, payload.green, payload.blue, payload.white)
self._brightness = payload.brightness
self._temperature = payload.temperature

if self._status_cb:
self._status_cb(self)
Expand Down
Loading