You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
tinge/tinge/HueGroup/__init__.py

106 lines
3.3 KiB

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import json
import requests
from ..HueLight import HueLight
from ..HueUtils import make_request
class HueGroup:
class Action:
def __init__(self, data_slice: dict):
keys = data_slice.keys()
self.m_on: bool = data_slice['on']
self.m_bri: int = data_slice['bri']
self.m_alert: str = data_slice['alert']
if 'hue' in keys:
self.m_hue: int = data_slice['hue']
else:
self.m_hue = 0
if 'sat' in keys:
self.m_sat: int = data_slice['sat']
else:
self.m_sat = 0
if 'effect' in keys:
self.m_effect: str = data_slice['effect']
else:
self.m_effect = str()
if 'xy' in keys:
self.m_xy: list[float] = data_slice['xy']
else:
self.m_xy = list()
if 'ct' in keys:
self.m_ct: int = data_slice['ct']
else:
self.m_ct = 0
if 'colormode' in keys:
self.m_colormode: str = data_slice['colormode']
else:
self.m_colormode = str()
class State:
def __init__(self, data_slice: dict):
self.m_all_on: bool = data_slice['all_on']
self.m_any_on: bool = data_slice['any_on']
def is_all_on(self) -> bool:
return self.m_all_on
def is_any_on(self) -> bool:
return self.m_any_on
def __init__(self, id: int, lights: list[HueLight], data: dict, parent_bridge_ip: str, parent_bridge_user: str):
self.m_id: int = id
self.m_parent_bridge_ip = parent_bridge_ip
self.m_parent_bridge_user = parent_bridge_user
self.m_name: str = data['name']
self.m_lights = lights
self.m_sensors: list[str] = data['sensors']
self.m_type: str = data['type']
self.m_state: HueGroup.State = HueGroup.State(data['state'])
self.m_recycle: bool = data['recycle']
if 'class' in data.keys():
self.m_class: str = data['class']
else:
self.m_class: str = str()
self.m_action: HueGroup.Action(data['action'])
def __str__(self):
return self.m_name
def get_id(self) -> int:
return self.m_id
def is_all_on(self) -> bool:
return self.m_state.is_all_on()
def is_any_on(self) -> bool:
return self.m_state.is_any_on()
def toggle(self):
for light in self.m_lights:
light.toggle()
self.update_state()
def turn_off(self):
state: str = '{"on": false}'
self.set_state(state)
def turn_on(self):
state: str = '{"on": true}'
self.set_state(state)
def set_state(self, state: str) -> requests.Response:
path: str = "{}/groups/{}/action".format(self.m_parent_bridge_user, self.m_id)
method: str = "PUT"
response = make_request(self.m_parent_bridge_ip, path, method, state)
self.update_state()
return response
def update_state(self):
path: str = "{}/groups/{}".format(self.m_parent_bridge_user, self.m_id)
response = make_request(self.m_parent_bridge_ip, path)
self.m_state = HueGroup.State(json.loads(response.text)['state'])