@@ -75,21 +75,42 @@ static void cb_ws_event(const void *arg_evh, const esp_event_base_t *base_ev, co
7575 goto cleanup ;
7676 }
7777
78+ cJSON * type = cJSON_GetObjectItemCaseSensitive (cjson , "type" );
79+ if (!cJSON_IsString (type ) || type -> valuestring == NULL ) {
80+ goto cleanup ;
81+ }
82+
83+ if (strcmp (type -> valuestring , "auth_required" ) == 0 ) {
84+ char * hass_token = config_get_char ("hass_token" , DEFAULT_TOKEN );
85+ int len_auth = strlen (hass_token ) + 34 ;
86+ char * auth = calloc (len_auth , sizeof (char ));
87+ snprintf (auth , len_auth , "{\"type\":\"auth\",\"access_token\":\"%s\"}" , hass_token );
88+ free (hass_token );
89+
90+ // we must not send the terminating null byte
91+ int ret = esp_websocket_client_send_text (hdl_wc , auth , len_auth - 1 , 2000 / portTICK_PERIOD_MS );
92+ free (auth );
93+ if (ret < 0 ) {
94+ ESP_LOGE (TAG , "failed to authenticate WebSocket client" );
95+ }
96+ goto cleanup ;
97+ }
98+
7899 cJSON * event = cJSON_GetObjectItemCaseSensitive (cjson , "event" );
79100 if (!cJSON_IsObject (event )) {
80101 goto cleanup ;
81102 }
82103
83- cJSON * type = cJSON_GetObjectItemCaseSensitive (event , "type" );
84- if (!cJSON_IsString (type ) || type -> valuestring == NULL ) {
104+ cJSON * event_type = cJSON_GetObjectItemCaseSensitive (event , "type" );
105+ if (!cJSON_IsString (event_type ) || event_type -> valuestring == NULL ) {
85106 goto cleanup ;
86107 }
87108
88- if (strcmp (type -> valuestring , "run-end" ) == 0 ) {
109+ if (strcmp (event_type -> valuestring , "run-end" ) == 0 ) {
89110 goto end ;
90111 }
91112
92- if (strcmp (type -> valuestring , "intent-end" ) != 0 ) {
113+ if (strcmp (event_type -> valuestring , "intent-end" ) != 0 ) {
93114 goto cleanup ;
94115 }
95116
@@ -215,12 +236,7 @@ static void hass_get_url(char **url, const char *path, const bool ws)
215236
216237static void init_hass_ws_client (void )
217238{
218- char * auth = NULL ;
219- char * hass_token = NULL ;
220239 char * url = NULL ;
221- esp_err_t err ;
222- int len_auth , ret ;
223-
224240 hass_get_url (& url , HASS_URI_WEBSOCKET , true);
225241
226242 const esp_websocket_client_config_t cfg_wc = {
@@ -236,23 +252,9 @@ static void init_hass_ws_client(void)
236252
237253 esp_websocket_register_events (hdl_wc , WEBSOCKET_EVENT_ANY , (esp_event_handler_t )cb_ws_event , NULL );
238254
239- err = esp_websocket_client_start (hdl_wc );
255+ esp_err_t err = esp_websocket_client_start (hdl_wc );
240256 if (err != ESP_OK ) {
241257 ESP_LOGE (TAG , "failed to start WebSocket client: %s" , esp_err_to_name (err ));
242- return ;
243- }
244-
245- hass_token = config_get_char ("hass_token" , DEFAULT_TOKEN );
246- len_auth = strlen (hass_token ) + 34 ;
247- auth = calloc (len_auth , sizeof (char ));
248- snprintf (auth , len_auth , "{\"type\":\"auth\",\"access_token\":\"%s\"}" , hass_token );
249- free (hass_token );
250-
251- // we must not send the terminating null byte
252- ret = esp_websocket_client_send_text (hdl_wc , auth , len_auth - 1 , 2000 / portTICK_PERIOD_MS );
253- free (auth );
254- if (ret < 0 ) {
255- ESP_LOGE (TAG , "failed to authenticate WebSocket client" );
256258 }
257259}
258260
0 commit comments