power: add support for Hue device groups

Signed-off-by: Clayton Merkle <clay.merkle@gmail.com>
This commit is contained in:
cmerkle 2023-02-09 11:15:27 -05:00 committed by GitHub
parent 5325693ed6
commit 0de712e822
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 4 deletions

View File

@ -1159,6 +1159,11 @@ device_id:
# The device id of the light/socket you want to control. # The device id of the light/socket you want to control.
# An explanation on how you could get the device id, can be found here: # An explanation on how you could get the device id, can be found here:
# https://developers.meethue.com/develop/get-started-2/#turning-a-light-on-and-off # https://developers.meethue.com/develop/get-started-2/#turning-a-light-on-and-off
device_type: light
# Set to light to control a single hue light, or group to control a hue light gorup.
# If device_type is set to light, the device_id should be the light id,
# and if the device_type is group, the device_id should be the group id.
# The default is "light.
``` ```

View File

@ -1363,28 +1363,40 @@ class HueDevice(HTTPDevice):
def __init__(self, config: ConfigHelper) -> None: def __init__(self, config: ConfigHelper) -> None:
super().__init__(config) super().__init__(config)
self.device_id = config.get("device_id") self.device_id = config.get("device_id")
self.device_type = config.get("device_type", "light")
if self.device_type == "group":
self.state_key = "action"
self.on_state = "all_on"
else:
self.state_key = "state"
self.on_state = "on"
async def _send_power_request(self, state: str) -> str: async def _send_power_request(self, state: str) -> str:
new_state = True if state == "on" else False new_state = True if state == "on" else False
url = ( url = (
f"http://{self.addr}/api/{self.user}/lights/{self.device_id}/state" f"http://{self.addr}/api/{self.user}/{self.device_type}s"
f"/{self.device_id}/{self.state_key}"
) )
url = self.client.escape_url(url) url = self.client.escape_url(url)
ret = await self.client.request("PUT", url, body={"on": new_state}) ret = await self.client.request("PUT", url, body={"on": new_state})
resp = cast(List[Dict[str, Dict[str, Any]]], ret.json()) resp = cast(List[Dict[str, Dict[str, Any]]], ret.json())
state_url = (
f"/{self.device_type}s/{self.device_id}/{self.state_key}/on"
)
return ( return (
"on" if resp[0]["success"][f"/lights/{self.device_id}/state/on"] "on" if resp[0]["success"][state_url]
else "off" else "off"
) )
async def _send_status_request(self) -> str: async def _send_status_request(self) -> str:
url = ( url = (
f"http://{self.addr}/api/{self.user}/lights/{self.device_id}" f"http://{self.addr}/api/{self.user}/{self.device_type}s"
f"/{self.device_id}"
) )
url = self.client.escape_url(url) url = self.client.escape_url(url)
ret = await self.client.request("GET", url) ret = await self.client.request("GET", url)
resp = cast(Dict[str, Dict[str, Any]], ret.json()) resp = cast(Dict[str, Dict[str, Any]], ret.json())
return "on" if resp["state"]["on"] else "off" return "on" if resp["state"][self.on_state] else "off"
# The power component has multiple configuration sections # The power component has multiple configuration sections