To control a bulb, instantiate the Bulb class with the device's IP address. Once instantiated, you can call various methods to control power, brightness, color, and temperature.
Important Notes:
- Power State: By default,
yeelight will refuse to make changes (like setting brightness) if the bulb is currently off. This will raise an AssertionError. - Auto-On: To ensure commands work even when the bulb is off, set
auto_on=True during instantiation or set the bulb.auto_on property to True. This will automatically turn the bulb on before executing commands. - Rate Limiting: YeeLight connections are rate-limited to 60 commands per minute. Using
auto_on=True increases the message count per command and may hit this limit faster. - Connection Errors: The library uses a single TCP connection for efficiency. If an error occurs, a
socket.error might be raised and the command should be retried.
from yeelight import Bulb
# Instantiate the bulb
bulb = Bulb("192.168.0.19")
# Basic power controls
bulb.turn_on()
bulb.turn_off()
bulb.toggle()
# Brightness and Color
bulb.set_brightness(50) # Set brightness to 50%
bulb.set_rgb(255, 0, 0) # Set RGB value
bulb.set_hsv(320, 100, 50) # Set HSV (Hue, Saturation, Value)
bulb.set_hsv(320, 100) # Set Hue and Saturation, keep brightness same
bulb.set_color_temp(4700) # Set color temperature
# Persistence
bulb.set_default() # Save current settings as default