ha: discovery refactoring

- retain setting
- generate topic and message in the discovery object instead of
creating them all at once
- re-implement relays, lights and sensors discovery
- rework name normalization, use relative magnitude indexes
- replace original lights code with schema: json, subscribe to a special
topic to handle lights instead of using the common API
(still wip - colors, hs, temperature settings)

resolve #630
resolve #1564
resolve #2403
This commit is contained in:
Maxim Prokhorov
2021-01-26 00:50:45 +03:00
parent 0b43af8ed9
commit 4d157ccd5b
26 changed files with 23234 additions and 23031 deletions

View File

@@ -1385,6 +1385,10 @@
#define HOMEASSISTANT_PREFIX "homeassistant" // Default MQTT prefix
#endif
#ifndef HOMEASSISTANT_RETAIN
#define HOMEASSISTANT_RETAIN MQTT_RETAIN // Make broker retain the messages
#endif
// -----------------------------------------------------------------------------
// INFLUXDB
// -----------------------------------------------------------------------------

Binary file not shown.

File diff suppressed because it is too large Load Diff

View File

@@ -109,6 +109,7 @@ Ticker _light_save_ticker;
unsigned long _light_report_delay = LIGHT_REPORT_DELAY;
Ticker _light_report_ticker;
LightReportListener _light_report;
bool _light_has_controls = false;
bool _light_has_color = false;
@@ -1452,6 +1453,12 @@ bool lightUseCCT() {
return _light_use_cct;
}
// -----------------------------------------------------------------------------
void lightSetReportListener(LightReportListener func) {
_light_report = func;
}
void _lightReport(int report) {
#if MQTT_SUPPORT
if (report & Light::Report::Mqtt) {
@@ -1474,6 +1481,10 @@ void _lightReport(int report) {
lightBroker();
}
#endif
if (_light_report) {
_light_report();
}
}
void _lightReport(Light::Report report) {

View File

@@ -7,6 +7,7 @@
#include "espurna.h"
#define MQTT_TOPIC_LIGHT "light"
#define MQTT_TOPIC_LIGHT_JSON "light_json"
#define MQTT_TOPIC_CHANNEL "channel"
#define MQTT_TOPIC_COLOR_RGB "rgb"
#define MQTT_TOPIC_COLOR_HSV "hsv"
@@ -68,6 +69,7 @@ constexpr int DefaultReport {
} // namespace Light
using LightStateListener = std::function<void(bool)>;
using LightReportListener = void(*)();
class LightProvider {
public:
@@ -130,6 +132,7 @@ bool lightUseCCT();
void lightMQTT();
void lightSetReportListener(LightReportListener);
void lightSetStateListener(LightStateListener);
void lightSetProvider(std::unique_ptr<LightProvider>&&);
bool lightAdd();

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@@ -809,15 +809,6 @@ function doScan() {
return false;
}
function doHAConfig() {
$("#haConfig")
.text("")
.height(0)
.show();
sendAction("haconfig", {});
return false;
}
function doDebugCommand() {
var el = $("input[name='dbgcmd']");
var command = el.val();
@@ -1965,18 +1956,6 @@ function processData(data) {
$("#scanResult").show();
}
// -----------------------------------------------------------------------------
// Home Assistant
// -----------------------------------------------------------------------------
if ("haConfig" === key) {
send("{}");
$("#haConfig")
.append(new Text(value))
.height($("#haConfig")[0].scrollHeight);
return;
}
// -----------------------------------------------------------------------------
// Relays scheduler
// -----------------------------------------------------------------------------
@@ -2371,7 +2350,6 @@ $(function() {
$(".button-generate-password").on("click", doGeneratePassword);
$(".button-reboot").on("click", doReboot);
$(".button-reconnect").on("click", doReconnect);
$(".button-ha-config").on("click", doHAConfig);
$(".button-dbgcmd").on("click", doDebugCommand);
$("input[name='dbgcmd']").enterKey(doDebugCommand);
$(".button-dbg-clear").on("click", doDebugClear);

View File

@@ -1433,40 +1433,41 @@
<div class="page">
<fieldset>
<legend>Discover</legend>
<legend><a rel="noopener" target="_blank" href="https://www.home-assistant.io/docs/mqtt/discovery/">Discovery</a></legend>
<div class="pure-g">
<label class="pure-u-1 pure-u-lg-1-4">Discover</label>
<div class="pure-u-1 pure-u-lg-1-4"><input type="checkbox" name="haEnabled" tabindex="14" /></div>
<label class="pure-u-1 pure-u-lg-1-4">Enabled</label>
<div class="pure-u-1 pure-u-lg-1-4"><input type="checkbox" name="haEnabled" /></div>
<div class="pure-u-0 pure-u-lg-1-2"></div>
<div class="pure-u-0 pure-u-lg-1-4"></div>
<div class="pure-u-1 pure-u-lg-3-4 hint">
Home Assistant auto-discovery feature. Enable and save to add the device to your HA console.
Home Assistant MQTT discovery. Enable / disable, then save to trigger the update.
</div>
</div>
<div class="pure-g">
<label class="pure-u-1 pure-u-lg-1-4">Prefix</label>
<input class="pure-u-1 pure-u-lg-1-4" name="haPrefix" type="text" tabindex="15" />
</div>
<legend>Configuration</legend>
<div class="pure-g">
<label class="pure-u-1 pure-u-lg-1-4">Configuration</label>
<div class="pure-u-1-4 pure-u-lg-3-4"><button type="button" class="pure-button button-ha-config pure-u-1-3">Show</button></div>
<input class="pure-u-1 pure-u-lg-1-4" name="haPrefix" type="text" />
<div class="pure-u-0 pure-u-lg-1-2"></div>
<div class="pure-u-0 pure-u-lg-1-4"></div>
<div class="pure-u-1 pure-u-lg-3-4 hint">
These are the settings you should copy to your Home Assistant "configuration.yaml" file.
If any of the sections below (switch, light, sensor) already exists, do not duplicate it,
simply copy the contents of the section below the ones already present.
The prefix for the discovery topic e.g. <code><strong>&lt;prefix&gt;</strong>/switch/espurna-123456/config</code>
</div>
</div>
<div class="pure-g">
<textarea class="pure-u-1 terminal" id="haConfig" name="haConfig" wrap="off" readonly></textarea>
</div>
<legend>MQTT</legend>
<div class="pure-g">
<label class="pure-u-1 pure-u-lg-1-4">Retain</label>
<div class="pure-u-1 pure-u-lg-1-4"><input type="checkbox" name="haRetain" /></div>
<div class="pure-u-0 pure-u-lg-1-2"></div>
<div class="pure-u-0 pure-u-lg-1-4"></div>
<div class="pure-u-1 pure-u-lg-3-4 hint">
Set the <strong>Retain</strong> flag when sending the messages.
</div>
</div>
</fieldset>
</div>