Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unify login procedure. Remove home owner checkbox. Auto detect token type. #142

Merged
merged 2 commits into from
Aug 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 0 additions & 5 deletions custom_components/enphase_envoy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@
DEFAULT_SCAN_INTERVAL,
DEFAULT_REALTIME_UPDATE_THROTTLE,
LIVE_UPDATEABLE_ENTITIES,
DISABLE_INSTALLER_ACCOUNT_USE,
DEFAULT_GETDATA_TIMEOUT,
)

Expand Down Expand Up @@ -79,10 +78,6 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
enlighten_serial_num=config[CONF_SERIAL],
store=store,
disable_negative_production=options.get("disable_negative_production", False),
disable_installer_account_use=options.get(
DISABLE_INSTALLER_ACCOUNT_USE,
config.get(DISABLE_INSTALLER_ACCOUNT_USE, False),
),
)
await envoy_reader._sync_store(load=True)

Expand Down
14 changes: 0 additions & 14 deletions custom_components/enphase_envoy/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
CONF_SERIAL,
DEFAULT_SCAN_INTERVAL,
DEFAULT_REALTIME_UPDATE_THROTTLE,
DISABLE_INSTALLER_ACCOUNT_USE,
ENABLE_ADDITIONAL_METRICS,
DEFAULT_GETDATA_TIMEOUT,
)
Expand All @@ -46,7 +45,6 @@ async def validate_input(hass: HomeAssistant, data: dict[str, Any]) -> EnvoyRead
enlighten_pass=data[CONF_PASSWORD],
inverters=False,
enlighten_serial_num=data[CONF_SERIAL],
disable_installer_account_use=data[DISABLE_INSTALLER_ACCOUNT_USE],
)

try:
Expand Down Expand Up @@ -85,7 +83,6 @@ def _async_generate_schema(self):
schema[vol.Required(CONF_SERIAL, default=self.unique_id)] = str
schema[vol.Required(CONF_USERNAME, default=self.username)] = str
schema[vol.Required(CONF_PASSWORD, default="")] = str
schema[vol.Required(DISABLE_INSTALLER_ACCOUNT_USE, default=False)] = bool

return vol.Schema(schema)

Expand Down Expand Up @@ -254,17 +251,6 @@ async def async_step_user(self, user_input=None):
"getdata_timeout", DEFAULT_GETDATA_TIMEOUT
),
): vol.All(vol.Coerce(int), vol.Range(min=30)),
vol.Optional(
DISABLE_INSTALLER_ACCOUNT_USE,
default=(
self.config_entry.options.get(
DISABLE_INSTALLER_ACCOUNT_USE,
self.config_entry.data.get(
DISABLE_INSTALLER_ACCOUNT_USE, False
),
)
),
): bool,
vol.Optional(
"disable_negative_production",
default=self.config_entry.options.get(
Expand Down
1 change: 0 additions & 1 deletion custom_components/enphase_envoy/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@
CONF_SERIAL = "serial"

LIVE_UPDATEABLE_ENTITIES = "live-update-entities"
DISABLE_INSTALLER_ACCOUNT_USE = "disable_installer_account_use"
ENABLE_ADDITIONAL_METRICS = "enable_additional_metrics"
ADDITIONAL_METRICS = []

Expand Down
65 changes: 12 additions & 53 deletions custom_components/enphase_envoy/envoy_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,6 @@
ENVOY_MODEL_M = "Metered"
ENVOY_MODEL_S = "Standard"

# paths for the enlighten installer token
ENLIGHTEN_AUTH_URL = "https://enlighten.enphaseenergy.com/login/login.json"
ENLIGHTEN_TOKEN_URL = "https://entrez.enphaseenergy.com/tokens"

# paths used for fetching enlighten token through envoy
ENLIGHTEN_LOGIN_URL = "https://entrez.enphaseenergy.com/login"
ENDPOINT_URL_GET_JWT = "https://{}/auth/get_jwt"
Expand Down Expand Up @@ -649,7 +645,6 @@ def __init__(
token_refresh_buffer_seconds=0,
store=None,
disable_negative_production=False,
disable_installer_account_use=False,
):
"""Init the EnvoyReader."""
self.host = host.lower()
Expand Down Expand Up @@ -705,7 +700,7 @@ def url(endpoint, *a, **kw):
pass

self.disable_negative_production = disable_negative_production
self.disable_installer_account_use = disable_installer_account_use
self.disable_installer_account_use = False

self.is_receiving_realtime_data = False

Expand Down Expand Up @@ -920,56 +915,20 @@ async def _fetch_envoy_token_json(self):

return resp.json()["access_token"]

async def _fetch_owner_token_json(self):
"""
Try to fetch the owner token json from Enlighten API
:return:
"""
_LOGGER.debug("Fetching owner token")
async with self.async_client as client:
# login to Enlighten
payload_login = {
"user[email]": self.enlighten_user,
"user[password]": self.enlighten_pass,
}
resp = await client.post(ENLIGHTEN_AUTH_URL, data=payload_login, timeout=30)
if resp.status_code >= 400:
raise EnlightenError("Could not Authenticate via Enlighten")

# now that we're in a logged in session, we can request the installer token
login_data = resp.json()
payload_token = {
"session_id": login_data["session_id"],
"serial_num": self.enlighten_serial_num,
"username": self.enlighten_user,
}
resp = await client.post(
ENLIGHTEN_TOKEN_URL, json=payload_token, timeout=30
)
if resp.status_code != 200:
raise EnlightenError("Could not get installer token")
return resp.text

async def _get_enphase_token(self):
# First attempt should be to auth using envoy token, as this could result in a installer token
if not self.disable_installer_account_use:
self._token = await self._fetch_envoy_token_json()
self.envoy_token_fetch_attempted = True

_LOGGER.debug("Envoy Token")
if self._is_enphase_token_expired(self._token):
raise EnlightenError("Just received token already expired")
self._token = await self._fetch_envoy_token_json()
self.envoy_token_fetch_attempted = True

if self.token_type != "installer":
_LOGGER.warning(
"Received token is of type %s, disabling installer account usage",
self.token_type,
)
self.disable_installer_account_use = True
_LOGGER.debug("Envoy Token")
if self._is_enphase_token_expired(self._token):
raise EnlightenError("Just received token already expired")

else:
self._token = await self._fetch_owner_token_json()
_LOGGER.debug("Commissioned Token")
if self.token_type != "installer":
_LOGGER.warning(
"Received token is of type %s, disabling installer account usage",
self.token_type,
)
self.disable_installer_account_use = True

if self._is_enphase_token_expired(self._token):
raise EnlightenError("Just received token already expired")
Expand Down
5 changes: 2 additions & 3 deletions custom_components/enphase_envoy/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,9 @@
"host": "Host",
"password": "Enlighten Password",
"username": "Enlighten Username",
"serial": "Envoy Serial Number",
"disable_installer_account_use": "I have no installer or DIY enphase account, just Home owner"
"serial": "Envoy Serial Number"
},
"description": "Enter the hostname/ip and serial of your Envoy. Use your Enlighten Installer account credentials."
"description": "Enter the hostname/ip and serial of your Envoy. Use your Enlighten (installer) account credentials."
}
}
},
Expand Down
5 changes: 2 additions & 3 deletions custom_components/enphase_envoy/translations/nl.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,9 @@
"host": "Host",
"password": "Enlighten Wachtwoord",
"username": "Enlighten Gebruikersnaam",
"serial": "Envoy Serienummer",
"disable_installer_account_use": "Ik heb geen installer of DHZ enphase account, alleen Home owner"
"serial": "Envoy Serienummer"
},
"description": "Voer de hostname/ip en serienummer van je Envoy in. Gebruik je Enlighten installer account gegevens."
"description": "Voer de hostname/ip en serienummer van je Envoy in. Gebruik je Enlighten (installer) account gegevens."
}
}
},
Expand Down
Loading