Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Slider support for cover #184

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
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
34 changes: 33 additions & 1 deletion src/device-types/HACover.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ HACover::HACover(const char* uniqueId, const Features features) :
_icon(nullptr),
_retain(false),
_optimistic(false),
_commandCallback(nullptr)
_commandCallback(nullptr),
_setPosCallback(nullptr)
{

}
Expand Down Expand Up @@ -83,6 +84,7 @@ void HACover::buildSerializer()

if (_features & PositionFeature) {
_serializer->topic(AHATOFSTR(HAPositionTopic));
_serializer->topic(AHATOFSTR(HASetPositionTopic));
}
}

Expand All @@ -101,6 +103,11 @@ void HACover::onMqttConnected()
}

subscribeTopic(uniqueId(), AHATOFSTR(HACommandTopic));

if (_features & PositionFeature) {
subscribeTopic(uniqueId(), AHATOFSTR(HASetPositionTopic));
}

}

void HACover::onMqttMessage(
Expand All @@ -116,6 +123,14 @@ void HACover::onMqttMessage(
)) {
handleCommand(payload, length);
}

if (HASerializer::compareDataTopics(
topic,
uniqueId(),
AHATOFSTR(HASetPositionTopic)
)) {
handleSetPosition(payload, length);
}
}

bool HACover::publishState(CoverState state)
Expand Down Expand Up @@ -180,4 +195,21 @@ void HACover::handleCommand(const uint8_t* cmd, const uint16_t length)
}
}

void HACover::handleSetPosition(const uint8_t* cmd, const uint16_t length)
{
if (!_setPosCallback) {
return;
}

if (memcmp_P(cmd, HAStateNone, length) == 0) {
_setPosCallback(HANumeric(), this);
} else {
HANumeric number = HANumeric::fromStr(cmd, length);
if (number.isSet()) {
number.setPrecision(0);
_setPosCallback(number, this);
}
}
}

#endif
23 changes: 23 additions & 0 deletions src/device-types/HACover.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@
#define AHA_HACOVER_H

#include "HABaseDeviceType.h"
#include "../utils/HANumeric.h"

#ifndef EX_ARDUINOHA_COVER

#define HACOVER_CALLBACK(name) void (*name)(CoverCommand cmd, HACover* sender)
#define HACOVER_SET_POS_CALLBACK(name) void (*name)(HANumeric number, HACover* sender)

/**
* HACover allows to control a cover (such as blinds, a roller shutter or a garage door).
Expand Down Expand Up @@ -147,6 +149,15 @@ class HACover : public HABaseDeviceType
inline void onCommand(HACOVER_CALLBACK(callback))
{ _commandCallback = callback; }

/**
* Registers callback that will be called each time the set position command from HA is received.
* Please note that it's not possible to register multiple callbacks for the same cover.
*
* @param callback
*/
inline void onSetPosition(HACOVER_SET_POS_CALLBACK(callback))
{ _setPosCallback = callback; }

protected:
virtual void buildSerializer() override;
virtual void onMqttConnected() override;
Expand Down Expand Up @@ -180,6 +191,14 @@ class HACover : public HABaseDeviceType
* @param length Length of the command.
*/
void handleCommand(const uint8_t* cmd, const uint16_t length);

/**
* Parses the given set position command and executes the cover's callback..
*
* @param cmd The data of the command.
* @param length Length of the command.
*/
void handleSetPosition(const uint8_t *cmd, const uint16_t length);

/// Features enabled for the cover.
const uint8_t _features;
Expand All @@ -204,6 +223,10 @@ class HACover : public HABaseDeviceType

/// The command callback that will be called when clicking the cover's button in the HA panel.
HACOVER_CALLBACK(_commandCallback);

/// The command callback that will be called when sliding the cover's slider in the HA panel.
HACOVER_SET_POS_CALLBACK(_setPosCallback);

};

#endif
Expand Down
1 change: 1 addition & 0 deletions src/utils/HADictionary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ const char HATopic[] PROGMEM = {"t"};
const char HAStateTopic[] PROGMEM = {"stat_t"};
const char HACommandTopic[] PROGMEM = {"cmd_t"};
const char HAPositionTopic[] PROGMEM = {"pos_t"};
const char HASetPositionTopic[] PROGMEM = {"set_pos_t"};
const char HAPercentageStateTopic[] PROGMEM = {"pct_stat_t"};
const char HAPercentageCommandTopic[] PROGMEM = {"pct_cmd_t"};
const char HABrightnessCommandTopic[] PROGMEM = {"bri_cmd_t"};
Expand Down
1 change: 1 addition & 0 deletions src/utils/HADictionary.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ extern const char HATopic[];
extern const char HAStateTopic[];
extern const char HACommandTopic[];
extern const char HAPositionTopic[];
extern const char HASetPositionTopic[];
extern const char HAPercentageStateTopic[];
extern const char HAPercentageCommandTopic[];
extern const char HABrightnessCommandTopic[];
Expand Down