Skip to content

Commit

Permalink
Fixed a bug where the "Register relay 1/2 as light" didn't do anythin…
Browse files Browse the repository at this point in the history
…g. Also fixed a bug where default relay state for relay 1 was applied to relay 2
  • Loading branch information
tpanajott committed Mar 29, 2024
1 parent 358ce86 commit b8d9ff7
Show file tree
Hide file tree
Showing 11 changed files with 26 additions and 23 deletions.
35 changes: 17 additions & 18 deletions docker/MQTTManager/include/nspanel/nspanel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -141,16 +141,15 @@ void NSPanel::update_config(nlohmann::json &init_data) {
} else {
SPDLOG_DEBUG("Loaded NSPanel {} with no ID.", this->_name);
}
if (init_data.contains("relay1_is_light")) {
this->_relay1_is_mqtt_light = init_data["relay1_is_light"];
} else {
this->_relay1_is_mqtt_light = false;
if (this->_relay1_is_mqtt_light != init_data["relay1_is_light"]) {
rebuilt_mqtt = true;
}
if (init_data.contains("relay2_is_light")) {
this->_relay2_is_mqtt_light = init_data["relay2_is_light"];
} else {
this->_relay2_is_mqtt_light = false;
this->_relay1_is_mqtt_light = init_data["relay1_is_light"];

if (this->_relay2_is_mqtt_light != init_data["relay2_is_light"]) {
rebuilt_mqtt = true;
}
this->_relay2_is_mqtt_light = init_data["relay2_is_light"];

if (rebuilt_mqtt) {
this->reset_mqtt_topics();
Expand Down Expand Up @@ -671,9 +670,9 @@ void NSPanel::register_to_home_assistant() {
relay1_data["name"] = "Relay 1";
relay1_data["state_topic"] = this->_mqtt_relay1_state_topic;
relay1_data["command_topic"] = this->_mqtt_relay1_command_topic;
relay1_data["state_template"] = "{% if value == 1 %}on{% else %}off{% endif %}";
relay1_data["command_on_template"] = "1";
relay1_data["command_off_template"] = "0";
// relay1_data["state_template"] = "{% if value == 1 %}on{% else %}off{% endif %}";
relay1_data["payload_on"] = "1";
relay1_data["payload_off"] = "0";
relay1_data["unique_id"] = fmt::format("{}_relay1", this->_mqtt_register_mac);
std::string relay1_data_str = relay1_data.dump();
MQTT_Manager::clear_retain(this->_mqtt_switch_relay1_topic);
Expand All @@ -682,7 +681,7 @@ void NSPanel::register_to_home_assistant() {
}

// Register relay2
if (!this->_relay1_is_mqtt_light) {
if (!this->_relay2_is_mqtt_light) {
nlohmann::json relay2_data = nlohmann::json(base_json);
relay2_data["device_class"] = "switch";
relay2_data["name"] = "Relay 2";
Expand All @@ -701,12 +700,12 @@ void NSPanel::register_to_home_assistant() {
nlohmann::json relay2_data = nlohmann::json(base_json);
relay2_data["device_class"] = "light";
relay2_data["name"] = "Relay 2";
relay2_data["state_topic"] = this->_mqtt_relay1_state_topic;
relay2_data["command_topic"] = this->_mqtt_relay1_command_topic;
relay2_data["state_template"] = "{% if value == 1 %}on{% else %}off{% endif %}";
relay2_data["command_on_template"] = "1";
relay2_data["command_off_template"] = "0";
relay2_data["unique_id"] = fmt::format("{}_relay1", this->_mqtt_register_mac);
relay2_data["state_topic"] = this->_mqtt_relay2_state_topic;
relay2_data["command_topic"] = this->_mqtt_relay2_command_topic;
// relay2_data["state_template"] = "{% if value == 1 %}on{% else %}off{% endif %}";
relay2_data["payload_on"] = "1";
relay2_data["payload_off"] = "0";
relay2_data["unique_id"] = fmt::format("{}_relay2", this->_mqtt_register_mac);
std::string relay2_data_str = relay2_data.dump();
MQTT_Manager::clear_retain(this->_mqtt_switch_relay2_topic);
SPDLOG_DEBUG("Registring relay2 as light for NSPanel {}::{} to Home Assistant.", this->_id, this->_name);
Expand Down
Binary file modified docker/web/nspanelmanager/firmware.bin
Binary file not shown.
Binary file modified docker/web/nspanelmanager/manual.pdf
Binary file not shown.
Binary file modified docker/web/nspanelmanager/merged_flash.bin
Binary file not shown.
4 changes: 2 additions & 2 deletions docker/web/nspanelmanager/web/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,8 +179,8 @@ def get_nspanel_json_representation(panel):
"name": panel.friendly_name,
"is_us_panel": get_nspanel_setting_with_default(panel.id, "is_us_panel", "False") == "True",
"address": panel.ip_address,
"relay1_is_light": panel.register_relay1_as_light,
"relay2_is_light": panel.register_relay2_as_light
"relay1_is_light": get_nspanel_setting_with_default(panel.id, "relay1_is_light", "False") == "True",
"relay2_is_light": get_nspanel_setting_with_default(panel.id, "relay2_is_light", "False") == "True"
}
return panel_config

Expand Down
Binary file modified docs/tex/manual.pdf
Binary file not shown.
5 changes: 5 additions & 0 deletions docs/tex/manual.tex
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,11 @@
\item \textbf{Follow mode} - When the button is pressed the relay is engaged, when the button is release the relay is disconnected.
\end{itemize}
\item \textbf{Relay 1/2 default mode} - Select the default mode of the relay when the NSPanel starts.
\item \textbf{Register relay 1/2 as}:
\begin{itemize}
\item \textbf{Switch} - The relay will be registered to Home Assistant as a switch entity.
\item \textbf{Light} - The relay will be registered to Home Assistant as a light entity.
\end{itemize}
\item \textbf{Screen brightness} - The brightness of the screen when on (1-100\%).
\item \textbf{Screensaver brightness} - The brightness of the screen when the screensaver is active (0-100\%). A value of 0 means that the screen is off.
\item \textbf{Screensaver mode} - Select what to display on the screensaver. Available options are:
Expand Down
2 changes: 1 addition & 1 deletion firmware/NSPanelManagerFirmware/include/nspm-bin-version.h
Original file line number Diff line number Diff line change
@@ -1 +1 @@
#define NSPanelManagerFirmwareVersion "0.1.20"
#define NSPanelManagerFirmwareVersion "0.1.21"
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ void ButtonManager::init() {
LOG_DEBUG("Setting relay 1 to default state: ", NSPMConfig::instance->relay1_default_mode ? "ON" : "OFF");
ButtonManager::setRelayState(1, NSPMConfig::instance->relay1_default_mode);
LOG_DEBUG("Setting relay 2 to default state: ", NSPMConfig::instance->relay2_default_mode ? "ON" : "OFF");
ButtonManager::setRelayState(2, NSPMConfig::instance->relay1_default_mode);
ButtonManager::setRelayState(2, NSPMConfig::instance->relay2_default_mode);

xTaskCreatePinnedToCore(ButtonManager::_loop, "_taskButtonManagerLoop", 5000, NULL, 1, NULL, CONFIG_ARDUINO_RUNNING_CORE);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#include "NSPMConfig.h"
#include "freertos/portmacro.h"
#include <ArduinoJson.h>
#include <ButtonManager.hpp>
#include <HomePage.hpp>
#include <InterfaceConfig.hpp>
#include <InterfaceManager.hpp>
Expand Down
Binary file modified firmware/NSPanelManagerFirmware/merged-flash.bin
Binary file not shown.

0 comments on commit b8d9ff7

Please sign in to comment.