Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
start = data.get("start", 0)
stop = data.get("stop", 0)
length = data.get("len", (stop - start))
colors = data.get("col", [])
primary_color, secondary_color, tertiary_color = (0, 0, 0)
try:
primary_color = tuple(colors.pop(0)) # type: ignore
secondary_color = tuple(colors.pop(0)) # type: ignore
tertiary_color = tuple(colors.pop(0)) # type: ignore
except IndexError:
pass
effect = next(
(item for item in effects if item.effect_id == data.get("fx", 0)),
Effect(effect_id=0, name="Unknown"),
)
palette = next(
(item for item in palettes if item.palette_id == data.get("pal", 0)),
Palette(palette_id=0, name="Unknown"),
)
return Segment(
brightness=data.get("bri", state_brightness),
clones=data.get("cln", -1),
color_primary=primary_color, # type: ignore
color_secondary=secondary_color, # type: ignore
color_tertiary=tertiary_color, # type: ignore
effect=effect,
intensity=data.get("ix", 0),
length=length,
on=data.get("on", state_on),
def update_from_dict(self, data: dict) -> "Device":
"""Return Device object from WLED API response."""
if "effects" in data and data["effects"]:
effects = [
Effect(effect_id=effect_id, name=effect)
for effect_id, effect in enumerate(data["effects"])
]
effects.sort(key=lambda x: x.name)
self.effects = effects
if "palettes" in data and data["palettes"]:
palettes = [
Palette(palette_id=palette_id, name=palette)
for palette_id, palette in enumerate(data["palettes"])
]
palettes.sort(key=lambda x: x.name)
self.palettes = palettes
if "info" in data and data["info"]:
self.info = Info.from_dict(data["info"])
return State(
brightness=brightness,
nightlight=Nightlight.from_dict(data),
on=on,
playlist=data.get("pl", -1),
preset=data.get("ps", -1),
segments=segments,
sync=Sync.from_dict(data),
transition=data.get("transition", 0),
)
class Device:
"""Object holding all information of WLED."""
effects: List[Effect] = []
info: Info
palettes: List[Palette] = []
state: State
def __init__(self, data: dict):
"""Initialize an empty WLED device class."""
# Check if all elements are in the passed dict, else raise an Error
if any(
k not in data and data[k] is not None
for k in ["effects", "palettes", "info", "state"]
):
raise WLEDError("WLED data is incomplete, cannot construct device object")
self.update_from_dict(data)
def update_from_dict(self, data: dict) -> "Device":
"""Return Device object from WLED API response."""