From 61f78acf2dde2386ed2fbf0fab3cb6eef9387853 Mon Sep 17 00:00:00 2001 From: Steven B <51370195+sdb9696@users.noreply.github.com> Date: Fri, 18 Oct 2024 11:50:15 +0100 Subject: [PATCH] Add light command to cli --- ring_doorbell/cli.py | 43 ++++++++++++++++++++++++++++++++++++ ring_doorbell/stickup_cam.py | 11 +++++++++ 2 files changed, 54 insertions(+) diff --git a/ring_doorbell/cli.py b/ring_doorbell/cli.py index 4144eca..9a50f47 100644 --- a/ring_doorbell/cli.py +++ b/ring_doorbell/cli.py @@ -26,7 +26,9 @@ RingDoorBell, RingEvent, RingGeneric, + RingStickUpCam, RingOther, + RingCapability, ) from ring_doorbell.const import ( CLI_TOKEN_FILE, @@ -384,6 +386,47 @@ async def motion_detection(ctx, ring: Ring, device_name, turn_on, turn_off): return None +@cli.command() +@pass_ring +@click.pass_context +@click.argument("enable", type=click.BOOL, default=None, required=False) +@click.option( + "--device-name", + "-dn", + required=True, + default=None, + help="Name of the ring device", +) +async def light(ctx, ring: Ring, device_name, enable): + """Get and change the light state of a device.""" + device = ring.get_device_by_name(device_name) + + if not device: + echo( + f"No device with name {device_name} found." + + " List of found device names (kind) is:" + ) + return await ctx.invoke(list_command) + if not device.has_capability(RingCapability.LIGHT): + echo(f"{device!s} does not have a light") + return None + device = cast(RingStickUpCam, device) + state = "on" if device.light else "off" + if enable is None: + echo(f"{device!s} light is {state}") + return None + is_on = device.light + if (enable and is_on) or (not enable and not is_on): + echo(f"{device!s} light is already {state}") + return None + + await device.async_set_light(enable) + await ring.async_update_devices() + state = "on" if device.light else "off" + echo(f"{device!s} light set to {state}") + return None + + @cli.command() @click.option( "--device-name", diff --git a/ring_doorbell/stickup_cam.py b/ring_doorbell/stickup_cam.py index 0db5dae..2f43d98 100644 --- a/ring_doorbell/stickup_cam.py +++ b/ring_doorbell/stickup_cam.py @@ -151,6 +151,17 @@ async def async_set_lights(self, state: str) -> None: url = LIGHTS_ENDPOINT.format(self.device_api_id, state) await self._ring.async_query(url, method="PUT") + @property + def light(self) -> bool: + """Return lights status.""" + return self._attrs.get("led_status", "") == "on" + + async def async_set_light(self, value: bool) -> None: # noqa: FBT001 + """Control the lights.""" + state = "on" if value else "off" + url = LIGHTS_ENDPOINT.format(self.device_api_id, state) + await self._ring.async_query(url, method="PUT") + @property def siren(self) -> int: """Return siren status."""