guerrerotook / securitas-direct-new-api

This repository contains the new securitas direct API that can be integrated in Home Assistant
Apache License 2.0
79 stars 30 forks source link

AttributeError: 'coroutine' object has no attribute 'text' - Wrong alarm status #42

Closed ccorderod closed 2 years ago

ccorderod commented 2 years ago

Hi.

This is the information for my HA instance: System Health

version: core-2022.2.9 installation_type: Home Assistant OS dev: false hassio: true docker: true user: root virtualenv: false python_version: 3.9.7 os_name: Linux os_version: 5.10.98 arch: x86_64 timezone: Europe/Madrid

GitHub API: ok GitHub Content: ok GitHub Web: ok GitHub API Calls Remaining: 4685 Installed Version: 1.23.0 Stage: running Available Repositories: 1065 Downloaded Repositories: 52

can_reach_server: ok remaining_requests: 30

logged_in: true subscription_expiration: March 9, 2022, 1:00 AM relayer_connected: true remote_enabled: true remote_connected: true alexa_enabled: true google_enabled: true remote_server: eu-central-1-2.ui.nabu.casa can_reach_cert_server: ok can_reach_cloud_auth: ok can_reach_cloud: ok

host_os: Home Assistant OS 7.4 update_channel: beta supervisor_version: supervisor-2022.01.1 docker_version: 20.10.9 disk_total: 491.5 GB disk_used: 293.7 GB healthy: true supported: true board: ova supervisor_api: ok version_api: ok installed_addons: Terminal & SSH (9.3.0), A better presence (1.2.0), AdGuard Home (4.4.4), AppDaemon 4 (0.8.1), Check Home Assistant configuration (3.10.0), Duck DNS (1.14.0), File editor (5.3.3), Folding@home (0.4.1), Glances (0.14.2), Grafana (7.4.1), Grocy (0.16.0), Mosquitto broker (6.0.1), Network UPS Tools (0.9.1), Nginx Proxy Manager (0.11.0), Node-RED (11.0.3), Samba share (9.5.1), Syncthing (1.13.0), chrony (2.2.2), php-nginx Docker Image (latest), phpMyAdmin (0.7.0), rest980 Docker Image (20220113), roombapw Docker Image (1.0.0), Git pull (7.13.1), Let's Encrypt (4.12.0), MariaDB (2.4.0), InfluxDB (4.3.0), ESPHome (2022.2.4), Portainer (2.0.0)

dashboards: 3 resources: 26 views: 22 mode: yaml

Securitas-direct-new-api: v1.1.3.0

Get this error in the log from time to time: 2022-02-21 16:05:54 ERROR (MainThread) [homeassistant.helpers.entity] Update for alarm_control_panel.securitas_xxxxxxxx fails Traceback (most recent call last): File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 521, in async_update_ha_state await self.async_device_update() File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 725, in async_device_update raise exc File "/config/custom_components/securitas/alarm_control_panel.py", line 271, in async_update alarm_status: CheckAlarmStatus = await self.client.update_overview( File "/config/custom_components/securitas/init.py", line 277, in update_overview reference_id: str = await self.session.check_alarm(installation) File "/config/custom_components/securitas/securitas_direct_new_api/apimanager.py", line 179, in check_alarm result_json = json.loads(await response.text()) AttributeError: 'coroutine' object has no attribute 'text'

Today I have had the following issues with the component:

I have a full log with DEBUG enabled for the component if required.

Thank you. Carlos

guerrerotook commented 2 years ago

The operation in there is the check_alarm and it seems like it fails. There could be multiple reason. If the session is expired a message appear saying "Login is expired. Login again" and then if the login is succeeded, something like "Re-loging result (true|false)" depending on if succeed or fails.

ccorderod commented 2 years ago

You are right: session expired. `2022-02-21 16:05:53 DEBUG (MainThread) [custom_components.securitas.securitas_direct_new_api.apimanager] {'operationName': 'CheckAlarm', 'variables': {'numinst': 'XXXXXXX', 'panel': 'SDVFAST'}, 'query': 'query CheckAlarm($numinst: String!, $panel: String!) {\n xSCheckAlarm(numinst: $numinst, panel: $panel) {\n res\n msg\n referenceId\n }\n}\n'} 2022-02-21 16:05:53 DEBUG (MainThread) [custom_components.securitas.securitas_direct_new_api.apimanager] {'operationName': 'CheckAlarm', 'variables': {'numinst': 'YYYYYYY', 'panel': 'SDVFSW'}, 'query': 'query CheckAlarm($numinst: String!, $panel: String!) {\n xSCheckAlarm(numinst: $numinst, panel: $panel) {\n res\n msg\n referenceId\n }\n}\n'} 2022-02-21 16:05:53 DEBUG (MainThread) [custom_components.securitas.securitas_direct_new_api.apimanager] {'operationName': 'CheckAlarm', 'variables': {'numinst': 'YYYYYYY', 'panel': 'SDVFSW'}, 'query': 'query CheckAlarm($numinst: String!, $panel: String!) {\n xSCheckAlarm(numinst: $numinst, panel: $panel) {\n res\n msg\n referenceId\n }\n}\n'} 2022-02-21 16:05:53 DEBUG (MainThread) [custom_components.securitas.securitas_direct_new_api.apimanager] {"errors":[{"message":"Invalid token: Expired","name":"ApiError","time_thrown":"2022-02-21T15:05:53.868Z","data":{"res":"ERROR","err":"60022"},"path":["xSCheckAlarm"]}],"data":{"xSCheckAlarm":null}}

2022-02-21 16:05:53 INFO (MainThread) [custom_components.securitas.securitas_direct_new_api.apimanager] Login is expired. Login again 2022-02-21 16:05:53 DEBUG (MainThread) [custom_components.securitas.securitas_direct_new_api.apimanager] {'operationName': 'LoginToken', 'variables': {'id': 'xxxxxxxxxxxxxxxxxxxx', 'country': 'ES', 'callby': 'AAAAA', 'lang': 'es', 'user': 'johndoe', 'password': 'npass'}, 'query': 'mutation LoginToken($user: String!, $password: String!, $id: String!, $country: String!, $lang: String!, $callby: String!) {\n xSLoginToken(user: $user, password: $password, id: $id, country: $country, lang: $lang, callby: $callby) {\n res\n msg\n hash\n lang\n legals\n mainUser\n changePassword\n }\n}\n'} 2022-02-21 16:05:53 DEBUG (MainThread) [custom_components.securitas.securitas_direct_new_api.apimanager] {"errors":[{"message":"Invalid token: Expired","name":"ApiError","time_thrown":"2022-02-21T15:05:53.875Z","data":{"res":"ERROR","err":"60022"},"path":["xSCheckAlarm"]}],"data":{"xSCheckAlarm":null}}

2022-02-21 16:05:53 INFO (MainThread) [custom_components.securitas.securitas_direct_new_api.apimanager] Login is expired. Login again 2022-02-21 16:05:53 DEBUG (MainThread) [custom_components.securitas.securitas_direct_new_api.apimanager] {'operationName': 'LoginToken', 'variables': {'id': 'xxxxxxxx', 'country': 'ES', 'callby': 'aaaaa', 'lang': 'es', 'user': 'johndoe', 'password': 'nopass'}, 'query': 'mutation LoginToken($user: String!, $password: String!, $id: String!, $country: String!, $lang: String!, $callby: String!) {\n xSLoginToken(user: $user, password: $password, id: $id, country: $country, lang: $lang, callby: $callby) {\n res\n msg\n hash\n lang\n legals\n mainUser\n changePassword\n }\n}\n'} 2022-02-21 16:05:53 DEBUG (MainThread) [custom_components.securitas.securitas_direct_new_api.apimanager] {"errors":[{"message":"Invalid token: Expired","name":"ApiError","time_thrown":"2022-02-21T15:05:53.887Z","data":{"res":"ERROR","err":"60022"},"path":["xSCheckAlarm"]}],"data":{"xSCheckAlarm":null}}

2022-02-21 16:05:53 INFO (MainThread) [custom_components.securitas.securitas_direct_new_api.apimanager] Login is expired. Login again 2022-02-21 16:05:53 DEBUG (MainThread) [custom_components.securitas.securitas_direct_new_api.apimanager] {'operationName': 'LoginToken', 'variables': {'id': 'xxxxxxxx', 'country': 'ES', 'callby': 'aaaaaa', 'lang': 'es', 'user': 'johndoe', 'password': 'nopass'}, 'query': 'mutation LoginToken($user: String!, $password: String!, $id: String!, $country: String!, $lang: String!, $callby: String!) {\n xSLoginToken(user: $user, password: $password, id: $id, country: $country, lang: $lang, callby: $callby) {\n res\n msg\n hash\n lang\n legals\n mainUser\n changePassword\n }\n}\n'} 2022-02-21 16:05:54 DEBUG (MainThread) [custom_components.securitas.securitas_direct_new_api.apimanager] {"data":{"xSLoginToken":{"res":"OK","msg":"Usuario validado correctamente","hash":"myhash","lang":"ES","legals":true,"mainUser":true,"changePassword":false}}}

2022-02-21 16:05:54 DEBUG (MainThread) [custom_components.securitas.securitas_direct_new_api.apimanager] Re-loging result True 2022-02-21 16:05:54 ERROR (MainThread) [homeassistant.helpers.entity] Update for alarm_control_panel.securitas_XXXXXXX fails Traceback (most recent call last): File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 521, in async_update_ha_state await self.async_device_update() File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 725, in async_device_update raise exc File "/config/custom_components/securitas/alarm_control_panel.py", line 271, in async_update alarm_status: CheckAlarmStatus = await self.client.update_overview( File "/config/custom_components/securitas/init.py", line 277, in update_overview reference_id: str = await self.session.check_alarm(installation) File "/config/custom_components/securitas/securitas_direct_new_api/apimanager.py", line 179, in check_alarm result_json = json.loads(await response.text()) AttributeError: 'coroutine' object has no attribute 'text' 2022-02-21 16:05:54 DEBUG (MainThread) [custom_components.securitas.securitas_direct_new_api.apimanager] {"data":{"xSLoginToken":{"res":"OK","msg":"Usuario validado correctamente","hash":"myhass","lang":"ES","legals":true,"mainUser":true,"changePassword":false}}}

2022-02-21 16:05:54 DEBUG (MainThread) [custom_components.securitas.securitas_direct_new_api.apimanager] Re-loging result True 2022-02-21 16:05:54 DEBUG (MainThread) [custom_components.securitas.securitas_direct_new_api.apimanager] {"data":{"xSLoginToken":{"res":"OK","msg":"Usuario validado correctamente","hash":"myash","lang":"ES","legals":true,"mainUser":true,"changePassword":false}}}

2022-02-21 16:05:54 DEBUG (MainThread) [custom_components.securitas.securitas_direct_new_api.apimanager] Re-loging result True 2022-02-21 16:05:54 ERROR (MainThread) [homeassistant.helpers.entity] Update for alarm_control_panel.securitas_XXXXXXX fails Traceback (most recent call last): File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 521, in async_update_ha_state await self.async_device_update() File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 725, in async_device_update raise exc File "/config/custom_components/securitas/alarm_control_panel.py", line 271, in async_update alarm_status: CheckAlarmStatus = await self.client.update_overview( File "/config/custom_components/securitas/init.py", line 277, in update_overview reference_id: str = await self.session.check_alarm(installation) File "/config/custom_components/securitas/securitas_direct_new_api/apimanager.py", line 179, in check_alarm result_json = json.loads(await response.text()) AttributeError: 'coroutine' object has no attribute 'text' 2022-02-21 16:05:54 ERROR (MainThread) [homeassistant.helpers.entity] Update for alarm_control_panel.securitas_xxxxxxx fails Traceback (most recent call last): File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 521, in async_update_ha_state await self.async_device_update() File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 725, in async_device_update raise exc File "/config/custom_components/securitas/alarm_control_panel.py", line 271, in async_update alarm_status: CheckAlarmStatus = await self.client.update_overview( File "/config/custom_components/securitas/init.py", line 277, in update_overview reference_id: str = await self.session.check_alarm(installation) File "/config/custom_components/securitas/securitas_direct_new_api/apimanager.py", line 179, in check_alarm result_json = json.loads(await response.text()) AttributeError: 'coroutine' object has no attribute 'text' `

I have 3 alarms under this account. The component does a re-logging for each alarm (IMHO, one re-logging should be enough). Is there is anyway to avoid this error? One more question: what about the alarm status? why is it taking so much time to get updated? Gracias!. Carlos

guerrerotook commented 2 years ago

The check alarm function connects to your alarm and check the status. That process takes a lot of time.

That is why I add a new setting check_alarm_panel (you can check the readme.md) that check the last status that Securitas has in their server. That is more fast and less intrusive. And also, doesn't leave any logs in your account like you're checking your alarm every 10 minutes.

About the installation, I also have multiple, and the client API is the same instance for all components, alarms_panel and senors, so only one client is created.

Try to set the check_alarm_panel to false to remove this check_alarm call.

guerrerotook commented 2 years ago

Solved the origina issue in the last release.