guerrerotook / securitas-direct-new-api

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

Error while setting up securitas platform for alarm_control_panel #137

Open SirLouen opened 1 year ago

SirLouen commented 1 year ago

For some reason, this integration has broken lately. I am not sure if it has anything to do with the latest HA patches... It stopped working for me approximately 1 week ago, it has been working fine for the past months (since the last breaking patch of the await update)

This is the error I can find in the logs:

Error while setting up securitas platform for alarm_control_panel
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/entity_platform.py", line 320, in _async_setup_platform
    await asyncio.shield(task)
  File "/config/custom_components/securitas/alarm_control_panel.py", line 58, in async_setup_entry
    current_state: CheckAlarmStatus = await client.update_overview(
                                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/config/custom_components/securitas/__init__.py", line 407, in update_overview
    alarm_status = await self.session.check_alarm_status(
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/config/custom_components/securitas/securitas_direct_new_api/apimanager.py", line 486, in check_alarm_status
    response: ClientResponse = await self._execute_request(
                               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/config/custom_components/securitas/securitas_direct_new_api/apimanager.py", line 127, in _execute_request
    error_login: bool = await self._check_errros(response_text)
                        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/config/custom_components/securitas/securitas_direct_new_api/apimanager.py", line 149, in _check_errros
    response = json.loads(value)
               ^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/json/__init__.py", line 346, in loads
    return _default_decoder.decode(s)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/json/decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/json/decoder.py", line 355, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

BTW, there is also a warning, but not sure if it's a consequence of the error, or the cause or completely unrelated.

Unable to find services.yaml for the securitas integration

guerrerotook commented 1 year ago

The issue is that the code is trying to parse some json but is not a json file. You can enable the debug output to see the requests and response for the Securitas Direct API and then try to figure out what would be the issue.

SirLouen commented 1 year ago

The issue is that the code is trying to parse some json but is not a json file. You can enable the debug output to see the requests and response for the Securitas Direct API and then try to figure out what would be the issue.

Where I'm supposed to view such debug logs?

image

In System > Logs I can't see anything related Securitas direct plugin

image

The configuration yaml file is mandatory? I think I always have used the UI configuration method

SirLouen commented 1 year ago

I have reconfigured it and I find in the debug that there is an error authenticating (but it asks for the 2FA and it sends the right code and everything seems right at a first glance, but then it can't login for some strange reason

2023-06-17 21:06:36.030 DEBUG (MainThread) [custom_components.securitas.securitas_direct_new_api.apimanager] Making request with device_id e2f704e1575333, uuid e2f704e654445343and idDeviceIndigitall 
2023-06-17 21:06:36.131 DEBUG (MainThread) [custom_components.securitas.securitas_direct_new_api.apimanager] --------------Response--------------
2023-06-17 21:06:36.131 DEBUG (MainThread) [custom_components.securitas.securitas_direct_new_api.apimanager] {"data":{"xSCheckAlarmStatus":{"res":"WAIT","msg":"Petición en proceso","status":null,"numinst":null,"protomResponse":null,"protomResponseDate":null}}}
2023-06-17 21:06:38.263 DEBUG (MainThread) [custom_components.securitas.securitas_direct_new_api.apimanager] --------------Response--------------
2023-06-17 21:06:38.263 DEBUG (MainThread) [custom_components.securitas.securitas_direct_new_api.apimanager] <html style="height:100%"><head><META NAME="ROBOTS" CONTENT="NOINDEX, NOFOLLOW"><meta name="format-detection" content="telephone=no"><meta name="viewport" content="initial-scale=1.0"><meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"></head><body style="margin:0px;height:100%"><iframe id="main-iframe" src="/_Incapsula_Resource?CWUDNSAI=23&xinfo=xxxxxx"> Request unsuccessful. Incapsula incident ID: 313000540156145721</iframe></body></html>
2023-06-17 21:06:38.266 ERROR (MainThread) [homeassistant.components.alarm_control_panel] Error while setting up securitas platform for alarm_control_pane

Generally this last HTML code, happens when you try to log many times wrongly, the Incapsula system is like a protection service they use to avoid brute force attacks.

I can't understand whats going on, but my fair guess is that the integration can't login after the 2FA code.

SirLouen commented 1 year ago

I've tried configured via configuration.yaml and the only difference I see is that it goes straight to the 2FA in the device configuration system, but after that, throw the same errors, and it doesn't configure.

The weird thing is that I have not changed anything in my installation, and it has been working with this integration for the past months without issues. Even more weird is that anyone has reported yet the same scenario as me.

@guerrerotook have you been able to update your HA to latest and your integration is working for you fine?

zav13r commented 1 year ago

@SirLouen, I can confirm that I am having the same issue.

I have removed and reconfigured the integration twice, on latest HA (2023.6.3) and once configured I see the same errors as you and only '1 x Entry' on the integration page, no devices or entities.

SirLouen commented 1 year ago

@zav13r I have an automation set with this integration, that it connects every day at 00.00 and disconnects every day at 8.00 So basically I can spot when things start going weird pretty soon because I use this every single day.

But the thing is that I did not have much time last 2 weeks to research the issue and go into logs to report earlier.

From what I've read in the logs, it seems that despite the first steps of the login procedure are being done and the first login with user/pass goes well, the 2FA seems to be failing and after multiple 2FA login intents, the Securita's Incapsula firewall blocks any future intent.

I'm not 100% confident, maybe they have modified the login method, so the plugin has to be updated.

I hope that @guerrerotook that have found the way to debug into the webserver input and outputs, can find what has been modified in the Securitas side.

SirLouen commented 1 year ago

@guerrerotook is your setting working? This issue is not generalized?

guerrerotook commented 1 year ago

I have no idea about this, I can have my own installation working with the latest version. How do you know about this login issue? Are you using you main user and not a managed user? Sometime it fails because of that

SirLouen commented 1 year ago

I have no idea about this, I can have my own installation working with the latest version. How do you know about this login issue? Are you using you main user and not a managed user? Sometime it fails because of that

What is a managed user and the main user? I'm using the main user (the owner)

It says that the configuration is created but not real entities appear image

Have you tried to reconfigure your integration from scratch? Maybe you are living on an old token.

SirLouen commented 1 year ago

Today I was checking some things in HA and I happened to find this error that I did not spot before:

2023-07-26 12:43:31.772 ERROR (MainThread) [custom_components.securitas.securitas_direct_new_api.apimanager] Unauthorized
2023-07-26 12:43:50.801 WARNING (SyncWorker_1) [homeassistant.helpers.service] Unable to find services.yaml for the securitas integration

It appears that this was reported back in February https://github.com/guerrerotook/securitas-direct-new-api/issues/57

So I have decided to give it another go and completely wipe the custom_component first and then reinstall it back (maybe something got corrupted after some update).

But nothing solved this issue. Maybe those guys, back in February, ditched the integration because they could never make it work ¯_(ツ)_/¯. But I need to make this work because it's probably one of the integrations I use the most by far.

So now I think this is a situational scenario given probably my specific installation. I've been digging a little more into the DEBUG logs after reintegrating everything. In the bottom I attach a massive log I've been able to fetch (I have removed all the things I believe are personal data and a little more)

This is my theory now: the system recognizes a total of 4 installations I do have. But for some reason, after recognizing the installations and the services per installation, it starts failing with xSCheckAlarmStatus request.

I have to say that out of those 4 installations, only 1 is currently working. The others are dummy installations the guys from Securitas made over the years under my account though multiple weird modifications in the contract. I'm not into coding in python, but my theory here is that the xSCheckAlarmStatus with the check_alarm_status function is selecting the wrong installation; hence, it cannot retrieve the alarm status because there is no alarm status for such installation (because it's dummy).

And since it never gets any information from here, it gets stuck in that loop until the Securita's Incapsula Firewall blocks any more repeated petitions and it completely fails to finish the integration.

@guerrerotook I hope this helps you to figure out a solution in the code :)

If you need anything else, if you like, or you need some specific log with more detail, you can contact me directly in telegram at @mkt4p. Thanks, by the way, for the efforts. I hope I could learn a little py in the future and help you out with any future updates this integration could need!

2023-07-26 12:54:59.697 DEBUG (MainThread) [custom_components.securitas.securitas_direct_new_api.apimanager] Making request with device_id knCuPS
2023-07-26 12:55:00.906 DEBUG (MainThread) [custom_components.securitas.securitas_direct_new_api.apimanager] --------------Response--------------
2023-07-26 12:55:00.906 DEBUG (MainThread) [custom_components.securitas.securitas_direct_new_api.apimanager] {"data":{"xSLoginToken":{"__typename":"XSLoginToken","res":"OK","msg":"Usuario validado correctamente","hash":null,"refreshToken":null,"legals":true,"changePassword":false,"needDeviceAuthorization":true,"mainUser":true}}}
2023-07-26 12:55:00.907 INFO (MainThread) [custom_components.securitas] 2FA needed for the device
2023-07-26 13:00:53.833 DEBUG (MainThread) [custom_components.securitas.securitas_direct_new_api.apimanager] Making request with device_id knCuPS
2023-07-26 13:00:53.993 DEBUG (MainThread) [custom_components.securitas.securitas_direct_new_api.apimanager] --------------Response--------------
2023-07-26 13:00:53.993 DEBUG (MainThread) [custom_components.securitas.securitas_direct_new_api.apimanager] {"errors":[{"message":"Unauthorized","name":"SecurityError","time_thrown":"2023-07-26T11:00:53.972Z","data":{"auth-type":"OTP","auth-code":"11111","auth-phones":[{"id":0,"phone":"**********236"}],"auth-otp-hash":"0e0c7cff"},"path":["xSValidateDevice"]}],"data":{"xSValidateDevice":null}}
2023-07-26 13:00:53.993 ERROR (MainThread) [custom_components.securitas.securitas_direct_new_api.apimanager] Unauthorized
2023-07-26 13:01:36.072 DEBUG (MainThread) [custom_components.securitas.securitas_direct_new_api.apimanager] Making request with device_id knCuPS
2023-07-26 13:01:36.311 DEBUG (MainThread) [custom_components.securitas.securitas_direct_new_api.apimanager] --------------Response--------------
2023-07-26 13:01:36.312 DEBUG (MainThread) [custom_components.securitas.securitas_direct_new_api.apimanager] {"data":{"xSSendOtp":{"res":"OK","msg":"Petición procesada correctamente"}}}

2023-07-26 13:01:58.124 DEBUG (MainThread) [custom_components.securitas.securitas_direct_new_api.apimanager] Making request with device_id knCuPS
2023-07-26 13:01:58.220 DEBUG (MainThread) [custom_components.securitas.securitas_direct_new_api.apimanager] --------------Response--------------
2023-07-26 13:01:58.220 DEBUG (MainThread) [custom_components.securitas.securitas_direct_new_api.apimanager] {"data":{"xSValidateDevice":{"res":"OK","msg":"Petición procesada correctamente","hash":null,"refreshToken":null,"legals":null}}}

2023-07-26 13:01:58.220 DEBUG (MainThread) [custom_components.securitas.securitas_direct_new_api.apimanager] Making request with device_id knCuPS
2023-07-26 13:01:58.518 DEBUG (MainThread) [custom_components.securitas.securitas_direct_new_api.apimanager] --------------Response--------------
2023-07-26 13:01:58.518 DEBUG (MainThread) [custom_components.securitas.securitas_direct_new_api.apimanager] {"data":{"xSLoginToken":{"__typename":"XSLoginToken","res":"OK","msg":"Usuario validado correctamente","hash":"eyJhbG","refreshToken":"eyJhbG","legals":true,"changePassword":false,"needDeviceAuthorization":false,"mainUser":true}}}

2023-07-26 13:01:58.519 DEBUG (MainThread) [custom_components.securitas] Log in Securitas: True
2023-07-26 13:01:58.519 DEBUG (MainThread) [custom_components.securitas.securitas_direct_new_api.apimanager] Making request with device_id knCuPS
2023-07-26 13:01:58.625 DEBUG (MainThread) [custom_components.securitas.securitas_direct_new_api.apimanager] --------------Response--------------
2023-07-26 13:01:58.625 DEBUG (MainThread) [custom_components.securitas.securitas_direct_new_api.apimanager] {"data":{"xSInstallations":{"installations":[{"numinst":"1","alias":"A"},{"numinst":"2","alias":"B"},{"numinst":"3","alias":"C"},{"numinst":"4","alias":"D"}]}}}

2023-07-26 13:01:58.627 DEBUG (MainThread) [custom_components.securitas.securitas_direct_new_api.apimanager] Making request with device_id knCuPS
2023-07-26 13:01:58.984 DEBUG (MainThread) [custom_components.securitas.securitas_direct_new_api.apimanager] --------------Response--------------
2023-07-26 13:01:58.984 DEBUG (MainThread) [custom_components.securitas.securitas_direct_new_api.apimanager] {"data":{"xSLoginToken":{"__typename":"XSLoginToken","res":"OK","msg":"Usuario validado correctamente","hash":"eyJhbG","needDeviceAuthorization":false,"mainUser":true}}}

2023-07-26 13:01:58.984 DEBUG (MainThread) [custom_components.securitas] Log in Securitas: True
2023-07-26 13:01:58.985 DEBUG (MainThread) [custom_components.securitas.securitas_direct_new_api.apimanager] Making request with device_id knCuPS
2023-07-26 13:01:59.118 DEBUG (MainThread) [custom_components.securitas.securitas_direct_new_api.apimanager] --------------Response--------------
2023-07-26 13:01:59.118 DEBUG (MainThread) [custom_components.securitas.securitas_direct_new_api.apimanager] {"data":{"xSInstallations":{"installations":[{"numinst":"1","alias":"A"},{"numinst":"2","alias":"B"},{"numinst":"3","alias":"C"},{"numinst":"4","alias":"D"}]}}}

2023-07-26 13:01:59.134 DEBUG (MainThread) [custom_components.securitas.securitas_direct_new_api.apimanager] Making request with device_id knCuPS
2023-07-26 13:01:59.137 DEBUG (MainThread) [custom_components.securitas.securitas_direct_new_api.apimanager] Making request with device_id knCuPS
2023-07-26 13:01:59.304 DEBUG (MainThread) [custom_components.securitas.securitas_direct_new_api.apimanager] --------------Response--------------
2023-07-26 13:01:59.305 DEBUG (MainThread) [custom_components.securitas.securitas_direct_new_api.apimanager] {"data":{"xSSrv":{"res":"OK","msg":"Listado de servicios","language":"ES","installation":{"id":"1","alarm":false},{"id":"3","idService":"47"},{"id":"5","idService":"401"},{"id":"6","idService":"51"},{"id":"7","idService":"510"},{"id":"8","idService":"89"}}}

2023-07-26 13:01:59.306 DEBUG (MainThread) [custom_components.securitas.securitas_direct_new_api.apimanager] Making request with device_id knCuPS
2023-07-26 13:01:59.326 DEBUG (MainThread) [custom_components.securitas.securitas_direct_new_api.apimanager] --------------Response--------------
2023-07-26 13:01:59.327 DEBUG (MainThread) [custom_components.securitas.securitas_direct_new_api.apimanager] {"data":{"xSCheckAlarm":{"res":"OK","msg":"Su solicitud ha sido enviada","referenceId":"OWA"}}}

2023-07-26 13:01:59.522 DEBUG (MainThread) [custom_components.securitas.securitas_direct_new_api.apimanager] --------------Response--------------
2023-07-26 13:01:59.522 DEBUG (MainThread) [custom_components.securitas.securitas_direct_new_api.apimanager] {"data":{"xSSrv":{"res":"OK","msg":"Listado de servicios","language":"ES","installation":{"id":"1","alarm":false},{"id":"3","idService":"47"},{"id":"5","idService":"401"},{"id":"6","idService":"51"},{"id":"7","idService":"510"},{"id":"8","idService":"89"}}}

2023-07-26 13:01:59.524 DEBUG (MainThread) [custom_components.securitas.securitas_direct_new_api.apimanager] Making request with device_id knCuPS
2023-07-26 13:02:00.328 DEBUG (MainThread) [custom_components.securitas.securitas_direct_new_api.apimanager] Making request with device_id knCuPS
2023-07-26 13:02:00.460 DEBUG (MainThread) [custom_components.securitas.securitas_direct_new_api.apimanager] --------------Response--------------
2023-07-26 13:02:00.461 DEBUG (MainThread) [custom_components.securitas.securitas_direct_new_api.apimanager] {"data":{"xSCheckAlarmStatus":{"res":"WAIT","msg":"Petición en proceso","status":null,"numinst":null,"protomResponse":null,"protomResponseDate":null}}}

2023-07-26 13:02:01.448 DEBUG (MainThread) [custom_components.securitas.securitas_direct_new_api.apimanager] --------------Response--------------
2023-07-26 13:02:01.449 DEBUG (MainThread) [custom_components.securitas.securitas_direct_new_api.apimanager] {"data":{"xSSrv":{"res":"OK","msg":"Listado de servicios","language":"ES","installation":{"id":"1","alarm":false},{"id":"3","idService":"47"},{"id":"5","idService":"401"},{"id":"6","idService":"51"},{"id":"7","idService":"510"},{"id":"8","idService":"89"}}}

2023-07-26 13:02:01.453 DEBUG (MainThread) [custom_components.securitas.securitas_direct_new_api.apimanager] Making request with device_id knCuPS
2023-07-26 13:02:01.462 DEBUG (MainThread) [custom_components.securitas.securitas_direct_new_api.apimanager] Making request with device_id knCuPS
2023-07-26 13:02:01.605 DEBUG (MainThread) [custom_components.securitas.securitas_direct_new_api.apimanager] --------------Response--------------
2023-07-26 13:02:01.605 DEBUG (MainThread) [custom_components.securitas.securitas_direct_new_api.apimanager] {"data":{"xSCheckAlarmStatus":{"res":"WAIT","msg":"Petición en proceso","status":null,"numinst":null,"protomResponse":null,"protomResponseDate":null}}}

2023-07-26 13:02:01.673 DEBUG (MainThread) [custom_components.securitas.securitas_direct_new_api.apimanager] --------------Response--------------
2023-07-26 13:02:01.674 DEBUG (MainThread) [custom_components.securitas.securitas_direct_new_api.apimanager] {"data":{"xSSrv":{"res":"OK","msg":"Listado de servicios","language":"ES","installation":{"id":"1","alarm":false},{"id":"3","idService":"47"},{"id":"5","idService":"401"},{"id":"6","idService":"51"},{"id":"7","idService":"510"},{"id":"8","idService":"89"}}}

2023-07-26 13:02:02.607 DEBUG (MainThread) [custom_components.securitas.securitas_direct_new_api.apimanager] Making request with device_id knCuPS
2023-07-26 13:02:02.693 DEBUG (MainThread) [custom_components.securitas.securitas_direct_new_api.apimanager] --------------Response--------------
2023-07-26 13:02:02.694 DEBUG (MainThread) [custom_components.securitas.securitas_direct_new_api.apimanager] {"data":{"xSCheckAlarmStatus":{"res":"WAIT","msg":"Petición en proceso","status":null,"numinst":null,"protomResponse":null,"protomResponseDate":null}}}

2023-07-26 13:02:03.696 DEBUG (MainThread) [custom_components.securitas.securitas_direct_new_api.apimanager] Making request with device_id knCuPS
2023-07-26 13:02:03.800 DEBUG (MainThread) [custom_components.securitas.securitas_direct_new_api.apimanager] --------------Response--------------
2023-07-26 13:02:03.800 DEBUG (MainThread) [custom_components.securitas.securitas_direct_new_api.apimanager] {"data":{"xSCheckAlarmStatus":{"res":"WAIT","msg":"Petición en proceso","status":null,"numinst":null,"protomResponse":null,"protomResponseDate":null}}}

2023-07-26 13:02:04.131 WARNING (SyncWorker_1) [homeassistant.helpers.service] Unable to find services.yaml for the securitas integration
2023-07-26 13:02:04.802 DEBUG (MainThread) [custom_components.securitas.securitas_direct_new_api.apimanager] Making request with device_id knCuPS
2023-07-26 13:02:04.901 DEBUG (MainThread) [custom_components.securitas.securitas_direct_new_api.apimanager] --------------Response--------------
2023-07-26 13:02:04.902 DEBUG (MainThread) [custom_components.securitas.securitas_direct_new_api.apimanager] {"data":{"xSCheckAlarmStatus":{"res":"WAIT","msg":"Petición en proceso","status":null,"numinst":null,"protomResponse":null,"protomResponseDate":null}}}

2023-07-26 13:02:05.904 DEBUG (MainThread) [custom_components.securitas.securitas_direct_new_api.apimanager] Making request with device_id knCuPS
2023-07-26 13:02:06.095 DEBUG (MainThread) [custom_components.securitas.securitas_direct_new_api.apimanager] --------------Response--------------
2023-07-26 13:02:06.095 DEBUG (MainThread) [custom_components.securitas.securitas_direct_new_api.apimanager] {"data":{"xSCheckAlarmStatus":{"res":"WAIT","msg":"Petición en proceso","status":null,"numinst":null,"protomResponse":null,"protomResponseDate":null}}}

2023-07-26 13:02:07.097 DEBUG (MainThread) [custom_components.securitas.securitas_direct_new_api.apimanager] Making request with device_id knCuPS
2023-07-26 13:02:07.186 DEBUG (MainThread) [custom_components.securitas.securitas_direct_new_api.apimanager] --------------Response--------------
2023-07-26 13:02:07.186 DEBUG (MainThread) [custom_components.securitas.securitas_direct_new_api.apimanager] {"data":{"xSCheckAlarmStatus":{"res":"WAIT","msg":"Petición en proceso","status":null,"numinst":null,"protomResponse":null,"protomResponseDate":null}}}

2023-07-26 13:02:08.189 DEBUG (MainThread) [custom_components.securitas.securitas_direct_new_api.apimanager] Making request with device_id knCuPS
2023-07-26 13:02:08.317 DEBUG (MainThread) [custom_components.securitas.securitas_direct_new_api.apimanager] --------------Response--------------
2023-07-26 13:02:08.317 DEBUG (MainThread) [custom_components.securitas.securitas_direct_new_api.apimanager] {"data":{"xSCheckAlarmStatus":{"res":"WAIT","msg":"Petición en proceso","status":null,"numinst":null,"protomResponse":null,"protomResponseDate":null}}}

2023-07-26 13:02:09.131 WARNING (MainThread) [homeassistant.components.alarm_control_panel] Setup of alarm_control_panel platform securitas is taking over 10 seconds.
2023-07-26 13:02:09.319 DEBUG (MainThread) [custom_components.securitas.securitas_direct_new_api.apimanager] Making request with device_id knCuPS
2023-07-26 13:02:09.412 DEBUG (MainThread) [custom_components.securitas.securitas_direct_new_api.apimanager] --------------Response--------------
2023-07-26 13:02:09.413 DEBUG (MainThread) [custom_components.securitas.securitas_direct_new_api.apimanager] {"data":{"xSCheckAlarmStatus":{"res":"WAIT","msg":"Petición en proceso","status":null,"numinst":null,"protomResponse":null,"protomResponseDate":null}}}

2023-07-26 13:02:10.414 DEBUG (MainThread) [custom_components.securitas.securitas_direct_new_api.apimanager] Making request with device_id knCuPS
2023-07-26 13:02:10.527 DEBUG (MainThread) [custom_components.securitas.securitas_direct_new_api.apimanager] --------------Response--------------
2023-07-26 13:02:10.527 DEBUG (MainThread) [custom_components.securitas.securitas_direct_new_api.apimanager] {"data":{"xSCheckAlarmStatus":{"res":"WAIT","msg":"Petición en proceso","status":null,"numinst":null,"protomResponse":null,"protomResponseDate":null}}}

2023-07-26 13:02:11.529 DEBUG (MainThread) [custom_components.securitas.securitas_direct_new_api.apimanager] Making request with device_id knCuPS
2023-07-26 13:02:11.642 DEBUG (MainThread) [custom_components.securitas.securitas_direct_new_api.apimanager] --------------Response--------------
2023-07-26 13:02:11.642 DEBUG (MainThread) [custom_components.securitas.securitas_direct_new_api.apimanager] {"data":{"xSCheckAlarmStatus":{"res":"WAIT","msg":"Petición en proceso","status":null,"numinst":null,"protomResponse":null,"protomResponseDate":null}}}

2023-07-26 13:02:12.644 DEBUG (MainThread) [custom_components.securitas.securitas_direct_new_api.apimanager] Making request with device_id knCuPS
2023-07-26 13:02:12.761 DEBUG (MainThread) [custom_components.securitas.securitas_direct_new_api.apimanager] --------------Response--------------
2023-07-26 13:02:12.762 DEBUG (MainThread) [custom_components.securitas.securitas_direct_new_api.apimanager] {"data":{"xSCheckAlarmStatus":{"res":"WAIT","msg":"Petición en proceso","status":null,"numinst":null,"protomResponse":null,"protomResponseDate":null}}}

2023-07-26 13:02:13.763 DEBUG (MainThread) [custom_components.securitas.securitas_direct_new_api.apimanager] Making request with device_id knCuPS
2023-07-26 13:02:13.858 DEBUG (MainThread) [custom_components.securitas.securitas_direct_new_api.apimanager] --------------Response--------------
2023-07-26 13:02:13.859 DEBUG (MainThread) [custom_components.securitas.securitas_direct_new_api.apimanager] {"data":{"xSCheckAlarmStatus":{"res":"WAIT","msg":"Petición en proceso","status":null,"numinst":null,"protomResponse":null,"protomResponseDate":null}}}

2023-07-26 13:02:14.861 DEBUG (MainThread) [custom_components.securitas.securitas_direct_new_api.apimanager] Making request with device_id knCuPS
2023-07-26 13:02:14.969 DEBUG (MainThread) [custom_components.securitas.securitas_direct_new_api.apimanager] --------------Response--------------
2023-07-26 13:02:14.969 DEBUG (MainThread) [custom_components.securitas.securitas_direct_new_api.apimanager] {"data":{"xSCheckAlarmStatus":{"res":"WAIT","msg":"Petición en proceso","status":null,"numinst":null,"protomResponse":null,"protomResponseDate":null}}}

2023-07-26 13:02:15.971 DEBUG (MainThread) [custom_components.securitas.securitas_direct_new_api.apimanager] Making request with device_id knCuPS
2023-07-26 13:02:16.076 DEBUG (MainThread) [custom_components.securitas.securitas_direct_new_api.apimanager] --------------Response--------------
2023-07-26 13:02:16.076 DEBUG (MainThread) [custom_components.securitas.securitas_direct_new_api.apimanager] {"data":{"xSCheckAlarmStatus":{"res":"WAIT","msg":"Petición en proceso","status":null,"numinst":null,"protomResponse":null,"protomResponseDate":null}}}

2023-07-26 13:02:17.078 DEBUG (MainThread) [custom_components.securitas.securitas_direct_new_api.apimanager] Making request with device_id knCuPS
2023-07-26 13:02:17.176 DEBUG (MainThread) [custom_components.securitas.securitas_direct_new_api.apimanager] --------------Response--------------
2023-07-26 13:02:17.176 DEBUG (MainThread) [custom_components.securitas.securitas_direct_new_api.apimanager] {"data":{"xSCheckAlarmStatus":{"res":"WAIT","msg":"Petición en proceso","status":null,"numinst":null,"protomResponse":null,"protomResponseDate":null}}}

2023-07-26 13:02:18.178 DEBUG (MainThread) [custom_components.securitas.securitas_direct_new_api.apimanager] Making request with device_id knCuPS
2023-07-26 13:02:18.280 DEBUG (MainThread) [custom_components.securitas.securitas_direct_new_api.apimanager] --------------Response--------------
2023-07-26 13:02:18.280 DEBUG (MainThread) [custom_components.securitas.securitas_direct_new_api.apimanager] {"data":{"xSCheckAlarmStatus":{"res":"WAIT","msg":"Petición en proceso","status":null,"numinst":null,"protomResponse":null,"protomResponseDate":null}}}

2023-07-26 13:02:19.283 DEBUG (MainThread) [custom_components.securitas.securitas_direct_new_api.apimanager] Making request with device_id knCuPS
2023-07-26 13:02:19.376 DEBUG (MainThread) [custom_components.securitas.securitas_direct_new_api.apimanager] --------------Response--------------
2023-07-26 13:02:19.377 DEBUG (MainThread) [custom_components.securitas.securitas_direct_new_api.apimanager] {"data":{"xSCheckAlarmStatus":{"res":"WAIT","msg":"Petición en proceso","status":null,"numinst":null,"protomResponse":null,"protomResponseDate":null}}}

2023-07-26 13:02:20.378 DEBUG (MainThread) [custom_components.securitas.securitas_direct_new_api.apimanager] Making request with device_id knCuPS
2023-07-26 13:02:20.486 DEBUG (MainThread) [custom_components.securitas.securitas_direct_new_api.apimanager] --------------Response--------------
2023-07-26 13:02:20.486 DEBUG (MainThread) [custom_components.securitas.securitas_direct_new_api.apimanager] {"data":{"xSCheckAlarmStatus":{"res":"WAIT","msg":"Petición en proceso","status":null,"numinst":null,"protomResponse":null,"protomResponseDate":null}}}

2023-07-26 13:02:21.488 DEBUG (MainThread) [custom_components.securitas.securitas_direct_new_api.apimanager] Making request with device_id knCuPS
2023-07-26 13:02:21.579 DEBUG (MainThread) [custom_components.securitas.securitas_direct_new_api.apimanager] --------------Response--------------
2023-07-26 13:02:21.579 DEBUG (MainThread) [custom_components.securitas.securitas_direct_new_api.apimanager] {"data":{"xSCheckAlarmStatus":{"res":"WAIT","msg":"Petición en proceso","status":null,"numinst":null,"protomResponse":null,"protomResponseDate":null}}}

2023-07-26 13:02:22.580 DEBUG (MainThread) [custom_components.securitas.securitas_direct_new_api.apimanager] Making request with device_id knCuPS
2023-07-26 13:02:22.661 DEBUG (MainThread) [custom_components.securitas.securitas_direct_new_api.apimanager] --------------Response--------------
2023-07-26 13:02:22.661 DEBUG (MainThread) [custom_components.securitas.securitas_direct_new_api.apimanager] {"data":{"xSCheckAlarmStatus":{"res":"WAIT","msg":"Petición en proceso","status":null,"numinst":null,"protomResponse":null,"protomResponseDate":null}}}

2023-07-26 13:02:23.663 DEBUG (MainThread) [custom_components.securitas.securitas_direct_new_api.apimanager] Making request with device_id knCuPS
2023-07-26 13:02:23.763 DEBUG (MainThread) [custom_components.securitas.securitas_direct_new_api.apimanager] --------------Response--------------
2023-07-26 13:02:23.763 DEBUG (MainThread) [custom_components.securitas.securitas_direct_new_api.apimanager] {"data":{"xSCheckAlarmStatus":{"res":"WAIT","msg":"Petición en proceso","status":null,"numinst":null,"protomResponse":null,"protomResponseDate":null}}}

2023-07-26 13:02:40.419 DEBUG (MainThread) [custom_components.securitas.securitas_direct_new_api.apimanager] Making request with device_id knCuPS
2023-07-26 13:02:40.493 DEBUG (MainThread) [custom_components.securitas.securitas_direct_new_api.apimanager] --------------Response--------------
2023-07-26 13:02:40.493 DEBUG (MainThread) [custom_components.securitas.securitas_direct_new_api.apimanager] <html style="height:100%"><head><META NAME="ROBOTS" CONTENT="NOINDEX, NOFOLLOW"><meta name="format-detection" content="telephone=no"><meta name="viewport" content="initial-scale=1.0"><meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"></head><body style="margin:0px;height:100%"><iframe id="main-iframe>Request unsuccessful. Incapsula incident ID: 1732655</iframe></body></html>
2023-07-26 13:02:40.497 ERROR (MainThread) [homeassistant.components.alarm_control_panel] Error while setting up securitas platform for alarm_control_panel
SirLouen commented 1 year ago

Little extra info From the four installations the only one that has the idService = 11 it's the one working

{"id":"8","idService":"11","active":true,"visible":true,"bde":false,"isPremium":false}

So digging into the code, I've set in the line

https://github.com/guerrerotook/securitas-direct-new-api/blob/a5bd47ca96daff3f8211518bd3b87cac46b76861/custom_components/securitas/securitas_direct_new_api/apimanager.py#L474

A debug line to see which installation is being referenced, as I exactly expected, it's referencing only one of the legacy installations that doesn't have an alarm anymore

2023-07-26 14:23:26.071 DEBUG (MainThread) [custom_components.securitas.securitas_direct_new_api.apimanager] --------------INSTALLATION ID-------------- 2023-07-26 14:23:26.071 DEBUG (MainThread) [custom_components.securitas.securitas_direct_new_api.apimanager] 33117

I have contacted Securitas, to see if they could clean up the installations I don't use anymore (except for two). In this case I have an special installation because of a minor issue I had to have with Securitas (for invoicing purposes). So I have to keep one active installation and one dummy installation permanently and I cannot ditch it unfortunatelly yet.

But after cleaning the other two installations that were hindering the system, now new logs appear:

First appear the current active alarm, and all services and devices appear in the logs, as it should be. Perfect. The logs end like this:

2023-07-26 14:56:35.836 DEBUG (MainThread) [custom_components.securitas.securitas_direct_new_api.apimanager] {"data":{"xSCheckAlarm":{"res":"OK","msg":"Su solicitud ha sido enviada","referenceId":"OWA"}}}

After that it jumps into the second installation and after an endless loop, it crashes at it happened before.

So now I can say 100% for sure that the system is faulty when multiple installations are involved (and not all have an alarm).

A possible super-fast solution could be, by simply enabling optionally to set the installation ID on the configuration.xml file and picking it from there (and not anywhere else) in case it has been set.

I'm going to keep digging into the code to see if I can avoid the dummy installation to be called, but I've tried to trick the code and since I'm not confident with data types in Python, so far I have not been able to hard-code this at least to make it work temporarily (if someone could give me a hard-code fix it would be great to make it work until @guerrerotook finds a way to perma-solve this). Anyway if I find anything I will report back.

SirLouen commented 1 year ago

Final update: It has been semi-automatically sorted out.

In fact, both installations now appear in the entities' table which is even weirder (because in the past, only one showed up)

How so? Not 100% sure but here some steps:

Firstly, I modified all the strings that said "instalation" instead of installation

image

But not 100% confident this was the solution or it has been pure coincidence.

I was trying to debug the update_overview function to see if I could stop the calls to the dummy installation. But after restarting HA and reconfiguring the integration, it suddenly popped not only the right installation but also the dummy installation...

The installations part is definitely buggy. Probably the idea of using "INSTALATION" with one L in the code has not been the best idea.

So far so good, I've tried restarting the system and it still works, so I'm not going to touch it more until @guerrerotook returns 😝

guerrerotook commented 1 year ago

Let me take a look at all of this!

guerrerotook commented 1 year ago

Thanks for all the detailed information. The integration when is setup for the first time, list all the installations that the user has (in my case for example two), by calling this method here, https://github.com/guerrerotook/securitas-direct-new-api/blob/a5bd47ca96daff3f8211518bd3b87cac46b76861/custom_components/securitas/__init__.py#L187 Then for each for the installation found on the system it will create a new SecurirtasDirectDevice.

So, in your scenario what would happen is that you should have multiple alarm panel in your installation. Check your home assistant installation and tell me what do you find.

SirLouen commented 1 year ago

Thanks for all the detailed information. The integration when is setup for the first time, list all the installations that the user has (in my case for example two), by calling this method here,

https://github.com/guerrerotook/securitas-direct-new-api/blob/a5bd47ca96daff3f8211518bd3b87cac46b76861/custom_components/securitas/__init__.py#L187

Then for each for the installation found on the system it will create a new SecurirtasDirectDevice. So, in your scenario what would happen is that you should have multiple alarm panel in your installation. Check your home assistant installation and tell me what do you find.

Yep, that was broken It did not list all the installations until I did the mod. It just got stucked as I showed on the logs trying to retrieve info.

After I did the mod, it showed the two installations (because I deleted the other two by calling the Securitas service because I thought this could solve the issue)

The only difference I can say is this:

I had 4 installations

Two installations with alarm, and two installations without

I turned down one of the two installations with alarm some months ago (March). But maybe they did not deactivate it until May (and days after I created this bug). So maybe this disabled installation within my control panel was creating some kind of issue.

But after they disabled my other two installations, I kept having this issue, until I modified the "instaLlation" thing I comment here https://github.com/guerrerotook/securitas-direct-new-api/issues/137#issuecomment-1651906840

Definitely this is buggy and by the logs I can clearly see this that this is not being handled

Those WAIT "Petición en proceso"

2023-07-26 13:02:23.663 DEBUG (MainThread) [custom_components.securitas.securitas_direct_new_api.apimanager] Making request with device_id knCuPS
2023-07-26 13:02:23.763 DEBUG (MainThread) [custom_components.securitas.securitas_direct_new_api.apimanager] --------------Response--------------
2023-07-26 13:02:23.763 DEBUG (MainThread) [custom_components.securitas.securitas_direct_new_api.apimanager] {"data":{"xSCheckAlarmStatus":{"res":"WAIT","msg":"Petición en proceso","status":null,"numinst":null,"protomResponse":null,"protomResponseDate":null}}}

Should be handled before it returns (maybe like 5 retries or something like that before stopping)

2023-07-26 13:02:40.419 DEBUG (MainThread) [custom_components.securitas.securitas_direct_new_api.apimanager] Making request with device_id knCuPS
2023-07-26 13:02:40.493 DEBUG (MainThread) [custom_components.securitas.securitas_direct_new_api.apimanager] --------------Response--------------
2023-07-26 13:02:40.493 DEBUG (MainThread) [custom_components.securitas.securitas_direct_new_api.apimanager] <html style="height:100%"><head><META NAME="ROBOTS" CONTENT="NOINDEX, NOFOLLOW"><meta name="format-detection" content="telephone=no"><meta name="viewport" content="initial-scale=1.0"><meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"></head><body style="margin:0px;height:100%"><iframe id="main-iframe>Request unsuccessful. Incapsula incident ID: 1732655</iframe></body></html>
2023-07-26 13:02:40.497 ERROR (MainThread) [homeassistant.components.alarm_control_panel] Error while setting up securitas platform for alarm_control_panel

The thing is that the alarm cannot configured if one of the installations doesn't have an alarm. Or maybe the "instaLlation" solves it. I'm not 100% sure because as I said, I don't have python skills to progress on this. These are the two installations after I modified that:

image

guerrerotook commented 1 year ago

But do you have like two panels where you can arm or disarm your alarms?

Something like this?

image

SirLouen commented 1 year ago

I don't have two panels configured on Lovelace. Only one Still I won't be able to use one becuase one of the two installations doesn't have an alarm. It's just a "virtual" installation for invoicing purposes

guerrerotook commented 1 year ago

Still confused by this. You only have one panel in total showing up in your installation? Because for each alarm in your installation you should get an alarm object. I can create an special configuration to skip installation id if you know them. But for every operation for arming and disarming the installation_id is required in the API, and the alarm panel provide this. So there is no mixing in terms of using the wrong installation_id.

Can you share how many devices do you have in your securitas direct integration?

SirLouen commented 1 year ago

Te explico en Español porque no nos estamos enterando de nada. Por lo general, te escribo en inglés para que otros puedan seguir la conversación (pero como se ve que no le afecta a casi nadie porque es un poco raro este escenario y a los pocos que les afecto, pues le debieron de dar la patada a la integración y la olvidaron, pues tampoco nos vamos a volver locos), y dadas las circunstancias creo que es más importante ir al tiro para ver si le damos salida a este problema.

Yo tenía 4 instalaciones.

En Abril o Mayo, me dieron de baja una de las alarmas de las instalaciones con Alarma. Creo que ese fue el momento en el que la integración empezó a darme problemas porque pase a tener:

La semana pasada que fue cuando ya decidí coger el toro por los cuernos y revisar tu código, lo primero que hice fue llamar a Securitas para que eliminaran 1 instalación virtual para facturación y la instalación dummy y entonces me quede con:

Esto es a nivel de instalaciones. Porque el problema está 1000% en el código que se encarga de analizar las instalaciones.

Dicho esto, a pesar de dar de baja las que ya no usaba, seguía dando el problema que te explicaba antes: tu integración configuraba bien la instalación con alarma contratada, pero luego revisaba la instalación virtual se quedaba en un bucle de WAIT y al final moría, quedándose toda la integración sin configurar y sin entidades ni objetos ni nada. La configuración de la integración moría y se quedaba en un estado inerte (que fue lo que te pase el 9 de Junio, un error en el JSON porque basicamente el error final no es un JSON es un HTML que tira Securitas cuando después de intentar configurarse comprobando JSON vacios 12 veces, te bloquea el sistema).

¿Qué solución tomé?

Teniendo más claro que el agua que el bug viene del código que gestiona las instalaciones, me dispuse a revisar tu código y me di cuenta que en los strings ponias instalation con una sola L en vez de installation. Así que entendí que lo mismo al revisar los campos del JSON que te devuelve securitas, no estaba leyendo bien el campo que corresponde a la instalación. Después de arreglar el string, funcionó.

Pero como no entiendo el código a la perfección, no estoy 100% seguro si fue cambiar ese string, o que casualmente se arregló todo por repetir el intento de configuración como unas 500 veces, provocase que la API de Securitas devolviese algo novedoso por arte de magia.

Dicho esto:

Ahora mismo que todo me funciona bien, tengo dos instalaciones en la integración:

image

Pero como te he explicado una es totalmente inútil, es la instalación virtual que usan los de Securitas para facturación (una chapuza que se inventaron para resolverme un problema de facturación). Por lo tanto, no puedo armarla y desarmarla, así que no tiene sentido que le meta una tarjeta en Lovelace. Solo tengo la tarjeta de Lovelace de la instalación que sí puedo manejar porque si tiene una alarma de verdad.

Conclusión:

Ahora mismo me funciona todo, sí, pero hay que tener presente que existe un bug en el código. No sé exactamente cuál es. ¿Puede sea lo del string de instalation? Podría ser. Te he dado todas las pistas que he encontrado después de intentar echar un ojo a un código en Python que no entiendo demasiado bien. Lo dicho, si quieres ver algo del API mi Telegram es @mkt4p

También te adjunto mi fichero init.py con la pequeña modificación que le hice por si te sirve de algo (no me deja subir ficheros python y tampoco te lo quiero pegar aquí te lo paso por paste bin:

https://pastebin.com/TdWT4uJb