Skip to main content

python-magichue

MagicHue (aka MagicHome) is a cheap smart led bulb that you can control hue/saturation/brightness and power over WiFi. They are available at Amazon or other online web shop.

I tested this library with RGB+WW+CW(v7), RGB(v8), RGB+WW(v8) bulbs.

Example

Rainbow cross-fade.

import time
import magichue


light = magichue.Light('192.168.0.20')  # change to your address

light.on = True
light.is_white = False
light.saturation = 1
light.brightness = 255

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

Installation

$ pip install magichue

Usage

import magichue.

import magichue

light = magichue.Light('192.168.0.20')

Discover Bulbs on LAN

from magichue import discover_bulbs

print(discover_bulbs())  # returns a list of bulb addresses

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

Getting Color

This shows a tuple of current RGB:

print(light.rgb)  # (255, 127, 63)

or access individually:

print(light.r)  # 255
print(light.g)  # 127
print(light.b)  # 63

White LEDs

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

Enable the white led:

light.is_white = True

If white led is enabled, you can't change color of bulb!
You will need to disable it before you can change the color.

Disable the white led:

light.is_white = False

Warm White (ww)

light.cw = 0
light.w = 255

Cold White (cw)

light.w = 0
light.cw = 255

Setting Color

Red, Green, Blue (RGB)

Assign all colors with a tuple of integers:

light.rgb = (128, 0, 32)

or assign each color individually with an integer:

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

Hue, Saturation, Brightness (HSB)

Hue and saturation are float values from 0 to 1.
Brightness is a integer value from 0 to 255.

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

Note About Stripe Bulb

Stripe bulb doesn't seem to allow jumping to another color when you change color.

To disable the fading effect,

light.rgb = (128, 0, 20)    # Fades to set color
light.allow_fading = False  # Set to True by default
light.rgb = (20, 0, 128)    # Jumps to set color

Changing Modes

MagicHue bulb has built-in patterns.

Check current mode:

print(light.mode)           # <Mode: NORMAL>
print(light.mode.name)      # 'NORMAL'
print(light.mode.value)     # 97

Set current mode:

light.mode = magichue.RAINBOW_CROSSFADE

These are the built-in modes:

RED_GRADUALLY
GREEN_GRADUALLY
BLUE_GRADUALLY
YELLOW_GRADUALLY
PURPLE_GRADUALLY
WHITE_GRADUALLY
BLUE_GREEN_GRADUALLY

RAINBOW_CROSSFADE
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 a Mode

The instance member speed is a float value from 0 to 1:

light.speed = 0.5  # set speed to 50%

Creating 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 tuples. Max length of 17 tuples.

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.

Release files for magichue 0.2.9.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 magichue 0.2.9.2
File Size Uploaded
magichue-0.2.9.2.tar.gz 10.8 kB Details

Built distribution (wheel)

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

Total release size: 20.5 kB

Release files / magichue-0.2.9.2.tar.gz

Download URL magichue-0.2.9.2.tar.gz
Size 10.8 kB
Tags Source
SHA-256 checksum
How to use checksums
6835731140fc3788a3029f72f97ee001fe0b2cefe6543e2b4b47a3f13e0d1d4d
BLAKE2b-256 checksum
How to use checksums
3f38cbedd5d077b5e503a2d2c0b8594aacc6cef0f3ae2e5d9de995177c1615a8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.2.0 pkginfo/1.6.1 requests/2.24.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.51.0 CPython/3.8.2

Release files / magichue-0.2.9.2-py3-none-any.whl

Download URL magichue-0.2.9.2-py3-none-any.whl
Size 9.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
38122bde2d1ceff1d773fdd14301880c46578003f639b5974f3da2dc379f0291
BLAKE2b-256 checksum
How to use checksums
005a497d82ccc0dc855390265e2462174a29a0a88e4e52f7077e1041b37d176a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.2.0 pkginfo/1.6.1 requests/2.24.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.51.0 CPython/3.8.2

Release history Release notifications | RSS feed

This release

0.2.9.2 This release

2 release files

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