Skip to main content

A Philips Hue Python library

Project description

phue2

modern Python library to control the Philips Hue lighting system

This is a fork of the original phue library by Nathanaël Lécaudé, modernized with type annotations, improved error handling, and a fully-featured CLI. The library remains MIT licensed.

[!IMPORTANT]

I appreciate the original authors work and if there's any interest in merging these (substantial) changes back into the original library, I'm happy to do so.

Installation

Using uv

uv add phue2

uv pip install phue2

Using pip

pip install phue2

Repository Structure

.
├── LICENSE
├── README.md
├── examples/
├── src/
│   └── phue/
│       ├── __init__.py
│       ├── __main__.py      # CLI interface
│       ├── bridge.py        # Bridge connection handling
│       ├── exceptions.py    # Custom exceptions
│       ├── group.py         # Group controls
│       ├── light.py         # Light controls
│       ├── scene.py         # Scene handling
│       └── sensor.py        # Sensor controls
└── tests/              # Tests

Requirements

  • Python 3.10 or higher (not compatible with Python 2.x)
  • httpx (for network requests)

Features

  • Fully typed with Python type annotations
  • Robust error handling with custom exceptions
  • Comprehensive test suite
  • Colorful, user-friendly command-line interface
  • Support for Lights, Groups, Scenes, and Sensors
  • Auto-discovery of Hue bridges on the network
  • Simple and intuitive API for controlling Hue devices

Command Line Usage

The library includes a command-line interface for controlling your Hue lights:

# List all lights
phue ls

# Get details about a specific light
phue get light "Living Room"

# Turn on a light and set brightness
phue set light "Kitchen" --on --bri 200

# List all groups
phue ls groups

# Turn off lights in a group
phue set group "Downstairs" --off

Basic Usage

Using the set_light and get_light methods you can control pretty much all the parameters:

from phue2 import Bridge

# Connect to the bridge
b = Bridge('192.168.1.100')

# If the app is not registered and the button is not pressed, press the button and call connect()
# This only needs to be run a single time
b.connect()

# Get the bridge state (This returns the full dictionary that you can explore)
b.get_api()

# Prints if light 1 is on or not
b.get_light(1, 'on')

# Set brightness of lamp 1 to max
b.set_light(1, 'bri', 254)

# Turn lamp 2 on
b.set_light(2, 'on', True)

# You can also control multiple lamps by sending a list as lamp_id
b.set_light([1, 2], 'on', True)

# You can also use light names instead of the id
b.get_light('Kitchen')
b.set_light('Kitchen', 'bri', 254)

# Also works with lists
b.set_light(['Bathroom', 'Garage'], 'on', False)

Light Objects

If you want to work in a more object-oriented way, you can get Light objects:

# Get a flat list of light objects
lights = b.lights

# Print light names
for light in lights:
    print(light.name)

# Set brightness of each light to 127
for light in lights:
    light.brightness = 127

# Get a dictionary with the light name as the key
light_names = b.get_light_objects('name')

# Set lights using name as key
for light_name in ['Kitchen', 'Bedroom', 'Garage']:
    light = light_names.get(light_name)
    if light:
        light.on = True
        light.hue = 15000
        light.saturation = 120

Error Handling

The library provides custom exceptions for better error handling:

from phue2 import Bridge, PhueRegistrationException, PhueRequestTimeout

try:
    b = Bridge('192.168.1.100')
    b.connect()
except PhueRegistrationException:
    print("Press the button on the bridge and try again")
except PhueRequestTimeout:
    print("Could not connect to the bridge - check your network")

Acknowledgments

This project is a fork of the original phue library created by Nathanaël Lécaudé.

The modernized version was created by zzstoatzz to add type annotations and a more opinionated CLI.

License

MIT - http://opensource.org/licenses/MIT

"Hue Personal Wireless Lighting" is a trademark owned by Koninklijke Philips Electronics N.V., see www.meethue.com for more information. I am in no way affiliated with the Philips organization.

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

phue2-0.0.1.dev279.tar.gz (70.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

phue2-0.0.1.dev279-py3-none-any.whl (27.5 kB view details)

Uploaded Python 3

File details

Details for the file phue2-0.0.1.dev279.tar.gz.

File metadata

  • Download URL: phue2-0.0.1.dev279.tar.gz
  • Upload date:
  • Size: 70.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.6.14

File hashes

Hashes for phue2-0.0.1.dev279.tar.gz
Algorithm Hash digest
SHA256 e99dce82f02c1797819deb39cac6a33522b4448bc3bf88477a9a7a5977314e58
MD5 41b2c4dc26a9923df7320d6a9f12c2fc
BLAKE2b-256 8515fc9eee2efa4e46f0320ccfc73dfc6eb1b06637608dfd947f2a5b333467dc

See more details on using hashes here.

File details

Details for the file phue2-0.0.1.dev279-py3-none-any.whl.

File metadata

File hashes

Hashes for phue2-0.0.1.dev279-py3-none-any.whl
Algorithm Hash digest
SHA256 f32f7ab9173dc266d0320d619337d28d5e2a0879abaaa5a91df3497f44e18108
MD5 837c94f8fd2a207202f00ac33b3cf0dd
BLAKE2b-256 01294437fb4552620b1c7f54d4f3fa80c72266a072ca5e7806e861a6de1d7226

See more details on using hashes here.

Supported by

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