Created
January 18, 2024 09:01
-
-
Save bugmancx/0cbe4e78d5a44463c504689924064221 to your computer and use it in GitHub Desktop.
PowerView Hub Blind State
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
import base64 | |
# URL of the PowerView Hub API | |
hub_url = "http://10.0.27.26/api/shades" | |
def fetch_shade_data(url): | |
try: | |
response = requests.get(url) | |
response.raise_for_status() # Raises an HTTPError if the response was an error | |
return response.json() | |
except requests.RequestException as e: | |
print(f"Error fetching data from PowerView Hub: {e}") | |
return None | |
def decode_base64(data): | |
"""Decode base64, padding being optional.""" | |
missing_padding = len(data) % 4 | |
if missing_padding: | |
data += '='* (4 - missing_padding) | |
return base64.b64decode(data).decode('utf-8') | |
def interpret_position(shade): | |
"""Interpret the blind position as a percentage.""" | |
if "positions" not in shade or "position1" not in shade["positions"]: | |
return "Unknown" | |
position = shade["positions"]["position1"] | |
if position == 65535: | |
return "100% Open" | |
elif position == 0: | |
return "Closed" | |
else: | |
# Assuming position 65535 is fully open and 0 is closed, calculate percentage | |
percentage_open = int((position / 65535) * 100) | |
return f"{percentage_open}% Open" | |
def print_shade_summary(shade_data): | |
if not shade_data or "shadeData" not in shade_data: | |
print("No shade data available") | |
return | |
# Sort the shades by their decoded names | |
sorted_shades = sorted(shade_data["shadeData"], key=lambda x: decode_base64(x["name"])) | |
# Formatting the output | |
print(f"{'ID':<10} {'Name':<20} {'Position':<20} {'Battery Strength':<17} {'Battery Status':<15}") | |
print("-" * 82) # Print a line for clarity | |
for shade in sorted_shades: | |
name = decode_base64(shade["name"]) | |
position = interpret_position(shade) | |
print(f"{shade['id']:<10} {name:<20} {position:<20} {shade['batteryStrength']:<17} {shade['batteryStatus']:<15}") | |
# Fetch and print the shade data summary | |
shade_data = fetch_shade_data(hub_url) | |
print_shade_summary(shade_data) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment