Skip to content

Commit

Permalink
Fix PWM Outputs not recording values of 0 to database, add ability to…
Browse files Browse the repository at this point in the history
… edit Input and Input Measurement unique_id
  • Loading branch information
kizniche committed Sep 9, 2024
1 parent d9be541 commit c32caaa
Show file tree
Hide file tree
Showing 12 changed files with 53 additions and 5 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ This release changes the install directory from ~/Mycodo to /opt/Mycodo. This ne
- Fix displaying Tags on Highcharts Widget
- Fix dependencies of Mijia LYWSD03MMC
- Fix Restart Frontend returning an error
- Fix PWM Outputs not recording values of 0 to database

### Features

Expand All @@ -36,6 +37,7 @@ This release changes the install directory from ~/Mycodo to /opt/Mycodo. This ne
- Add controller_restart as client endpoint
- Add option for custom CSS
- Add options for changing title and brand text
- Add ability to edit Input and Input Measurement unique_id

### Miscellaneous

Expand Down
3 changes: 3 additions & 0 deletions mycodo/config_translations.py
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,9 @@
'uart_location': {
'title': lazy_gettext('UART Device'),
'phrase': lazy_gettext('The UART device location (e.g. /dev/ttyUSB1)')},
'unique_id': {
'title': lazy_gettext('Unique ID'),
'phrase': lazy_gettext('A unique ID to distinguish this from others')},
'use_pylint': {
'title': lazy_gettext('Analyze Python with Pylint'),
'phrase': lazy_gettext('Analyze the Python code with pylint and return the output')},
Expand Down
4 changes: 4 additions & 0 deletions mycodo/mycodo_flask/forms/forms_input.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,10 @@ class InputMod(FlaskForm):
TRANSLATIONS['name']['title'],
validators=[DataRequired()]
)
unique_id = StringField(
TRANSLATIONS['unique_id']['title'],
validators=[DataRequired()]
)
period = DecimalField(
TRANSLATIONS['period']['title'],
validators=[DataRequired(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,13 @@ <h5>{{_('Options')}}</h5>
</div>
</div>

<div class="col-auto">
{{form_mod_input.unique_id.label(class_='control-label')}}
<div>
{{form_mod_input.unique_id(class_='form-control', value=each_input.unique_id, **{'title': dict_translation['unique_id']['phrase']})}}
</div>
</div>

{% if each_input.device != 'input_spacer' %}

<div class="col-auto">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,13 @@ <h5>Configure Measurements</h5>

{% endif %}

<div class="col-auto">
<label class="control-label" for="measurement_id_{{each_measurement.unique_id}}">{{dict_translation['name']['title']}}</label>
<div>
<input class="form-control" id="measurement_id_{{each_measurement.unique_id}}" name="measurement_id_{{each_measurement.unique_id}}" title="{{dict_translation['unique_id']['phrase']}}" type="text" value="{{each_measurement.unique_id}}">
</div>
</div>

<div class="col-auto">
<label class="control-label" for="measurement_convert_name_{{each_measurement.unique_id}}">{{dict_translation['name']['title']}}</label>
<div>
Expand Down
11 changes: 11 additions & 0 deletions mycodo/mycodo_flask/utils/utils_input.py
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,17 @@ def input_mod(form_mod, request_form):
mod_input.name = form_mod.name.data
messages["name"] = form_mod.name.data

if mod_input.unique_id != form_mod.unique_id.data:
test_unique_id = Input.query.filter(Input.unique_id == form_mod.unique_id.data).first()
if test_unique_id:
messages["error"].append(
f"Input ID must be unique. "
f"ID already exists: '{form_mod.unique_id.data}'")
elif not form_mod.unique_id.data:
messages["error"].append(f"Input ID is required")
else:
mod_input.unique_id = form_mod.unique_id.data

if form_mod.location.data:
mod_input.location = form_mod.location.data
if form_mod.i2c_location.data:
Expand Down
14 changes: 14 additions & 0 deletions mycodo/mycodo_flask/utils/utils_measurement.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,20 @@ def measurement_mod_form(messages, page_refresh, form):
f"{gettext('Deactivate controller before modifying its settings')}: {mod_device.name}")
break

if ("measurement_id_{}".format(each_meas_id) in form and
form["measurement_id_{}".format(each_meas_id)] and
mod_meas.unique_id != form["measurement_id_{}".format(each_meas_id)]):
test_meas_id = DeviceMeasurements.query.filter(
DeviceMeasurements.unique_id == form["measurement_id_{}".format(each_meas_id)]).first()
if test_meas_id:
messages["error"].append(
f"Measurement ID must be unique. "
f"ID already exists: '{form['measurement_id_{}'.format(each_meas_id)]}'")
elif not form["measurement_id_{}".format(each_meas_id)]:
messages["error"].append(f"Measurement ID is required")
else:
mod_meas.unique_id = form["measurement_id_{}".format(each_meas_id)]

if ("measurement_meas_name_{}".format(each_meas_id) in form and
form["measurement_meas_name_{}".format(each_meas_id)]):
mod_meas.name = form["measurement_meas_name_{}".format(each_meas_id)]
Expand Down
2 changes: 1 addition & 1 deletion mycodo/outputs/pwm_gpio.py
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ def output_switch(self, state, output_type=None, amount=None, output_channel=Non
if self.options_channels['pwm_invert_stored_signal'][0]:
amount = 100.0 - abs(amount)

measure_dict[0]['value'] = amount
measure_dict[0]['value'] = float(amount)
add_measurements_influxdb(self.unique_id, measure_dict)

return "success"
Expand Down
2 changes: 1 addition & 1 deletion mycodo/outputs/pwm_mqtt.py
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ def output_switch(self, state, output_type=None, amount=None, output_channel=0):

self.output_states[output_channel] = amount

measure_dict[0]['value'] = amount
measure_dict[0]['value'] = float(amount)
add_measurements_influxdb(self.unique_id, measure_dict)

return "success"
Expand Down
2 changes: 1 addition & 1 deletion mycodo/outputs/pwm_pca9685.py
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ def output_switch(self, state, output_type=None, amount=0, output_channel=None):
if self.options_channels['pwm_invert_stored_signal'][output_channel]:
amount = 100.0 - abs(amount)

measure_dict[output_channel]['value'] = amount
measure_dict[output_channel]['value'] = float(amount)
add_measurements_influxdb(self.unique_id, measure_dict)

return "success"
Expand Down
2 changes: 1 addition & 1 deletion mycodo/outputs/pwm_python.py
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,7 @@ def output_switch(self, state, output_type=None, amount=None, output_channel=Non
if self.options_channels['pwm_invert_stored_signal'][0]:
amount = 100.0 - abs(amount)

measure_dict[0]['value'] = amount
measure_dict[0]['value'] = float(amount)
add_measurements_influxdb(self.unique_id, measure_dict)

def is_on(self, output_channel=None):
Expand Down
2 changes: 1 addition & 1 deletion mycodo/outputs/pwm_shell.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ def output_switch(self, state, output_type=None, amount=None, output_channel=Non
if self.options_channels['pwm_invert_stored_signal'][0]:
amount = 100.0 - abs(amount)

measure_dict[0]['value'] = amount
measure_dict[0]['value'] = float(amount)
add_measurements_influxdb(self.unique_id, measure_dict)

def is_on(self, output_channel=None):
Expand Down

0 comments on commit c32caaa

Please sign in to comment.