mirror of
https://github.com/home-assistant/core.git
synced 2025-04-26 18:27:51 +00:00
Simplify WLED segment tracking (#52174)
* Simplify WLED segment tracking * Fix master controls
This commit is contained in:
parent
e6c850136c
commit
b939570c9c
@ -86,9 +86,8 @@ async def async_setup_entry(
|
|||||||
|
|
||||||
update_segments = partial(
|
update_segments = partial(
|
||||||
async_update_segments,
|
async_update_segments,
|
||||||
entry,
|
|
||||||
coordinator,
|
coordinator,
|
||||||
{},
|
set(),
|
||||||
async_add_entities,
|
async_add_entities,
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -363,30 +362,24 @@ class WLEDSegmentLight(WLEDEntity, LightEntity):
|
|||||||
|
|
||||||
@callback
|
@callback
|
||||||
def async_update_segments(
|
def async_update_segments(
|
||||||
entry: ConfigEntry,
|
|
||||||
coordinator: WLEDDataUpdateCoordinator,
|
coordinator: WLEDDataUpdateCoordinator,
|
||||||
current: dict[int, WLEDSegmentLight | WLEDMasterLight],
|
current_ids: set[int],
|
||||||
async_add_entities,
|
async_add_entities,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Update segments."""
|
"""Update segments."""
|
||||||
segment_ids = {light.segment_id for light in coordinator.data.state.segments}
|
segment_ids = {light.segment_id for light in coordinator.data.state.segments}
|
||||||
current_ids = set(current)
|
new_entities: list[WLEDMasterLight | WLEDSegmentLight] = []
|
||||||
new_entities = []
|
|
||||||
|
|
||||||
# Discard master (if present)
|
|
||||||
current_ids.discard(-1)
|
|
||||||
|
|
||||||
# Process new segments, add them to Home Assistant
|
|
||||||
for segment_id in segment_ids - current_ids:
|
|
||||||
current[segment_id] = WLEDSegmentLight(coordinator, segment_id)
|
|
||||||
new_entities.append(current[segment_id])
|
|
||||||
|
|
||||||
# More than 1 segment now? No master? Add master controls
|
# More than 1 segment now? No master? Add master controls
|
||||||
if not coordinator.keep_master_light and (
|
if not coordinator.keep_master_light and (
|
||||||
len(current_ids) < 2 and len(segment_ids) > 1
|
len(current_ids) < 2 and len(segment_ids) > 1
|
||||||
):
|
):
|
||||||
current[-1] = WLEDMasterLight(coordinator)
|
new_entities.append(WLEDMasterLight(coordinator))
|
||||||
new_entities.append(current[-1])
|
|
||||||
|
# Process new segments, add them to Home Assistant
|
||||||
|
for segment_id in segment_ids - current_ids:
|
||||||
|
current_ids.add(segment_id)
|
||||||
|
new_entities.append(WLEDSegmentLight(coordinator, segment_id))
|
||||||
|
|
||||||
if new_entities:
|
if new_entities:
|
||||||
async_add_entities(new_entities)
|
async_add_entities(new_entities)
|
||||||
|
@ -31,7 +31,7 @@ async def async_setup_entry(
|
|||||||
update_segments = partial(
|
update_segments = partial(
|
||||||
async_update_segments,
|
async_update_segments,
|
||||||
coordinator,
|
coordinator,
|
||||||
{},
|
set(),
|
||||||
async_add_entities,
|
async_add_entities,
|
||||||
)
|
)
|
||||||
coordinator.async_add_listener(update_segments)
|
coordinator.async_add_listener(update_segments)
|
||||||
@ -118,19 +118,18 @@ class WLEDPaletteSelect(WLEDEntity, SelectEntity):
|
|||||||
@callback
|
@callback
|
||||||
def async_update_segments(
|
def async_update_segments(
|
||||||
coordinator: WLEDDataUpdateCoordinator,
|
coordinator: WLEDDataUpdateCoordinator,
|
||||||
current: dict[int, WLEDPaletteSelect],
|
current_ids: set[int],
|
||||||
async_add_entities,
|
async_add_entities,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Update segments."""
|
"""Update segments."""
|
||||||
segment_ids = {segment.segment_id for segment in coordinator.data.state.segments}
|
segment_ids = {segment.segment_id for segment in coordinator.data.state.segments}
|
||||||
current_ids = set(current)
|
|
||||||
|
|
||||||
new_entities = []
|
new_entities = []
|
||||||
|
|
||||||
# Process new segments, add them to Home Assistant
|
# Process new segments, add them to Home Assistant
|
||||||
for segment_id in segment_ids - current_ids:
|
for segment_id in segment_ids - current_ids:
|
||||||
current[segment_id] = WLEDPaletteSelect(coordinator, segment_id)
|
current_ids.add(segment_id)
|
||||||
new_entities.append(current[segment_id])
|
new_entities.append(WLEDPaletteSelect(coordinator, segment_id))
|
||||||
|
|
||||||
if new_entities:
|
if new_entities:
|
||||||
async_add_entities(new_entities)
|
async_add_entities(new_entities)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user