22
33from dataclasses import dataclass
44import logging
5- from typing import TYPE_CHECKING , Final
5+ from typing import TYPE_CHECKING , Any , Final
66
77from saic_ismart_client_ng .exceptions import SaicApiException , SaicLogoutException
88
@@ -69,6 +69,15 @@ async def handle_mqtt_command(self, *, topic: str, payload: str) -> None:
6969 handler = handler , payload = payload , analyzed_topic = analyzed_topic
7070 )
7171
72+ def __publish_command_error (self , command : str , detail : str ) -> None :
73+ error_topic = self .vehicle_state .get_topic (mqtt_topics .COMMAND_ERROR )
74+ event_payload : dict [str , Any ] = {
75+ "event_type" : "command_error" ,
76+ "command" : command ,
77+ "detail" : detail ,
78+ }
79+ self .publisher .publish_json (error_topic , event_payload )
80+
7281 async def __execute_mqtt_command_handler (
7382 self ,
7483 * ,
@@ -91,6 +100,7 @@ async def __execute_mqtt_command_handler(
91100 self .publisher .clear_topic (topic_no_global )
92101 except MqttGatewayException as e :
93102 self .publisher .publish_str (result_topic , f"Failed: { e .message } " )
103+ self .__publish_command_error (topic , e .message )
94104 LOG .exception (e .message , exc_info = e )
95105 except SaicLogoutException :
96106 LOG .warning (
@@ -99,9 +109,11 @@ async def __execute_mqtt_command_handler(
99109 try :
100110 await self .relogin_handler .force_login ()
101111 except Exception as login_err :
112+ detail = f"relogin failed ({ login_err } )"
102113 self .publisher .publish_str (
103- result_topic , f"Failed: relogin failed ( { login_err } ) "
114+ result_topic , f"Failed: { detail } "
104115 )
116+ self .__publish_command_error (topic , detail )
105117 LOG .error ("Immediate relogin failed" , exc_info = login_err )
106118 return
107119 try :
@@ -115,17 +127,21 @@ async def __execute_mqtt_command_handler(
115127 if execution_result .clear_command :
116128 self .publisher .clear_topic (topic_no_global )
117129 except Exception as retry_err :
130+ detail = str (retry_err )
118131 self .publisher .publish_str (
119- result_topic , f"Failed: { retry_err } "
132+ result_topic , f"Failed: { detail } "
120133 )
134+ self .__publish_command_error (topic , detail )
121135 LOG .error (
122136 "Command retry after relogin failed" , exc_info = retry_err
123137 )
124138 except SaicApiException as se :
125139 self .publisher .publish_str (result_topic , f"Failed: { se .message } " )
140+ self .__publish_command_error (topic , se .message )
126141 LOG .exception (se .message , exc_info = se )
127142 except Exception as se :
128143 self .publisher .publish_str (result_topic , "Failed unexpectedly" )
144+ self .__publish_command_error (topic , str (se ))
129145 LOG .exception (
130146 "handle_mqtt_command failed with an unexpected exception" , exc_info = se
131147 )
0 commit comments