power: add support for Hue device groups
Signed-off-by: Clayton Merkle <clay.merkle@gmail.com>
This commit is contained in:
parent
5325693ed6
commit
0de712e822
|
@ -1159,6 +1159,11 @@ device_id:
|
|||
# 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:
|
||||
# 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.
|
||||
|
||||
```
|
||||
|
||||
|
|
|
@ -1363,28 +1363,40 @@ class HueDevice(HTTPDevice):
|
|||
def __init__(self, config: ConfigHelper) -> None:
|
||||
super().__init__(config)
|
||||
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:
|
||||
new_state = True if state == "on" else False
|
||||
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)
|
||||
ret = await self.client.request("PUT", url, body={"on": new_state})
|
||||
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 (
|
||||
"on" if resp[0]["success"][f"/lights/{self.device_id}/state/on"]
|
||||
"on" if resp[0]["success"][state_url]
|
||||
else "off"
|
||||
)
|
||||
|
||||
async def _send_status_request(self) -> str:
|
||||
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)
|
||||
ret = await self.client.request("GET", url)
|
||||
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
|
||||
|
|
Loading…
Reference in New Issue