KiraPC / ha-switchbot-remote

A Custom HomeAssistant Integration to control Switchbot Hub
MIT License
54 stars 3 forks source link

Errors and exceptions handling improvement #43

Closed MrXANA91 closed 7 months ago

MrXANA91 commented 8 months ago

This PR aims to fix #36 and to generally improve the exceptions potentially raised by the integration using the Home Assistant exception classes

If the Switchbot servers return a 500 status, the integration will send a warning to the Home Assistant logs. It will wait 0.5s and will then try to resend the same command. After 5 consecutive 500 status failures, it will (should ?) abort and raise an error to Home Assistant.

MrXANA91 commented 8 months ago

Currently testing this version on my HA instance.

I have tested by forcing a returned status to 500 at random, and it effectively resends the same request after 0.5s repeatedly, until the return status is 200. If the 5 tries all failed with 500, then it aborts and raise a SwitchbotInternal500Error exception to the Home Assistant core.

MrXANA91 commented 7 months ago

And of course, since I started testing this fix, the 500 error status no longer appear for me ...

Making it ready for review anyway, still testing

MrXANA91 commented 7 months ago

Going back to draft during testing

Not tested yet, I'll let you know

MrXANA91 commented 7 months ago

A few minutes ago, when trying to send a turn_off command, i've noticed a unusual slightly longer delay between me pressing the button and the AC signaling a received command. When checking the logs, here is what I've found :

1-Caught 500

...
2024-04-08 19:35:00.318 DEBUG (SyncWorker_27) [custom_components.switchbotremote.client.remote] Sending command turnOff
2024-04-08 19:35:00.318 DEBUG (SyncWorker_27) [custom_components.switchbotremote.client.remote] Command payload {'commandType': 'command', 'command': 'turnOff', 'parameter': 'default'}
2024-04-08 19:35:00.318 DEBUG (SyncWorker_27) [custom_components.switchbotremote.client.client] Calling service https://api.switch-bot.com/v1.1/devices/01-202404032246-40583347/commands
2024-04-08 19:35:00.662 DEBUG (SyncWorker_27) [custom_components.switchbotremote.client.client] Received http error 500 {"message": "Internal server error"}
2024-04-08 19:35:00.662 WARNING (SyncWorker_27) [custom_components.switchbotremote.client.client] Caught returned status 500 from SwitchBot API server
2024-04-08 19:35:00.662 DEBUG (SyncWorker_27) [custom_components.switchbotremote.client.client] tryNumber = 0, waiting 500 ms
2024-04-08 19:35:01.164 DEBUG (SyncWorker_27) [custom_components.switchbotremote.client.client] Calling service https://api.switch-bot.com/v1.1/devices/01-202404032246-40583347/commands
2024-04-08 19:35:01.694 DEBUG (SyncWorker_27) [custom_components.switchbotremote.client.client] Call service https://api.switch-bot.com/v1.1/devices/01-202404032246-40583347/commands OK
...

So the fix is actually working properly. Marking it as ready!

KiraPC commented 7 months ago

Great, merging