hsakoh / switchbot-mqtt

SwitchBot MQTT Home Assistant add-on
MIT License
41 stars 1 forks source link

start webhook failed #18

Closed noeltam closed 4 months ago

noeltam commented 5 months ago

Hi, your development is so great. I can see swithbot hub and control the curtain 3.

Sorry, I config the polling service work well , but my curtain 3 sliding position seems can't update? How long of the intervals?

or I require to setup the webhook service to see the most update sliding position? what's the different between polling and webhook?

I tried to setup the webhook without Ngrok, but I can't start the webhook service. My setting at below: WebhookService:UseNgrok: false WebhookService:HostUrl: http://ip-address:8123 or http://ip-address:8123/webhook

Please advise. Thank you very much.

hsakoh commented 5 months ago

Thank you for the feedback.

The value of the slidePosition sensor can be obtained either through webhook reception or polling.

In the case of webhook, in my environment, I receive it approximately once every 1 or 2 seconds while opening or closing operations are in progress. If automatic polling is configured, it retrieves at the specified frequency. (PT1H -> 1 hour) For manual polling, it retrieves at the timing of pressing.

The Position input for the setPosition command is write-only and does not change the actual value.

When receiving webhooks without Ngrok, the port was not exposed externally. I just released v1.0.11 with a fix to config.yaml, so please update and try again. https://github.com/hsakoh/ha-addon/blob/main/switchbot-mqtt/config.yaml#L6-L7 If specifying HostUrl to expose the default port, try specifying http://ip-address:8098.

If the webhook service fails to start even with the new version, I believe there might be an error in the add-on's logs. Could you please provide the details of any errors you see?

noeltam commented 4 months ago

Thanks your advise. I update the polling frequency and can received the curtain sliding position update. I success using the external public ip to start the webhook now. When I use scene to open / close the curtain, the slidng position still can't update by webhook. Is it because I am using scene to open/close the curtain? below is the log. Thanks again

10:26:45 warn: Microsoft.AspNetCore.DataProtection.KeyManagement.XmlKeyManager[35] No XML encryptor configured. Key {xxx} may be persisted to storage in unencrypted form. 10:26:45 info: Microsoft.Hosting.Lifetime[14] Now listening on: http://[::]:8098 10:26:45 info: Microsoft.Hosting.Lifetime[14] Now listening on: http://[::]:8099 10:26:45 info: Microsoft.Hosting.Lifetime[0] Application started. Press Ctrl+C to shut down. 10:26:45 info: Microsoft.Hosting.Lifetime[0] Hosting environment: Production 10:26:45 info: Microsoft.Hosting.Lifetime[0] Content root path: /app/ 10:27:02 info: SwitchBotMqttApp.Logics.DeviceConfigurationManager[0] device configuration file found. 4,0 10:27:03 trce: SwitchBotMqttApp.Logics.SwitchBotApiClient[0] Send GET,devices/xxx/status 10:27:05 trce: SwitchBotMqttApp.Logics.SwitchBotApiClient[0] Response GET,https://api.switch-bot.com/v1.1/devices/xxx/status,{"statusCode":100,"body":{"deviceId":"xxx","deviceType":"Curtain3","hubDeviceId":"xxx","calibrate":true,"group":false,"moving":24,"slidePosition":24,"version":"V0.9","battery":85},"message":"success"} 10:27:05 trce: SwitchBotMqttApp.Logics.SwitchBotApiClient[0] Send GET,devices/xxx/status 10:27:06 trce: SwitchBotMqttApp.Logics.SwitchBotApiClient[0] Response GET,https://api.switch-bot.com/v1.1/devices/xxx/status,{"statusCode":100,"body":{"deviceId":"xxx","deviceType":"Curtain3","hubDeviceId":"xxx","calibrate":true,"group":true,"moving":25,"slidePosition":25,"version":"V0.9","battery":85},"message":"success"} 10:27:06 trce: SwitchBotMqttApp.Logics.SwitchBotApiClient[0] Send GET,devices/xxx/status 10:27:07 trce: SwitchBotMqttApp.Logics.SwitchBotApiClient[0] Response GET,https://api.switch-bot.com/v1.1/devices/xxx/status,{"statusCode":100,"body":{"deviceId":"xxx","deviceType":"Hub 2","hubDeviceId":"xxx","humidity":51,"temperature":25.2,"lightLevel":17,"version":"V2.0-1.2"},"message":"success"} 10:27:07 trce: SwitchBotMqttApp.Logics.SwitchBotApiClient[0] Send GET,scenes 10:27:08 trce: SwitchBotMqttApp.Logics.SwitchBotApiClient[0] Response GET,https://api.switch-bot.com/v1.1/scenes,{"statusCode":100,"body":[{"sceneId":"xxx","sceneName":"Curtain Open"},{"sceneId":"xxx","sceneName":"Curtain Full Open"},{"sceneId":"xxx","sceneName":"Curtain Close"}],"message":"success"} 10:27:08 info: SwitchBotMqttApp.Services.MqttCoreService[0] started 10:27:10 info: SwitchBotMqttApp.Services.PollingService[0] started 10:27:12 trce: SwitchBotMqttApp.Logics.SwitchBotApiClient[0] Send POST,webhook/queryWebhook 10:27:13 trce: SwitchBotMqttApp.Logics.SwitchBotApiClient[0] Response POST,https://api.switch-bot.com/v1.1/webhook/queryWebhook,{"statusCode":190,"body":{},"message":""} 10:27:13 trce: SwitchBotMqttApp.Logics.SwitchBotApiClient[0] Send POST,webhook/setupWebhook 10:27:14 trce: SwitchBotMqttApp.Logics.SwitchBotApiClient[0] Response POST,https://api.switch-bot.com/v1.1/webhook/setupWebhook,{"statusCode":100,"body":{},"message":"success"} 10:27:14 info: SwitchBotMqttApp.Services.WebhookService[0] start listen http://[public-ip-address]:8098/webhook 10:30:24 trce: SwitchBotMqttApp.Logics.SwitchBotApiClient[0] Send POST,scenes/xxx/execute 10:30:25 trce: SwitchBotMqttApp.Logics.SwitchBotApiClient[0] Response POST,https://api.switch-bot.com/v1.1/scenes/xxx/execute,{"statusCode":100,"body":{},"message":"success"}

hsakoh commented 4 months ago

In my testing, webhooks are triggered even when operating the curtains via scenes.

From what I can see in the logs, there don't seem to be any specific errors... It's possible that the webhook endpoint is not reachable from the internet.

Could you verify if webhooks can be called from external sources?

For instance, using a service like https://reqbin.com/, input the following:

URL: http://[public-ip-address]:8098/webhook Method: POST Content-Type: JSON (application/json) Body:

{"eventType":"changeReport","eventVersion":"1","context":{"deviceType":"WoCurtain","deviceMac":"FFF921xxxx","calibrate":true,"group":false,"slidePosition":100,"battery":99,"timeOfSample":1715158038876}}

If webhooks can be invoked externally, you'll see logs like the following:

For an unknown device ID:

warn: SwitchBotMqttApp.Services.MqttCoreService[0] unknown deviceMac FFF921xxxx

For a known device ID:

info: SwitchBotMqttApp.Controllers.WebhookController[0] {"eventType":"changeReport","eventVersion":"1","context":{"deviceType":"WoCurtain","deviceMac":"FFF921xxxx","calibrate":true,"group":false,"slidePosition":50,"battery":99,"timeOfSample":1715335584586}}
noeltam commented 4 months ago

oh . you are correct. Thanks for your help.