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
150 changes: 103 additions & 47 deletions miio/gateway.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import logging
from datetime import datetime
from enum import Enum, IntEnum
from typing import Optional
from typing import Optional, Tuple

import attr
import click
Expand Down Expand Up @@ -643,74 +643,132 @@ def set_default_music(self):


class GatewayLight(GatewayDevice):
"""Light controls for the gateway."""
"""
Light controls for the gateway.

The gateway LEDs can be controlled using 'rgb' or 'night_light' methods.
The 'night_light' methods control the same light as the 'rgb' methods, but has a separate memory for brightness and color.
Changing the 'rgb' light does not affect the stored state of the 'night_light', while changing the 'night_light' does effect the state of the 'rgb' light.
"""

@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 represents the current status of the light as opposed to 'night_light_status'.

Example:
{"is_on": false, "brightness": 0, "rgb": (0, 0, 0)}
"""
# 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)
is_on = brightness > 0

return {"is_on": is_on, "brightness": brightness, "rgb": rgb}

@command()
def night_light_status(self):
"""
Get status of the night light.
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

Example:
{"is_on": false, "brightness": 0, "rgb": (0, 0, 0)}
"""
state_int = self._gateway.send("get_night_light_rgb").pop()
brightness = int_to_brightness(state_int)
rgb = int_to_rgb(state_int)
is_on = brightness > 0

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: int, rgb: Tuple[int, int, int]):
"""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: int, rgb: Tuple[int, int, int]):
"""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: int):
"""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: int):
"""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: str):
"""Set gateway light color using color name ('color_map' variable in the source holds the valid values)."""
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: str):
"""Set night light color using color name ('color_map' variable in the source holds the valid values)."""
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"]

@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])
return self.set_night_light(current_brightness, color_map[color_name])

@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: str, brightness: int):
"""Set gateway light color (using color name, 'color_map' variable in the source holds the valid values) 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: str, brightness: int):
"""Set night light color (using color name, 'color_map' variable in the source holds the valid values) 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 +777,8 @@ 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