From 0ed842bef0b386787123e2ee52df6355651ac8ac Mon Sep 17 00:00:00 2001 From: ross-p-smith Date: Wed, 16 Jun 2021 22:45:53 +0100 Subject: [PATCH 1/3] Toggle for Window Shades --- SmartThingsTerminal/API/DeviceHelper.cs | 73 ++++++++++++++++++++ SmartThingsTerminal/API/SmartThingsClient.cs | 15 ++++ SmartThingsTerminal/Scenarios/Devices.cs | 22 ++---- 3 files changed, 93 insertions(+), 17 deletions(-) create mode 100644 SmartThingsTerminal/API/DeviceHelper.cs diff --git a/SmartThingsTerminal/API/DeviceHelper.cs b/SmartThingsTerminal/API/DeviceHelper.cs new file mode 100644 index 0000000..625f0bb --- /dev/null +++ b/SmartThingsTerminal/API/DeviceHelper.cs @@ -0,0 +1,73 @@ +using System; +using System.Collections.Generic; +using SmartThingsNet.Model; + +namespace SmartThingsTerminal +{ + public class DeviceHelper + { + private readonly SmartThingsClient smartThingsClient; + private readonly Device device; + + public DeviceHelper(SmartThingsClient smartThingsClient, Device device) + { + this.smartThingsClient = smartThingsClient; + this.device = device; + } + + public bool ToggleDevice() + { + if (!ToggleDeviceSwitch()) + { + return ToggleDeviceWindowShade(); + } + + return true; + } + + private bool ToggleDeviceSwitch() + { + var deviceCurrentStatus = new Dictionary(); + if (this.smartThingsClient.TryGetDeviceCapabilityStatus(this.device.DeviceId, this.device.Components[0].Id, "switch", out deviceCurrentStatus) + && deviceCurrentStatus.Count > 0) + { + string state = deviceCurrentStatus["switch"].Value.ToString().ToLower(); + string newState = state == "on" ? "off" : "on"; + + DeviceCommandsRequest commandsRequest = new DeviceCommandsRequest() { Commands = new List() }; + DeviceCommand command = new DeviceCommand(capability: "switch", command: newState); + commandsRequest.Commands.Add(command); + this.smartThingsClient.ExecuteDevicecommand(this.device.DeviceId, commandsRequest); + //ShowStatusBarMessage($"Switch {newState} at {DateTime.UtcNow.ToLongTimeString()}"); + + return true; + } + else + { + return false; + } + } + + private bool ToggleDeviceWindowShade() + { + var deviceCurrentStatus = new Dictionary(); + if (this.smartThingsClient.TryGetDeviceCapabilityStatus(this.device.DeviceId, this.device.Components[0].Id, "windowShade", out deviceCurrentStatus) + && deviceCurrentStatus.Count > 0) + { + string state = deviceCurrentStatus["windowShade"].Value.ToString().ToLower(); + string newState = state == "open" ? "close" : "open"; + + DeviceCommandsRequest commandsRequest = new DeviceCommandsRequest() { Commands = new List() }; + DeviceCommand command = new DeviceCommand(capability: "windowShade", command: newState); + commandsRequest.Commands.Add(command); + this.smartThingsClient.ExecuteDevicecommand(this.device.DeviceId, commandsRequest); + + return true; + } + else + { + return false; + } + } + } +} \ No newline at end of file diff --git a/SmartThingsTerminal/API/SmartThingsClient.cs b/SmartThingsTerminal/API/SmartThingsClient.cs index d466889..6b43e98 100644 --- a/SmartThingsTerminal/API/SmartThingsClient.cs +++ b/SmartThingsTerminal/API/SmartThingsClient.cs @@ -344,6 +344,21 @@ public object ExecuteDevicecommand(string deviceId, DeviceCommandsRequest comman return _devicesApi.ExecuteDeviceCommands(deviceId, commandRequest); } + public bool TryGetDeviceCapabilityStatus(string deviceId, string componentId, string capabilityId, out Dictionary capabilityStatus) + { + capabilityStatus = new Dictionary(); + + try + { + capabilityStatus = GetDeviceCapabilityStatus(deviceId, componentId, capabilityId); + return true; + } + catch + { + return false; + } + } + public Dictionary GetDeviceCapabilityStatus(string deviceId, string componentId, string capabilityId) { return _devicesApi.GetDeviceStatusByCapability(deviceId, componentId, capabilityId); diff --git a/SmartThingsTerminal/Scenarios/Devices.cs b/SmartThingsTerminal/Scenarios/Devices.cs index 735e54f..88a506a 100644 --- a/SmartThingsTerminal/Scenarios/Devices.cs +++ b/SmartThingsTerminal/Scenarios/Devices.cs @@ -170,7 +170,7 @@ public override void ConfigureStatusBar() { StatusBar = new StatusBar(new StatusItem[] { new StatusItem(Key.F1, "~F1~ Component Status", () => ToggleComponentStatus()), - new StatusItem(Key.F4, "~F4~ Toggle Device Switch", () => ToggleDeviceSwitch()), + new StatusItem(Key.F4, "~F4~ Toggle Device Switch", () => ToggleDevice()), new StatusItem(Key.F5, "~F5~ Refresh Data", () => RefreshScreen()), new StatusItem(Key.F9, "~F9~ Menu", () => { }), new StatusItem(Key.Home, "~Home~ Back", () => Quit()) @@ -223,29 +223,17 @@ private void ConfigureComponentsStatusPane(Device selectedDevice) HostPane.ColorScheme = Colors.TopLevel; } - private void ToggleDeviceSwitch() + private void ToggleDevice() { if (SelectedItem != null) { Device selectedDevice = (Device)SelectedItem; try { - var deviceCurrentStatus = STClient.GetDeviceCapabilityStatus(selectedDevice.DeviceId, selectedDevice.Components[0].Id, "switch"); - - if (deviceCurrentStatus.Count > 0) - { - string state = deviceCurrentStatus["switch"].Value.ToString().ToLower(); - string newState = state == "on" ? "off" : "on"; - - DeviceCommandsRequest commandsRequest = new DeviceCommandsRequest() { Commands = new List() }; - DeviceCommand command = new DeviceCommand(capability: "switch", command: newState); - commandsRequest.Commands.Add(command); - STClient.ExecuteDevicecommand(selectedDevice.DeviceId, commandsRequest); - //ShowStatusBarMessage($"Switch {newState} at {DateTime.UtcNow.ToLongTimeString()}"); - } - else + var deviceHelper = new DeviceHelper(STClient, selectedDevice); + if (!deviceHelper.ToggleDevice()) { - ShowErrorMessage($"{selectedDevice.Name} has no switch capability"); + ShowErrorMessage($"Cannot toggle {selectedDevice.Name}."); } } catch (SmartThingsNet.Client.ApiException exp) From 961ec8cd9a6bd05ad392cc55099c2aee5115ba09 Mon Sep 17 00:00:00 2001 From: ross-p-smith Date: Wed, 16 Jun 2021 23:06:51 +0100 Subject: [PATCH 2/3] Update docs --- README.md | 2 +- SmartThingsTerminal/API/DeviceHelper.cs | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/README.md b/README.md index a2d3d50..9d0cc7d 100644 --- a/README.md +++ b/README.md @@ -87,7 +87,7 @@ You can use an environment variable called `STT_ACCESSTOKEN` and `STT_SCREEN` in * View Devices * View all of the registered devices * View device components status - * Toggle lights/switches on/off + * Toggle lights/switches on/off or Window Shades open/closed * Export device to a json file * Install Applications * View details of installed applications diff --git a/SmartThingsTerminal/API/DeviceHelper.cs b/SmartThingsTerminal/API/DeviceHelper.cs index 625f0bb..787b444 100644 --- a/SmartThingsTerminal/API/DeviceHelper.cs +++ b/SmartThingsTerminal/API/DeviceHelper.cs @@ -38,7 +38,6 @@ private bool ToggleDeviceSwitch() DeviceCommand command = new DeviceCommand(capability: "switch", command: newState); commandsRequest.Commands.Add(command); this.smartThingsClient.ExecuteDevicecommand(this.device.DeviceId, commandsRequest); - //ShowStatusBarMessage($"Switch {newState} at {DateTime.UtcNow.ToLongTimeString()}"); return true; } From 8703a1deb401b4c38d1e4617d8e183b501cdd2b1 Mon Sep 17 00:00:00 2001 From: ross-p-smith Date: Thu, 17 Jun 2021 15:10:51 +0100 Subject: [PATCH 3/3] Use Switch Level rather than Window Shade --- SmartThingsTerminal/API/DeviceHelper.cs | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/SmartThingsTerminal/API/DeviceHelper.cs b/SmartThingsTerminal/API/DeviceHelper.cs index 787b444..012f79b 100644 --- a/SmartThingsTerminal/API/DeviceHelper.cs +++ b/SmartThingsTerminal/API/DeviceHelper.cs @@ -1,4 +1,3 @@ -using System; using System.Collections.Generic; using SmartThingsNet.Model; @@ -17,12 +16,12 @@ public DeviceHelper(SmartThingsClient smartThingsClient, Device device) public bool ToggleDevice() { - if (!ToggleDeviceSwitch()) + if (ToggleDeviceSwitch() || ToggleDeviceSwitchLevel()) { - return ToggleDeviceWindowShade(); + return true; } - return true; + return false; } private bool ToggleDeviceSwitch() @@ -47,17 +46,18 @@ private bool ToggleDeviceSwitch() } } - private bool ToggleDeviceWindowShade() + private bool ToggleDeviceSwitchLevel() { var deviceCurrentStatus = new Dictionary(); - if (this.smartThingsClient.TryGetDeviceCapabilityStatus(this.device.DeviceId, this.device.Components[0].Id, "windowShade", out deviceCurrentStatus) + if (this.smartThingsClient.TryGetDeviceCapabilityStatus(this.device.DeviceId, this.device.Components[0].Id, "switchLevel", out deviceCurrentStatus) && deviceCurrentStatus.Count > 0) { - string state = deviceCurrentStatus["windowShade"].Value.ToString().ToLower(); - string newState = state == "open" ? "close" : "open"; + string currentLevel = deviceCurrentStatus["level"].Value.ToString(); - DeviceCommandsRequest commandsRequest = new DeviceCommandsRequest() { Commands = new List() }; - DeviceCommand command = new DeviceCommand(capability: "windowShade", command: newState); + var commandArgs = new List() { { currentLevel == "0" ? 100 : 0 } }; + var command = new DeviceCommand(capability: "switchLevel", command: "setLevel", arguments: commandArgs); + + var commandsRequest = new DeviceCommandsRequest() { Commands = new List() }; commandsRequest.Commands.Add(command); this.smartThingsClient.ExecuteDevicecommand(this.device.DeviceId, commandsRequest);