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

improve gateway light class #770

Merged
merged 11 commits into from
Jul 21, 2020
130 changes: 84 additions & 46 deletions miio/gateway.py
Original file line number Diff line number Diff line change
Expand Up @@ -646,71 +646,112 @@ class GatewayLight(GatewayDevice):
"""Light controls for the gateway."""

@command()
def get_night_light_rgb(self):
"""Unknown."""
# Returns 0 when light is off?"""
# looks like this is the same as get_rgb
# id': 65064, 'method': 'set_night_light_rgb', 'params': [419407616]}
# {'method': 'props', 'params':
# {'light': 'on', 'from.light': '4,,,'}, 'id': 88457} ?!
return self._gateway.send("get_night_light_rgb")
def rgb_status(self):
"""Get current status of the light (always correct)."""
# Returns {"is_on": false, "brightness": 0, "rgb": (0, 0, 0)} when light is off
starkillerOG marked this conversation as resolved.
Show resolved Hide resolved
state_int = self._gateway.send("get_rgb").pop()
brightness = int_to_brightness(state_int)
rgb = int_to_rgb(state_int)
if brightness > 0:
is_on = True
else:
is_on = False
starkillerOG marked this conversation as resolved.
Show resolved Hide resolved
return {"is_on": is_on, "brightness": brightness, "rgb": rgb}

@command()
def night_light_status(self):
"""Get status of the night light."""
# The night light controls the same LEDs as the rgb light, but has a seprate memory for brightness and color.
# Changing the rgb light does not effect the stored state of the night light, while changing the night light does effect the state of the rgb light.
# Therefore this command only gives the correct status of the LEDs if the last command was a night light command and not a rgb light command, otherwise it gives the stored values of the night light.
starkillerOG marked this conversation as resolved.
Show resolved Hide resolved
# Returns {"is_on": false, "brightness": 0, "rgb": (0, 0, 0)} when night light is off
state_int = self._gateway.send("get_night_light_rgb").pop()
brightness = int_to_brightness(state_int)
rgb = int_to_rgb(state_int)
if brightness > 0:
is_on = True
else:
is_on = False
return {"is_on": is_on, "brightness": brightness, "rgb": rgb}
starkillerOG marked this conversation as resolved.
Show resolved Hide resolved

@command(
click.argument("brightness", type=int),
click.argument("rgb", type=(int, int, int)),
)
def set_rgb(self, brightness, rgb):
starkillerOG marked this conversation as resolved.
Show resolved Hide resolved
"""Set gateway light using brightness and rgb tuple."""
brightness_and_color = brightness_and_color_to_int(brightness, rgb)
return self._gateway.send("set_rgb", [brightness_and_color])

@command(
click.argument("brightness", type=int),
click.argument("rgb", type=(int, int, int)),
starkillerOG marked this conversation as resolved.
Show resolved Hide resolved
)
def set_night_light(self, brightness, rgb):
"""Set gateway night light using brightness and rgb tuple."""
brightness_and_color = brightness_and_color_to_int(brightness, rgb)
return self._gateway.send("set_night_light_rgb", [brightness_and_color])

@command(click.argument("brightness", type=int))
def set_rgb_brightness(self, brightness):
"""Set gateway light brightness (0-100)."""
if 100 < brightness < 0:
raise Exception("Brightness must be between 0 and 100")
current_color = self.rgb_status()["rgb"]
return self.set_rgb(brightness, current_color)

@command(click.argument("brightness", type=int))
def set_night_light_brightness(self, brightness):
"""Set night light brightness (0-100)."""
if 100 < brightness < 0:
raise Exception("Brightness must be between 0 and 100")
current_color = self.night_light_status()["rgb"]
return self.set_night_light(brightness, current_color)

@command(click.argument("color_name", type=str))
def set_night_light_color(self, color_name):
"""Set night light color using color name (red, green, etc)."""
def set_rgb_color(self, color_name):
starkillerOG marked this conversation as resolved.
Show resolved Hide resolved
"""Set gateway light color using color name (red, green, etc)."""
starkillerOG marked this conversation as resolved.
Show resolved Hide resolved
if color_name not in color_map.keys():
raise Exception(
"Cannot find {color} in {colors}".format(
color=color_name, colors=color_map.keys()
)
)
current_brightness = int_to_brightness(
self._gateway.send("get_night_light_rgb")[0]
)
brightness_and_color = brightness_and_color_to_int(
current_brightness, color_map[color_name]
)
return self._gateway.send("set_night_light_rgb", [brightness_and_color])
current_brightness = self.rgb_status()["brightness"]
return self.set_rgb(current_brightness, color_map[color_name])

@command(click.argument("color_name", type=str))
def set_color(self, color_name):
"""Set gateway lamp color using color name (red, green, etc)."""
def set_night_light_color(self, color_name):
"""Set night light color using color name (red, green, etc)."""
starkillerOG marked this conversation as resolved.
Show resolved Hide resolved
if color_name not in color_map.keys():
raise Exception(
"Cannot find {color} in {colors}".format(
color=color_name, colors=color_map.keys()
)
)
current_brightness = int_to_brightness(self._gateway.send("get_rgb")[0])
brightness_and_color = brightness_and_color_to_int(
current_brightness, color_map[color_name]
)
return self._gateway.send("set_rgb", [brightness_and_color])
current_brightness = self.night_light_status()["brightness"]
return self.set_night_light(current_brightness, color_map[color_name])

@command(click.argument("brightness", type=int))
def set_brightness(self, brightness):
"""Set gateway lamp brightness (0-100)."""
if 100 < brightness < 0:
raise Exception("Brightness must be between 0 and 100")
current_color = int_to_rgb(self._gateway.send("get_rgb")[0])
brightness_and_color = brightness_and_color_to_int(brightness, current_color)
return self._gateway.send("set_rgb", [brightness_and_color])

@command(click.argument("brightness", type=int))
def set_night_light_brightness(self, brightness):
"""Set night light brightness (0-100)."""
@command(
click.argument("color_name", type=str), click.argument("brightness", type=int),
)
def set_rgb_using_name(self, color_name, brightness):
"""Set gateway light color (using color name) and brightness (0-100)."""
if 100 < brightness < 0:
raise Exception("Brightness must be between 0 and 100")
current_color = int_to_rgb(self._gateway.send("get_night_light_rgb")[0])
brightness_and_color = brightness_and_color_to_int(brightness, current_color)
print(brightness, current_color)
return self._gateway.send("set_night_light_rgb", [brightness_and_color])
if color_name not in color_map.keys():
raise Exception(
"Cannot find {color} in {colors}".format(
color=color_name, colors=color_map.keys()
)
)
return self.set_rgb(brightness, color_map[color_name])

@command(
click.argument("color_name", type=str), click.argument("brightness", type=int),
)
def set_light(self, color_name, brightness):
"""Set color (using color name) and brightness (0-100)."""
def set_night_light_using_name(self, color_name, brightness):
"""Set night light color (using color name) and brightness (0-100)."""
if 100 < brightness < 0:
raise Exception("Brightness must be between 0 and 100")
if color_name not in color_map.keys():
Expand All @@ -719,10 +760,7 @@ def set_light(self, color_name, brightness):
color=color_name, colors=color_map.keys()
)
)
brightness_and_color = brightness_and_color_to_int(
brightness, color_map[color_name]
)
return self._gateway.send("set_rgb", [brightness_and_color])
return self.set_night_light(brightness, color_map[color_name])


class SubDevice:
Expand Down