Skip to content

Commit a4e50b7

Browse files
Release 1.3.0 (#36)
* Add support for HVAC (#15) * remove HASwitch::triggerCallback method * fix typo in docs * HASwitch: simplify callback #define * work in progress: HVAC * replace local mqtt instances with singleton * device types optimization * cleanup * finished aux heating and away mode * wip: HVAC * finished HVAC implementation * HVAC implementation * add ifdefs for all HA components * wip: optimization * update readme * update examples * remove unused constructor from HAMqtt * update HAUtils * remove mqtt from constructors, fix compilation warnings * finished HVAC * add HVAC example, add changelog * update changelog, add documentation * Add support for icons in HASwitch and HASensor (#16) * add support for icon in HASwitch * add support for icon in HASensor * update changelog * add support for setting retain flag in HASwitch * Add support for text payload in HASensor (#17) * refactored HASensor * update changelog * fix sensor example * Add support for fans (HAFan) (#18) * finished basic implementation of the HAFan * add retain method to DeviceTypeSerializer * finished HAFan implementation * update HAHVAC * device types cleanup * add HAFan example * update changelog * update readme * update fan example * optimization, add support for ActionFeature in HVAC * Add support for MQTT LWT (#19) * add support for hostname in HAMqtt * add support for LWT and shared availability * minor fixes * added advanced availability example * update examples * update changelog * cleanup & bug fix * bump up version * minor improvements and fixes * update readme * add HAMqtt::onMessage method * Add support for covers (#28) * add HACovers * add cover example, update implementation * disable debug * update mqtt-events example * Add support for setting different prefix for non-discovery topics (#29) * add support for data prefix * cleanup * update comments and logs * update multi-state-button example * rename rexample from mqtt-events to mqtt-advanced * update changelog * update links in changelog * Home Assistant 2021.4.5 - MQTT upgrade (#33) * updae HAFan implementation * change lib version * update changelog * fix setSpeed method * update HAFan example * Separate uniqueId from name (#34) * rename name to uniqueId * remove legacy labels * update examples * rename isMyTopic to compareTopics * update changelog, rename name to uniqueId * Implement onBeforeStateChanged callback in HASwitch (#35) * add low latency mode to HASwitch * add onBeforeStateChanged callback to HASwitch * update readme * rename DEPRECATED macro * update advanced MQTT example
1 parent 15562fa commit a4e50b7

40 files changed

+977
-442
lines changed

CHANGELOG.md

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,35 @@
11
# Changelog
22

3+
## 1.3.0
4+
5+
**New features:**
6+
* Added `onMessage()` method to HAMqtt class
7+
* Added support for HA Covers
8+
* Added support for setting different prefix for non-discovery topics (see [Advanced MQTT example](examples/mqtt-advanced/mqtt-advanced.ino))
9+
* Added `setName` method to HASensor
10+
* Added `setName` method to HASwitch
11+
* Added `onBeforeStateChanged` callback to HASwitch
12+
13+
**Updates:**
14+
* Removed legacy properties from HAFan (Home Assistant 2021.4.4). Deprecated methods will be removed after a quarter (2021.7)
15+
* Separated `uniqueID` field from `name` in all devices types
16+
317
## 1.2.0
418

519
**Breaking changes:**
6-
* Refactored HASensor implementation. Please take a look at updated example in `examples/sensor/sensor.ino`
20+
* Refactored HASensor implementation. Please take a look at [updated example](examples/sensor/sensor.ino)
721

822
**New features:**
923
* Added support for HVAC
10-
* Added support for excluding devices types from the compilation using defines (see `src/ArduinoHADefines.h`)
24+
* Added support for excluding devices types from the compilation using defines (see [src/ArduinoHADefines.h](src/ArduinoHADefines.h))
1125
* Added support for setting icon in HASwitch and HASensor
1226
* Added support for setting retain flag in HASwitch
1327
* Added support for text (const char*) payload in HASensor
1428
* Added support for fans (HAFan)
1529
* Added support for connecting to the MQTT broker using hostname
1630
* Added `onConnected()` method in the HAMqtt
1731
* Added `onConnectionFailed()` method in the HAMqtt
18-
* Added support for MQTT LWT (see `examples/advanced-availability/advanced-availability.ino`)
32+
* Added support for MQTT LWT (see [Advanced Availability example](examples/advanced-availability/advanced-availability.ino))
1933

2034
**Updates:**
2135
* Optimized codebase and logic in all devices types

README.md

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,25 @@ but I successfully use it on ESP8266/ESP8255 boards in my projects.
99

1010
* MQTT discovery (device is added to the Home Assistant panel automatically)
1111
* MQTT Last Will and Testament
12+
* Support for custom MQTT messages (publishing and subscribing)
1213
* Auto reconnect with MQTT broker
14+
* Reporting availability (online/offline states) of a device
15+
16+
## Supported HA types
17+
18+
* Binary sensors
19+
* Covers
20+
* Fans
21+
* Device triggers
22+
* Switches
23+
* Sensors
24+
* Tag scanner
25+
* HVACs *(side note: HVACs requires more flash size than other HA types. It's not suitable for Arduino Nano/Uno)*
1326

1427
## Examples
1528

1629
* [Binary Sensor](examples/binary-sensor/binary-sensor.ino)
30+
* [Cover](examples/cover/cover.ino)
1731
* [Fan](examples/fan/fan.ino)
1832
* [LED switch](examples/led-switch/led-switch.ino)
1933
* [Multi-state button](examples/multi-state-button/multi-state-button.ino)
@@ -24,40 +38,37 @@ but I successfully use it on ESP8266/ESP8255 boards in my projects.
2438
* [Availability feature](examples/availability)
2539
* [Advanced availability (MQTT LWT)](examples/advanced-availability/advanced-availability.ino)
2640
* [MQTT with credentials](examples/mqtt-with-credentials/mqtt-with-credentials.ino)
27-
* [MQTT events](examples/mqtt-events/mqtt-events.ino)
41+
* [MQTT advanced](examples/mqtt-advanced/mqtt-advanced.ino)
2842

2943
## Tested boards
3044

3145
* Arduino Uno
3246
* Arduino Mega
33-
* Controllino Maxi (standard/pure/automation/power)
34-
* Controllino Mega (standard/pure)
3547
* NodeMCU
3648
* ESP-01
3749
* Generic ESP8266/ESP8255
3850
* Arduino Nano 33 IoT (SAMD)
3951

40-
## Tested Arduino Shields
52+
## Tested devices
4153

42-
* Arduino Ethernet Shield (WizNet W5100)
54+
* Controllino Maxi (standard/pure/automation/power)
55+
* Controllino Mega (standard/pure)
56+
* Sonoff Dual R2
57+
* Sonoff Basic
58+
* Sonoff Mini
59+
* Tuya Wi-Fi switch module
60+
* Tuya Wi-Fi curtain module
4361

44-
## Supported HA types
62+
## Tested Arduino Shields
4563

46-
* Binary sensors
47-
* Fans
48-
* Device triggers
49-
* Switches
50-
* Sensors
51-
* Tag scanner
52-
* HVACs *(side note: HVACs requires more flash size than other HA types. It's not suitable for Arduino Nano/Uno)*
64+
* Arduino Ethernet Shield (WizNet W5100)
5365

5466
## Roadmap
5567

5668
* FAQ + Home Assistant setup instructions
5769
* Documentation of the library
5870
* Unit tests
5971
* Reduce flash memory usage
60-
* Add support for HA covers
6172
* Add support for HA lights
6273

6374
## Unsupported features

examples/advanced-availability/advanced-availability.ino

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ EthernetClient client;
1313
HADevice device(mac, sizeof(mac));
1414
HAMqtt mqtt(client, device);
1515

16-
// "input" may be anything you want to be displayed in HA panel
16+
// "input" is unique ID of the sensor. You should define you own ID.
1717
// "door" is device class (based on the class HA displays different icons in the panel)
1818
// "true" is initial state of the sensor. In this example it's "true" as we use pullup resistor
1919
HABinarySensor sensor("input", "door", true);
@@ -32,6 +32,8 @@ void setup() {
3232
device.setName("Arduino");
3333
device.setSoftwareVersion("1.0.0");
3434

35+
sensor.setName("Door sensor"); // optional
36+
3537
// This method enables availability for all device types registered on the device.
3638
// For example, if you have 5 sensors on the same device, you can enable
3739
// shared availability and change availability state of all sensors using

examples/availability/availability.ino

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ EthernetClient client;
1313
HADevice device(mac, sizeof(mac));
1414
HAMqtt mqtt(client, device);
1515

16-
// "input" may be anything you want to be displayed in HA panel
16+
// "input" is unique ID of the sensor. You should define you own ID.
1717
// "door" is device class (based on the class HA displays different icons in the panel)
1818
// "true" is initial state of the sensor. In this example it's "true" as we use pullup resistor
1919
HABinarySensor sensor("input", "door", true);
@@ -36,6 +36,8 @@ void setup() {
3636
device.setName("Arduino");
3737
device.setSoftwareVersion("1.0.0");
3838

39+
sensor.setName("Door sensor"); // optional
40+
3941
mqtt.begin(BROKER_ADDR);
4042
}
4143

examples/binary-sensor/binary-sensor.ino

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ EthernetClient client;
1212
HADevice device(mac, sizeof(mac));
1313
HAMqtt mqtt(client, device);
1414

15-
// "input" may be anything you want to be displayed in HA panel
15+
// "input" is unique ID of the sensor. You should define you own ID.
1616
// "door" is device class (based on the class HA displays different icons in the panel)
1717
// "true" is initial state of the sensor. In this example it's "true" as we use pullup resistor
1818
HABinarySensor sensor("input", "door", true);
@@ -28,6 +28,8 @@ void setup() {
2828
device.setName("Arduino");
2929
device.setSoftwareVersion("1.0.0");
3030

31+
sensor.setName("Door sensor"); // optional
32+
3133
mqtt.begin(BROKER_ADDR);
3234
}
3335

examples/cover/cover.ino

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#include <Ethernet.h>
2+
#include <ArduinoHA.h>
3+
4+
#define BROKER_ADDR IPAddress(192,168,0,17)
5+
6+
byte mac[] = {0x00, 0x10, 0xFA, 0x6E, 0x38, 0x4A};
7+
8+
EthernetClient client;
9+
HADevice device(mac, sizeof(mac));
10+
HAMqtt mqtt(client, device);
11+
HACover cover("my-cover"); // "my-cover" is unique ID of the cover. You should define your own ID.
12+
13+
void onCoverCommand(HACover::CoverCommand cmd) {
14+
if (cmd == HACover::CommandOpen) {
15+
Serial.println("Command: Open");
16+
cover.setState(HACover::StateOpening);
17+
} else if (cmd == HACover::CommandClose) {
18+
Serial.println("Command: Close");
19+
cover.setState(HACover::StateClosing);
20+
} else if (cmd == HACover::CommandStop) {
21+
Serial.println("Command: Stop");
22+
cover.setState(HACover::StateStopped);
23+
}
24+
25+
// Available states:
26+
// HACover::StateClosed
27+
// HACover::StateClosing
28+
// HACover::StateOpen
29+
// HACover::StateOpening
30+
// HACover::StateStopped
31+
32+
// You can also report position using setPosition() method
33+
}
34+
35+
void setup() {
36+
Serial.begin(9600);
37+
Ethernet.begin(mac);
38+
39+
cover.onCommand(onCoverCommand);
40+
cover.setName("My cover"); // optional
41+
// cover.setRetain(true); // optionally you can set retain flag
42+
43+
mqtt.begin(BROKER_ADDR);
44+
}
45+
46+
void loop() {
47+
Ethernet.maintain();
48+
mqtt.loop();
49+
}

examples/fan/fan.ino

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,27 +11,22 @@ HAMqtt mqtt(client, device);
1111

1212
// HAFan::SpeedsFeature enables support for setting different speeds of fan.
1313
// You can skip this argument if you don't need speed management.
14+
// "ventilation" is unique ID of the fan. You should define your own ID.
1415
HAFan fan("ventilation", HAFan::SpeedsFeature);
1516

1617
void onStateChanged(bool state) {
1718
Serial.print("State: ");
1819
Serial.println(state);
1920
}
2021

21-
void onSpeedChanged(HAFan::Speed speed) {
22+
void onSpeedChanged(uint16_t speed) {
2223
Serial.print("Speed: ");
23-
if (speed == HAFan::OffSpeed) {
24-
Serial.print("off");
25-
} else if (speed == HAFan::LowSpeed) {
26-
Serial.print("low");
27-
} else if (speed == HAFan::MediumSpeed) {
28-
Serial.print("medium");
29-
} else if (speed == HAFan::HighSpeed) {
30-
Serial.print("high");
31-
}
24+
Serial.println(speed);
3225
}
3326

3427
void setup() {
28+
Serial.begin(9600);
29+
3530
// you don't need to verify return status
3631
Ethernet.begin(mac);
3732

@@ -40,10 +35,10 @@ void setup() {
4035
device.setSoftwareVersion("1.0.0");
4136

4237
// configure fan (optional)
43-
// default speeds are: Off | Low | Medium | High
44-
fan.setSpeeds(HAFan::OffSpeed | HAFan::LowSpeed | HAFan::HighSpeed);
4538
fan.setName("Bathroom");
4639
fan.setRetain(true);
40+
fan.setSpeedRangeMin(1);
41+
fan.setSpeedRangeMax(100);
4742

4843
// handle fan states
4944
fan.onStateChanged(onStateChanged);

examples/led-switch/led-switch.ino

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,13 @@ byte mac[] = {0x00, 0x10, 0xFA, 0x6E, 0x38, 0x4A};
99
EthernetClient client;
1010
HADevice device(mac, sizeof(mac));
1111
HAMqtt mqtt(client, device);
12-
HASwitch led("led", false); // you can use custom name in place of "led"
12+
HASwitch led("led", false); // "led" is unique ID of the switch. You should define your own ID.
13+
14+
void onBeforeSwitchStateChanged(bool state, HASwitch* s)
15+
{
16+
// this callback will be called before publishing new state to HA
17+
// in some cases there may be delay before onStateChanged is called due to network latency
18+
}
1319

1420
void onSwitchStateChanged(bool state, HASwitch* s)
1521
{
@@ -29,8 +35,10 @@ void setup() {
2935

3036
// set icon (optional)
3137
led.setIcon("mdi:lightbulb");
38+
led.setName("My LED");
3239

3340
// handle switch state
41+
led.onBeforeStateChanged(onBeforeSwitchStateChanged); // optional
3442
led.onStateChanged(onSwitchStateChanged);
3543

3644
mqtt.begin(BROKER_ADDR);
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#include <Ethernet.h>
2+
#include <ArduinoHA.h>
3+
4+
#define BROKER_ADDR IPAddress(192,168,0,17)
5+
6+
byte mac[] = {0x00, 0x10, 0xFA, 0x6E, 0x38, 0x4A};
7+
8+
EthernetClient client;
9+
HADevice device(mac, sizeof(mac));
10+
HAMqtt mqtt(client, device);
11+
12+
void onMqttMessage(const char* topic, const uint8_t* payload, uint16_t length) {
13+
// This callback is called when message from MQTT broker is received.
14+
// Please note that you should always verify if the message's topic is the one you expect.
15+
// For example: if (memcmp(topic, "myCustomTopic") == 0) { ... }
16+
17+
Serial.print("New message on topic: ");
18+
Serial.println(topic);
19+
Serial.print("Data: ");
20+
Serial.println((const char*)payload);
21+
22+
mqtt.publish("myPublishTopic", "hello");
23+
}
24+
25+
void onMqttConnected() {
26+
Serial.println("Connected to the broker!");
27+
28+
// You can subscribe to custom topic if you need
29+
mqtt.subscribe("myCustomTopic");
30+
}
31+
32+
void onMqttConnectionFailed() {
33+
Serial.println("Failed to connect to the broker!");
34+
}
35+
36+
void setup() {
37+
Serial.begin(9600);
38+
Ethernet.begin(mac);
39+
40+
mqtt.onMessage(onMqttMessage);
41+
mqtt.onConnected(onMqttConnected);
42+
mqtt.onConnectionFailed(onMqttConnectionFailed);
43+
44+
// If you use custom discovery prefix you can change it as following:
45+
// mqtt.setDiscoveryPrefix("customPrefix");
46+
47+
// If you want to change prefix only for non-discovery prefix:
48+
// mqtt.setDataPrefix("data");
49+
50+
mqtt.begin(BROKER_ADDR);
51+
}
52+
53+
void loop() {
54+
Ethernet.maintain();
55+
mqtt.loop();
56+
}

examples/mqtt-events/mqtt-events.ino

Lines changed: 0 additions & 32 deletions
This file was deleted.

0 commit comments

Comments
 (0)