Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Publish internal temperature to Home Assistant #1243

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions src/httpserver/hass.c
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,9 @@ void hass_populate_unique_id(ENTITY_TYPE type, int index, char* uniq_id) {
case PRESSURE_SENSOR:
sprintf(uniq_id, "%s_%s_%d", longDeviceName, "pressure", index);
break;
case HASS_TEMP:
sprintf(uniq_id, "%s_temp", longDeviceName);
break;
case HASS_RSSI:
sprintf(uniq_id, "%s_rssi", longDeviceName);
break;
Expand Down Expand Up @@ -281,6 +284,9 @@ HassDeviceInfo* hass_init_device_info(ENTITY_TYPE type, int index, const char* p
case ILLUMINANCE_SENSOR:
sprintf(g_hassBuffer, "Illuminance");
break;
case HASS_TEMP:
sprintf(g_hassBuffer, "Temperature");
break;
case HASS_RSSI:
sprintf(g_hassBuffer, "RSSI");
break;
Expand Down Expand Up @@ -649,6 +655,13 @@ HassDeviceInfo* hass_init_sensor_device_info(ENTITY_TYPE type, int channel, int
cJSON_AddStringToObject(info->root, "stat_t", g_hassBuffer);
cJSON_AddStringToObject(info->root, "val_tpl", g_template_lowMidHigh);

break;
case HASS_TEMP:
cJSON_AddStringToObject(info->root, "dev_cla", "temperature");
cJSON_AddStringToObject(info->root, "stat_t", "~/temp");
cJSON_AddStringToObject(info->root, "unit_of_meas", "°C");
cJSON_AddStringToObject(info->root, "entity_category", "diagnostic");
//cJSON_AddStringToObject(info->root, "icon_template", "mdi:access-point");
break;
case HASS_RSSI:
cJSON_AddStringToObject(info->root, "dev_cla", "signal_strength");
Expand Down
2 changes: 2 additions & 0 deletions src/httpserver/hass.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ typedef enum {
READONLYLOWMIDHIGH_SENSOR,
// lx unit
ILLUMINANCE_SENSOR,
/// @brief °C unit
HASS_TEMP,
/// @brief dBm unit
HASS_RSSI,
/// @brief Time firmware is alive in secs
Expand Down
2 changes: 2 additions & 0 deletions src/httpserver/http_fns.c
Original file line number Diff line number Diff line change
Expand Up @@ -2010,6 +2010,8 @@ void doHomeAssistantDiscovery(const char* topic, http_request_t* request) {
}
if (1) {
//use -1 for channel as these don't correspond to channels
dev_info = hass_init_sensor_device_info(HASS_TEMP, -1, -1, -1, 1);
MQTT_QueuePublish(topic, dev_info->channel, hass_build_discovery_json(dev_info), OBK_PUBLISH_FLAG_RETAIN);
dev_info = hass_init_sensor_device_info(HASS_RSSI, -1, -1, -1, 1);
MQTT_QueuePublish(topic, dev_info->channel, hass_build_discovery_json(dev_info), OBK_PUBLISH_FLAG_RETAIN);
hass_free_device_info(dev_info);
Expand Down
8 changes: 8 additions & 0 deletions src/mqtt/new_mqtt.c
Original file line number Diff line number Diff line change
Expand Up @@ -1776,6 +1776,9 @@ void MQTT_init()
CMD_RegisterCommand("publishFile", MQTT_PublishFile, NULL);
#endif
}
static float getInternalTemperature() {
return g_wifi_temperature;
}

OBK_Publish_Result MQTT_DoItemPublishString(const char* sChannel, const char* valueStr)
{
Expand Down Expand Up @@ -1850,6 +1853,11 @@ OBK_Publish_Result MQTT_DoItemPublish(int idx)
sprintf(dataStr, "%d", LWIP_GetActiveSockets());
return MQTT_DoItemPublishString("sockets", dataStr);


case PUBLISHITEM_SELF_TEMP:
sprintf(dataStr, "%.2f", getInternalTemperature());
return MQTT_DoItemPublishString("temp", dataStr);

case PUBLISHITEM_SELF_RSSI:
sprintf(dataStr, "%d", HAL_GetWifiStrength());
return MQTT_DoItemPublishString("rssi", dataStr);
Expand Down
17 changes: 9 additions & 8 deletions src/mqtt/new_mqtt.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,21 @@ void MQTT_BroadcastTasmotaTeleSTATE();
void MQTT_BroadcastTasmotaTeleSENSOR();


#define PUBLISHITEM_ALL_INDEX_FIRST -16
#define PUBLISHITEM_ALL_INDEX_FIRST -17

//These 3 values are pretty much static
#define PUBLISHITEM_SELF_STATIC_RESERVED_2 -16
#define PUBLISHITEM_SELF_STATIC_RESERVED_1 -15
#define PUBLISHITEM_SELF_HOSTNAME -14 //Device name
#define PUBLISHITEM_SELF_BUILD -13 //Build
#define PUBLISHITEM_SELF_MAC -12 //Device mac
#define PUBLISHITEM_SELF_STATIC_RESERVED_2 -17
#define PUBLISHITEM_SELF_STATIC_RESERVED_1 -16
#define PUBLISHITEM_SELF_HOSTNAME -15 //Device name
#define PUBLISHITEM_SELF_BUILD -14 //Build
#define PUBLISHITEM_SELF_MAC -13 //Device mac

#define PUBLISHITEM_DYNAMIC_INDEX_FIRST -11
#define PUBLISHITEM_DYNAMIC_INDEX_FIRST -12

#define PUBLISHITEM_QUEUED_VALUES -11 //Publish queued items
#define PUBLISHITEM_QUEUED_VALUES -12 //Publish queued items

//These values are dynamic
#define PUBLISHITEM_SELF_TEMP -11 // Internal temp
#define PUBLISHITEM_SELF_SSID -10 // SSID
#define PUBLISHITEM_SELF_DATETIME -9 //Current unix datetime
#define PUBLISHITEM_SELF_SOCKETS -8 //Active sockets
Expand Down
Loading