Controlling your Samsung TV via SmartThings

The purpose of this article is to present an example of integration of a Samsung TV with the Grenton Smart Home System using Gate Http and SmartThings.


Example banner

 

The device used in this example is a Samsung Q70 series TV.

This integration allows us, among others, to

  • Turning the device on and off
  • Volume control
  • Source selection
  • Launch selected applications

 

To implement the example below we need:

  • Gate HTTP
  • SmartThings compatible Samsung TV

 

Preparation

1. Creating a token

Create a token using the link. Don't forget to save it because it will only be displayed once.

2. Adding your device to SmartThings

Add your device to SmartThings, then make sure it's ONLINE:

3. Copy the DeviceID of the integrated device:

4. Checking communication operation:

Click on the device name. In the opened page, we can check the available commands and test them.

Preparing the configuration

Creating user features:

1. Double-click the Gate_HTTP object to enter the module settings:

2. Go to the "User Features" tab and create the necessary features:

  • DeviceID - Initial value: {DeviceID} Type: string
  • Token - Initial value: {Token} Type: string
  • TVOnOffStatus - Initial value: 0 Type: number
  • TVAudioMuteStatus - Initial value: 0 Type: number
  • TVAudioVolumeStatus - Initial value: 0 Type: number
  • TVSourceStatus - Initial value: 0 Type: string

Paste the DeviceID of the integrated device into the initial value of the DeviceID feature. Paste Token into the initial value of the Token feature.

 

 

Creating objects

1. Create an Http Request object named HttpRequestTVCommand and configure it as follows:

Host: https://api.smartthings.com

Path: /v1/devices/{DeviceId}/commands -  instead of {DeviceId}, enter the DeviceId of the integrated device

2. Create an HttpRequest object called HttpRequestStatusRefresh and configure it as follows:

Host: https://api.smartthings.com

Path: /v1/devices/{DeviceId}/commands -  instead of {DeviceId}, enter the DeviceId of the integrated device

3. Create an HttpRequest object called HtttpRequestStatus and configure it as follows:

Host: https://api.smartthings.com

Path: /v1/devices/{DeviceId}/status -  instead of {DeviceId}, enter the DeviceId of the integrated device

4. Create a Timer object called RefreshTimer  and configure it as follows:

Creating scripts:

1. Create a script called sHttpRequestTVCommandRequest  with two parameters:

Paste the following code into the script:

local reqbody = {}
reqbody.commands = {}
reqbody.commands[1] = {}
reqbody.commands[1]["component"] = "main"

if param == "on" or param == "off" then 
reqbody.commands[1]["capability"] = "switch"
reqbody.commands[1]["command"] = param
end

if param == "volumeUp" or param == "volumeDown" then
reqbody.commands[1]["capability"] = "audioVolume"
reqbody.commands[1]["command"] = param
end 

if param == "channelUp" or param == "channelDown" then
reqbody.commands[1]["capability"] = "tvChannel"
reqbody.commands[1]["command"] = param
 end

if param == "muteSwitch" then
     if Gate_HTTP->TVAudioMuteStatus == 1 then
     reqbody.commands[1]["capability"] = "audioMute"
    reqbody.commands[1]["command"] = "unmute"
     else
    reqbody.commands[1]["capability"] = "audioMute"
    reqbody.commands[1]["command"] = "mute" 
     end
end

if param == "setVolume" then
reqbody.commands[1]["capability"] = "audioVolume"
reqbody.commands[1]["command"] = param
reqbody.commands[1]["arguments"] = {}
reqbody.commands[1]["arguments"][1] = param2
end 

if param == "HDMI3" or param == "HDMI4" then
reqbody.commands[1]["capability"] = "mediaInputSource"
reqbody.commands[1]["command"] = "setInputSource"
reqbody.commands[1]["arguments"] = {}
reqbody.commands[1]["arguments"][1] = param
end 

if param == "UP" or param == "DOWN" or param == "LEFT" or param == "RIGHT" or param == "HOME" or param == "MENU" or param == "OK"  or param == "BACK" then
reqbody.commands[1]["capability"] = "samsungvd.remoteControl"
reqbody.commands[1]["command"] = "send"
reqbody.commands[1]["arguments"] = {}
reqbody.commands[1]["arguments"][1] = param
end

if param == "pause" or param == "fastForward" or param == "rewind" or param == "play" then
reqbody.commands[1]["capability"] = "mediaPlayback"
reqbody.commands[1]["command"] = param
end

if param == "Netflix" or param == "HBO" or param == "Prime" or param == "YouTube" then
reqbody.commands[1]["capability"] = "custom.launchapp"
reqbody.commands[1]["command"] = "launchApp"
reqbody.commands[1]["arguments"] = {}
    if param == "Netflix" then
    reqbody.commands[1]["arguments"][1] = "3201907018807"
     end
    if param == "HBO" then
    reqbody.commands[1]["arguments"][1] = "3201601007230"
     end
     if param == "Prime" then
    reqbody.commands[1]["arguments"][1] = "3201910019365"
     end
     if param == "YouTube" then
    reqbody.commands[1]["arguments"][1] = "111299001912"
     end
 end

Gate_HTTP->HttpRequestTVCommand->SetRequestBody(reqbody)

local key = "Authorization: Bearer "..Gate_HTTP->Token.."\r\n" 
Gate_HTTP->HttpRequestTVCommand->SetRequestHeaders(key) 

Gate_HTTP->HttpRequestTVCommand->SendRequest()

2. Create a script called sHttpRequestStatus_request:

Script content:

local key = "Authorization: Bearer "..Gate_HTTP->Token.."\r\n" 
Gate_HTTP->HtttpRequestStatus->SetRequestHeaders(key) 
Gate_HTTP->HtttpRequestStatus->SendRequest()

3. Create a script called sHttpRequestStatus response:

local resp = Gate_HTTP->HtttpRequestStatus->ResponseBody

if resp.components.main.switch.switch.value == "on"
then
Gate_HTTP->TVOnOffStatus = 1
else
Gate_HTTP->TVOnOffStatus = 0
end

if resp.components.main.audioMute.mute.value == "muted" then
Gate_HTTP->TVAudioMuteStatus = 1
else
Gate_HTTP->TVAudioMuteStatus = 0
end

Gate_HTTP->TVAudioVolumeStatus = resp.components.main.audioVolume.volume.value
Gate_HTTP->TVSourceStatus = resp.components.main.mediaInputSource.inputSource.value

4.Create a script called sHttpRequestStatusRefresh:

Association assignment:

1. Assign the execution of the sHttpRequestStatusRefresh script to the OnResponse Event of the HttpRequestTVCommand object

2. Assign the execution of the sHttpRequestStatus_response script to the OnResponse Event of the HtttpRequestStatus object

3. Assign the execution of the sHttpRequestStatus_request script to the OnResponse Event of the HttpRequestStatusRefresh object

4. Assign the execution of the sHttpRequestStatusRefresh script to the OnTimer Event of the RefreshTimer object

Send the prepared configuration to the Gate Http module

 

Creating the myGrenton interface

1. Create a HEADER widget and configure it as follows:

2. Create the TV REMOTE CONTROL widget and configure it as follows:

3. Create a HEADER widget and configure it as follows:

4. Create a SLIDER widget and configure it as follows:

5. Create a HEADER widget and configure it as follows:

6. Create the VALUE_V2 widget and configure it as follows:

7. Create the SENE_DOUBLE widget and configure it as follows:

8. Create a HEADER widget and configure it as follows:

9. Create the SENE_DOUBLE widget and configure it as follows:

10. Create the SENE_DOUBLE widget and configure it as follows:

After sending to the phone, the interface looks like this:

After clicking the Pilot widget, the following window will appear:

Below is a video showing how integration works