Authentication
Before firmware 7, authentication was based on username/password. In these cases either Envoy
or Installer
usernames with a blank password or a known username/password can be used. The token is not utilized. The authentication module will calculate the passwords for these 2 accounts, if left blank, based on the serial number retrieved by the setup method.
As of firmware 7, token based authentication is required. The authentication module can retrieve the token from the Enlighten website using the retrieved Envoy serial number and the Enlighten username and password which need to be specified. If the token is known, it can be specified and it will be used instead of obtaining one from the internet.
envoy = Envoy(host_ip_or_name)
await envoy.setup()
await envoy.authenticate(username=username, password=password, token=token)
Obtain and renew token
Upon completion of the authentication, the token can be requested and stored for later reuse in authentication. The application should check for token expiry and request timely renewal. Until the token is expired it can be used with each authenticate request.
from pyenphase import Envoy
from pyenphase.auth import EnvoyTokenAuth
token: str = "get token from some storage"
envoy = Envoy(host_ip_or_name)
await envoy.setup()
await envoy.authenticate(username=username, password=password, token=token)
assert isinstance(envoy.auth, EnvoyTokenAuth)
expire_time = envoy.auth.expire_timestamp
if expire_time < (datetime.now() - timedelta(days=7)):
await self.envoy.auth.refresh()
token = envoy.auth.token
# save token in some storage
Re-Authentication
When authentication is omitted or data requests experience an authorization failure (401 or 403) an EnvoyAuthenticationRequired
error is returned. When this occurs, authentication should be repeated.
try:
data: EnvoyData = await envoy.update()
except EnvoyAuthenticationRequired:
await envoy.authenticate(username=username, password=password,token=token)