Skip to main content

A library to interface with Magichue(or Magichome)

Project description

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_with_user_password(user=user, password=password)
bulbs = api.get_online_devices()
light = RemoteLight(api=api, macaddr=online_bulbs[0].macaddr)


light = magichue.LocalLight(lights[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

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_devices()
light = RemoteLight(api=api, macaddr=online_bulbs[0].macaddr)

# It is also possible to retrieve all bulbs 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)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

python-magichue-0.3.1.tar.gz (15.2 kB view details)

Uploaded Source

Built Distribution

python_magichue-0.3.1-py3-none-any.whl (16.3 kB view details)

Uploaded Python 3

File details

Details for the file python-magichue-0.3.1.tar.gz.

File metadata

  • Download URL: python-magichue-0.3.1.tar.gz
  • Upload date:
  • Size: 15.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/34.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.62.3 importlib-metadata/4.11.3 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.10.0

File hashes

Hashes for python-magichue-0.3.1.tar.gz
Algorithm Hash digest
SHA256 e6a83bb2520d8ad44587fa894ce5921dad4e4bda71c534dd1800fb974cfca756
MD5 c2ab73056772ac240860c686b8493ee9
BLAKE2b-256 098db630b10ee23e85cdcb6cd4b6a55853d167c6caa30962793ecb8f35038e73

See more details on using hashes here.

File details

Details for the file python_magichue-0.3.1-py3-none-any.whl.

File metadata

  • Download URL: python_magichue-0.3.1-py3-none-any.whl
  • Upload date:
  • Size: 16.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/34.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.62.3 importlib-metadata/4.11.3 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.10.0

File hashes

Hashes for python_magichue-0.3.1-py3-none-any.whl
Algorithm Hash digest
SHA256 fecbb3bb4690e3d5f222c3ac49a0fa5192a126597d9bb66446a967bca42a6632
MD5 3f0945fda06499d195274c50a62dc647
BLAKE2b-256 3965af08f9bc20990ae69c591ed379c5f2e1782d142f2b27bb8745631c379879

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page