ThalamOS
a powerful Flask web application designed to enhance your storage management.
Loading...
Searching...
No Matches
wled_requests Namespace Reference

Functions

 get_power_state ()
 
None turn_off_lights ()
 
None color_pos (int pos)
 
None load_default_state ()
 
None change_power_state (state)
 

Variables

Annotated ENV_PATH
 
 dotenv_path
 
Annotated WLED_HOST
 
Annotated API = f"http://{WLED_HOST}/json"
 

Detailed Description

This module provides functions to interact with a WLED device via its JSON API.
It allows for turning off lights, setting colors at specific positions, loading default states,
changing power states, and retrieving the current power state of the WLED device.

Functions:
    turn_off_lights(): Sends a request to turn off the lights by setting the segment color to black.
    color_pos(pos): Sends a request to set the color of a specific position on a WLED device.
    load_default_state(): Sends a POST request to the WLED API to load the default state.
    change_power_state(state): Change the power state of the device.
    get_power_state(): Retrieves the power state of a WLED device.

Function Documentation

◆ change_power_state()

None wled_requests.change_power_state ( state)
Change the power state of the device.
Args:
    state (bool): The desired power state. True to turn on, False to turn off.
Returns:
    None

Definition at line 87 of file wled_requests.py.

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)

◆ color_pos()

None wled_requests.color_pos ( int pos)
Sends a request to set the  color of a specific position on a WLED device.
Args:
    pos (int): The position to set the color for.
Returns:
    None

Definition at line 64 of file wled_requests.py.

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

◆ get_power_state()

wled_requests.get_power_state ( )
Retrieves the power state of a WLED device.
Sends a GET request to the WLED device's JSON API endpoint to fetch the current state
Returns:
    bool: True if the WLED device is on, False otherwise. Returns None if the device is unreachable.

Definition at line 35 of file wled_requests.py.

35def get_power_state():
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

◆ load_default_state()

None wled_requests.load_default_state ( )
Sends a POST request to the WLED API to load the default state.
This function creates a JSON payload with a preset state identifier and sends it to the WLED API endpoint using a POST request.
Raises:
    requests.exceptions.RequestException: If there is an issue with the HTTP request.

Definition at line 76 of file wled_requests.py.

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

◆ turn_off_lights()

None wled_requests.turn_off_lights ( )
Sends a request to turn off the lights by setting the segment color to black.
This function creates a JSON payload to set the color of a segment of lights to black (hex code "000000")
and sends a POST request to the specified API endpoint to turn off the lights.
Raises:
    requests.exceptions.RequestException: If there is an issue with the HTTP request.

Definition at line 52 of file wled_requests.py.

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

Variable Documentation

◆ API

Annotated wled_requests.API = f"http://{WLED_HOST}/json"

Definition at line 33 of file wled_requests.py.

◆ dotenv_path

wled_requests.dotenv_path

Definition at line 26 of file wled_requests.py.

◆ ENV_PATH

Annotated wled_requests.ENV_PATH
Initial value:
1= os.path.join(
2 os.path.dirname(__file__), "data/.env"
3)

Definition at line 23 of file wled_requests.py.

◆ WLED_HOST

Annotated wled_requests.WLED_HOST
Initial value:
1= os.getenv(
2 "WLED_HOST"
3)

Definition at line 28 of file wled_requests.py.