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/HueBridge/__init__.py

92 lines
3.6 KiB

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import json
from ..HueGroup import HueGroup
from ..HueLight import HueLight
from ..HueUtils import make_request
class HueBridge:
def __init__(self, ipaddress: str, username: str):
self.m_ipaddress: str = ipaddress
self.m_username: str = username
self.m_lights: list[HueLight] = self.discover_lights()
self.m_groups: list[HueGroup] = self.discover_groups()
def discover_groups(self) -> list[HueGroup]:
path: str = "{}/groups".format(self.m_username)
response = make_request(self.m_ipaddress, path)
groups: list[HueGroup] = list()
for key, value in json.loads(response.text).items():
lights: list[HueLight] = list()
for light in value['lights']:
lights.append(self.get_light_by_id(int(light)))
groups.append(HueGroup(int(key), lights, value, self.get_ipaddress(), self.get_user()))
return groups
def create_group(self, lights: list[HueLight], name: str, group_type: str = "LightGroup",
group_class: str = "Other") -> bool:
path = "{}/groups".format(self.get_user())
method = "POST"
data: dict = {'lights': [], 'name': name, 'type': group_type, 'class': group_class}
for light in lights:
data['lights'].append(str(light.get_id()))
response = make_request(self.get_ipaddress(), path, method, json.dumps(data))
r_json = response.json()
if 'success' in r_json.keys():
new_id = r_json['success']['id']
new_path = "{}/groups/{}".format(self.get_user(), new_id)
new_group = make_request(self.get_ipaddress(), new_path).json()
self.m_groups.append(HueGroup(int(new_id), lights, new_group, self.get_ipaddress(), self.get_user()))
return True
else:
return False
def discover_lights(self) -> list[HueLight]:
path: str = "{}/lights".format(self.m_username)
response = make_request(self.m_ipaddress, path)
lights: list[HueLight] = list()
for key, value in json.loads(response.text).items():
lights.append(HueLight(int(key), value, self.get_ipaddress(), self.get_user()))
return lights
def discover_new_lights(self, device_id_list=None) -> bool:
body: str = ""
if device_id_list is not None:
body_dict = {'deviceid': []}
for device in device_id_list:
body_dict['deviceid'].append(device)
body = json.dumps(body_dict)
path: str = "{}/lights".format(self.m_username)
method: str = "POST"
response = make_request(self.m_ipaddress, path, method, body)
return 'success' in response.json()[0].keys()
def append_new_lights(self) -> bool:
path: str = "{}/lights/new".format(self.m_username)
response = make_request(self.m_ipaddress, path)
for key, value in json.loads(response.text).items():
if key != 'lastscan':
path: str = "{}/lights/{}".format(self.m_username, key)
response = make_request(self.m_ipaddress, path)
self.m_lights.append(HueLight(int(key), response.json(), self.get_ipaddress(), self.get_user()))
return response.ok
def get_groups(self):
return self.m_groups
def get_lights(self):
return self.m_lights
def get_light_by_id(self, id: int) -> HueLight:
for light in self.m_lights:
if light.get_id() == id:
return light
def get_ipaddress(self):
return self.m_ipaddress
def get_user(self):
return self.m_username