mysolenso.auth - Authentication
MySolenso authentication module.
Provides the MySolensoAuth class, which manages the HTTP session,
authentication via password or token, and generation of authorisation headers
for the two underlying APIs (Solenso and Hoymiles).
Source: https://github.com/thanatos-vf-2000/mysolenso
Example
Connect with a password (the token is retrieved automatically):
from mysolenso.auth import MySolensoAuth
auth = MySolensoAuth(username="jdoe", password="encrypted_pass")
print(auth.isConnect()) # True
print(auth.token) # "eyJ..."
print(auth.get_auth_headers_solenso())
Connect directly with an existing token:
auth = MySolensoAuth(username="jdoe", token="eyJ...")
headers = auth.get_auth_headers_hoymiles()
- class mysolenso.auth.MySolensoAuth(username, password=None, token=None)[source]
Bases:
objectSecure and robust HTTP client for the Solenso API.
Handles authentication (via encrypted password or a pre-existing token), maintains the HTTP session with the correct headers, and exposes the helper methods required by other library modules.
The class is thread-safe: token reads and writes are protected by an internal
threading.Lock.- Parameters:
username (
str) – Email address or account identifier on Solenso. Must not be empty or blank.password (
Optional[str]) – Encrypted password as sent by the Solenso web interface. Triggers a call to_authenticate()when provided.token (
Optional[str]) – Existing session token. Used as-is without any additional network call.
- Raises:
MySolensoConnectionException – If
usernameis empty, or if neitherpasswordnortokenis provided.MySolensoAuthenticationException – If the API returns an error status or a missing token during password-based authentication.
MySolensoException – On timeout or network error during authentication.
Example
auth = MySolensoAuth(username="jdoe", token="tok_abc123") if auth.isConnect(): headers = auth.get_auth_headers_solenso()
- property token: str
Current session token.
- Returns:
JWT/opaque token returned by the API, or an empty string if not authenticated.
- Return type:
- property token_language: str
Language code sent in the session cookie.
- Returns:
Language code in
ll_LLformat (e.g."fr_fr").- Return type:
- isConnect()[source]
Check whether a valid session token is present in memory.
- Returns:
Trueif the token is non-empty,Falseotherwise.- Return type:
Example
auth = MySolensoAuth(username="jdoe", token="tok_abc123") assert auth.isConnect() is True auth.disconnect() assert auth.isConnect() is False
- get_auth_headers_solenso()[source]
Build the
Cookieheaders for the Solenso API.Calls to the Solenso API (profile, stations…) authenticate via two cookies:
solenso_token_languageandsolenso_token.- Returns:
Dictionary containing the
"Cookie"key with the formatted value.- Return type:
- Raises:
MySolensoAuthenticationException – If no valid token is available (disconnected session).
Example
headers = auth.get_auth_headers_solenso() # {"Cookie": "solenso_token_language=fr_fr; solenso_token=eyJ..."}
- get_auth_headers_hoymiles()[source]
Build the
Authorizationheaders for the Hoymiles API.Hoymiles micro-inverters used by Solenso require an
Authorization: Bearer <token>header.- Returns:
Dictionary containing the
"Authorization"key with theBearerscheme.- Return type:
- Raises:
MySolensoAuthenticationException – If no valid token is available.
Example
headers = auth.get_auth_headers_hoymiles() # {"Authorization": "Bearer eyJ..."}
- disconnect()[source]
Remove the session token from memory.
After this call,
isConnect()returnsFalseand any call toget_auth_headers_*methods will raise an exception.Note
This method does not revoke the token server-side on Solenso; it only clears the local token.
- Return type: