Publish IP and SSID to Home Assistant (#1262)

This commit is contained in:
CrewMdk
2024-06-14 00:34:02 -06:00
committed by GitHub
parent 0ea29bb7b4
commit 2c6686e8ac
3 changed files with 37 additions and 7 deletions

View File

@ -104,10 +104,16 @@ void hass_populate_unique_id(ENTITY_TYPE type, int index, char* uniq_id) {
break;
case HASS_UPTIME:
sprintf(uniq_id, "%s_uptime", longDeviceName);
break;
break;
case HASS_BUILD:
sprintf(uniq_id, "%s_build", longDeviceName);
break;
break;
case HASS_SSID:
sprintf(uniq_id, "%s_ssid", longDeviceName);
break;
case HASS_IP:
sprintf(uniq_id, "%s_ip", longDeviceName);
break;
default:
// TODO: USE type here as well?
// If type is not set, and we use "sensor" naming, we can easily make collision
@ -295,7 +301,13 @@ HassDeviceInfo* hass_init_device_info(ENTITY_TYPE type, int index, const char* p
break;
case HASS_BUILD:
sprintf(g_hassBuffer, "Build");
break;
break;
case HASS_SSID:
sprintf(g_hassBuffer, "SSID");
break;
case HASS_IP:
sprintf(g_hassBuffer, "IP");
break;
case ENERGY_SENSOR:
isSensor = true;
sprintf(g_hassBuffer, "Energy");
@ -661,14 +673,12 @@ HassDeviceInfo* hass_init_sensor_device_info(ENTITY_TYPE type, int channel, int
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");
cJSON_AddStringToObject(info->root, "stat_t", "~/rssi");
cJSON_AddStringToObject(info->root, "unit_of_meas", "dBm");
cJSON_AddStringToObject(info->root, "entity_category", "diagnostic");
//cJSON_AddStringToObject(info->root, "icon_template", "mdi:access-point");
break;
case HASS_UPTIME:
cJSON_AddStringToObject(info->root, "dev_cla", "duration");
@ -680,14 +690,24 @@ HassDeviceInfo* hass_init_sensor_device_info(ENTITY_TYPE type, int channel, int
case HASS_BUILD:
cJSON_AddStringToObject(info->root, "stat_t", "~/build");
cJSON_AddStringToObject(info->root, "entity_category", "diagnostic");
break;
break;
case HASS_SSID:
cJSON_AddStringToObject(info->root, "stat_t", "~/ssid");
cJSON_AddStringToObject(info->root, "entity_category", "diagnostic");
cJSON_AddStringToObject(info->root, "icon", "mdi:access-point-network");
break;
case HASS_IP:
cJSON_AddStringToObject(info->root, "stat_t", "~/ip");
cJSON_AddStringToObject(info->root, "entity_category", "diagnostic");
cJSON_AddStringToObject(info->root, "icon", "mdi:ip-network");
break;
default:
sprintf(g_hassBuffer, "~/%d/get", channel);
cJSON_AddStringToObject(info->root, "stat_t", g_hassBuffer);
return NULL;
}
if (type != READONLYLOWMIDHIGH_SENSOR && type != HASS_BUILD && !cJSON_HasObjectItem(info->root, "stat_cla")) {
if (type != READONLYLOWMIDHIGH_SENSOR && type != HASS_BUILD && type != HASS_SSID && type != HASS_IP && !cJSON_HasObjectItem(info->root, "stat_cla")) {
cJSON_AddStringToObject(info->root, "stat_cla", "measurement");
}

View File

@ -73,6 +73,10 @@ typedef enum {
HASS_UPTIME,
/// @brief Firmware build info
HASS_BUILD,
/// @brief
HASS_SSID,
/// @brief
HASS_IP,
/// @brief Wh, kWh
ENERGY_SENSOR,
// hPa

View File

@ -2037,6 +2037,12 @@ void doHomeAssistantDiscovery(const char* topic, http_request_t* request) {
dev_info = hass_init_sensor_device_info(HASS_BUILD, -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);
dev_info = hass_init_sensor_device_info(HASS_SSID, -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);
dev_info = hass_init_sensor_device_info(HASS_IP, -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);
discoveryQueued = true;
}