I will openly admit that I am not a scripter, however, I have devised a way to get MagicHome v.9 bulbs working with colour and using their API.
— Steps to reproduce —
Prerequsites:
- Download/Install (NetCapture / Android).
- Install CA Certificate.
- Run this capture program and grab your token from the MagicHome app packet.
Setup:
-
Download/Extract:
GitHub - namacha/python-magichue: Control Magic Hue(Magichome) in Python.
-
Install this into Python3 / Pip3
-
Download/Install:
colorir · PyPI
-
Copy this script - I can’t remember what imports I installed as part of my testing, please check for yourself while preparing.
import time
import magichue
import colorsys
import sys
import re
from magichue import RemoteAPI
from magichue import RemoteLight
from colorir import HSV, sRGB
h,s,v = sys.argv[2].split(',')
h_str = str(h)
h_out = re.sub(r'\..*',"",h_str)
h_int = int(h_out)
s_str =str(s)
s_out = re.sub(r'\..*',"",s_str)
s_int =int(s_out)
v_str = str(v)
v_out = re.sub(r'\..*',"",v_str)
v_int = int(v_out)
h = int(h_int)
s = int(s_int)
v = int(v_int)
rgb = HSV(h, s, v, max_sva=100).rgb()
rgb_sRGB = str(rgb)
rgb_sRGB_1 = rgb_sRGB.replace('sRGB', '')
rgb_bracket1 = str(rgb_sRGB_1)
rgb_bracket_1 = rgb_bracket1.replace('(', '')
rgb_bracket2 = str(rgb_bracket_1)
rgb_bracket_2 = rgb_bracket2.replace(')', '')
rgb_zero = str(rgb_bracket_2)
rgb_zero_1 = rgb_zero.replace('.0', '')
rgb_output = str(rgb_zero_1)
TOKEN = ''
api = magichue.RemoteAPI.login_with_token(TOKEN)
light = RemoteLight(api=api, macaddr=sys.argv[1])
r,g,b = rgb_output.split(',')
r_str = str(r)
r_out = re.sub(r'\..*',"",r_str)
r_int = int(r_out)
g_str = str(g)
g_out = re.sub(r'\..*',"",g_str)
g_int = int(g_out)
b_str = str(b)
b_out = re.sub(r'\..*',"",b_str)
b_int = int(b_out)
#b_int = int(b)
light.r = r_int
light.g = g_int
light.b = b_int
- Create a rule/script that executes the script:
var thread = Java.type('java.lang.Thread')
var Exec = Java.type("org.openhab.core.model.script.actions.Exec");
Exec.executeCommandLine("python3","/magichue/magichome.py","MAC",itemRegistry.getItem('COLOR').getState());
thread.sleep(200);
Exec.executeCommandLine("python3","/magichue/magichome.py","MAC",itemRegistry.getItem('COLOR').getState());
thread.sleep(200);
And this is how I have ended up getting the bulbs to work and connect via Google Home! ~ I really hope this helps someone, I’m certainly no scripter and this forced me to gulge my eye’s out in frustration… However, I have an end product and I hope you are able to use it.