High-Level API¶
The high-level API provides simplified functions for common LIFX operations. These are the recommended entry points for most users.
Discovery Functions¶
discover
async
¶
discover(
timeout: float = DISCOVERY_TIMEOUT,
broadcast_address: str = "255.255.255.255",
port: int = LIFX_UDP_PORT,
max_response_time: float = MAX_RESPONSE_TIME,
idle_timeout_multiplier: float = IDLE_TIMEOUT_MULTIPLIER,
device_timeout: float = DEFAULT_REQUEST_TIMEOUT,
max_retries: int = DEFAULT_MAX_RETRIES,
) -> AsyncGenerator[Device, None]
Discover LIFX devices and yield them as they are found.
| PARAMETER | DESCRIPTION |
|---|---|
timeout
|
Discovery timeout in seconds (default 3.0)
TYPE:
|
broadcast_address
|
Broadcast address to use (default "255.255.255.255")
TYPE:
|
port
|
Port to use (default LIFX_UDP_PORT)
TYPE:
|
max_response_time
|
Max time to wait for responses
TYPE:
|
idle_timeout_multiplier
|
Idle timeout multiplier
TYPE:
|
device_timeout
|
request timeout set on discovered devices
TYPE:
|
max_retries
|
max retries per request set on discovered devices
TYPE:
|
Yields: Device instances as they are discovered
Example
Source code in src/lifx/api.py
find_by_serial
async
¶
find_by_serial(
serial: str,
timeout: float = DISCOVERY_TIMEOUT,
broadcast_address: str = "255.255.255.255",
port: int = LIFX_UDP_PORT,
max_response_time: float = MAX_RESPONSE_TIME,
idle_timeout_multiplier: float = IDLE_TIMEOUT_MULTIPLIER,
device_timeout: float = DEFAULT_REQUEST_TIMEOUT,
max_retries: int = DEFAULT_MAX_RETRIES,
) -> Device | None
Find a specific device by serial number.
| PARAMETER | DESCRIPTION |
|---|---|
serial
|
Serial number as hex string (with or without separators)
TYPE:
|
timeout
|
Discovery timeout in seconds (default DISCOVERY_TIMEOUT)
TYPE:
|
broadcast_address
|
Broadcast address to use (default "255.255.255.255")
TYPE:
|
port
|
Port to use (default LIFX_UDP_PORT)
TYPE:
|
max_response_time
|
Max time to wait for responses
TYPE:
|
idle_timeout_multiplier
|
Idle timeout multiplier
TYPE:
|
device_timeout
|
request timeout set on discovered device
TYPE:
|
max_retries
|
max retries per request set on discovered device
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Device | None
|
Device instance if found, None otherwise |
Example
Source code in src/lifx/api.py
find_by_label
async
¶
find_by_label(
label: str,
exact_match: bool = False,
timeout: float = DISCOVERY_TIMEOUT,
broadcast_address: str = "255.255.255.255",
port: int = LIFX_UDP_PORT,
max_response_time: float = MAX_RESPONSE_TIME,
idle_timeout_multiplier: float = IDLE_TIMEOUT_MULTIPLIER,
device_timeout: float = DEFAULT_REQUEST_TIMEOUT,
max_retries: int = DEFAULT_MAX_RETRIES,
) -> AsyncGenerator[Device]
Find LIFX devices by label (name).
Uses a protocol trick by broadcasting GetLabel instead of GetService, which returns all device labels in StateLabel responses. This is more efficient than querying each device individually.
| PARAMETER | DESCRIPTION |
|---|---|
label
|
Device label to search for (case-insensitive)
TYPE:
|
exact_match
|
If True, match label exactly and yield at most one device; if False, match substring and yield all matching devices (default False)
TYPE:
|
timeout
|
Discovery timeout in seconds (default DISCOVERY_TIMEOUT)
TYPE:
|
broadcast_address
|
Broadcast address to use (default "255.255.255.255")
TYPE:
|
port
|
Port to use (default LIFX_UDP_PORT)
TYPE:
|
max_response_time
|
Max time to wait for responses
TYPE:
|
idle_timeout_multiplier
|
Idle timeout multiplier
TYPE:
|
device_timeout
|
request timeout set on discovered device(s)
TYPE:
|
max_retries
|
max retries per request set on discovered device(s)
TYPE:
|
| YIELDS | DESCRIPTION |
|---|---|
AsyncGenerator[Device]
|
Matching Device instance(s) |
Example
# Find all devices with "Living" in the label
async for device in find_by_label("Living"):
async with device:
await device.set_power(True)
# Find device by exact label match (yields at most one)
async for device in find_by_label("Living Room", exact_match=True):
async with device:
await device.set_power(True)
break # exact_match yields at most one device
Source code in src/lifx/api.py
914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 | |
find_by_ip
async
¶
find_by_ip(
ip: str,
timeout: float = DISCOVERY_TIMEOUT,
port: int = LIFX_UDP_PORT,
max_response_time: float = MAX_RESPONSE_TIME,
idle_timeout_multiplier: float = IDLE_TIMEOUT_MULTIPLIER,
device_timeout: float = DEFAULT_REQUEST_TIMEOUT,
max_retries: int = DEFAULT_MAX_RETRIES,
) -> Device | None
Find a LIFX device by IP address.
Uses a targeted discovery by sending the broadcast to the specific IP address, which means only that device will respond (if it exists). This is more efficient than broadcasting to all devices and filtering.
| PARAMETER | DESCRIPTION |
|---|---|
ip
|
Target device IP address
TYPE:
|
timeout
|
Discovery timeout in seconds (default DISCOVERY_TIMEOUT)
TYPE:
|
port
|
Port to use (default LIFX_UDP_PORT)
TYPE:
|
max_response_time
|
Max time to wait for responses
TYPE:
|
idle_timeout_multiplier
|
Idle timeout multiplier
TYPE:
|
device_timeout
|
request timeout set on discovered device
TYPE:
|
max_retries
|
max retries per request set on discovered device
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Device | None
|
Device instance if found, None otherwise |
Example
Source code in src/lifx/api.py
Device Group¶
DeviceGroup
¶
DeviceGroup(
devices: Sequence[
Device | Light | HevLight | InfraredLight | MultiZoneLight | MatrixLight
],
)
A group of devices for batch operations.
Provides convenient methods to control multiple devices simultaneously.
Example
| PARAMETER | DESCRIPTION |
|---|---|
devices
|
List of Device instances
TYPE:
|
| METHOD | DESCRIPTION |
|---|---|
__aenter__ |
Enter async context manager. |
__aexit__ |
Exit async context manager. |
__iter__ |
Iterate over devices in the group. |
__len__ |
Get number of devices in the group. |
__getitem__ |
Get device by index. |
set_power |
Set power state for all devices in the group. |
set_color |
Set color for all Light devices in the group. |
set_brightness |
Set brightness for all Light devices in the group. |
pulse |
Pulse effect for all Light devices. |
organize_by_location |
Organize devices by location label. |
organize_by_group |
Organize devices by group label. |
filter_by_location |
Filter devices to a specific location. |
filter_by_group |
Filter devices to a specific group. |
get_unassigned_devices |
Get devices without location or group assigned. |
apply_theme |
Apply a theme to all devices in the group. |
invalidate_metadata_cache |
Clear all cached location and group metadata. |
| ATTRIBUTE | DESCRIPTION |
|---|---|
devices |
Get all the devices in the group.
TYPE:
|
lights |
Get all Light devices in the group. |
hev_lights |
Get the HEV lights in the group. |
infrared_lights |
Get the Infrared lights in the group.
TYPE:
|
multizone_lights |
Get all MultiZone light devices in the group.
TYPE:
|
matrix_lights |
Get all Matrix light devices in the group.
TYPE:
|
Source code in src/lifx/api.py
Attributes¶
devices
property
¶
devices: Sequence[
Device | HevLight | InfraredLight | Light | MultiZoneLight | MatrixLight
]
Get all the devices in the group.
infrared_lights
property
¶
infrared_lights: list[InfraredLight]
Get the Infrared lights in the group.
multizone_lights
property
¶
multizone_lights: list[MultiZoneLight]
Get all MultiZone light devices in the group.
matrix_lights
property
¶
matrix_lights: list[MatrixLight]
Get all Matrix light devices in the group.
Functions¶
__aenter__
async
¶
__aenter__() -> DeviceGroup
__aexit__
async
¶
__aexit__(
exc_type: type[BaseException] | None,
exc_val: BaseException | None,
exc_tb: TracebackType | None,
) -> None
__iter__
¶
__iter__() -> Iterator[
Device | Light | HevLight | InfraredLight | MultiZoneLight | MatrixLight
]
__getitem__
¶
__getitem__(
index: int,
) -> Device | Light | HevLight | InfraredLight | MultiZoneLight | MatrixLight
set_power
async
¶
Set power state for all devices in the group.
| PARAMETER | DESCRIPTION |
|---|---|
on
|
True to turn on, False to turn off
TYPE:
|
duration
|
Transition duration in seconds (default 0.0)
TYPE:
|
Example
Source code in src/lifx/api.py
set_color
async
¶
Set color for all Light devices in the group.
| PARAMETER | DESCRIPTION |
|---|---|
color
|
HSBK color to set
TYPE:
|
duration
|
Transition duration in seconds (default 0.0)
TYPE:
|
Example
Source code in src/lifx/api.py
set_brightness
async
¶
Set brightness for all Light devices in the group.
| PARAMETER | DESCRIPTION |
|---|---|
brightness
|
Brightness level (0.0-1.0)
TYPE:
|
duration
|
Transition duration in seconds (default 0.0)
TYPE:
|
Example
Source code in src/lifx/api.py
pulse
async
¶
Pulse effect for all Light devices.
| PARAMETER | DESCRIPTION |
|---|---|
color
|
Color to pulse to
TYPE:
|
period
|
Period of one cycle in seconds
TYPE:
|
cycles
|
Number of cycles
TYPE:
|
Example
Source code in src/lifx/api.py
organize_by_location
async
¶
organize_by_location(
include_unassigned: bool = False,
) -> dict[str, DeviceGroup]
Organize devices by location label.
Fetches location metadata if not cached and groups devices by location label.
| PARAMETER | DESCRIPTION |
|---|---|
include_unassigned
|
Include "Unassigned" group
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
dict[str, DeviceGroup]
|
Dictionary mapping location labels to DeviceGroup instances |
Example
Source code in src/lifx/api.py
organize_by_group
async
¶
organize_by_group(include_unassigned: bool = False) -> dict[str, DeviceGroup]
Organize devices by group label.
Fetches group metadata if not cached and groups devices by group label.
| PARAMETER | DESCRIPTION |
|---|---|
include_unassigned
|
Include "Unassigned" group
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
dict[str, DeviceGroup]
|
Dictionary mapping group labels to DeviceGroup instances |
Example
Source code in src/lifx/api.py
filter_by_location
async
¶
filter_by_location(label: str, case_sensitive: bool = False) -> DeviceGroup
Filter devices to a specific location.
| PARAMETER | DESCRIPTION |
|---|---|
label
|
Location label to filter by
TYPE:
|
case_sensitive
|
If True, performs case-sensitive matching (default False)
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
DeviceGroup
|
DeviceGroup containing devices in the specified location |
| RAISES | DESCRIPTION |
|---|---|
KeyError
|
If location label not found |
Example
Source code in src/lifx/api.py
filter_by_group
async
¶
filter_by_group(label: str, case_sensitive: bool = False) -> DeviceGroup
Filter devices to a specific group.
| PARAMETER | DESCRIPTION |
|---|---|
label
|
Group label to filter by
TYPE:
|
case_sensitive
|
If True, performs case-sensitive matching (default False)
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
DeviceGroup
|
DeviceGroup containing devices in the specified group |
| RAISES | DESCRIPTION |
|---|---|
KeyError
|
If group label not found |
Example
Source code in src/lifx/api.py
get_unassigned_devices
¶
Get devices without location or group assigned.
| PARAMETER | DESCRIPTION |
|---|---|
metadata_type
|
Type of metadata to check ("location" or "group")
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
list[Device]
|
List of devices without the specified metadata type |
| RAISES | DESCRIPTION |
|---|---|
RuntimeError
|
If metadata hasn't been fetched yet |
Example
Source code in src/lifx/api.py
apply_theme
async
¶
Apply a theme to all devices in the group.
Each device applies the theme according to its capabilities: - Light: Selects random color from theme - MultiZoneLight: Distributes colors evenly across zones - MatrixLight: Uses interpolation for smooth gradients - Other devices: No action (themes only apply to color devices)
| PARAMETER | DESCRIPTION |
|---|---|
theme
|
Theme to apply
TYPE:
|
power_on
|
Turn on devices if True
TYPE:
|
duration
|
Transition duration in seconds
TYPE:
|
Example
Source code in src/lifx/api.py
invalidate_metadata_cache
¶
Clear all cached location and group metadata.
Use this if you've changed device locations/groups and want to re-fetch.
Example
devices = []
async for device in discover():
devices.append(device)
group = DeviceGroup(devices)
# First organization
by_location = await group.organize_by_location()
# ... change device locations ...
# Clear cache and re-organize
group.invalidate_metadata_cache()
by_location = await group.organize_by_location()
Source code in src/lifx/api.py
Examples¶
Simple Discovery¶
from lifx import discover, DeviceGroup, Colors
async def main():
count: int = 0
async for device in discover():
count += 1
await device.set_power(True)
await device.set_color(Colors.BLUE)
print(f"Found {count} devices")
Find by Serial Number¶
from lifx import find_by_serial
async def main():
# Find specific device by serial number
device = await find_by_serial("d073d5123456")
if device:
async with device:
await device.set_power(True)
Find by Label¶
from lifx import find_by_label, Colors
async def main():
# Find all devices with "Living" in the label (substring match)
async for device in find_by_label("Living"): # May match "Living Room", "Living Area", etc.
await device.set_power(True)
# Find device with exact label match
async for device in find_by_label("Living Room", exact_match=True):
await device.set_color(Colors.WARM_WHITE)
break # exact_match returns at most one device