Skip to content

Commit 75a5d8f

Browse files
committed
Update InfluxDB 2.x to 2.7.3, Update Neopixel Change Color Action to work with Neopixel Function, Add Actions: Neopixel Flashing On and Neopixel Flashing Off (#1353)
1 parent 674e03a commit 75a5d8f

File tree

6 files changed

+445
-43
lines changed

6 files changed

+445
-43
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@
55
- Add ability to use Actions in Custom Functions
66
- Add Input Action: Execute Python 3 Code ([#1334](https://github.com/kizniche/Mycodo/issues/1334))
77
- Add Function: Adafruit Neokey (Key Press Executes Actions) ([#1353](https://github.com/kizniche/Mycodo/issues/1353))
8+
- Add Action: Neopixel Flashing On
9+
- Add Action: Neopixel Flashing Off
810
- Change deprecated threading.currentThread to threading.current_thread
11+
- Update InfluxDB 2.x to 2.7.3
912

1013
### Bugfixes
1114

mycodo/actions/led_neopixel_change_color.py

Lines changed: 49 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@
55

66
from mycodo.actions.base_action import AbstractFunctionAction
77
from mycodo.databases.models import Actions
8+
from mycodo.databases.models import CustomController
89
from mycodo.databases.models import Output
910
from mycodo.utils.database import db_retrieve_table_daemon
11+
from mycodo.utils.functions import parse_function_information
1012

1113
ACTION_INFORMATION = {
1214
'name_unique': 'action_led_neopixel_change_color',
13-
'name': f"LED: Neopixel RGB Strip: Change Color",
15+
'name': f"LED: Neopixel: Change Pixel Color",
1416
'library': None,
1517
'manufacturer': 'Mycodo',
1618
'application': ['functions'],
@@ -20,21 +22,22 @@
2022
'url_product_purchase': None,
2123
'url_additional': None,
2224

23-
'message': 'Change the color of an LED in a Neopixel LED strip. Select the Neopixel LED Strip Output.',
25+
'message': 'Change the color of an LED in a Neopixel LED strip. Select the Neopixel LED Strip Controller, pixel number, and color.',
2426

2527
'usage': 'Executing <strong>self.run_action("ACTION_ID")</strong> will set the selected LED to the selected Color. '
26-
'Executing <strong>self.run_action("ACTION_ID", value={"output_id": "959019d1-c1fa-41fe-a554-7be3366a9c5b", "led": 0, "color": "10, 10, 0"})</strong> will set the color of the specified LED for the Neopixel LED Strip Output with the specified ID. Don\'t forget to change the output_id value to an actual Output ID that exists in your system.',
28+
'Executing <strong>self.run_action("ACTION_ID", value={"controller_id": "959019d1-c1fa-41fe-a554-7be3366a9c5b", "led": 0, "color": "10, 10, 0"})</strong> will set the color of the specified LED for the Neopixel LED Strip Controller with the specified ID. Don\'t forget to change the controller_id value to an actual Controller ID that exists in your system.',
2729

2830
'custom_options': [
2931
{
3032
'id': 'controller',
3133
'type': 'select_device',
3234
'default_value': '',
3335
'options_select': [
36+
'Function',
3437
'Output'
3538
],
3639
'name': lazy_gettext('Controller'),
37-
'phrase': 'Select the energy meter Input'
40+
'phrase': 'Select the controller that modulates your neopixels'
3841
},
3942
{
4043
'id': 'led_number',
@@ -50,7 +53,7 @@
5053
'default_value': '10, 0, 0',
5154
'required': True,
5255
'name': 'RGB Color',
53-
'phrase': 'The color in RGB format, each from 0 to 255 (e.g "10, 0 0")'
56+
'phrase': 'The color in RGB format, each from 0 to 255 (e.g "10, 0, 0")'
5457
},
5558
]
5659
}
@@ -77,20 +80,29 @@ def initialize(self):
7780
self.action_setup = True
7881

7982
def run_action(self, dict_vars):
83+
controller_id = self.controller_id
84+
led_number = self.led_number
85+
led_color = self.led_color
86+
8087
try:
81-
controller_id = dict_vars["value"]["output_id"]
88+
controller_id = dict_vars["value"]["output_id"] # From previous version of this Action. Keep for backwards-compatibility
8289
except:
83-
controller_id = self.controller_id
90+
pass
91+
92+
try:
93+
controller_id = dict_vars["value"]["controller_id"]
94+
except:
95+
pass
8496

8597
try:
8698
led_number = dict_vars["value"]["led"]
8799
except:
88-
led_number = self.led_number
100+
pass
89101

90102
try:
91103
led_color = dict_vars["value"]["color"]
92104
except:
93-
led_color = self.led_color
105+
pass
94106

95107
try:
96108
red = int(led_color.split(',')[0])
@@ -110,8 +122,11 @@ def run_action(self, dict_vars):
110122
this_output = db_retrieve_table_daemon(
111123
Output, unique_id=controller_id, entry='first')
112124

113-
if not this_output:
114-
msg = f" Error: Output with ID '{controller_id}' not found."
125+
this_function = db_retrieve_table_daemon(
126+
CustomController, unique_id=controller_id, entry='first')
127+
128+
if not this_output and not this_function:
129+
msg = f" Error: Controller with ID '{controller_id}' not found."
115130
dict_vars['message'] += msg
116131
self.logger.error(msg)
117132
return dict_vars
@@ -121,11 +136,29 @@ def run_action(self, dict_vars):
121136
"led_color": led_color
122137
}
123138

124-
dict_vars['message'] += f" Set color of LED {led_number} to {led_color} for Output {controller_id} ({this_output.name})."
125-
clear_volume = threading.Thread(
126-
target=self.control.module_function,
127-
args=("Output", this_output.unique_id, "set_led", payload,))
128-
clear_volume.start()
139+
if this_output:
140+
dict_vars['message'] += f" Set color of LED {led_number} to {led_color} of Controller with ID {controller_id} ({this_output.name})."
141+
142+
clear_volume = threading.Thread(
143+
target=self.control.module_function,
144+
args=("Output", this_output.unique_id, "set_led", payload,))
145+
clear_volume.start()
146+
147+
elif this_function:
148+
functions = parse_function_information()
149+
if this_function.device in functions and "function_actions" in functions[this_function.device]:
150+
if "neopixel_set_color" not in functions[this_function.device]["function_actions"]:
151+
msg = " Selected neopixel Function is not capable of setting an LED to a color"
152+
dict_vars['message'] += msg
153+
self.logger.error(msg)
154+
return dict_vars
155+
156+
dict_vars['message'] += f" Set color of LED {led_number} to {led_color} of Controller with ID {controller_id} ({this_function.name})."
157+
158+
start_flashing = threading.Thread(
159+
target=self.control.module_function,
160+
args=("Function", controller_id, "set_color", payload,))
161+
start_flashing.start()
129162

130163
self.logger.debug(f"Message: {dict_vars['message']}")
131164

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
# coding=utf-8
2+
import threading
3+
4+
from flask_babel import lazy_gettext
5+
6+
from mycodo.actions.base_action import AbstractFunctionAction
7+
from mycodo.databases.models import Actions
8+
from mycodo.databases.models import CustomController
9+
from mycodo.utils.database import db_retrieve_table_daemon
10+
from mycodo.utils.functions import parse_function_information
11+
12+
ACTION_INFORMATION = {
13+
'name_unique': 'action_led_neopixel_flash_off',
14+
'name': "LED: Neopixel: {} {}".format(lazy_gettext('Flashing'), lazy_gettext('Off')),
15+
'library': None,
16+
'manufacturer': 'Mycodo',
17+
'application': ['functions'],
18+
19+
'url_manufacturer': None,
20+
'url_datasheet': None,
21+
'url_product_purchase': None,
22+
'url_additional': None,
23+
24+
'message': 'Stop flashing an LED in a Neopixel LED strip. Select the Neopixel LED Strip Controller and pixel number.',
25+
26+
'usage': 'Executing <strong>self.run_action("ACTION_ID")</strong> will set the selected LED to the selected Color. '
27+
'Executing <strong>self.run_action("ACTION_ID", value={"controller_id": "959019d1-c1fa-41fe-a554-7be3366a9c5b", "led": 0})</strong> will stop flashing the specified LED for the Neopixel LED Strip Controller with the specified ID. Don\'t forget to change the controller_id value to an actual Controller ID that exists in your system.',
28+
29+
'custom_options': [
30+
{
31+
'id': 'controller',
32+
'type': 'select_device',
33+
'default_value': '',
34+
'options_select': [
35+
'Function'
36+
],
37+
'name': lazy_gettext('Controller'),
38+
'phrase': 'Select the controller that modulates your neopixels'
39+
},
40+
{
41+
'id': 'led_number',
42+
'type': 'integer',
43+
'default_value': 0,
44+
'required': True,
45+
'name': 'LED Position',
46+
'phrase': 'The position of the LED on the strip'
47+
}
48+
]
49+
}
50+
51+
52+
class ActionModule(AbstractFunctionAction):
53+
"""Function Action: Clear Total kWh."""
54+
def __init__(self, action_dev, testing=False):
55+
super().__init__(action_dev, testing=testing, name=__name__)
56+
57+
self.controller_id = None
58+
self.led_number = None
59+
60+
action = db_retrieve_table_daemon(
61+
Actions, unique_id=self.unique_id)
62+
self.setup_custom_options(
63+
ACTION_INFORMATION['custom_options'], action)
64+
65+
if not testing:
66+
self.try_initialize()
67+
68+
def initialize(self):
69+
self.action_setup = True
70+
71+
def run_action(self, dict_vars):
72+
controller_id = self.controller_id
73+
led_number = self.led_number
74+
75+
try:
76+
controller_id = dict_vars["value"]["controller_id"]
77+
except:
78+
pass
79+
80+
try:
81+
led_number = dict_vars["value"]["led"]
82+
except:
83+
pass
84+
85+
this_function = db_retrieve_table_daemon(
86+
CustomController, unique_id=controller_id, entry='first')
87+
88+
payload = {
89+
"led_number": led_number
90+
}
91+
92+
if this_function:
93+
functions = parse_function_information()
94+
if this_function.device in functions and "function_actions" in functions[this_function.device]:
95+
if "neopixel_start_flashing" not in functions[this_function.device]["function_actions"]:
96+
msg = " Selected neopixel Function is not capable of setting an LED to a color"
97+
dict_vars['message'] += msg
98+
self.logger.error(msg)
99+
return dict_vars
100+
101+
dict_vars['message'] += f"Stop flashing the LED {led_number} of Controller with ID {controller_id} ({this_function.name})."
102+
103+
start_flashing = threading.Thread(
104+
target=self.control.module_function,
105+
args=("Function", controller_id, "flashing_off", payload,))
106+
start_flashing.start()
107+
108+
self.logger.debug(f"Message: {dict_vars['message']}")
109+
110+
return dict_vars
111+
112+
def is_setup(self):
113+
return self.action_setup

0 commit comments

Comments
 (0)