Skip to content

Comments

Support for reconfigure and accompanying improvements#191

Merged
sveinse merged 5 commits intocustom-components:masterfrom
sveinse:feature-add-reconfigure
Jul 15, 2025
Merged

Support for reconfigure and accompanying improvements#191
sveinse merged 5 commits intocustom-components:masterfrom
sveinse:feature-add-reconfigure

Conversation

@sveinse
Copy link
Collaborator

@sveinse sveinse commented Jul 14, 2025

This PR started with the intention to fix #76: Reconfigure the Zaptec integration. However, once pulling the threads in the code revealed a few things that had to be fixed for this to work. When reconfiguring an integration, it needs to shut down the old to start a new, and the current design didn't do that very well. In particular the stream methods didn't clean up properly and were failing miserably on reconfigure.

Next the setup weren't implemented in a good way, as any login/password issues should be handled earlier so it had to be improved. We needed a separate login method instead of just refreshing the token when doing the first access.

There are a few method being changed from this PR. However the api.py file exists to serve the integration, so I think its ok to do these changes.

  • Refactor of configuration flows
    • Add support for reconfigure config flow
    • Add support for reauthenticate config flow
    • Refactor user setup config flow
  • Refactor stream methods
    • Rename Installation._stream() to stream_main()
    • Move Account.update() to Installation.stream_update()
    • Make sure only one stream can run
    • Implement Installation.stream_close()
  • Refactor API request methods
    • Remove Account.check_login()
    • Add Account.login()
    • Rename Account._retry_request() to _request_worker()
  • Improve integration setup
    • Move Account initalization to early setup and fail there
    • Handle streams from the coordinator
    • Move first-time-setup to ZaptecUpdateCoordinator._first_time_setup()

This PR is the first in a series to move towards a more Zaptec API friendly design.

… for requests and streams.

* Refactor of configuration flows
  * Add support for reconfigure config flow
  * Add support for reauthenticate config flow
  * Refactor user setup config flow
* Refactor stream methods
  * Rename Installation._stream() to stream_main()
  * Move Account.update() to Installation.stream_update()
  * Make sure only one stream can run
  * Implement Installation.stream_close()
* Refactor API methods
  * Remove Account.check_login()
  * Add Account.login()
  * Rename Account._retry_request() to _request_worker()
* Improve integration setup
  * Move Account initalization to early setup and fail there
  * Handle streams from the coordinator
  * Move first-time-setup to ZaptecUpdateCoordinator._first_time_setup()
@sveinse sveinse added this to the v0.8 milestone Jul 14, 2025
Comment on lines 451 to 458
if (charger := self._account.map.get(charger_id)) is None:
_LOGGER.warning("Got update for unknown charger id %s", charger_id)
return

charger = self.map.get(charger_id)
if charger is None:
_LOGGER.warning("Got update for unknown charger id %s", charger_id)
return
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks like a copypasta gone wrong, I think the first charger assignment is the correct one, and the second is an artifact from before the function was moved from the account object?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch. It is right, but I changed the text a little bit to be a little more informative of the error.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But the installation class doesn't have a map attribute, that is in the account class:

2025-07-15 13:14:38.060 ERROR (MainThread) [custom_components.zaptec.api] Couldn't process stream message: 'map'
Traceback (most recent call last):
  File "/workspaces/zaptec/custom_components/zaptec/api.py", line 158, in __getattr__
    return self._attrs[to_under(key)]
           ~~~~~~~~~~~^^^^^^^^^^^^^^^
KeyError: 'map'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/workspaces/zaptec/custom_components/zaptec/api.py", line 402, in stream_main
    self.stream_update(json_result)
    ~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^
  File "/workspaces/zaptec/custom_components/zaptec/api.py", line 467, in stream_update
    charger = self.map.get(charger_id)
              ^^^^^^^^
  File "/workspaces/zaptec/custom_components/zaptec/api.py", line 160, in __getattr__
    raise AttributeError(exc) from exc
AttributeError: 'map'

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, right. How did that miss my testing? I'll fix this.

@sveinse
Copy link
Collaborator Author

sveinse commented Jul 15, 2025

I isolated out the formatting changes into a separate PR, so this PR should now be easier and more on-point to review.

Comment on lines 463 to 470
if (charger := self._account.map.get(charger_id)) is None:
_LOGGER.warning("Got update for unknown charger id %s", charger_id)
return

charger = self.map.get(charger_id)
if charger is None:
_LOGGER.warning("Got update for unregistered charger, id %s", charger_id)
return
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (charger := self._account.map.get(charger_id)) is None:
_LOGGER.warning("Got update for unknown charger id %s", charger_id)
return
charger = self.map.get(charger_id)
if charger is None:
_LOGGER.warning("Got update for unregistered charger, id %s", charger_id)
return
if (charger := self._account.map.get(charger_id)) is None:
_LOGGER.warning("Got update for unregistered charger, id %s", charger_id)
return

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

New version pushed

* Minor docstring updates
Copy link
Contributor

@steinmn steinmn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@sveinse
Copy link
Collaborator Author

sveinse commented Jul 15, 2025

Thank you @steinmn

@sveinse sveinse merged commit be38ee3 into custom-components:master Jul 15, 2025
1 check passed
@sveinse sveinse deleted the feature-add-reconfigure branch July 15, 2025 14:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add support to reconfigure zaptec

2 participants