Refactor screens into new classes and allow changing light brightness
This commit is contained in:
40
api.py
40
api.py
@@ -15,8 +15,11 @@ class Api:
|
||||
"content-type": "application/json"
|
||||
}
|
||||
print("Starting request for " + endpoint)
|
||||
response = get(url, headers=headers)
|
||||
return response.json()
|
||||
try:
|
||||
response = get(url, headers=headers)
|
||||
return response.json()
|
||||
except:
|
||||
return {}
|
||||
|
||||
def __post(self, endpoint, d) -> bool:
|
||||
gc.collect()
|
||||
@@ -26,23 +29,26 @@ class Api:
|
||||
"content-type": "application/json"
|
||||
}
|
||||
print("Starting post request to " + endpoint)
|
||||
response = post(url, headers=headers, json=d)
|
||||
return response.status_code == 200
|
||||
try:
|
||||
response = post(url, headers=headers, json=d)
|
||||
return response.status_code == 200
|
||||
except:
|
||||
return False
|
||||
|
||||
def getLightData(self, entity_id: str) -> dict:
|
||||
# TODO: error handling if can't access hass (e.g. no connection, invalid token)
|
||||
response = self.__request("/api/states/" + entity_id)
|
||||
on = response["state"] == "on"
|
||||
on = "state" in response and response["state"] == "on"
|
||||
return {
|
||||
"on": on,
|
||||
"rgb_color": response["attributes"]["rgb_color"] if on else None,
|
||||
"rgbw_color": response["attributes"]["rgbw_color"] if on else None,
|
||||
"brightness": response["attributes"]["brightness"] if on else 0,
|
||||
"friendly_name": response["attributes"]["friendly_name"]
|
||||
"brightness": response["attributes"]["brightness"] if on else 0.0
|
||||
}
|
||||
|
||||
def getMediaPlayerData(self, entity_id: str) -> dict:
|
||||
response = self.__request("/api/states/" + entity_id)
|
||||
e = "state" in response and "attributes" in response
|
||||
if ("media_position" in (dict)(response["attributes"])):
|
||||
p = response["attributes"]["media_position"]
|
||||
else:
|
||||
@@ -53,16 +59,15 @@ class Api:
|
||||
p += time.mktime(time.localtime()) - t
|
||||
return {
|
||||
"playing": response["state"] in ["on", "playing", "buffering"],
|
||||
"shuffle": response["attributes"]["shuffle"],
|
||||
"repeat": response["attributes"]["repeat"] == "on",
|
||||
"volume_level": response["attributes"]["volume_level"],
|
||||
"friendly_name": response["attributes"]["friendly_name"],
|
||||
"entity_picture": response["attributes"]["entity_picture"] if "entity_picture" in (dict)(response["attributes"]) else None,
|
||||
"media_duration": response["attributes"]["media_duration"] if "media_duration" in (dict)(response["attributes"]) else 0,
|
||||
"shuffle": response["attributes"]["shuffle"] if e and "shuffle" in response["attributes"] else False,
|
||||
"repeat": response["attributes"]["repeat"] == "on" if e and "repeat" in response["attributes"] else False,
|
||||
"volume_level": response["attributes"]["volume_level"] if e and "volume_level" in response["attributes"] else 0,
|
||||
"entity_picture": response["attributes"]["entity_picture"] if e and "entity_picture" in (dict)(response["attributes"]) else None,
|
||||
"media_duration": response["attributes"]["media_duration"] if e and "media_duration" in (dict)(response["attributes"]) else 0,
|
||||
"media_position": p,
|
||||
"media_title": response["attributes"]["media_title"] if "media_title" in (dict)(response["attributes"]) else "Nothing playing",
|
||||
"media_artist": response["attributes"]["media_artist"] if "media_artist" in (dict)(response["attributes"]) else "",
|
||||
"media_album_name": response["attributes"]["media_album_name"] if "media_album_name" in (dict)(response["attributes"]) else ""
|
||||
"media_title": response["attributes"]["media_title"] if e and "media_title" in (dict)(response["attributes"]) else "Nothing playing",
|
||||
"media_artist": response["attributes"]["media_artist"] if e and "media_artist" in (dict)(response["attributes"]) else "",
|
||||
"media_album_name": response["attributes"]["media_album_name"] if e and "media_album_name" in (dict)(response["attributes"]) else ""
|
||||
}
|
||||
|
||||
def changeVolume(self, entity_id: str, up: bool = True) -> None:
|
||||
@@ -82,3 +87,6 @@ class Api:
|
||||
|
||||
def toggleLight(self, entity_id: str) -> None:
|
||||
self.__post("/api/services/light/toggle", {"entity_id": entity_id})
|
||||
|
||||
def setBrightness(self, entity_id: str, v: int) -> None:
|
||||
self.__post("/api/services/light/turn_on", {"entity_id": entity_id, "brightness": v})
|
||||
|
||||
Reference in New Issue
Block a user