Last active
September 2, 2017 16:05
-
-
Save hvanderlaan/3d8e11869f86ba94d9d6df1c815af3aa to your computer and use it in GitHub Desktop.
getting Ikea tardfri lightbulb status
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
[tardfri] | |
hubip = x.x.x.x | |
securityid = AABBCCDDEEFFGGHH |
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
#!/usr/bin/env python | |
# file : smartlight.py | |
# purpose : control ikea tardfri smart light | |
# | |
# author : harald van der laan | |
# date : 2017/04/06 | |
# version : v1.0.0 | |
# | |
# requirements: | |
# - libcoap | |
# | |
# changelog : | |
# - v1.0.0 initial release (harald) | |
""" smartlight - python framework for the tardfri ikea smart light hub | |
this frame works requires libcoap with dtls compiled, more info is found | |
in the bin directory. there is also a build script. | |
libcoap requires cunit, a2x and doxygen """ | |
from __future__ import print_function | |
import sys | |
import os | |
import json | |
import ConfigParser | |
def get_tardfri_devices(hubip, securityid): | |
""" function for getting all paired devices to the hub """ | |
hub = 'coaps://{}:5684/15001' .format(hubip) | |
apicall = 'bin/coap-client -m get -u "Client_identity" -k "{}" "{}" | awk \'NR==4\'' .format(securityid, hub) | |
if os.path.exists('bin/coap-client'): | |
result = os.popen(apicall) | |
else: | |
sys.stderr.write('[-] error: could not find libcoap\n') | |
return json.loads(result.read().strip('\n')) | |
def get_tardfri_lightbulb(hubip, securityid, deviceid): | |
""" function for getting information of a tardfri lightbulb """ | |
hub = 'coaps://{}:5684/15001/{}' .format(hubip, deviceid) | |
apicall = 'bin/coap-client -m get -u "Client_identity" -k "{}" "{}" | awk \'NR==4\'' .format(securityid, hub) | |
if os.path.exists('bin/coap-client'): | |
result = os.popen(apicall) | |
else: | |
sys.stderr.write('[-] error: could not find libcoap\n') | |
sys.exit(1) | |
return json.loads(result.read().strip('\n')) | |
def main(): | |
conf = ConfigParser.ConfigParser() | |
conf.read('smartlight.cfg') | |
hubip = conf.get('tardfri', 'hubip') | |
securityid = conf.get('tardfri', 'securityid') | |
lightbulb = [] | |
print('[ ] smartlight: receiving tardfri lightbulbs information') | |
devices = get_tardfri_devices(hubip, securityid) | |
for deviceid in range(len(devices)): | |
lightbulb.append(get_tardfri_lightbulb(hubip, securityid, str(devices[deviceid]))) | |
print('[+] smartlight: done getting tardfri lightbulbs information') | |
print('===========================================================\n') | |
for counter in range(len(lightbulb)): | |
try: | |
if lightbulb[counter]["3311"][0]["5850"] == 0: | |
# lightbulb is off | |
print('bulbid: {}, name: {}, state: off' .format(counter, | |
lightbulb[counter]["9001"])) | |
else: | |
# lightbulb is on | |
print('bulbid: {}, name: {}, state: on' .format(counter, | |
lightbulb[counter]["9001"])) | |
except KeyError: | |
# not a lightbulb but a controler | |
pass | |
if __name__ == "__main__": | |
main() | |
sys.exit(0) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment