Files
alex b9f4fd7f4b Per-entity availability, deferred discovery publish, suggested precision
- Add HAAvailabilityConfig for per-entity availability_topic and payloads
- Defer discovery until MQTT connected; publish availability after discovery
- HASensor/HANumber: optional suggested_display_precision in discovery
- HASerializer/HADictionary: extend for new keys; update device types and mocks
- Docs and examples reflect availability and precision usage
- Tests updated for discovery and availability behavior
- Ignore .cursor/ in repo root
2026-05-05 17:47:55 +10:00

46 lines
1.2 KiB
Arduino

#include <Ethernet.h>
#include <ArduinoHA.h>
#define BROKER_ADDR IPAddress(192,168,0,17)
byte mac[] = {0x00, 0x10, 0xFA, 0x6E, 0x38, 0x4A};
EthernetClient client;
HADevice device(mac, sizeof(mac));
HAMqtt mqtt(client, device);
// "myValve" is unique ID of the sensor. You should define your own ID.
HASensor valve("myValve");
void setup() {
// you don't need to verify return status
Ethernet.begin(mac);
// set device's details (optional)
device.setName("Arduino");
device.setSoftwareVersion("1.0.0");
// configure sensor (optional)
valve.setIcon("mdi:home");
valve.setName("Water valve");
// valve.setValueTemplate("{{ value_json.state }}");
// valve.setDeviceClass("enum");
// valve.setOptions("open;opening;closed");
// valve.setDefaultEntityId("sensor.water_valve");
mqtt.begin(BROKER_ADDR);
}
void loop() {
Ethernet.maintain();
mqtt.loop();
// you can report different states as follows:
// valve.setValue("open");
// valve.setValue("opening");
// valve.setValue("close");
// Unlike the other device types, the HASensor doesn't store the previous value that was set.
// It means that the MQTT message is produced each time the setValue method is called.
}