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.
111 lines
4.6 KiB
111 lines
4.6 KiB
#!/usr/bin/env python3
|
|
# -*- coding: utf-8 -*-
|
|
import os
|
|
from typing import Union
|
|
|
|
import simplejson
|
|
import toml
|
|
from upnpy import UPnP
|
|
|
|
from .HueBridge import HueBridge
|
|
from .HueUtils import connect, is_valid_config, make_request
|
|
from .UserOrError import UserOrError
|
|
|
|
|
|
class Tinge:
|
|
"""The class that keeps track of bridges and config
|
|
"""
|
|
|
|
def __init__(self):
|
|
"""Constructor
|
|
"""
|
|
self.m_bridges: list[HueBridge] = list()
|
|
self.m_discovered: list[str] = list()
|
|
self.m_config = os.path.join(os.environ['HOME'], ".config/tinge/config")
|
|
self.create_confdir()
|
|
self.read_bridges_from_file()
|
|
|
|
def append_bridge(self, bridge: HueBridge):
|
|
self.m_bridges.append(bridge)
|
|
self.m_discovered.append(bridge.get_ipaddress())
|
|
|
|
def create_confdir(self):
|
|
"""Create the config dir if it does not allready exist
|
|
"""
|
|
if not os.path.exists(os.path.dirname(self.m_config)):
|
|
os.makedirs(os.path.dirname(self.m_config))
|
|
|
|
def discover_new_bridges(self) -> Union[None, list[dict]]:
|
|
"""Use UPnP to discover bridges on the current network
|
|
"""
|
|
upnp: UPnP = UPnP()
|
|
discovered_devices = upnp.discover()
|
|
discovered_bridges: list[dict] = list()
|
|
seen_ips: list[str] = list()
|
|
if not discovered_devices:
|
|
print("No devices discovered at this time")
|
|
return None
|
|
for device in discovered_devices:
|
|
discovered: bool = False
|
|
if (device.host not in self.m_discovered) and (device.host not in seen_ips):
|
|
seen_ips.append(device.host)
|
|
# Let's check if the device has the default name, if so we assume it's a hue bridge
|
|
if device.get_friendly_name().startswith("Philips hue"):
|
|
discovered = True
|
|
# If not we try to do a request against the api and see if we get an answer we can understand
|
|
else:
|
|
try:
|
|
response = make_request(device.host, "1234/lights")
|
|
if response:
|
|
resp = response.json()[0]
|
|
if 'error' in resp.keys():
|
|
m_keys = resp['error'].keys()
|
|
if 'description' in m_keys and 'address' in m_keys and 'type' in m_keys:
|
|
# This is kinda ugly but line is too long with and statement
|
|
if resp['error']['description'] == "unauthorized user":
|
|
if resp['error']['address'] == "/lights":
|
|
if resp['error']['type'] == 1:
|
|
discovered = True
|
|
except simplejson.errors.JSONDecodeError:
|
|
pass
|
|
|
|
if discovered:
|
|
bridge = {'ipaddress': device.host, 'name': device.get_friendly_name()}
|
|
if bridge not in discovered_bridges:
|
|
discovered_bridges.append(bridge)
|
|
return discovered_bridges
|
|
|
|
def get_bridges(self) -> list[HueBridge]:
|
|
"""Get the bridges
|
|
|
|
Returns:
|
|
list[HueBridge]: The bridges we keep track off
|
|
"""
|
|
return self.m_bridges
|
|
|
|
def read_bridges_from_file(self):
|
|
"""Read config file and add back previously discovered bridges
|
|
"""
|
|
if is_valid_config(self.m_config):
|
|
with open(self.m_config, 'r') as configfile:
|
|
mbridges = toml.loads(configfile.read())
|
|
for key, value in mbridges.items():
|
|
if key not in self.m_discovered:
|
|
response = make_request(key, "{}/".format(value['user']))
|
|
if response:
|
|
name = "{} ({})".format(response.json()['config']['name'], key)
|
|
bridge: HueBridge = HueBridge(key, value['user'], name)
|
|
self.m_bridges.append(bridge)
|
|
self.m_discovered.append(key)
|
|
else:
|
|
bridge: HueBridge = HueBridge(key, value['user'], is_reachable=False)
|
|
self.m_bridges.append(bridge)
|
|
self.m_discovered.append(key)
|
|
|
|
def write_all_bridges_to_conf(self):
|
|
"""Save to file
|
|
"""
|
|
with open(self.m_config, 'w') as configfile:
|
|
for bridge in self.m_bridges:
|
|
configfile.write('["{}"]\nuser = "{}"\n'.format(bridge.get_ipaddress(), bridge.get_user()))
|