Skip to content

Commit

Permalink
Some refactoring and fault tolerance if a philips light ball is used.
Browse files Browse the repository at this point in the history
  • Loading branch information
syssi committed Aug 29, 2017
1 parent 27b0004 commit edcd9e4
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 17 deletions.
37 changes: 30 additions & 7 deletions mirobo/ceil.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import logging
from .device import Device
from typing import Any, Dict

_LOGGER = logging.getLogger(__name__)

class CeilStatus:
"""Container for status reports from Xiaomi Philips LED Ceiling Lamp"""

Expand All @@ -20,19 +23,19 @@ def is_on(self) -> bool:
return self.power == "on"

@property
def bright(self) -> int:
def brightness(self) -> int:
return self.data["bright"]

@property
def snm(self) -> int:
def scene(self) -> int:
return self.data["snm"]

@property
def dv(self) -> int:
return self.data["dv"]

@property
def cct(self) -> int:
def color_temperature(self) -> int:
return self.data["cct"]

@property
Expand All @@ -44,9 +47,11 @@ def ac(self) -> int:
return self.data["ac"]

def __str__(self) -> str:
s = "<CeilStatus power=%s, bright=%s, cct=%s, snm=%s, dv=%s, " \
s = "<CeilStatus power=%s, brightness=%s, " \
"correlated_color_temperature=%s, scene=%s, dv=%s, " \
"bl=%s, ac=%, >" % \
(self.power, self.bright, self.cct, self.snm, self.dv,
(self.power, self.brightness,
self.color_temperature, self.scene, self.dv,
self.bl, self.ac)
return s

Expand All @@ -65,11 +70,11 @@ def off(self):
"""Power off."""
return self.send("set_power", ["off"])

def set_bright(self, level: int):
def set_brightness(self, level: int):
"""Set brightness level."""
return self.send("set_bright", [level])

def set_cct(self, level: int):
def set_color_temperature(self, level: int):
"""Set Correlated Color Temperature."""
return self.send("set_cct", [level])

Expand Down Expand Up @@ -104,4 +109,22 @@ def status(self) -> CeilStatus:
"get_prop",
properties
)
properties_count = len(properties)
values_count = len(values)


if properties_count != values_count:
if properties_count == values_count+2:
_LOGGER.info("The values of two properties are missing. "
"Assumption: A Xiaomi Philips Light LED Ball is "
"used which doesn't provide the property bl and ac.",
properties_count, values_count)

values.extend((None, None))
else:
_LOGGER.error("Count (%s) of requested properties does not "
"match the count (%s) of received values.",
properties_count, values_count)

print(values)
return CeilStatus(dict(zip(properties, values)))
21 changes: 11 additions & 10 deletions mirobo/ceil_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
pass_dev = click.make_pass_decorator(mirobo.Ceil)


def validate_bright(ctx, param, value):
def validate_percentage(ctx, param, value):
value = int(value)
if value < 1 or value > 100:
raise click.BadParameter('Should be a positive int between 1-100.')
Expand Down Expand Up @@ -97,9 +97,9 @@ def status(dev: mirobo.Ceil):
return # bail out

click.echo(click.style("Power: %s" % res.power, bold=True))
click.echo("Brightness: %s" % res.bright)
click.echo("CCT: %s" % res.cct)
click.echo("Scene Number: %s" % res.snm)
click.echo("Brightness: %s" % res.brightness)
click.echo("Correlated color temperatur: %s" % res.color_temperature)
click.echo("Scene: %s" % res.scene)
click.echo("dv: %s" % res.dv)
click.echo("Smart Midnight Light: %s" % res.bl)
click.echo("Auto CCT: %s" % res.ac)
Expand All @@ -120,19 +120,20 @@ def off(dev: mirobo.Ceil):


@cli.command()
@click.argument('level', callback=validate_bright, required=True,)
@click.argument('level', callback=validate_percentage, required=True,)
@pass_dev
def set_bright(dev: mirobo.Ceil, level):
def set_brightness(dev: mirobo.Ceil, level):
"""Set brightness level."""
click.echo("Brightness: %s" % dev.set_bright(level))
click.echo("Brightness: %s" % dev.set_brightness(level))


@cli.command()
@click.argument('level', callback=validate_bright, required=True,)
@click.argument('level', callback=validate_percentage, required=True,)
@pass_dev
def set_cct(dev: mirobo.Ceil, level):
def set_color_temperature(dev: mirobo.Ceil, level):
"""Set CCT level."""
click.echo("CCT level: %s" % dev.set_cct(level))
click.echo("Correlated color temperatur level: %s" %
dev.set_color_temperature(level))


@cli.command()
Expand Down

0 comments on commit edcd9e4

Please sign in to comment.