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¶
All discovery functions return devices whose state has not been initialised
yet — async with device does that. Devices start with fetch_wifi_info and
fetch_ambient_light off, so set either property before entering the context
manager for the reading to be included in the initial state. See
Opt-In State Fields on Discovered Devices.
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 15.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
Note
Internal device-class construction errors are logged and isolated to that responder so the remaining discovery sweep can continue.
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If |
LifxNetworkError
|
If the validated destination cannot be resolved to a native socket address or the discovery transport fails. |
Example
Source code in src/lifx/api.py
1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 | |
discover_mdns
async
¶
discover_mdns(
timeout: float = DISCOVERY_TIMEOUT,
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 via mDNS and yield them as they are found.
Uses mDNS/DNS-SD discovery with the _lifx._udp.local service type. Device type metadata in DNS-SD responses avoids a separate LIFX product query for every device.
The IPv4 multicast query is sent from an ephemeral source port and the transport accepts legacy-unicast replies addressed directly to that socket. It does not join the multicast group and does not receive unsolicited announcements. Each call observes direct traffic delivered to its per-call socket during the sweep and has its own non-reusable discovery cache, but it does not authenticate or correlate responders with outstanding queries. An mDNS result proves an advertisement exists, not that the device is currently reachable; see the discovery guide's Limitations section for detail, including how discover()'s mDNS candidates verify reachability with a correlated device request.
Each yielded device reports connectivity from its mDNS TXT record:
Connectivity.THREAD only after an exact positive Thread report, and
Connectivity.WIFI for every other construction or discovery outcome.
Once the device answers any request, its own frame address report
supersedes this in both directions. This descriptive value does not
authenticate the device or change its routing, retry, or tuning
behaviour.
| PARAMETER | DESCRIPTION |
|---|---|
timeout
|
Discovery timeout in seconds (default 15.0)
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 | DESCRIPTION |
|---|---|
AsyncGenerator[Device, None]
|
Device instances (Light, MatrixLight, MultiZoneLight, etc.) |
AsyncGenerator[Device, None]
|
as they are discovered |
Example
Source code in src/lifx/api.py
1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 | |
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 |
Note
Internal device-class construction errors are logged and return
None rather than escaping this lookup.
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If |
LifxNetworkError
|
If the validated destination cannot be resolved to a native socket address or the discovery transport fails. |
Example
Source code in src/lifx/api.py
1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 | |
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) |
Note
Internal device-class construction errors are logged and isolated to that responder so remaining matches can still be yielded.
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If |
LifxNetworkError
|
If the validated destination cannot be resolved to a native socket address or the discovery transport fails. |
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)
Source code in src/lifx/api.py
1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 | |
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 targeted discovery by sending the request to the validated IPv4 or IPv6 literal, which means only that device will respond (if it exists). Zoned IPv6 link-local literals are supported when they include their interface identifier. This is more efficient than broadcasting to all devices and filtering.
| PARAMETER | DESCRIPTION |
|---|---|
ip
|
Validated IPv4 or IPv6 device address literal, including a zoned link-local IPv6 literal
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 |
Note
Internal device-class construction errors are logged and return
None rather than escaping this lookup.
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If |
LifxNetworkError
|
If the validated destination cannot be resolved to a native socket address or the discovery transport fails. |
Example
Source code in src/lifx/api.py
1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 | |
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 and close all device connections. |
__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.
Methods:¶
__aenter__
async
¶
__aenter__() -> DeviceGroup
__aexit__
async
¶
__aexit__(
exc_type: type[BaseException] | None,
exc_val: BaseException | None,
exc_tb: TracebackType | None,
) -> None
Exit async context manager and close all device connections.
Source code in src/lifx/api.py
__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 (and CeilingLight): Uses interpolation for smooth gradients
- Other devices: No action (themes only apply to color devices)
Each device is painted exactly once, using the most specific
apply_theme() implementation for its class.
| 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
Organizational Groupings¶
Dataclasses for organizing devices by location or group. Returned by DeviceGroup.organize_by_location() and DeviceGroup.organize_by_group().
LocationGrouping¶
Location-based device grouping returned by DeviceGroup.organize_by_location().
LocationGrouping
dataclass
¶
Organizational structure for location-based grouping.
GroupGrouping¶
Group-based device grouping returned by DeviceGroup.organize_by_group().
GroupGrouping
dataclass
¶
Organizational structure for group-based grouping.
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)
break # exact_match returns at most one device