Skip to content

Commit

Permalink
Update InfluxDB 2.x to 2.7.3, Update Neopixel Change Color Action to …
Browse files Browse the repository at this point in the history
…work with Neopixel Function, Add Actions: Neopixel Flashing On and Neopixel Flashing Off (#1353)
  • Loading branch information
kizniche committed Dec 12, 2023
1 parent 674e03a commit 75a5d8f
Show file tree
Hide file tree
Showing 6 changed files with 445 additions and 43 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@
- Add ability to use Actions in Custom Functions
- Add Input Action: Execute Python 3 Code ([#1334](https://github.com/kizniche/Mycodo/issues/1334))
- Add Function: Adafruit Neokey (Key Press Executes Actions) ([#1353](https://github.com/kizniche/Mycodo/issues/1353))
- Add Action: Neopixel Flashing On
- Add Action: Neopixel Flashing Off
- Change deprecated threading.currentThread to threading.current_thread
- Update InfluxDB 2.x to 2.7.3

### Bugfixes

Expand Down
65 changes: 49 additions & 16 deletions mycodo/actions/led_neopixel_change_color.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@

from mycodo.actions.base_action import AbstractFunctionAction
from mycodo.databases.models import Actions
from mycodo.databases.models import CustomController
from mycodo.databases.models import Output
from mycodo.utils.database import db_retrieve_table_daemon
from mycodo.utils.functions import parse_function_information

ACTION_INFORMATION = {
'name_unique': 'action_led_neopixel_change_color',
'name': f"LED: Neopixel RGB Strip: Change Color",
'name': f"LED: Neopixel: Change Pixel Color",
'library': None,
'manufacturer': 'Mycodo',
'application': ['functions'],
Expand All @@ -20,21 +22,22 @@
'url_product_purchase': None,
'url_additional': None,

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

'usage': 'Executing <strong>self.run_action("ACTION_ID")</strong> will set the selected LED to the selected Color. '
'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.',
'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.',

'custom_options': [
{
'id': 'controller',
'type': 'select_device',
'default_value': '',
'options_select': [
'Function',
'Output'
],
'name': lazy_gettext('Controller'),
'phrase': 'Select the energy meter Input'
'phrase': 'Select the controller that modulates your neopixels'
},
{
'id': 'led_number',
Expand All @@ -50,7 +53,7 @@
'default_value': '10, 0, 0',
'required': True,
'name': 'RGB Color',
'phrase': 'The color in RGB format, each from 0 to 255 (e.g "10, 0 0")'
'phrase': 'The color in RGB format, each from 0 to 255 (e.g "10, 0, 0")'
},
]
}
Expand All @@ -77,20 +80,29 @@ def initialize(self):
self.action_setup = True

def run_action(self, dict_vars):
controller_id = self.controller_id
led_number = self.led_number
led_color = self.led_color

try:
controller_id = dict_vars["value"]["output_id"]
controller_id = dict_vars["value"]["output_id"] # From previous version of this Action. Keep for backwards-compatibility
except:
controller_id = self.controller_id
pass

try:
controller_id = dict_vars["value"]["controller_id"]
except:
pass

try:
led_number = dict_vars["value"]["led"]
except:
led_number = self.led_number
pass

try:
led_color = dict_vars["value"]["color"]
except:
led_color = self.led_color
pass

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

if not this_output:
msg = f" Error: Output with ID '{controller_id}' not found."
this_function = db_retrieve_table_daemon(
CustomController, unique_id=controller_id, entry='first')

if not this_output and not this_function:
msg = f" Error: Controller with ID '{controller_id}' not found."
dict_vars['message'] += msg
self.logger.error(msg)
return dict_vars
Expand All @@ -121,11 +136,29 @@ def run_action(self, dict_vars):
"led_color": led_color
}

dict_vars['message'] += f" Set color of LED {led_number} to {led_color} for Output {controller_id} ({this_output.name})."
clear_volume = threading.Thread(
target=self.control.module_function,
args=("Output", this_output.unique_id, "set_led", payload,))
clear_volume.start()
if this_output:
dict_vars['message'] += f" Set color of LED {led_number} to {led_color} of Controller with ID {controller_id} ({this_output.name})."

clear_volume = threading.Thread(
target=self.control.module_function,
args=("Output", this_output.unique_id, "set_led", payload,))
clear_volume.start()

elif this_function:
functions = parse_function_information()
if this_function.device in functions and "function_actions" in functions[this_function.device]:
if "neopixel_set_color" not in functions[this_function.device]["function_actions"]:
msg = " Selected neopixel Function is not capable of setting an LED to a color"
dict_vars['message'] += msg
self.logger.error(msg)
return dict_vars

dict_vars['message'] += f" Set color of LED {led_number} to {led_color} of Controller with ID {controller_id} ({this_function.name})."

start_flashing = threading.Thread(
target=self.control.module_function,
args=("Function", controller_id, "set_color", payload,))
start_flashing.start()

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

Expand Down
113 changes: 113 additions & 0 deletions mycodo/actions/led_neopixel_flash_off.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
# coding=utf-8
import threading

from flask_babel import lazy_gettext

from mycodo.actions.base_action import AbstractFunctionAction
from mycodo.databases.models import Actions
from mycodo.databases.models import CustomController
from mycodo.utils.database import db_retrieve_table_daemon
from mycodo.utils.functions import parse_function_information

ACTION_INFORMATION = {
'name_unique': 'action_led_neopixel_flash_off',
'name': "LED: Neopixel: {} {}".format(lazy_gettext('Flashing'), lazy_gettext('Off')),
'library': None,
'manufacturer': 'Mycodo',
'application': ['functions'],

'url_manufacturer': None,
'url_datasheet': None,
'url_product_purchase': None,
'url_additional': None,

'message': 'Stop flashing an LED in a Neopixel LED strip. Select the Neopixel LED Strip Controller and pixel number.',

'usage': 'Executing <strong>self.run_action("ACTION_ID")</strong> will set the selected LED to the selected Color. '
'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.',

'custom_options': [
{
'id': 'controller',
'type': 'select_device',
'default_value': '',
'options_select': [
'Function'
],
'name': lazy_gettext('Controller'),
'phrase': 'Select the controller that modulates your neopixels'
},
{
'id': 'led_number',
'type': 'integer',
'default_value': 0,
'required': True,
'name': 'LED Position',
'phrase': 'The position of the LED on the strip'
}
]
}


class ActionModule(AbstractFunctionAction):
"""Function Action: Clear Total kWh."""
def __init__(self, action_dev, testing=False):
super().__init__(action_dev, testing=testing, name=__name__)

self.controller_id = None
self.led_number = None

action = db_retrieve_table_daemon(
Actions, unique_id=self.unique_id)
self.setup_custom_options(
ACTION_INFORMATION['custom_options'], action)

if not testing:
self.try_initialize()

def initialize(self):
self.action_setup = True

def run_action(self, dict_vars):
controller_id = self.controller_id
led_number = self.led_number

try:
controller_id = dict_vars["value"]["controller_id"]
except:
pass

try:
led_number = dict_vars["value"]["led"]
except:
pass

this_function = db_retrieve_table_daemon(
CustomController, unique_id=controller_id, entry='first')

payload = {
"led_number": led_number
}

if this_function:
functions = parse_function_information()
if this_function.device in functions and "function_actions" in functions[this_function.device]:
if "neopixel_start_flashing" not in functions[this_function.device]["function_actions"]:
msg = " Selected neopixel Function is not capable of setting an LED to a color"
dict_vars['message'] += msg
self.logger.error(msg)
return dict_vars

dict_vars['message'] += f"Stop flashing the LED {led_number} of Controller with ID {controller_id} ({this_function.name})."

start_flashing = threading.Thread(
target=self.control.module_function,
args=("Function", controller_id, "flashing_off", payload,))
start_flashing.start()

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

return dict_vars

def is_setup(self):
return self.action_setup
Loading

0 comments on commit 75a5d8f

Please sign in to comment.