Browse Source

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
mcspr-patch-1
Maxim Prokhorov 3 years ago
parent
commit
4d157ccd5b
26 changed files with 23125 additions and 22922 deletions
  1. +4
    -0
      code/espurna/config/general.h
  2. BIN
      code/espurna/data/index.all.html.gz
  3. BIN
      code/espurna/data/index.curtain.html.gz
  4. BIN
      code/espurna/data/index.garland.html.gz
  5. BIN
      code/espurna/data/index.light.html.gz
  6. BIN
      code/espurna/data/index.lightfox.html.gz
  7. BIN
      code/espurna/data/index.rfbridge.html.gz
  8. BIN
      code/espurna/data/index.rfm69.html.gz
  9. BIN
      code/espurna/data/index.sensor.html.gz
  10. BIN
      code/espurna/data/index.small.html.gz
  11. BIN
      code/espurna/data/index.thermostat.html.gz
  12. +671
    -414
      code/espurna/homeassistant.cpp
  13. +11
    -0
      code/espurna/light.cpp
  14. +3
    -0
      code/espurna/light.h
  15. +3066
    -3071
      code/espurna/static/index.all.html.gz.h
  16. +716
    -721
      code/espurna/static/index.curtain.html.gz.h
  17. +1482
    -1487
      code/espurna/static/index.garland.html.gz.h
  18. +2005
    -2010
      code/espurna/static/index.light.html.gz.h
  19. +2256
    -2261
      code/espurna/static/index.lightfox.html.gz.h
  20. +2261
    -2266
      code/espurna/static/index.rfbridge.html.gz.h
  21. +3839
    -3844
      code/espurna/static/index.rfm69.html.gz.h
  22. +2316
    -2321
      code/espurna/static/index.sensor.html.gz.h
  23. +2216
    -2222
      code/espurna/static/index.small.html.gz.h
  24. +2263
    -2268
      code/espurna/static/index.thermostat.html.gz.h
  25. +0
    -22
      code/html/custom.js
  26. +16
    -15
      code/html/index.html

+ 4
- 0
code/espurna/config/general.h View File

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


BIN
code/espurna/data/index.all.html.gz View File


BIN
code/espurna/data/index.curtain.html.gz View File


BIN
code/espurna/data/index.garland.html.gz View File


BIN
code/espurna/data/index.light.html.gz View File


BIN
code/espurna/data/index.lightfox.html.gz View File


BIN
code/espurna/data/index.rfbridge.html.gz View File


BIN
code/espurna/data/index.rfm69.html.gz View File


BIN
code/espurna/data/index.sensor.html.gz View File


BIN
code/espurna/data/index.small.html.gz View File


BIN
code/espurna/data/index.thermostat.html.gz View File


+ 671
- 414
code/espurna/homeassistant.cpp
File diff suppressed because it is too large
View File


+ 11
- 0
code/espurna/light.cpp View File

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


+ 3
- 0
code/espurna/light.h View File

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


+ 3066
- 3071
code/espurna/static/index.all.html.gz.h
File diff suppressed because it is too large
View File


+ 716
- 721
code/espurna/static/index.curtain.html.gz.h
File diff suppressed because it is too large
View File


+ 1482
- 1487
code/espurna/static/index.garland.html.gz.h
File diff suppressed because it is too large
View File


+ 2005
- 2010
code/espurna/static/index.light.html.gz.h
File diff suppressed because it is too large
View File


+ 2256
- 2261
code/espurna/static/index.lightfox.html.gz.h
File diff suppressed because it is too large
View File


+ 2261
- 2266
code/espurna/static/index.rfbridge.html.gz.h
File diff suppressed because it is too large
View File


+ 3839
- 3844
code/espurna/static/index.rfm69.html.gz.h
File diff suppressed because it is too large
View File


+ 2316
- 2321
code/espurna/static/index.sensor.html.gz.h
File diff suppressed because it is too large
View File


+ 2216
- 2222
code/espurna/static/index.small.html.gz.h
File diff suppressed because it is too large
View File


+ 2263
- 2268
code/espurna/static/index.thermostat.html.gz.h
File diff suppressed because it is too large
View File


+ 0
- 22
code/html/custom.js View File

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


+ 16
- 15
code/html/index.html View File

@ -1433,39 +1433,40 @@
<div class="page"> <div class="page">
<fieldset> <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"> <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-2"></div>
<div class="pure-u-0 pure-u-lg-1-4"></div> <div class="pure-u-0 pure-u-lg-1-4"></div>
<div class="pure-u-1 pure-u-lg-3-4 hint"> <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> </div>
<div class="pure-g"> <div class="pure-g">
<label class="pure-u-1 pure-u-lg-1-4">Prefix</label> <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" />
<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">
The prefix for the discovery topic e.g. <code><strong>&lt;prefix&gt;</strong>/switch/espurna-123456/config</code>
</div>
</div> </div>
<legend>Configuration</legend>
<legend>MQTT</legend>
<div class="pure-g"> <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>
<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-0 pure-u-lg-1-4"></div>
<div class="pure-u-1 pure-u-lg-3-4 hint"> <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.
Set the <strong>Retain</strong> flag when sending the messages.
</div> </div>
</div> </div>
<div class="pure-g">
<textarea class="pure-u-1 terminal" id="haConfig" name="haConfig" wrap="off" readonly></textarea>
</div>
</fieldset> </fieldset>
</div> </div>


Loading…
Cancel
Save