Skip to content

Commit

Permalink
Merge pull request #28 from ross-p-smith/ToggleForNonSwitch
Browse files Browse the repository at this point in the history
Toggle for non switch
  • Loading branch information
daltskin authored Feb 1, 2022
2 parents f5ec561 + 8703a1d commit 4b09333
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 18 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,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
Expand Down
72 changes: 72 additions & 0 deletions SmartThingsTerminal/API/DeviceHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
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() || ToggleDeviceSwitchLevel())
{
return true;
}

return false;
}

private bool ToggleDeviceSwitch()
{
var deviceCurrentStatus = new Dictionary<string, AttributeState>();
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>() };
DeviceCommand command = new DeviceCommand(capability: "switch", command: newState);
commandsRequest.Commands.Add(command);
this.smartThingsClient.ExecuteDevicecommand(this.device.DeviceId, commandsRequest);

return true;
}
else
{
return false;
}
}

private bool ToggleDeviceSwitchLevel()
{
var deviceCurrentStatus = new Dictionary<string, AttributeState>();
if (this.smartThingsClient.TryGetDeviceCapabilityStatus(this.device.DeviceId, this.device.Components[0].Id, "switchLevel", out deviceCurrentStatus)
&& deviceCurrentStatus.Count > 0)
{
string currentLevel = deviceCurrentStatus["level"].Value.ToString();

var commandArgs = new List<object>() { { currentLevel == "0" ? 100 : 0 } };
var command = new DeviceCommand(capability: "switchLevel", command: "setLevel", arguments: commandArgs);

var commandsRequest = new DeviceCommandsRequest() { Commands = new List<DeviceCommand>() };
commandsRequest.Commands.Add(command);
this.smartThingsClient.ExecuteDevicecommand(this.device.DeviceId, commandsRequest);

return true;
}
else
{
return false;
}
}
}
}
15 changes: 15 additions & 0 deletions SmartThingsTerminal/API/SmartThingsClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,21 @@ public object ExecuteDevicecommand(string deviceId, DeviceCommandsRequest comman
return _devicesApi.ExecuteDeviceCommands(_accessToken, deviceId, commandRequest);
}

public bool TryGetDeviceCapabilityStatus(string deviceId, string componentId, string capabilityId, out Dictionary<string, AttributeState> capabilityStatus)
{
capabilityStatus = new Dictionary<string, AttributeState>();

try
{
capabilityStatus = GetDeviceCapabilityStatus(deviceId, componentId, capabilityId);
return true;
}
catch
{
return false;
}
}

public Dictionary<string, AttributeState> GetDeviceCapabilityStatus(string deviceId, string componentId, string capabilityId)
{
return _devicesApi.GetDeviceStatusByCapability(_accessToken, deviceId, componentId, capabilityId);
Expand Down
22 changes: 5 additions & 17 deletions SmartThingsTerminal/Scenarios/Devices.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down Expand Up @@ -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>() };
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)
Expand Down

0 comments on commit 4b09333

Please sign in to comment.