Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 58 additions & 7 deletions docker/MQTTManager/include/cover/cover.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "mqtt_manager/mqtt_manager.hpp"
#include "mqtt_manager_config/mqtt_manager_config.hpp"
#include <algorithm>
#include <boost/algorithm/string/case_conv.hpp>
#include <boost/bind.hpp>
#include <fmt/format.h>
#include <google/protobuf/util/message_differencer.h>
Expand All @@ -28,6 +29,30 @@ void CoverEntity::_load_common_config() {
this->_entity_page_slot = cover.room_view_position;

nlohmann::json entity_data = cover.get_entity_data_json();
if (entity_data.contains("invert_positions") && entity_data["invert_positions"].is_boolean()) {
this->_invert_positions = entity_data["invert_positions"];
}

if (entity_data.contains("position_preset1") && entity_data["position_preset1"].is_number()) {
this->_position_preset1 = entity_data["position_preset1"];
}
if (entity_data.contains("position_preset2") && entity_data["position_preset2"].is_number()) {
this->_position_preset2 = entity_data["position_preset2"];
}
if (entity_data.contains("position_preset3") && entity_data["position_preset3"].is_number()) {
this->_position_preset3 = entity_data["position_preset3"];
}

if (entity_data.contains("tilt_preset1") && entity_data["tilt_preset1"].is_number()) {
this->_tilt_preset1 = entity_data["tilt_preset1"];
}
if (entity_data.contains("tilt_preset2") && entity_data["tilt_preset2"].is_number()) {
this->_tilt_preset2 = entity_data["tilt_preset2"];
}
if (entity_data.contains("tilt_preset3") && entity_data["tilt_preset3"].is_number()) {
this->_tilt_preset3 = entity_data["tilt_preset3"];
}

if (entity_data.contains("controller") && entity_data["controller"].is_string()) {
std::string controller = entity_data["controller"];
if (controller.compare("home_assistant") == 0) {
Expand Down Expand Up @@ -117,15 +142,25 @@ std::string CoverEntity::get_device_class() {
}

void CoverEntity::send_state_update_to_nspanel() {
int position = this->_position;
if (this->_invert_positions) {
position = 100 - this->_position;
}

int tilt_position = this->_tilt_position;
if (this->_invert_positions) {
tilt_position = 100 - this->_tilt_position;
}

NSPanelEntityState state;
NSPanelEntityState_Cover *cover_state = state.mutable_cover();
cover_state->set_cover_id(this->_id);
cover_state->set_name(this->_name);
cover_state->set_state(this->_state);
cover_state->set_has_position(this->_has_position);
cover_state->set_position(this->_has_position ? this->_position : 0);
cover_state->set_position(this->_has_position ? position : 0);
cover_state->set_has_tilt_position(this->_has_tilt_position);
cover_state->set_tilt_position(this->_has_tilt_position ? this->_tilt_position : 0);
cover_state->set_tilt_position(this->_has_tilt_position ? tilt_position : 0);
cover_state->set_can_open(this->_can_open);
cover_state->set_can_close(this->_can_close);
cover_state->set_can_stop(this->_can_stop);
Expand All @@ -136,6 +171,14 @@ void CoverEntity::send_state_update_to_nspanel() {
cover_state->set_can_set_tilt_position(this->_can_set_tilt_position);
cover_state->set_device_class(this->_device_class);

cover_state->set_position_preset1(this->_position_preset1);
cover_state->set_position_preset2(this->_position_preset2);
cover_state->set_position_preset3(this->_position_preset3);

cover_state->set_tilt_preset1(this->_tilt_preset1);
cover_state->set_tilt_preset2(this->_tilt_preset2);
cover_state->set_tilt_preset3(this->_tilt_preset3);

google::protobuf::util::MessageDifferencer differencer;
if (!differencer.Equals(this->_last_cover_state, state)) {
SPDLOG_DEBUG("Sending updated state for cover {}::{} over MQTT.", this->_id, this->_name);
Expand Down Expand Up @@ -182,10 +225,20 @@ void CoverEntity::command_callback(NSPanelMQTTManagerCommand &command) {
}

if (cover_command.has_position()) {
this->set_position(static_cast<uint8_t>(std::clamp(cover_command.position(), 0, 100)));
int requested_pos = std::clamp(cover_command.position(), 0, 100);
if (this->_invert_positions) {
requested_pos = 100 - std::clamp(cover_command.position(), 0, 100);
}

this->set_position(static_cast<uint8_t>(requested_pos));
}
if (cover_command.has_tilt_position()) {
this->set_tilt_position(static_cast<uint8_t>(std::clamp(cover_command.tilt_position(), 0, 100)));
int requested_pos = std::clamp(cover_command.tilt_position(), 0, 100);
if (this->_invert_positions) {
requested_pos = 100 - std::clamp(cover_command.tilt_position(), 0, 100);
}

this->set_tilt_position(requested_pos);
}
}

Expand Down Expand Up @@ -220,9 +273,7 @@ void CoverEntity::toggle() {
}

std::string_view CoverEntity::get_icon() {
bool open = this->_state != NSPanelEntityState_Cover_CoverState_UNAVAILABLE && this->_state != NSPanelEntityState_Cover_CoverState_UNKNOWN &&
(this->_has_position ? this->_position > 0 : this->_state == NSPanelEntityState_Cover_CoverState_OPEN);
return open ? EntityIcons::entity_icon_cover_open : EntityIcons::entity_icon_cover_closed;
return EntityIcons::entity_icon_blinds;
}

uint16_t CoverEntity::get_icon_color() {
Expand Down
9 changes: 9 additions & 0 deletions docker/MQTTManager/include/cover/cover.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ class CoverEntity : public MqttManagerEntity {
uint8_t _entity_page_slot;

NSPanelEntityState_Cover_CoverState _state = NSPanelEntityState_Cover_CoverState_UNKNOWN;
NSPanelEntityState_Cover_CoverState _tilt_state = NSPanelEntityState_Cover_CoverState_UNKNOWN;
bool _has_position = false;
uint8_t _position = 0;
bool _has_tilt_position = false;
Expand All @@ -116,6 +117,14 @@ class CoverEntity : public MqttManagerEntity {
bool _can_close_tilt = false;
bool _can_stop_tilt = false;
bool _can_set_tilt_position = false;
bool _invert_positions = false;

int32_t _position_preset1 = 0;
int32_t _position_preset2 = 0;
int32_t _position_preset3 = 0;
int32_t _tilt_preset1 = 0;
int32_t _tilt_preset2 = 0;
int32_t _tilt_preset3 = 0;

std::string _device_class;

Expand Down
2 changes: 2 additions & 0 deletions docker/MQTTManager/include/cover/home_assistant_cover.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ void HomeAssistantCover::set_position(uint8_t position) {
SPDLOG_WARN("Asked to set position of cover {}::{} but Home Assistant does not report the set position feature.", this->_id, this->_name);
return;
}
SPDLOG_DEBUG("Setting cover {}::{} to position {}%", this->_id, this->_name, position);
this->_call_service("set_cover_position", {{"position", position}});
}

Expand Down Expand Up @@ -147,6 +148,7 @@ void HomeAssistantCover::set_tilt_position(uint8_t position) {
SPDLOG_WARN("Asked to set tilt position of cover {}::{} but Home Assistant does not report the set tilt position feature.", this->_id, this->_name);
return;
}
SPDLOG_DEBUG("Setting cover {}::{} to tilt {}%", this->_id, this->_name, position);
this->_call_service("set_cover_tilt_position", {{"tilt_position", position}});
}

Expand Down
131 changes: 117 additions & 14 deletions docker/MQTTManager/include/cover/openhab_cover.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,16 @@ OpenhabCover::OpenhabCover(uint32_t cover_id) : CoverEntity(cover_id) {
return;
}

this->_can_open = !this->_openhab_item.empty();
this->_can_close = !this->_openhab_item.empty();
this->_can_stop = !this->_openhab_item.empty();
this->_has_position = !this->_openhab_item.empty();

this->_can_open_tilt = !this->_openhab_tilt_item.empty();
this->_can_close_tilt = !this->_openhab_tilt_item.empty();
this->_can_stop_tilt = !this->_openhab_tilt_item.empty(); //
this->_has_tilt_position = !this->_openhab_tilt_item.empty(); //

SPDLOG_DEBUG("Loaded cover {}::{}, OpenHAB item: {}", this->_id, this->_name, this->_openhab_item);
OpenhabManager::attach_event_observer(this->_openhab_item, boost::bind(&OpenhabCover::openhab_event_callback, this, _1));

Expand Down Expand Up @@ -57,10 +67,15 @@ void OpenhabCover::reload_config() {
CoverEntity::reload_config();

std::string previous_item = this->_openhab_item;
std::string previous_tilt_item = this->_openhab_tilt_item;
if (!this->_load_openhab_config()) {
if (!previous_item.empty()) {
OpenhabManager::detach_event_observer(previous_item, boost::bind(&OpenhabCover::openhab_event_callback, this, _1));
}
if (!previous_tilt_item.empty()) {
OpenhabManager::detach_event_observer(previous_tilt_item, boost::bind(&OpenhabCover::openhab_tilt_event_callback, this, _1));
}

this->send_state_update_to_nspanel();
return;
}
Expand All @@ -69,7 +84,14 @@ void OpenhabCover::reload_config() {
if (!previous_item.empty()) {
OpenhabManager::detach_event_observer(previous_item, boost::bind(&OpenhabCover::openhab_event_callback, this, _1));
}
OpenhabManager::attach_event_observer(this->_openhab_item, boost::bind(&OpenhabCover::openhab_event_callback, this, _1));
OpenhabManager::attach_event_observer(this->_openhab_item, boost::bind(&OpenhabCover::openhab_tilt_event_callback, this, _1));
}

if (previous_tilt_item != this->_openhab_tilt_item) {
if (!previous_tilt_item.empty()) {
OpenhabManager::detach_event_observer(previous_tilt_item, boost::bind(&OpenhabCover::openhab_event_callback, this, _1));
}
OpenhabManager::attach_event_observer(this->_openhab_tilt_item, boost::bind(&OpenhabCover::openhab_tilt_event_callback, this, _1));
}

this->send_state_update_to_nspanel();
Expand All @@ -86,13 +108,23 @@ void OpenhabCover::_send_command(const std::string &type, const std::string &val
OpenhabManager::send_json(event_data);
}

void OpenhabCover::_send_tilt_command(const std::string &type, const std::string &value) {
nlohmann::json event_data;
event_data["type"] = "ItemCommandEvent";
event_data["topic"] = fmt::format("openhab/items/{}/command", this->_openhab_tilt_item);
nlohmann::json payload;
payload["type"] = type;
payload["value"] = value;
event_data["payload"] = payload.dump();
OpenhabManager::send_json(event_data);
}

void OpenhabCover::open() {
// OpenHAB Rollershutter percentages run from 0 (open) to 100 (closed).
this->_send_command("Percent", "0");
this->_send_command("UpDown", "UP");
}

void OpenhabCover::close() {
this->_send_command("Percent", "100");
this->_send_command("UpDown", "DOWN");
}

void OpenhabCover::stop() {
Expand All @@ -105,19 +137,20 @@ void OpenhabCover::set_position(uint8_t position) {
}

void OpenhabCover::open_tilt() {
SPDLOG_WARN("OpenHAB cover {}::{} does not support tilt.", this->_id, this->_name);
this->_send_tilt_command("UpDown", "UP");
}

void OpenhabCover::close_tilt() {
SPDLOG_WARN("OpenHAB cover {}::{} does not support tilt.", this->_id, this->_name);
this->_send_tilt_command("UpDown", "DOWN");
}

void OpenhabCover::stop_tilt() {
SPDLOG_WARN("OpenHAB cover {}::{} does not support tilt.", this->_id, this->_name);
this->_send_tilt_command("StopMove", "STOP");
}

void OpenhabCover::set_tilt_position(uint8_t position) {
SPDLOG_WARN("OpenHAB cover {}::{} does not support tilt.", this->_id, this->_name);
this->_send_tilt_command("Percent", std::to_string(100 - position));
}

void OpenhabCover::_update_from_state(const nlohmann::json &state) {
Expand All @@ -128,7 +161,6 @@ void OpenhabCover::_update_from_state(const nlohmann::json &state) {
if (value.compare("NULL") == 0 || value.compare("UNDEF") == 0) {
this->_state = NSPanelEntityState_Cover_CoverState_UNAVAILABLE;
this->_has_position = false;
this->_has_tilt_position = false;
this->send_state_update_to_nspanel();
if (previous_icon_color != this->get_icon_color()) {
this->_signal_entity_changed();
Expand All @@ -150,14 +182,8 @@ void OpenhabCover::_update_from_state(const nlohmann::json &state) {
} else {
this->_state = NSPanelEntityState_Cover_CoverState_OPEN;
}
this->_can_open = true;
this->_can_close = true;
this->_can_stop = true;
this->_can_set_position = true;
this->_can_open_tilt = false;
this->_can_close_tilt = false;
this->_can_stop_tilt = false;
this->_can_set_tilt_position = false;
this->_device_class = "shutter";
} catch (std::exception &e) {
SPDLOG_ERROR("Could not parse OpenHAB cover state '{}' for {}::{}: {}", value, this->_id, this->_name, e.what());
Expand All @@ -177,6 +203,47 @@ void OpenhabCover::_update_from_state(const nlohmann::json &state) {
}
}

void OpenhabCover::_update_from_tilt_state(const nlohmann::json &state) {

if (state.is_string()) {
std::string value = state;
if (value.compare("NULL") == 0 || value.compare("UNDEF") == 0) {
this->_state = NSPanelEntityState_Cover_CoverState_UNAVAILABLE;
this->_has_tilt_position = false;
this->send_state_update_to_nspanel();
return;
}

try {
double percentage = std::stod(value);
percentage = std::clamp(percentage, 0.0, 100.0);
uint8_t openness = static_cast<uint8_t>(std::clamp<long>(std::lround(100.0 - percentage), 0, 100));

this->_has_tilt_position = true;
this->_tilt_position = openness;
if (percentage <= 0.0) {
this->_tilt_state = NSPanelEntityState_Cover_CoverState_OPEN;
} else if (percentage >= 100.0) {
this->_tilt_state = NSPanelEntityState_Cover_CoverState_CLOSED;
} else {
this->_tilt_state = NSPanelEntityState_Cover_CoverState_OPEN;
}
this->_can_set_tilt_position = true;
} catch (std::exception &e) {
SPDLOG_ERROR("Could not parse OpenHAB cover state '{}' for {}::{}: {}", value, this->_id, this->_name, e.what());
return;
}
} else if (state.is_number()) {
this->_update_from_tilt_state(nlohmann::json(std::to_string(state.get<double>())));
return;
} else {
SPDLOG_WARN("Unsupported OpenHAB cover state for {}::{}: {}", this->_id, this->_name, state.dump());
return;
}

this->send_state_update_to_nspanel();
}

void OpenhabCover::openhab_event_callback(nlohmann::json data) {
if (!data.contains("type")) {
return;
Expand Down Expand Up @@ -212,3 +279,39 @@ void OpenhabCover::openhab_event_callback(nlohmann::json data) {
this->_update_from_state(data["payload"]["state"]);
}
}

void OpenhabCover::openhab_tilt_event_callback(nlohmann::json data) {
if (!data.contains("type")) {
return;
}

std::string type = data["type"];
if (type.compare("ItemStateChangedEvent") == 0) {
if (!data.contains("topic") || !data.contains("payload")) {
return;
}
std::string topic = data["topic"];
std::vector<std::string> topic_parts;
boost::split(topic_parts, topic, boost::is_any_of("/"));
if (topic_parts.size() < 3 || topic_parts[2].compare(this->_openhab_item) != 0) {
return;
}

try {
nlohmann::json payload = nlohmann::json::parse(std::string(data["payload"]));
if (payload.contains("value")) {
this->_update_from_state(payload["value"]);
}
} catch (std::exception &e) {
SPDLOG_ERROR("Could not parse OpenHAB state event for cover {}::{}: {}", this->_id, this->_name, e.what());
}
} else if (type.compare("ItemStateFetched") == 0) {
if (!data.contains("payload") || !data["payload"].contains("name") || !data["payload"].contains("state")) {
return;
}
if (std::string(data["payload"]["name"]).compare(this->_openhab_item) != 0) {
return;
}
this->_update_from_state(data["payload"]["state"]);
}
}
12 changes: 12 additions & 0 deletions docker/MQTTManager/include/cover/openhab_cover.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class OpenhabCover : public CoverEntity {
~OpenhabCover();
void reload_config();
void openhab_event_callback(nlohmann::json event_data);
void openhab_tilt_event_callback(nlohmann::json event_data);

void open();
void close();
Expand All @@ -33,12 +34,23 @@ class OpenhabCover : public CoverEntity {
*/
void _send_command(const std::string &type, const std::string &value);

/**
* Send an ItemCommandEvent to OpenHAB.
*/
void _send_tilt_command(const std::string &type, const std::string &value);

/**
* Update the panel state from a Rollershutter percentage state.
*/
void _update_from_state(const nlohmann::json &state);

/**
* Update the panel state from a Rollershutter percentage state.
*/
void _update_from_tilt_state(const nlohmann::json &state);

std::string _openhab_item;
std::string _openhab_tilt_item;
};

#endif // !MQTT_MANAGER_OPENHAB_COVER_HPP
Loading
Loading