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

48 lines
1.4 KiB

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import os
import requests
from ..UserOrError import UserOrError
def connect(ipaddress: str) -> UserOrError:
user_or_error: UserOrError = UserOrError()
body: str = '{{"devicetype":"{0}"}}'.format("tinge")
path: str = ""
method: str = "POST"
response: requests.Response = make_request(ipaddress, path, method, body)
data: dict = response.json()[0]
if 'error' in data.keys():
user_or_error.set_error(data['error']['type'])
elif 'success' in data.keys():
user_or_error.set_user(data['success']['username'])
else:
user_or_error.set_error(user_or_error.UNKNOWNERROR)
return user_or_error
def is_valid_config(filename: str) -> bool:
return os.path.exists(filename) and os.path.getsize(filename) > 0
def make_request(ipaddress: str, path: str, method: str = "GET",
body: str = '') -> requests.Response:
rfunct = requests.get
url = "http://{}/api/{}".format(ipaddress, path)
if method == "PUT":
rfunct = requests.put
elif method == "POST":
rfunct = requests.post
elif method == "DELETE":
rfunct = requests.delete
if body and method == "GET":
response = rfunct(url, params=body)
elif body:
response = rfunct(url, data=body)
else:
response = rfunct(url)
return response