Release LIFX for v2 API Plugin - Home Theater - NATEKODILIFX
#16
OK try downloading it again.. I have made it so it should install those components when you try and do the installation.. I never did it because mine already had it installed from other plugins.
Reply
#17
I do have both of those:

Code:
cb01:~/.kodi/addons # grep version= script.module.requests/addon.xml
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
       version="2.6.0"
    <import addon="xbmc.python" version="2.14.0"/>
cb01:~/.kodi/addons # grep version= script.module.simplejson/addon.xml
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
       version="3.3.0"
    <import addon="xbmc.python" version="2.1.0"/>

Later today I'll try adding script.module.requests as a dependency in the LIFX plugin.
Reply
#18
(2015-04-03, 02:32)averylinden Wrote: Later today I'll try adding script.module.requests as a dependency in the LIFX plugin.

I've added that dependency back into the asdon.xml file so hopefully it should work now.. give it a go and let me know how it flows.
Reply
#19
Got past loading with dependencies, thanks. Hitting another error. I'm a python n00b so I'll have to hit the documentation. I see an error on movie start and another on movie stop. I use my api key with some other home automation stuff that I'm writing so I know those credentials are working.

Code:
20:20:52 T:139855829886720   ERROR: /storage/.kodi/addons/script.module.requests/lib/requests/packages/urllib3/util/ssl_.py:79: InsecurePlatformWarning: A true SSLContext object is not available. This prevents urllib3 from configuring SSL appropriately and may cause certain SSL connections to fail. For more information, see https://urllib3.readthedocs.org/en/latest/security.html#insecureplatformwarning.
                                              InsecurePlatformWarning
20:20:53 T:139855829886720   ERROR: EXCEPTION Thrown (PythonToCppException) : -->Python callback/script returned the following error<--
                                             - NOTE: IGNORING THIS CAN LEAD TO MEMORY LEAKS!
                                            Error Type: <type 'exceptions.TypeError'>
                                            Error Contents: string indices must be integers
                                            Traceback (most recent call last):
                                              File "/storage/.kodi/addons/service.NateKodi.LIFX/service.py", line 96, in onPlayBackStarted
                                                preVideolightState = getLightState(type=bulbs['type'], name=bulbs['set'])
                                              File "/storage/.kodi/addons/service.NateKodi.LIFX/service.py", line 36, in getLightState
                                                info.append({'id': light['id'], 'label': light['label'], 'power': light['power'], 'hue': light['color']['hue'], 'saturation': light['color']['saturation'],
                                            TypeError: string indices must be integers
                                            -->End of Python script error report<--
...normal playback messages deleted...
20:21:39 T:139855829886720   ERROR: EXCEPTION Thrown (PythonToCppException) : -->Python callback/script returned the following error<--
                                             - NOTE: IGNORING THIS CAN LEAD TO MEMORY LEAKS!
                                            Error Type: <type 'exceptions.TypeError'>
                                            Error Contents: 'bool' object is not iterable
                                            Traceback (most recent call last):
                                              File "/storage/.kodi/addons/service.NateKodi.LIFX/service.py", line 120, in onPlayBackStopped
                                                restoreLights(preVideolightState, duration=config.get('Delay', 'EndPlay'))
                                              File "/storage/.kodi/addons/service.NateKodi.LIFX/service.py", line 44, in restoreLights
                                                for light in LightStates:
                                            TypeError: 'bool' object is not iterable
                                            -->End of Python script error report<--

My config file looks like:

Code:
[Authentication]
apiKey = ?????????????????????????
cloudAccount = ??????????????????????
password = ???????????

[Bulbs]
type = label
set = tvroom

[Delay]
MovieStart = 10
TVStart = 10
Pause = 2
UnPause = 2
EndPlay = 15
Reply
#20
cloudAccount and password are not currently used...

try running the code in the shell.. you'll need to has out the includes for XBMC and try adding the line print(buldURL) just below loadConfig() show me what you get. as that error doesn't make sense

Also do the same for light['id'], light['label'], light['power'], light['color']['hue'] and light['color']['saturation'].
Reply
#21
I wasn't seeing anything printed until I added bulbURL to the globals in loadConfig, then I could see the right value outside of the loadConfig subroutine. So I printed setColor instead. I don't think light is defined at that part of the script.

Code:
cb01:~/.kodi/addons/service.NateKodi.LIFX # python service.py
https://api.lifx.co/v1beta1/lights/label%3Atvroom/color.json
Traceback (most recent call last):
  File "service.py", line 86, in <module>
    print(light['id'])
NameError: name 'light' is not defined
Reply
#22
Code:
def getLightState(name, type='label'):
    """ This function checks what lights are in the group and what color they are currently set to """
    if name == 'all':
        getAvaliableLights = requests.get(lifxCloud + '/' + name, headers=header)
    else:
        getAvaliableLights = requests.get(lifxCloud + '/' + urllib.quote(type + ':' + name), headers=header)

    if getAvaliableLights.status_code == requests.codes.ok:
        avaliableLights = json.loads(getAvaliableLights.text)
        info = []
        for light in avaliableLights:
            print (light['id'])
            print (light['label'])
            print (light['power'])
            print (light['color']['hue'])
            print (light['color']['saturation'])
            print (light['brightness'])        
            #info.append({'id': light['id'], 'label': light['label'], 'power': light['power'], 'hue': light['color']['hue'], 'saturation': light['color']['saturation'], 'brightness': light['brightness']})
        return info
    return False

Try changing this function to put the print's in it instead
Reply
#23
I can't print the values of light. I'll keep trying a few more things. This version of openelec has python 2.7.3. Not sure if that matters.

Code:
06:31:41 T:140626874050304  NOTICE: https://api.lifx.co/v1beta1/lights/label%3Atvroom/color.json
...video playback messages...
06:32:25 T:140626874050304   ERROR: /storage/.kodi/addons/script.module.requests/lib/requests/packages/urllib3/util/ssl_.py:79: InsecurePlatformWarning: A true SSLContext object is not available. This prevents urllib3 from configuring SSL appropriately and may cause certain SSL connections to fail. For more information, see https://urllib3.readthedocs.org/en/latest/security.html#insecureplatformwarning.
                                              InsecurePlatformWarning
06:32:26 T:140626874050304   ERROR: EXCEPTION Thrown (PythonToCppException) : -->Python callback/script returned the following error<--
                                             - NOTE: IGNORING THIS CAN LEAD TO MEMORY LEAKS!
                                            Error Type: <type 'exceptions.TypeError'>
                                            Error Contents: string indices must be integers
                                            Traceback (most recent call last):
                                              File "/storage/.kodi/addons/service.NateKodi.LIFX/service.py", line 103, in onPlayBackStarted
                                                preVideolightState = getLightState(type=bulbs['type'], name=bulbs['set'])
                                              File "/storage/.kodi/addons/service.NateKodi.LIFX/service.py", line 36, in getLightState
                                                print (light['id'])
                                            TypeError: string indices must be integers
                                            -->End of Python script error report<--
Reply
#24
Python 2.7 is the corret version.

to me the data you are getting back from the lifx cloud must be wrong or corrupt in some shape.. try printing out the json request you get back:
Code:
print(avaliableLights)
Put that just before info=[]
Reply
#25
Here are those results:

Code:
06:57:29 T:140358557062912   ERROR: /storage/.kodi/addons/script.module.requests/lib/requests/packages/urllib3/util/ssl_.py:79: InsecurePlatformWarning: A true SSLContext object is not available. This prevents urllib3 from configuring SSL appropriately and may cause certain SSL connections to fail. For more information, see https://urllib3.readthedocs.org/en/latest/security.html#insecureplatformwarning.
                                              InsecurePlatformWarning
06:57:29 T:140358557062912  NOTICE: {u'group': {u'id': u'ccdfb3c266712ac75867e448f17fc07e', u'name': u'Group1'}, u'uuid': u'02f2a281-b444-4965-bc42-9839782c24d9', u'power': u'on', u'brightness': 0.86904707408255133, u'color': {u'hue': 38.090180819409476, u'saturation': 0.0, u'kelvin': 4000}, u'label': u'tvroom', u'connected': True, u'location': {u'id': u'22d9156dfad1ade398fe5096c7a8c8c0', u'name': u'Home'}, u'seconds_since_seen': 0.0038925909999999999, u'id': u'd073d5006d5c', u'last_seen': u'2015-04-04T13:57:29.755+00:00'}
06:57:29 T:140358557062912   ERROR: EXCEPTION Thrown (PythonToCppException) : -->Python callback/script returned the following error<--
                                             - NOTE: IGNORING THIS CAN LEAD TO MEMORY LEAKS!
                                            Error Type: <type 'exceptions.TypeError'>
                                            Error Contents: string indices must be integers
                                            Traceback (most recent call last):
                                              File "/storage/.kodi/addons/service.NateKodi.LIFX/service.py", line 104, in onPlayBackStarted
                                                preVideolightState = getLightState(type=bulbs['type'], name=bulbs['set'])
                                              File "/storage/.kodi/addons/service.NateKodi.LIFX/service.py", line 37, in getLightState
                                                print (light['id'])
                                            TypeError: string indices must be integers
                                            -->End of Python script error report<--
Reply
#26
Looks like those extra "u"s in the json are the problem, I have no idea why as when I hit my lights with curl requests, the responses look normal.
Reply
#27
Ok, so now I am adding python to my list of languages.

It looks like the problem happens because I use labels for my lights. When getting the list of available lights with a label, you get an extra item, "group"

Code:
{
    "group": {
        "id": "ccdfb3c266712ac75867e448f17fc07e",
        "name": "Group1"
    },
    "uuid": "02f2a281-b444-4965-bc42-9839782c24d9",
    "power": "on",
    "brightness": 0.8690470740825513,
    "color": {
        "hue": 38.090180819409476,
        "saturation": 0.0,
        "kelvin": 4000
    },
    "label": "tvroom",
    "connected": true,
    "location": {
        "id": "22d9156dfad1ade398fe5096c7a8c8c0",
        "name": "Home"
    },
    "seconds_since_seen": 0.003484479,
    "id": "d073d5006d5c",
    "last_seen": "2015-04-04T15:58:39.037+00:00"
}

So when iterating through the list of lights in getLightState, it gets messed up and reads the word 'group' instead of the list of lights. I can probably fix this once I learn a bit more about python. Big Grin
Reply
#28
I have it working for me ... no idea if there's a smarter way to do this but a code diff is below. I can also send it via github if you want.

Code:
diff -u service.py service.alt.py
--- service.py    2015-04-04 10:23:53.378815153 -0700
+++ service.alt.py    2015-04-04 10:22:58.346965267 -0700
@@ -32,9 +32,11 @@
     if getAvaliableLights.status_code == requests.codes.ok:
         avaliableLights = json.loads(getAvaliableLights.text)
         info = []
-        for light in avaliableLights:
-            info.append({'id': light['id'], 'label': light['label'], 'power': light['power'], 'hue': light['color']['hue'], 'saturation': light['color']['saturation'],
-                'brightness': light['brightness']})
+        if reqtype == 'label':
+            info.append({'id': avaliableLights['id'], 'label': avaliableLights['label'], 'power': avaliableLights['power'], 'hue': avaliableLights['color']['hue'], 'saturation': avaliableLights['color']['saturation'], 'brightness': avaliableLights['brightness']})
+        else:
+            for light in avaliableLights:
+                info.append({'id': light['id'], 'label': light['label'], 'power': light['power'], 'hue': light['color']['hue'], 'saturation': light['color']['saturation'],'brightness': light['brightness']})
         return info
     return False

@@ -63,6 +65,7 @@

def loadConfig ():
     global header
+    global reqtype
     global bulbs
     global setColor
     global config
@@ -75,7 +78,8 @@
     apiKey = 'Bearer ' + apiKey
     header = {'content-type': 'application/json', 'authorization': apiKey}

-    bulbs = {'type': config.get('Bulbs', 'type'), 'set': config.get('Bulbs', 'set')}
+    reqtype = config.get('Bulbs', 'type')
+    bulbs = {'type': reqtype, 'set': config.get('Bulbs', 'set')}
     bulbURL = urllib.quote(bulbs['type'] + ':' + bulbs['set'])
     setColor = lifxCloud + bulbURL + '/color.json'
Reply
#29
Are you trying to get 1 light or a group of lights? In your config you have label:tvroom which is reffering to one light.. which is why you are getting that error.. if you have a group of bulbs that you refer to as tvroom the you need group:tvroom.

Looking at your lights there you have a group called "Group1" which tvroom is inside of. Try changing the config to
Code:
type = group
set = Group1
Don't modify any code.. I have a feeling you just had your settings file worng?

Having said that.. the lifx API should be accounting for that without any problems.. as reqtype is already handled. the API says "typeConfusedet" which is the same as the JSON request it pulls? double check that for me..
Reply
#30
OK.. I just tested it too.. and you're right.. that function is broken.. works for groups but not for single bulbs. good spot.. I will try and put your coe in and see how mine goes. Being Easter its going to be hard to check as it's a busy day for me..
Reply

Logout Mark Read Team Forum Stats Members Help
LIFX for v2 API Plugin - Home Theater - NATEKODILIFX1