[OBSOLETE] Kodi/XBMC Callback Endpoint for Light Control

Hi Everyone,

I am very new to the Smartthings community. One of my first objectives with my new toys was to integrate light controls to my Kodi media centers. Thanks to the community I got up and running, but none of the solutions I could find were very full-featured or easy to set up. I decided to code myself a better SmartApp for my own use and figured I would share it with all of you.

This app is designed to control lighting based on four main events in Kodi: play, pause, resume, and stop. Currently it allows you to select which lights you would like to control and set a lighting (dim) level for each event. It also supports restoring the lights to their last known level set outside of this app.

In order to accomplish this the app exposes URL endpoints including a security token for each event type and provides them to the user in a easy to copy/paste format (no more messing with REST endpoint examples and php files). Those endpoints need to be called by Kodi when the events occur. For this we use the kodi.callbacks plugin .

A few things have changed since initial release. This app is now actually two apps, a parent and a child app. The “main” app provides URLs and receives callbacks. The child app represents a grouping of lights. This allows you to have multiple groups of lights set at different levels from one event in Kodi. KenV99 has also created a new Kodi callbacks plugin that adds some features and works better in the newer builds of Kodi. He has been very active in replying to any issues on the plugin’s forum thread and I recommend everyone switch from the old xbmc callbacks2 plugin to this new one.

Install should go like this:

  1. Install both SmartApp’s code under My SmartApps (New SmartApp>From Code)
    2.For the main application (not required for the light group application): App Settings>oAuth, make sure oAuth is enabled and you see client secret and such. You don’t need these values, but must make sure oAuth is enabled for your app.
  2. For the main application (not required for the light group application): Publish>For Me
  3. On smartthings mobile app go to marketplace>SmartApps>My Apps
  4. Add Kodi Callback Endpoint
  5. Add groups of desired switches to control along with desired light levels for each event type (value of 101 will restore to last known level)
  6. Optionally name this instance of the app, and modes to operate in (for example don’t run in “night” mode)
  7. Scroll down and click the View URLs link
  8. On following page the app should display 6 URLs, one for each main event type, plus 2 custom. Copy these down and click done.
  9. Install the Kodi Callbacks plugin for Kodi
  10. Open the settings for the callbacks plugin
  11. On the Tasks page create a http task for each URL you wish to use using the URLs you copied above
  12. On the events tab create events for play/stop/pause/resume and assign them to the appropriate task you just created.
  13. Test!

This is a first version and I had to use some trickery to restore light values as there does not seem to be an officially documented way to do that normally. Because of this expect some bugs. I am happy to help answer questions as I am able. You may also want to check out some of the previous tutorials on setting up Kodi integration to get an idea of the process this was based on.

That said, here is the code:

(Make sure you install BOTH files)

Please make sure to read the Kodi Wiki for the callbacks addon, it is quite useful for detailed setup instructions. If you are like me and want to fiddle a bit more I pointed my callbacks tasks to python files and passed the %mt (mediatype argument). This way events only trigger for movie or TV playback (avoiding light changes when playing music files). This is the code I used in the python files:

import urllib2
def run(arg):
    if arg[0] == 'movie' or arg[0] == 'episode':
        urllib2.urlopen('Http://URLHERE').read()

@toliver182 is also working on a Kodi device type that uses some of this code and will allow control of Kodi from SmartThings. If you need bi-directional control check it out: [OBSOLETE] Kodi Manager - playing status and control - #32 by toliver182

Please let me know if you have issues or would like a feature added.

-Thildemar

11 Likes

Added some timestamp based code for detecting last light levels as my previous code based on event ID was not working for some devices. Doubt it is 100% still, but much more accurate now.

@thildemar this looks promising. I plan on giving it a try at some point tomorrow. Will let you know the results.

I am not seeing the 4 URLs when I click next on step 7 (going to step 8) of the Install. Not sure what I am missing here.

You should see this:

What are you seeing? And on what device?

On iOS and seeing below.

Everything else seemed fine just did not show the url page

In the SmartApp webpage go into App Settings>oAuth

Does it show some URLS or does it have a button to enable? If it has the button try enabling and then try setup on the phone again…

1 Like

That was it! All seems to work fine with a couple quick tests. I do have one enhancement request. Could you add the ability to only fire these commands if in a specific mode? For example during the day it won’t turn on/off lights but during evening mode it will always dim lights. Thanks for all the work here. Glad to be a tester :smile:

Go grab the latest file, it was actually in there already, but commented out. Mode selection does not play nice with simulator for some reason =)

You are right on that even when I set a specific mode for the commands to only run in that does not work. That is a real bummer we definitely need to figure out a way around that (got to get wife approval :stuck_out_tongue_winking_eye:). If I come up with something I will be sure to post. Thanks again for the work you put in here.

…I think I will try to figure something out with a virtual switch when I get some time later today. Will keep you posted.

So I went ahead and created a virtual dimmer and use that virtual dimmer in the Kodi Callback Endpoint Smartapp. I then used rule machine to create three rules (play TV, pause TV, and stop TV) which check the virtual switch I created for specific dim levels (the same levels I set in the Kodi Smartapp). In rule machine when those specific conditions are true ST dims or turns off the physical switch in my living room. This logic adds maybe an extra second before the action I want to occur happens, but allows me to have my lights dim/turn off ONLY during the times I specify. This solution is exactly what I was looking for!!!

Unfortunately, ST crashes when I attempt to add this SmartApp and save the settings on it.
I’m not sure how to enable advanced logging in Android to find out why.

Sorry for the slow replies this weekend. Have been stuck in the garage working on a construction project. I should have some time to fiddle today and see if I can add modes more reliably.

@Synthesis - I’ll see what I can find out. I have mostly iOS devices, but I should have an android tablet somewhere around I can test with. Let me know if you uncover anything else.

Ok, new version is up with mode support and a few other fixes. It would seem that the documented code for automatic mode support is not very good. I added my own checks that seem to be working to support “only work in these modes” and “never work in these modes”. Should support most scenarios without needing to use virtual switches etc.

@Synthesis - Try the new version. I am wondering if the mode support code I had in before may have caused some issues. I will also try and dig up an android device and test myself later this week.

Would the plugin for kodi work with Amazon Fire tv box?

Yup, thats what I use. Just download plugin as a zip and copy to fireTV with ADB Fire

EDIT: I suppose I should clarify. This will work for KODI running on FireTV, but not really the FireTV itself. It could in theory work for the built in media on FireTV, but you would need to find an android app that worked with FireTV to call out to a web address on media events (play, pause, stop, resume). Personally I don’t know of any app that does this, but will poke around a bit.

FYI, tried on my Android and it also crashed when clicking [Next]. Did work from my iPad. Once added, you can view those links fine on Android, you just can’t create them(?).

Sorry for the delayed response.

It crashed when attempting to add it on Android.
Going back into SmartThings and SmartApps shows that it is listed, but as @wipeout944 stated, I can’t actually do anything.
Opening the app, I get prompted to select the switch/light to control, and set options.
I tap Next and it crashes the entire ST app.

Hmm, odd. Honestly I have not yet had a change to install on Android yet (busy moving into new house =/). Good news ifs I did see an Android tablet unpacking the other day, so I will try and find a few min to grab it and install/test. In the mean time if you have access to any IOS devices you should be ok to setup from there, or even using the simulator. Don’t have to do much of anything with the app once setup =)

Right; iDevice worked for me. Not the first time I’ve had to do that with SmartThings…