Skip to content

Commit

Permalink
Fix OpenHAB discovery (#1153)
Browse files Browse the repository at this point in the history
* Fix OpenHAB discovery

* Add test for OpenHAB discovery
  • Loading branch information
kabili207 authored Feb 3, 2022
1 parent 0a8458f commit 457a8f4
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 39 deletions.
81 changes: 42 additions & 39 deletions main/config_mqttDiscovery.h
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,9 @@ void announceDeviceTrigger(bool use_gateway_info,

/*-------------- Auto discovery macros-----------------*/
// Set the line below to true so as to have autodiscovery working with OpenHAB
#define OpenHABDiscovery false
#ifndef OpenHABDiscovery
# define OpenHABDiscovery false
#endif

#if OpenHABDiscovery // OpenHAB autodiscovery value key definition (is defined command is not supported by OpenHAB)
# define jsonBatt "{{ value_json.batt }}"
Expand Down Expand Up @@ -156,46 +158,47 @@ void announceDeviceTrigger(bool use_gateway_info,
# define jsonCount "{{ value_json.count }}"
# define jsonAlarm "{{ value_json.alarm }}"
#else // Home assistant autodiscovery value key definition
# define jsonBatt "{{ value_json.batt | is_defined }}"
# define jsonLux "{{ value_json.lux | is_defined }}"
# define jsonPres "{{ value_json.pres | is_defined }}"
# define jsonFer "{{ value_json.fer | is_defined }}"
# define jsonFor "{{ value_json.for | is_defined }}"
# define jsonMoi "{{ value_json.moi | is_defined }}"
# define jsonHum "{{ value_json.hum | is_defined }}"
# define jsonStep "{{ value_json.steps | is_defined }}"
# define jsonWeight "{{ value_json.weight | is_defined }}"
# define jsonPresence "{{ value_json.presence | is_defined }}"
# define jsonAltim "{{ value_json.altim | is_defined }}"
# define jsonAltif "{{ value_json.altift | is_defined }}"
# define jsonTempc "{{ value_json.tempc | is_defined }}"
# define jsonTempc2 "{{ value_json.tempc2 | is_defined }}"
# define jsonTempc3 "{{ value_json.tempc3 | is_defined }}"
# define jsonTempc4 "{{ value_json.tempc4 | is_defined }}"
# define jsonTempf "{{ value_json.tempf | is_defined }}"
# define jsonMsg "{{ value_json.message | is_defined }}"
# define jsonVal "{{ value_json.value | is_defined }}"
# define jsonVolt "{{ value_json.volt | is_defined }}"
# define jsonCurrent "{{ value_json.current | is_defined }}"
# define jsonPower "{{ value_json.power | is_defined }}"
# define jsonEnergy "{{ value_json.energy | is_defined }}"
# define jsonGpio "{{ value_json.gpio | is_defined }}"
# define jsonFtcd "{{ value_json.ftcd | is_defined }}"
# define jsonWm2 "{{ value_json.wattsm2 | is_defined }}"
# define jsonAdc "{{ value_json.adc | is_defined }}"
# define jsonPa "{{ float(value_json.pa) * 0.01 | is_defined }}"
# define jsonId "{{ value_json.id | is_defined }}"
# define jsonAddress "{{ value_json.address | is_defined }}"
# define jsonOpen "{{ value_json.open | is_defined }}"
# define jsonTime "{{ value_json.time | is_defined }}"
# define jsonCount "{{ value_json.count | is_defined }}"
# define jsonAlarm "{{ value_json.alarm | is_defined }}"
# define stateClassNone ""
# define stateClassMeasurement "measurement"
# define stateClassTotal "total"
# define stateClassTotalIncreasing "total_increasing"
# define jsonBatt "{{ value_json.batt | is_defined }}"
# define jsonLux "{{ value_json.lux | is_defined }}"
# define jsonPres "{{ value_json.pres | is_defined }}"
# define jsonFer "{{ value_json.fer | is_defined }}"
# define jsonFor "{{ value_json.for | is_defined }}"
# define jsonMoi "{{ value_json.moi | is_defined }}"
# define jsonHum "{{ value_json.hum | is_defined }}"
# define jsonStep "{{ value_json.steps | is_defined }}"
# define jsonWeight "{{ value_json.weight | is_defined }}"
# define jsonPresence "{{ value_json.presence | is_defined }}"
# define jsonAltim "{{ value_json.altim | is_defined }}"
# define jsonAltif "{{ value_json.altift | is_defined }}"
# define jsonTempc "{{ value_json.tempc | is_defined }}"
# define jsonTempc2 "{{ value_json.tempc2 | is_defined }}"
# define jsonTempc3 "{{ value_json.tempc3 | is_defined }}"
# define jsonTempc4 "{{ value_json.tempc4 | is_defined }}"
# define jsonTempf "{{ value_json.tempf | is_defined }}"
# define jsonMsg "{{ value_json.message | is_defined }}"
# define jsonVal "{{ value_json.value | is_defined }}"
# define jsonVolt "{{ value_json.volt | is_defined }}"
# define jsonCurrent "{{ value_json.current | is_defined }}"
# define jsonPower "{{ value_json.power | is_defined }}"
# define jsonEnergy "{{ value_json.energy | is_defined }}"
# define jsonGpio "{{ value_json.gpio | is_defined }}"
# define jsonFtcd "{{ value_json.ftcd | is_defined }}"
# define jsonWm2 "{{ value_json.wattsm2 | is_defined }}"
# define jsonAdc "{{ value_json.adc | is_defined }}"
# define jsonPa "{{ float(value_json.pa) * 0.01 | is_defined }}"
# define jsonId "{{ value_json.id | is_defined }}"
# define jsonAddress "{{ value_json.address | is_defined }}"
# define jsonOpen "{{ value_json.open | is_defined }}"
# define jsonTime "{{ value_json.time | is_defined }}"
# define jsonCount "{{ value_json.count | is_defined }}"
# define jsonAlarm "{{ value_json.alarm | is_defined }}"
#endif

#define stateClassNone ""
#define stateClassMeasurement "measurement"
#define stateClassTotal "total"
#define stateClassTotalIncreasing "total_increasing"

// From https://github.com/home-assistant/core/blob/d7ac4bd65379e11461c7ce0893d3533d8d8b8cbf/homeassistant/const.py#L225
// List of classes available in Home Assistant
const char* availableHASSClasses[] = {"battery",
Expand Down
13 changes: 13 additions & 0 deletions platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ extra_configs =
;default_envs = uno-fastled
;default_envs = atmega-all-test
;default_envs = manual-wifi-test
;default_envs = openhab-test
;default_envs = esp32dev-mqtt-fw-test
;default_envs = nodemcuv2-mqtt-fw-test
;default_envs = nodemcuv2-rs232
Expand Down Expand Up @@ -1014,6 +1015,18 @@ build_flags =
'-DGateway_Name="OpenMQTTGateway_TEST_MANUAL_WIFI"'
board_build.flash_mode = dout

[env:openhab-test]
platform = ${com.esp8266_platform}
board = nodemcuv2
lib_deps =
${com-esp.lib_deps}
${libraries.wifimanager8266}
build_flags =
${com-esp.build_flags}
'-DOpenHABDiscovery=true'
'-DGateway_Name="OpenMQTTGateway_TEST_OPENHAB"'
board_build.flash_mode = dout

[env:nodemcuv2-mqtt-fw-test]
platform = ${com.esp8266_platform}
board = nodemcuv2
Expand Down

0 comments on commit 457a8f4

Please sign in to comment.