Public Access
55 lines
1.4 KiB
C++
55 lines
1.4 KiB
C++
#include "net/home_assistant.h"
|
|||
|
|
|
||
|
|
#include <ArduinoHA.h>
|
||
|
|
#include <WiFi.h>
|
||
|
|
|
||
|
|
#include "config.h"
|
||
|
|
#include "credentials.h"
|
||
|
|
|
||
|
|
static WiFiClient client;
|
||
|
|
static HADevice device(HA_DEVICE_ID, sizeof(HA_DEVICE_ID));
|
||
|
|
static HAMqtt mqtt(client, device);
|
||
|
|
|
||
|
|
// Unique ids: do not change, Home Assistant's entities are linked to them
|
||
|
|
static HASwitch clockSwitch("FlipDotDisplayClock");
|
||
|
|
static HASwitch powerSwitch("FlipDotDisplayPower");
|
||
|
|
|
||
|
|
static DisplayController *displayController = nullptr;
|
||
|
|
|
||
|
|
// The switches are not optimistic: the new state must be reported back to Home Assistant
|
||
|
|
static void onClockCommand(bool state, HASwitch *sender)
|
||
|
|
{
|
||
|
|
displayController->setClockEnabled(state);
|
||
|
|
sender->setState(state);
|
||
|
|
}
|
||
|
|
|
||
|
|
static void onPowerCommand(bool state, HASwitch *sender)
|
||
|
|
{
|
||
|
|
displayController->setPowerEnabled(state);
|
||
|
|
sender->setState(state);
|
||
|
|
}
|
||
|
|
|
||
|
|
void homeAssistantBegin(DisplayController &controller)
|
||
|
|
{
|
||
|
|
displayController = &controller;
|
||
|
|
|
||
|
|
device.setName(HA_DEVICE_NAME);
|
||
|
|
device.setSoftwareVersion(HA_SOFTWARE_VERSION);
|
||
|
|
|
||
|
|
clockSwitch.setName("FlipDot Display Clock");
|
||
|
|
clockSwitch.setIcon("mdi:lightbulb");
|
||
|
|
clockSwitch.setCurrentState(controller.clockEnabled());
|
||
|
|
clockSwitch.onCommand(onClockCommand);
|
||
|
|
|
||
|
|
powerSwitch.setName("FlipDot Display Power");
|
||
|
|
powerSwitch.setIcon("mdi:lightbulb");
|
||
|
|
powerSwitch.setCurrentState(controller.powerEnabled());
|
||
|
|
powerSwitch.onCommand(onPowerCommand);
|
||
|
|
|
||
|
|
mqtt.begin(BROKER_ADDR, MQTT_USER, MQTT_PASSWORD);
|
||
|
|
}
|
||
|
|
|
||
|
|
void homeAssistantLoop()
|
||
|
|
{
|
||
|
|
mqtt.loop();
|
||
|
|
}
|