Publish a state topic for each shade

This commit is contained in:
2024-01-03 13:41:01 +11:00
parent e02add87ff
commit dbed260fd1

View File

@@ -111,6 +111,8 @@ void processPacket(const Packet *packet) {
uint8_t position = (uint8_t)std::round(((float)value / 0xFFFF) * 100);
if (shades[i].lastPosition == position) {
shades[i].lastPositionCount++;
} else {
shades[i].lastPositionCount = 0;
}
shades[i].lastPosition = position;
String payload = String(position);
@@ -216,11 +218,21 @@ bool updatePosition(uint16_t shadeID) {
if (shades[i].lastTargetPosition != shades[i].lastPosition || shades[i].lastPosition == -1) {
if (shades[i].positionFetchCount >= MAX_FETCH_COUNT) {
// Give up waiting for the blind to reach the target position
if (shades[i].lastPosition > 0) {
client.publish("hotdog/" + shades[i].name + "/state", "open", true);
} else {
client.publish("hotdog/" + shades[i].name + "/state", "closed", true);
}
return false;
}
if (shades[i].lastPositionCount >= 3) {
if (shades[i].lastPositionCount >= 2) {
// Blind hasn't moved after 3 fetches, so give up waiting
if (shades[i].lastPosition > 0) {
client.publish("hotdog/" + shades[i].name + "/state", "open", true);
} else {
client.publish("hotdog/" + shades[i].name + "/state", "closed", true);
}
return false;
}
@@ -228,6 +240,11 @@ bool updatePosition(uint16_t shadeID) {
sendFetchPosition(shadeID);
return true;
} else {
if (shades[i].lastPosition > 0) {
client.publish("hotdog/" + shades[i].name + "/state", "open", true);
} else {
client.publish("hotdog/" + shades[i].name + "/state", "closed", true);
}
return false;
}
}
@@ -250,6 +267,8 @@ void processSetMessage(const String &topic, const String &payload) {
shades[i].positionFetchCount = 0;
shades[i].lastPositionCount = 0;
timer.every(2000, updatePosition, shades[i].ID);
client.publish("hotdog/" + shades[i].name + "/state", "opening", true);
} else if (payload == "CLOSE") {
sendClosePacket(shades[i].ID);
@@ -258,6 +277,8 @@ void processSetMessage(const String &topic, const String &payload) {
shades[i].positionFetchCount = 0;
shades[i].lastPositionCount = 0;
timer.every(2000, updatePosition, shades[i].ID);
client.publish("hotdog/" + shades[i].name + "/state", "closing", true);
} else if (payload == "STOP") {
sendStopPacket(shades[i].ID);
@@ -281,6 +302,12 @@ void processSetPositionMessage(const String& topic, const String &payload) {
float percentage = payload.toInt() / 100.0f;
sendSetPosition(shades[i].ID, percentage);
if (payload.toInt() > shades[i].lastPosition) {
client.publish("hotdog/" + shades[i].name + "/state", "opening", true);
} else if (payload.toInt() < shades[i].lastPosition) {
client.publish("hotdog/" + shades[i].name + "/state", "closing", true);
}
shades[i].lastPosition = -1;
shades[i].lastTargetPosition = payload.toInt();
shades[i].positionFetchCount = 0;