Skip to main content

python-magichue

demo

Magichue(as known as Magichome, FluxLED, etc.) is a cheap smart led bulb that you can controll hue/saturation/brightnes and power over WiFi. They are available at Amazon or other online web shop.

I tested this library with RGBWWCW(v7), RGB(v8), RGBWW(v8) bulbs.

Now it is possible to use Remote API !

Example

Rainbow cross-fade.

import time
import magichue

user = 'username@example.com'
password = 'password'
api = magichue.RemoteAPI.login_user_password(user=user, password=password)
light = api.get_online_bulbs()[0]

# local_device_ips = magcihue.discover_bulbs()
# light = magichue.LocalLight(local_device_ips[0])


if not light.on:
    light.on = True

if light.is_white:
  light.is_white = False

light.rgb = (0, 0, 0)
light.brightness = 255
light.saturation = 1

for hue in range(1000):
    light.hue = hue / 1000
    time.sleep(0.05)

Installation

$ pip install python-magichue

Usage

Remote API

Now it's available to use MagicHome HTTP API.

You have to login and register your bulb with MagicHome account in advance.

Login with Username/Password

api = magichue.RemoteAPI.login_with_user_password(user='xxx', password='xxx')
print(api.token)  # you can show TOKEN and save it.

Login with Token

It is recommended to use token string.

TOKEN = 'xxx'
api = magichue.RemoteAPI.login_with_token(TOKEN)

Make bulb instance

TOKEN = 'xxx'
api = magichue.RemoteAPI.login_with_token(TOKEN)
light = RemoteLight(api=api, macaddr='xxx')

Discover bulbs

Local bulbs

from magichue import discover_bulbs, LocalLight
addrs = discover_bulbs()  # returns list of bulb address
light = magichue.LocalLight(addrs[0])

Remote bulbs

from magichue import RemoteAPI

TOKEN = 'xxx'
api = magichue.RemoteAPI.login_with_token(TOKEN)
online_bulbs = api.get_online_bulbs()
light = online_bulbs[0]

# Getting online device information.
online_devices = api.get_online_devices()
# It is also possible to retrieve all device info binded with your account.
all_devices = api.get_all_devices()

Power State

Getting power status.

print(light.on)  # => True if light is on else False

Setting light on/off.

light.on = True
light.on = False
# or
light.turn_on()
light.turn_off()

Getting color

This shows a tuple of current RGB.

print(light.rgb)

or access individually.

print(light.r)
print(light.g)
print(light.b)

White LEDs

If your bulbs support white leds, you can change brightness(0-255) of white leds.

To use white led,

light.is_white = True
# light.is_white = False  # This disables white led.

If white led is enabled, you can't change color of bulb! So, you need to execute light.is_white = False before changing color.

Warm White(ww)

light.cw = 0
light.w = 255

Cold White (cw)

light.w = 0
light.cw = 255

Setting color

By rgb

light.rgb = (128, 0, 32)

or

light.r = 200
light.g = 0
light.b = 32

By hsb

light.hue = 0.3
light.saturation = 0.6
light.brightness = 255

hue, saturation are float value from 0 to 1. brightness is a integer value from 0 to 255. These variables are also readable.

Note about stripe bulb

Stripe bulb doesn't seem to allow jump to another color when you change color. To disable fading effect,

light.rgb = (128, 0, 20)  # It fades
light.allow_fading = False  # True by default
light.rgb = (20, 0, 128)  # Jumps

Bulb clock

print(light.get_current_time())

Changing mode

Magichue blub has a built-in flash patterns.

To check current mode, just

print(light.mode.name)  # string name of mode
print(light.mode.value)  # integer value

and changing modes,

light.mode = magichue.RAINBOW_CROSSFADE

These are built-in modes.

RAINBOW_CROSSFADE
RED_GRADUALLY
GREEN_GRADUALLY
BLUE_GRADUALLY
YELLOW_GRADUALLY
BLUE_GREEN_GRADUALLY
PURPLE_GRADUALLY
WHITE_GRADUALLY
RED_GREEN_CROSSFADE
RED_BLUE_CROSSFADE
GREEN_BLUE_CROSSFADE
RAINBOW_STROBE
GREEN_STROBE
BLUE_STROBE
YELLOW_STROBE
BLUE_GREEN_STROBE
PURPLE_STROBE
WHITE_STROBE
RAINBOW_FLASH
NORMAL

Changing the speed of mode

speed is a float value from 0 to 1.

print(light.speed)

light.speed = 0.5  # set speed to 50%

Custom Modes

You can create custom light flash patterns.

mode

  • MODE_JUMP
  • MODE_GRADUALLY
  • MODE_STROBE

speed A float value 0 to 1

colors A list of rgb(tuple or list) which has less than 17 length.

from magichue import (
    CustomMode,
    MODE_JUMP,
)


# Creating Mode
mypattern1 = CustomMode(
    mode=MODE_JUMP,
    speed=0.5,
    colors=[
        (128, 0, 32),
        (100, 20, 0),
        (30, 30, 100),
        (0, 0, 50)
    ]
)

# Apply Mode
light.mode = mypattern1

Other features are in development.

Debugging

Putting this snippet to begging of your code, this library outputs debug log.

import loggging
logging.basicConfig(level=logging.DEBUG)

Release files for python-magichue 0.3.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for python-magichue 0.3.2
File Size Uploaded
python-magichue-0.3.2.tar.gz 16.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for python-magichue 0.3.2
File Interpreter ABI Platform
python_magichue-0.3.2-py3-none-any.whl Python 3 none any Details

Total release size:32.5 kB

Release files / python-magichue-0.3.2.tar.gz

Download URL python-magichue-0.3.2.tar.gz
Size 16.2 kB
Tags Source
SHA-256 checksum
How to use checksums
939f00d1d8f1c87ff9e7cd09f5759d98ca8f109ac8bcfe98e50be5e06fff5570
BLAKE2b-256 checksum
How to use checksums
c455ce0a2f643e9ab4b9f47c72ff86a441c4fecaa3fc74acef05c17589c6b983
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.0 CPython/3.9.0

Release files / python_magichue-0.3.2-py3-none-any.whl

Download URL python_magichue-0.3.2-py3-none-any.whl
Size 16.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
929377fbe7662334367c9bc00e090f76b9971f4abd01658a6a1435a80ccf574b
BLAKE2b-256 checksum
How to use checksums
ef07aa8b539132182525d28bac8f58088ace7666210f833bc5713423e225a075
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.0 CPython/3.9.0

Release history Release notifications | RSS feed

This release

0.3.2 This release

2 release files

0.3.1

2 release files

0.3

2 release files

0.2.9.1

1 release file

0.2.8

1 release file

0.2.7

2 release files

0.2.6

1 release file

0.2.5

2 release files

0.2.4

1 release file

0.2.1

1 release file

0.1.2

1 release file

0.1

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page