-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #28 from ross-p-smith/ToggleForNonSwitch
Toggle for non switch
- Loading branch information
Showing
4 changed files
with
93 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters