Do not regenerate aes key pair (#1114)

And read it from `device_config` if provided.

This is required as key generation can eat up cpu when a device is not fully available and the library is retrying.
This commit is contained in:
Steven B.
2024-09-10 17:24:38 +01:00
committed by GitHub
parent 2a89e58ae0
commit fcf8f07232
3 changed files with 46 additions and 4 deletions

View File

@@ -106,6 +106,9 @@ class AesTransport(BaseTransport):
self._session_cookie: dict[str, str] | None = None
self._key_pair: KeyPair | None = None
if config.aes_keys:
aes_keys = config.aes_keys
self._key_pair = KeyPair(aes_keys["private"], aes_keys["public"])
self._app_url = URL(f"http://{self._host}:{self._port}/app")
self._token_url: URL | None = None
@@ -271,7 +274,14 @@ class AesTransport(BaseTransport):
can be made to the device.
"""
_LOGGER.debug("Generating keypair")
self._key_pair = KeyPair.create_key_pair()
if not self._key_pair:
kp = KeyPair.create_key_pair()
self._config.aes_keys = {
"private": kp.get_private_key(),
"public": kp.get_public_key(),
}
self._key_pair = kp
pub_key = (
"-----BEGIN PUBLIC KEY-----\n"
+ self._key_pair.get_public_key() # type: ignore[union-attr]
@@ -286,7 +296,6 @@ class AesTransport(BaseTransport):
"""Perform the handshake."""
_LOGGER.debug("Will perform handshaking...")
self._key_pair = None
self._token_url = None
self._session_expire_at = None
self._session_cookie = None