Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve how to update group capabilities from lights #123

Merged
merged 4 commits into from
Jun 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 4 additions & 7 deletions pydeconz/gateway.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ async def initialize(self) -> None:
self.lights = Lights(data["lights"], self.request) # type: ignore
self.sensors = Sensors(data["sensors"], self.request) # type: ignore

self.update_group_color(self.lights.keys())
self.update_group_color(list(self.lights.keys()))
self.update_scenes()

async def refresh_state(self) -> None:
Expand All @@ -87,7 +87,7 @@ async def refresh_state(self) -> None:
self.lights.process_raw(data["lights"]) # type: ignore
self.sensors.process_raw(data["sensors"]) # type: ignore

self.update_group_color(self.lights.keys()) # type: ignore
self.update_group_color(list(self.lights.keys())) # type: ignore
self.update_scenes()

async def request(
Expand Down Expand Up @@ -194,18 +194,15 @@ def update_group_color(self, lights: list) -> None:
if not any({*lights} & {*group.lights}):
continue

# More than one light means initialize called this method.
# Then we take first best light to be available.
light_ids = lights
if len(light_ids) > 1:
# More than one light means self.initialize called this method.
if len(light_ids := lights) > 1:
light_ids = group.lights

for light_id in light_ids:
light = self.lights[light_id] # type: ignore

if light.ZHATYPE == Light.ZHATYPE and light.reachable:
group.update_color_state(light)
break

def update_scenes(self) -> None:
"""Update scenes to hold all known scenes from existing groups."""
Expand Down
36 changes: 21 additions & 15 deletions pydeconz/group.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import logging
from pprint import pformat
from typing import Callable, Optional, Tuple
from typing import Callable, Dict, Optional, Tuple, Union

from .api import APIItems
from .deconzdevice import DeconzDevice
Expand Down Expand Up @@ -168,12 +168,12 @@ def id(self) -> Optional[str]:
return self.raw.get("id")

@property
def lights(self) -> Optional[list]:
def lights(self) -> list:
"""List of all light IDs in group.

Sequence is defined by the gateway.
"""
return self.raw.get("lights")
return self.raw.get("lights", [])

@property
def lightsequence(self) -> Optional[list]:
Expand All @@ -193,18 +193,24 @@ def multideviceids(self) -> Optional[list]:

def update_color_state(self, light: Light) -> None:
"""Sync color state with light."""
self.update(
{
"action": {
"bri": light.brightness,
"hue": light.hue,
"sat": light.sat,
"ct": light.ct,
"xy": light.xy or (None, None),
"colormode": light.colormode,
}
}
)
data: Dict[str, Union[float, int, str, tuple]] = {}

if light.brightness is not None:
data["bri"] = light.brightness
if light.hue is not None:
data["hue"] = light.hue
if light.sat is not None:
data["sat"] = light.sat
if light.ct is not None:
data["ct"] = light.ct
if light.xy is not None:
data["xy"] = light.xy
if light.colormode is not None:
data["colormode"] = light.colormode
if light.effect is not None:
data["effect"] = light.effect

self.update({"action": data})


class Scenes(APIItems):
Expand Down
57 changes: 45 additions & 12 deletions tests/test_gateway.py
Original file line number Diff line number Diff line change
Expand Up @@ -410,24 +410,57 @@ async def test_update_group_color(mock_aioresponse):
"colormode": "hs",
},
"id": "gid",
"lights": ["l1"],
"lights": ["l1", "l2", "l3", "l4"],
"scenes": [],
}
},
"lights": {
"l1": {
"type": "light",
"state": {
"bri": 1,
"hue": 1,
"sat": 1,
"xy": (0.1, 0.1),
"ct": 1,
"colormode": "xy",
"reachable": True,
},
},
"l2": {
"type": "light",
"state": {
"bri": 2,
"hue": 2,
"sat": 2,
"xy": (0.5, 0.5),
"ct": 2,
"colormode": "xy",
"colormode": "ct",
"reachable": True,
},
},
"l3": {
"type": "light",
"state": {
"bri": 3,
"reachable": True,
},
},
"l4": {
"type": "light",
"state": {
"bri": 4,
"ct": 4,
"colormode": "ct",
"reachable": False,
},
},
"l5": {
"type": "light",
"state": {
"bri": 5,
"ct": 5,
"colormode": "ct",
"reachable": True,
},
},
"l2": {"type": ""},
},
"sensors": {},
}
Expand All @@ -440,11 +473,11 @@ async def test_update_group_color(mock_aioresponse):

await session.initialize()

assert session.groups["g1"].brightness == 2
assert session.groups["g1"].hue == 2
assert session.groups["g1"].sat == 2
assert session.groups["g1"].xy == (0.5, 0.5)
assert session.groups["g1"].colormode == "xy"
assert session.groups["g1"].brightness == 3
assert session.groups["g1"].hue == 1
assert session.groups["g1"].sat == 1
assert session.groups["g1"].xy == (0.1, 0.1)
assert session.groups["g1"].colormode == "ct"
assert session.groups["g1"].ct == 2

await session.session.close()
await session.session.close()