Add --transition to bulb-specific cli commands, fix turn_{on,off} signatures (#81)

This commit is contained in:
Teemu R 2020-07-06 16:10:28 +02:00 committed by GitHub
parent 44e2998705
commit 4d722e25c1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 25 additions and 20 deletions

View File

@ -337,8 +337,9 @@ async def emeter(dev: SmartDevice, year, month, erase):
@cli.command() @cli.command()
@click.argument("brightness", type=click.IntRange(0, 100), default=None, required=False) @click.argument("brightness", type=click.IntRange(0, 100), default=None, required=False)
@click.option("--transition", type=int, required=False)
@pass_dev @pass_dev
async def brightness(dev, brightness): async def brightness(dev: SmartBulb, brightness: int, transition: int):
"""Get or set brightness.""" """Get or set brightness."""
await dev.update() await dev.update()
if not dev.is_dimmable: if not dev.is_dimmable:
@ -348,15 +349,16 @@ async def brightness(dev, brightness):
click.echo(f"Brightness: {dev.brightness}") click.echo(f"Brightness: {dev.brightness}")
else: else:
click.echo(f"Setting brightness to {brightness}") click.echo(f"Setting brightness to {brightness}")
click.echo(await dev.set_brightness(brightness)) click.echo(await dev.set_brightness(brightness, transition=transition))
@cli.command() @cli.command()
@click.argument( @click.argument(
"temperature", type=click.IntRange(2500, 9000), default=None, required=False "temperature", type=click.IntRange(2500, 9000), default=None, required=False
) )
@click.option("--transition", type=int, required=False)
@pass_dev @pass_dev
async def temperature(dev: SmartBulb, temperature): async def temperature(dev: SmartBulb, temperature: int, transition: int):
"""Get or set color temperature.""" """Get or set color temperature."""
await dev.update() await dev.update()
if temperature is None: if temperature is None:
@ -371,16 +373,17 @@ async def temperature(dev: SmartBulb, temperature):
) )
else: else:
click.echo(f"Setting color temperature to {temperature}") click.echo(f"Setting color temperature to {temperature}")
asyncio.run(dev.set_color_temp(temperature)) asyncio.run(dev.set_color_temp(temperature, transition=transition))
@cli.command() @cli.command()
@click.argument("h", type=click.IntRange(0, 360), default=None, required=False) @click.argument("h", type=click.IntRange(0, 360), default=None, required=False)
@click.argument("s", type=click.IntRange(0, 100), default=None, required=False) @click.argument("s", type=click.IntRange(0, 100), default=None, required=False)
@click.argument("v", type=click.IntRange(0, 100), default=None, required=False) @click.argument("v", type=click.IntRange(0, 100), default=None, required=False)
@click.option("--transition", type=int, required=False)
@click.pass_context @click.pass_context
@pass_dev @pass_dev
async def hsv(dev, ctx, h, s, v): async def hsv(dev, ctx, h, s, v, transition):
"""Get or set color in HSV. (Bulb only).""" """Get or set color in HSV. (Bulb only)."""
await dev.update() await dev.update()
if h is None or s is None or v is None: if h is None or s is None or v is None:
@ -389,7 +392,7 @@ async def hsv(dev, ctx, h, s, v):
raise click.BadArgumentUsage("Setting a color requires 3 values.", ctx) raise click.BadArgumentUsage("Setting a color requires 3 values.", ctx)
else: else:
click.echo(f"Setting HSV: {h} {s} {v}") click.echo(f"Setting HSV: {h} {s} {v}")
click.echo(await dev.set_hsv(h, s, v)) click.echo(await dev.set_hsv(h, s, v, transition=transition))
@cli.command() @cli.command()
@ -415,8 +418,9 @@ async def time(dev):
@cli.command() @cli.command()
@click.option("--index", type=int, required=False) @click.option("--index", type=int, required=False)
@click.option("--name", type=str, required=False) @click.option("--name", type=str, required=False)
@click.option("--transition", type=int, required=False)
@pass_dev @pass_dev
async def on(dev: SmartDevice, index, name): async def on(dev: SmartDevice, index: int, name: str, transition: int):
"""Turn the device on.""" """Turn the device on."""
await dev.update() await dev.update()
if index is not None or name is not None: if index is not None or name is not None:
@ -430,14 +434,15 @@ async def on(dev: SmartDevice, index, name):
dev = dev.get_plug_by_name(name) dev = dev.get_plug_by_name(name)
click.echo(f"Turning on {dev.alias}") click.echo(f"Turning on {dev.alias}")
await dev.turn_on() await dev.turn_on(transition=transition)
@cli.command() @cli.command()
@click.option("--index", type=int, required=False) @click.option("--index", type=int, required=False)
@click.option("--name", type=str, required=False) @click.option("--name", type=str, required=False)
@click.option("--transition", type=int, required=False)
@pass_dev @pass_dev
async def off(dev, index, name): async def off(dev: SmartDevice, index: int, name: str, transition: int):
"""Turn the device off.""" """Turn the device off."""
await dev.update() await dev.update()
if index is not None or name is not None: if index is not None or name is not None:
@ -451,7 +456,7 @@ async def off(dev, index, name):
dev = dev.get_plug_by_name(name) dev = dev.get_plug_by_name(name)
click.echo(f"Turning off {dev.alias}") click.echo(f"Turning off {dev.alias}")
await dev.turn_off() await dev.turn_off(transition=transition)
@cli.command() @cli.command()

View File

@ -337,14 +337,14 @@ class SmartBulb(SmartDevice):
light_state = self.light_state light_state = self.light_state
return bool(light_state["on_off"]) return bool(light_state["on_off"])
async def turn_off(self, *, transition: int = None) -> Dict: async def turn_off(self, *, transition: int = None, **kwargs) -> Dict:
"""Turn the bulb off. """Turn the bulb off.
:param int transition: transition in milliseconds. :param int transition: transition in milliseconds.
""" """
return await self.set_light_state({"on_off": 0}, transition=transition) return await self.set_light_state({"on_off": 0}, transition=transition)
async def turn_on(self, *, transition: int = None) -> Dict: async def turn_on(self, *, transition: int = None, **kwargs) -> Dict:
"""Turn the bulb on. """Turn the bulb on.
:param int transition: transition in milliseconds. :param int transition: transition in milliseconds.

View File

@ -590,7 +590,7 @@ class SmartDevice:
""" """
await self._query_helper("system", "reboot", {"delay": delay}) await self._query_helper("system", "reboot", {"delay": delay})
async def turn_off(self) -> Dict: async def turn_off(self, **kwargs) -> Dict:
"""Turn off the device.""" """Turn off the device."""
raise NotImplementedError("Device subclass needs to implement this.") raise NotImplementedError("Device subclass needs to implement this.")
@ -600,7 +600,7 @@ class SmartDevice:
"""Return True if device is off.""" """Return True if device is off."""
return not self.is_on return not self.is_on
async def turn_on(self) -> Dict: async def turn_on(self, **kwargs) -> Dict:
"""Turn device on.""" """Turn device on."""
raise NotImplementedError("Device subclass needs to implement this.") raise NotImplementedError("Device subclass needs to implement this.")

View File

@ -84,7 +84,7 @@ class SmartDimmer(SmartPlug):
self.DIMMER_SERVICE, "set_brightness", {"brightness": brightness} self.DIMMER_SERVICE, "set_brightness", {"brightness": brightness}
) )
async def turn_off(self, *, transition: int = None): async def turn_off(self, *, transition: int = None, **kwargs):
"""Turn the bulb off. """Turn the bulb off.
:param int transition: transition duration in milliseconds. :param int transition: transition duration in milliseconds.
@ -95,7 +95,7 @@ class SmartDimmer(SmartPlug):
return await super().turn_off() return await super().turn_off()
@requires_update @requires_update
async def turn_on(self, *, transition: int = None): async def turn_on(self, *, transition: int = None, **kwargs):
"""Turn the bulb on. """Turn the bulb on.
:param int transition: transition duration in milliseconds. :param int transition: transition duration in milliseconds.

View File

@ -48,11 +48,11 @@ class SmartPlug(SmartDevice):
sys_info = self.sys_info sys_info = self.sys_info
return bool(sys_info["relay_state"]) return bool(sys_info["relay_state"])
async def turn_on(self): async def turn_on(self, **kwargs):
"""Turn the switch on.""" """Turn the switch on."""
return await self._query_helper("system", "set_relay_state", {"state": 1}) return await self._query_helper("system", "set_relay_state", {"state": 1})
async def turn_off(self): async def turn_off(self, **kwargs):
"""Turn the switch off.""" """Turn the switch off."""
return await self._query_helper("system", "set_relay_state", {"state": 0}) return await self._query_helper("system", "set_relay_state", {"state": 0})

View File

@ -97,12 +97,12 @@ class SmartStrip(SmartDevice):
SmartStripPlug(self.host, parent=self, child_id=child["id"]) SmartStripPlug(self.host, parent=self, child_id=child["id"])
) )
async def turn_on(self): async def turn_on(self, **kwargs):
"""Turn the strip on.""" """Turn the strip on."""
await self._query_helper("system", "set_relay_state", {"state": 1}) await self._query_helper("system", "set_relay_state", {"state": 1})
await self.update() await self.update()
async def turn_off(self): async def turn_off(self, **kwargs):
"""Turn the strip off.""" """Turn the strip off."""
await self._query_helper("system", "set_relay_state", {"state": 0}) await self._query_helper("system", "set_relay_state", {"state": 0})
await self.update() await self.update()