Skip to content

Commit 4dfa99b

Browse files
committed
standalone: mqtt adapter: handle publish command response errors
1 parent 010a987 commit 4dfa99b

File tree

2 files changed

+24
-10
lines changed

2 files changed

+24
-10
lines changed

src/enapter/standalone/mqtt_adapter.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ async def _execute_commands(self) -> None:
105105
tg.create_task(self._execute_command(request))
106106

107107
async def _execute_command(self, request: mqtt.api.device.CommandRequest) -> None:
108-
await self._device_channel.publish_command_response(
108+
await self._publish_command_response(
109109
request.new_response(
110110
mqtt.api.device.CommandState.LOG, {"message": "Executing command..."}
111111
)
@@ -115,20 +115,28 @@ async def _execute_command(self, request: mqtt.api.device.CommandRequest) -> Non
115115
request.name, request.arguments
116116
)
117117
except NotImplementedError:
118-
await self._device_channel.publish_command_response(
118+
await self._publish_command_response(
119119
request.new_response(
120120
mqtt.api.device.CommandState.ERROR,
121121
{"message": "Command handler not implemented."},
122122
)
123123
)
124124
except Exception:
125-
await self._device_channel.publish_command_response(
125+
await self._publish_command_response(
126126
request.new_response(
127127
mqtt.api.device.CommandState.ERROR,
128128
{"message": traceback.format_exc()},
129129
)
130130
)
131131
else:
132-
await self._device_channel.publish_command_response(
132+
await self._publish_command_response(
133133
request.new_response(mqtt.api.device.CommandState.COMPLETED, payload)
134134
)
135+
136+
async def _publish_command_response(
137+
self, response: mqtt.api.device.CommandResponse
138+
) -> None:
139+
try:
140+
await self._device_channel.publish_command_response(response=response)
141+
except Exception as e:
142+
self._logger.error("failed to publish command response: %s", e)

tests/unit/test_standalone/test_mqtt_adapter.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -183,8 +183,10 @@ async def subscribe_to_command_requests():
183183
while True:
184184
yield await command_requests.get()
185185

186-
async def publish_command_response(r: enapter.mqtt.api.device.CommandResponse):
187-
await command_responses.put(r)
186+
async def publish_command_response(
187+
response: enapter.mqtt.api.device.CommandResponse,
188+
):
189+
await command_responses.put(response)
188190

189191
device_channel.subscribe_to_command_requests = subscribe_to_command_requests
190192
device_channel.publish_command_response = publish_command_response
@@ -228,8 +230,10 @@ async def subscribe_to_command_requests():
228230
while True:
229231
yield await command_requests.get()
230232

231-
async def publish_command_response(r: enapter.mqtt.api.device.CommandResponse):
232-
await command_responses.put(r)
233+
async def publish_command_response(
234+
response: enapter.mqtt.api.device.CommandResponse,
235+
):
236+
await command_responses.put(response)
233237

234238
device_channel.subscribe_to_command_requests = subscribe_to_command_requests
235239
device_channel.publish_command_response = publish_command_response
@@ -273,8 +277,10 @@ async def subscribe_to_command_requests():
273277
while True:
274278
yield await command_requests.get()
275279

276-
async def publish_command_response(r: enapter.mqtt.api.device.CommandResponse):
277-
await command_responses.put(r)
280+
async def publish_command_response(
281+
response: enapter.mqtt.api.device.CommandResponse,
282+
):
283+
await command_responses.put(response)
278284

279285
device_channel.subscribe_to_command_requests = subscribe_to_command_requests
280286
device_channel.publish_command_response = publish_command_response

0 commit comments

Comments
 (0)