ThalamOS
a powerful Flask web application designed to enhance your storage management.
Loading...
Searching...
No Matches
wled_requests.py
Go to the documentation of this file.
1"""
2This module provides functions to interact with a WLED device via its JSON API.
3It allows for turning off lights, setting colors at specific positions, loading default states,
4changing power states, and retrieving the current power state of the WLED device.
5
6Functions:
7 turn_off_lights(): Sends a request to turn off the lights by setting the segment color to black.
8 color_pos(pos): Sends a request to set the color of a specific position on a WLED device.
9 load_default_state(): Sends a POST request to the WLED API to load the default state.
10 change_power_state(state): Change the power state of the device.
11 get_power_state(): Retrieves the power state of a WLED device.
12"""
13from typing import Annotated
14import json
15import os
16
17import requests
18from dotenv import load_dotenv
19
20from logger_config import logger
21
22
23ENV_PATH: Annotated[str, "path to environment variables"] = os.path.join(
24 os.path.dirname(__file__), "data/.env"
25)
26load_dotenv(dotenv_path=ENV_PATH)
27
28WLED_HOST: Annotated[str, "environment variable for WLED address"] = os.getenv(
29 "WLED_HOST"
30)
31logger.info(f"WLED host is: {WLED_HOST}")
32
33API: Annotated[str, "URL to WLED"] = f"http://{WLED_HOST}/json"
34
36 """
37 Retrieves the power state of a WLED device.
38 Sends a GET request to the WLED device's JSON API endpoint to fetch the current state
39 Returns:
40 bool: True if the WLED device is on, False otherwise. Returns None if the device is unreachable.
41
42 """
43 try:
44 response = requests.get(f"http://{WLED_HOST}/json/state", timeout=3) # Timeout verkürzen
45 response.raise_for_status()
46 data = response.json()
47 return data.get("on", False)
48 except (requests.exceptions.RequestException, TimeoutError) as e:
49 logger.error(f"WLED nicht erreichbar: {e}")
50 return None
51
52def turn_off_lights() -> None:
53 """
54 Sends a request to turn off the lights by setting the segment color to black.
55 This function creates a JSON payload to set the color of a segment of lights to black (hex code "000000")
56 and sends a POST request to the specified API endpoint to turn off the lights.
57 Raises:
58 requests.exceptions.RequestException: If there is an issue with the HTTP request.
59 """
60 req = json.dumps({"seg": {"i": [0, 50, "000000"]}})
61 requests.post(API, req, timeout=10)
62
63
64def color_pos(pos: int) -> None:
65 """
66 Sends a request to set the color of a specific position on a WLED device.
67 Args:
68 pos (int): The position to set the color for.
69 Returns:
70 None
71 """
72 req = json.dumps({"seg": {"i": [pos, "FF0000"]}})
73 requests.post(API, req, timeout=10)
74
75
76def load_default_state() -> None:
77 """
78 Sends a POST request to the WLED API to load the default state.
79 This function creates a JSON payload with a preset state identifier and sends it to the WLED API endpoint using a POST request.
80 Raises:
81 requests.exceptions.RequestException: If there is an issue with the HTTP request.
82 """
83 req = json.dumps({"ps": 1})
84 requests.post(API, req, timeout=10)
85
86
87def change_power_state(state) -> None:
88 """
89 Change the power state of the device.
90 Args:
91 state (bool): The desired power state. True to turn on, False to turn off.
92 Returns:
93 None
94 """
95 req = json.dumps({"on": state})
96 requests.post(API, req, timeout=10)
None load_default_state()
None change_power_state(state)
None turn_off_lights()
None color_pos(int pos)