From f72740d89aec215d2ed043b004035d35b67af1db Mon Sep 17 00:00:00 2001 From: Correl Roush Date: Tue, 12 Nov 2024 15:35:42 -0500 Subject: [PATCH] Remove switch toggle option Toggling works for turning the switch off, but doesn't seem to work for turning it back on. --- ipowerswitch.py | 26 ++++++++------------------ 1 file changed, 8 insertions(+), 18 deletions(-) diff --git a/ipowerswitch.py b/ipowerswitch.py index 2e3b8e0..4d45876 100644 --- a/ipowerswitch.py +++ b/ipowerswitch.py @@ -10,8 +10,8 @@ import yarl class SwitchStatus(str, enum.Enum): - ON = "ON" - OFF = "OFF" + on = "on" + off = "off" @dataclasses.dataclass @@ -38,7 +38,7 @@ class Switch: f"SW{outlet}.y": 1, } if from_state: - params[f"STATUS{outlet}"] = f"{from_state.value:3s}" + params[f"STATUS{outlet}"] = f"{from_state.value.upper():3s}" logging.info("Switching outlet: %s", params) response = requests.post( @@ -50,10 +50,10 @@ class Switch: print(response) def switch_on(self, switch_id: int, outlet: int) -> None: - return self.switch(switch_id, outlet, from_state=SwitchStatus.OFF) + return self.switch(switch_id, outlet, from_state=SwitchStatus.off) def switch_off(self, switch_id: int, outlet: int) -> None: - return self.switch(switch_id, outlet, from_state=SwitchStatus.ON) + return self.switch(switch_id, outlet, from_state=SwitchStatus.on) app = typer.Typer() @@ -62,29 +62,19 @@ SwitchIdArgument = typing.Annotated[int, typer.Argument(min=1, max=16)] OutletArgument = typing.Annotated[int, typer.Argument(min=1, max=8)] -class SwitchAction(str, enum.Enum): - on = "on" - off = "off" - toggle = "toggle" - - @app.command() def switch( context: typer.Context, switch_id: SwitchIdArgument, outlet: OutletArgument, - action: typing.Annotated[ - SwitchAction, typer.Argument(case_sensitive=False) - ] = SwitchAction.toggle, + action: typing.Annotated[SwitchStatus, typer.Argument(case_sensitive=False)], ) -> None: switch: Switch = context.obj match action: - case SwitchAction.on: + case SwitchStatus.on: switch.switch_on(switch_id, outlet) - case SwitchAction.off: + case SwitchStatus.off: switch.switch_off(switch_id, outlet) - case SwitchAction.toggle: - switch.switch(switch_id, outlet) @app.callback()