Skip to content

Commit

Permalink
Move endpoint consts to seperate file. Cleanup.
Browse files Browse the repository at this point in the history
  • Loading branch information
vincentwolsink committed Apr 19, 2024
1 parent e452afd commit d9df779
Show file tree
Hide file tree
Showing 7 changed files with 171 additions and 46 deletions.
28 changes: 28 additions & 0 deletions custom_components/enphase_envoy/envoy_endpoints.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Generic endpoints
ENDPOINT_URL_HOME_JSON = "https://{}/home.json"
ENDPOINT_URL_INFO_XML = "https://{}/info.xml"

# Production/consumption endpoints
ENDPOINT_URL_PRODUCTION_JSON = "https://{}/production.json?details=1"
ENDPOINT_URL_PRODUCTION_V1 = "https://{}/api/v1/production"
ENDPOINT_URL_PRODUCTION_INVERTERS = "https://{}/api/v1/production/inverters"
ENDPOINT_URL_PRODUCTION_REPORT = "https://{}/ivp/meters/reports/production"
ENDPOINT_URL_PRODUCTION_POWER = "https://{}/ivp/mod/603980032/mode/power"
ENDPOINT_URL_PDM_ENERGY = "https://{}/ivp/pdm/energy"
ENDPOINT_URL_STREAM = "https://{}/stream/meter"

# Battery endpoints
ENDPOINT_URL_ENSEMBLE_INVENTORY = "https://{}/ivp/ensemble/inventory"
ENDPOINT_URL_ENSEMBLE_SECCTRL = "https://{}/ivp/ensemble/secctrl"
ENDPOINT_URL_ENSEMBLE_POWER = "https://{}/ivp/ensemble/power"

# Inverter endpoints
ENDPOINT_URL_INVENTORY = "https://{}/inventory.json"
ENDPOINT_URL_DEVSTATUS = "https://{}/ivp/peb/devstatus"

# Netprofile endpoints
ENDPOINT_URL_INSTALLER_AGF = "https://{}/installer/agf/index.json"
ENDPOINT_URL_INSTALLER_AGF_SET_PROFILE = "https://{}/installer/agf/set_profile.json"
ENDPOINT_URL_INSTALLER_AGF_UPLOAD_PROFILE = (
"https://{}/installer/agf/upload_profile_package"
)
69 changes: 31 additions & 38 deletions custom_components/enphase_envoy/envoy_reader.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
"""Module to read production and consumption values from an Enphase Envoy on the local network."""

import argparse
import asyncio
import datetime
import time
Expand All @@ -10,47 +9,34 @@
import httpx
import ipaddress
import json
import os

from jsonpath import jsonpath
from functools import partial

import hashlib
import base64
import secrets
import string
from urllib import parse

from jsonpath import jsonpath
from functools import partial
from urllib import parse
from json.decoder import JSONDecodeError

# Generic endpoints
ENDPOINT_URL_HOME_JSON = "https://{}/home.json"
ENDPOINT_URL_INFO_XML = "https://{}/info.xml"
ENDPOINT_URL_CHECK_JWT = "https://{}/auth/check_jwt"

# Production/consumption endpoints
ENDPOINT_URL_PRODUCTION_JSON = "https://{}/production.json?details=1"
ENDPOINT_URL_PRODUCTION_V1 = "https://{}/api/v1/production"
ENDPOINT_URL_PRODUCTION_INVERTERS = "https://{}/api/v1/production/inverters"
ENDPOINT_URL_PRODUCTION_REPORT = "https://{}/ivp/meters/reports/production"
ENDPOINT_URL_PRODUCTION_POWER = "https://{}/ivp/mod/603980032/mode/power"
ENDPOINT_URL_PDM_ENERGY = "https://{}/ivp/pdm/energy"
ENDPOINT_URL_STREAM = "https://{}/stream/meter"

# Battery endpoints
ENDPOINT_URL_ENSEMBLE_INVENTORY = "https://{}/ivp/ensemble/inventory"
ENDPOINT_URL_ENSEMBLE_SECCTRL = "https://{}/ivp/ensemble/secctrl"
ENDPOINT_URL_ENSEMBLE_POWER = "https://{}/ivp/ensemble/power"

# Inverter endpoints
ENDPOINT_URL_INVENTORY = "https://{}/inventory.json"
ENDPOINT_URL_DEVSTATUS = "https://{}/ivp/peb/devstatus"

# Netprofile endpoints
ENDPOINT_URL_INSTALLER_AGF = "https://{}/installer/agf/index.json"
ENDPOINT_URL_INSTALLER_AGF_SET_PROFILE = "https://{}/installer/agf/set_profile.json"
ENDPOINT_URL_INSTALLER_AGF_UPLOAD_PROFILE = (
"https://{}/installer/agf/upload_profile_package"
from .envoy_endpoints import (
ENDPOINT_URL_HOME_JSON,
ENDPOINT_URL_INFO_XML,
ENDPOINT_URL_PRODUCTION_JSON,
ENDPOINT_URL_PRODUCTION_V1,
ENDPOINT_URL_PRODUCTION_INVERTERS,
ENDPOINT_URL_PRODUCTION_REPORT,
ENDPOINT_URL_PRODUCTION_POWER,
ENDPOINT_URL_PDM_ENERGY,
ENDPOINT_URL_STREAM,
ENDPOINT_URL_ENSEMBLE_INVENTORY,
ENDPOINT_URL_ENSEMBLE_SECCTRL,
ENDPOINT_URL_ENSEMBLE_POWER,
ENDPOINT_URL_INVENTORY,
ENDPOINT_URL_DEVSTATUS,
ENDPOINT_URL_INSTALLER_AGF,
ENDPOINT_URL_INSTALLER_AGF_SET_PROFILE,
ENDPOINT_URL_INSTALLER_AGF_UPLOAD_PROFILE,
)

ENVOY_MODEL_M = "Metered"
Expand All @@ -63,6 +49,7 @@
# paths used for fetching enlighten token through envoy
ENLIGHTEN_LOGIN_URL = "https://entrez.enphaseenergy.com/login"
ENDPOINT_URL_GET_JWT = "https://{}/auth/get_jwt"
ENDPOINT_URL_CHECK_JWT = "https://{}/auth/check_jwt"

_LOGGER = logging.getLogger(__name__)

Expand Down Expand Up @@ -144,16 +131,22 @@ class EnvoyError(EnvoyReaderError):

class FileData:
def __init__(self, file):
with open(file) as json_file:
self.json_data = json.load(json_file)
if file.endswith(".json"):
self.content_type = "application/json"
with open(file) as json_file:
self.json_data = json.load(json_file)
elif file.endswith(".xml"):
self.content_type = "application/xml"
with open(file) as xml_file:
self.text = xml_file.read()

@property
def status_code(self):
return 200

@property
def headers(self):
return {"content-type": "application/json"}
return {"content-type": self.content_type}

def json(self):
return self.json_data
Expand Down
28 changes: 28 additions & 0 deletions custom_components/enphase_envoy/envoy_test_data.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
TEST_DATA = "/config/custom_components/enphase_envoy/test_data/envoy_metered/"

# Generic endpoints
ENDPOINT_URL_HOME_JSON = TEST_DATA + "endpoint_home_json.json"
ENDPOINT_URL_INFO_XML = TEST_DATA + "endpoint_info.xml"

# Production/consumption endpoints
ENDPOINT_URL_PRODUCTION_JSON = TEST_DATA + "endpoint_production_json.json"
ENDPOINT_URL_PRODUCTION_V1 = TEST_DATA + "endpoint_production_v1.json"
ENDPOINT_URL_PRODUCTION_INVERTERS = TEST_DATA + "endpoint_production_inverters.json"
ENDPOINT_URL_PRODUCTION_REPORT = TEST_DATA + "endpoint_production_report.json"
ENDPOINT_URL_PRODUCTION_POWER = TEST_DATA + "endpoint_production_power.json"
ENDPOINT_URL_PDM_ENERGY = TEST_DATA + "endpoint_pdm_energy.json"
ENDPOINT_URL_STREAM = None

# Battery endpoints
ENDPOINT_URL_ENSEMBLE_INVENTORY = TEST_DATA + "endpoint_ensemble_inventory.json"
ENDPOINT_URL_ENSEMBLE_SECCTRL = TEST_DATA + "endpoint_ensemble_secctrl.json"
ENDPOINT_URL_ENSEMBLE_POWER = TEST_DATA + "endpoint_ensemble_power.json"

# Inverter endpoints
ENDPOINT_URL_INVENTORY = TEST_DATA + "endpoint_inventory.json"
ENDPOINT_URL_DEVSTATUS = TEST_DATA + "endpoint_devstatus.json"

# Netprofile endpoints
ENDPOINT_URL_INSTALLER_AGF = TEST_DATA + "endpoint_installer_agf_index_json.json"
ENDPOINT_URL_INSTALLER_AGF_SET_PROFILE = None
ENDPOINT_URL_INSTALLER_AGF_UPLOAD_PROFILE = None
3 changes: 1 addition & 2 deletions custom_components/enphase_envoy/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@

from homeassistant.components.sensor import SensorEntity
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import STATE_UNAVAILABLE, STATE_UNKNOWN, CONF_HOST
from homeassistant.const import CONF_HOST
from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers.entity import DeviceInfo
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.event import async_track_state_change_event
from homeassistant.helpers.update_coordinator import CoordinatorEntity

from .const import (
Expand Down
6 changes: 3 additions & 3 deletions test_data/envoy_metered/endpoint_ensemble_inventory.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
{
"part_num": "830-01760-r46",
"installed": 1712942814,
"serial_num": "122323085065",
"serial_num": "999999995065",
"device_status": [
"envoy.global.ok",
"prop.done"
Expand Down Expand Up @@ -34,7 +34,7 @@
{
"part_num": "830-01760-r46",
"installed": 1712942814,
"serial_num": "122323085067",
"serial_num": "999999995067",
"device_status": [
"envoy.global.ok",
"prop.done"
Expand Down Expand Up @@ -63,7 +63,7 @@
{
"part_num": "830-01760-r46",
"installed": 1712942449,
"serial_num": "122323085069",
"serial_num": "999999995069",
"device_status": [
"envoy.global.ok",
"prop.done"
Expand Down
6 changes: 3 additions & 3 deletions test_data/envoy_metered/endpoint_ensemble_power.json
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
{
"devices:": [
{
"serial_num": "122323085065",
"serial_num": "999999995065",
"real_power_mw": 179000,
"apparent_power_mva": 179000,
"soc": 89
},
{
"serial_num": "122323085067",
"serial_num": "999999995067",
"real_power_mw": 190000,
"apparent_power_mva": 190000,
"soc": 89
},
{
"serial_num": "122323085069",
"serial_num": "999999995069",
"real_power_mw": 183000,
"apparent_power_mva": 183000,
"soc": 89
Expand Down
77 changes: 77 additions & 0 deletions test_data/envoy_metered/endpoint_installer_agf_index_json.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
{
"selected_profile": "EN 50549-1:2019 RfG E02 Netherlands:1.3.2",
"selected_profile_id": "65e6901caeee1200f07caeee",
"last_package_profile_md5sum": "00beeb6d88ac522947b15c97d630b48c",
"profile_auto_selected": false,
"mm_version": "01.03.06",
"bt_version": "1.3.6",
"migration_in_progress": false,
"profiles": [
{
"uuid": "65e6901caeee1200f07caeee",
"profile_id": "EN 50549-1:2019 RfG E02 Netherlands:1.3.2",
"profile_name": "EN 50549-1:2019 RfG E02 Netherlands",
"profile_version": "1.3.2",
"profile_description": "Profile for The Netherlands based on requirements drawn from EN 50549-1:2019 + Amendment 1 and Netbeheer E-02 - Netcode elektriciteit (2022-12-18) (the Dutch Grid Code).",
"policy": {
"min_base_template_version": "1.3.1",
"min_master_model_version": "1.3.1",
"min_envoy_version": "7.6",
"max_envoy_version": "",
"model_mismatch": false,
"logical_device_mismatch": false,
"attribute_mismatch": false,
"id_mismatch": false
},
"envoy_type": "europe",
"countries": [
"NL"
],
"states": [

],
"unpacked": true,
"profile_source": [
"enlighten"
],
"dynamic": true,
"mm_version": "1.3.7",
"bt_version": "1.3.7"
}
],
"profile_groups": {
"NL": [
{
"uuid": "65e6901caeee1200f07caeee",
"profile_id": "EN 50549-1:2019 RfG E02 Netherlands:1.3.2",
"profile_name": "EN 50549-1:2019 RfG E02 Netherlands",
"profile_version": "1.3.2",
"profile_description": "Profile for The Netherlands based on requirements drawn from EN 50549-1:2019 + Amendment 1 and Netbeheer E-02 - Netcode elektriciteit (2022-12-18) (the Dutch Grid Code).",
"policy": {
"min_base_template_version": "1.3.1",
"min_master_model_version": "1.3.1",
"min_envoy_version": "7.6",
"max_envoy_version": "",
"model_mismatch": false,
"logical_device_mismatch": false,
"attribute_mismatch": false,
"id_mismatch": false
},
"envoy_type": "europe",
"countries": [
"NL"
],
"states": [

],
"unpacked": true,
"profile_source": [
"enlighten"
],
"dynamic": true,
"mm_version": "1.3.7",
"bt_version": "1.3.7"
}
]
}
}

0 comments on commit d9df779

Please sign in to comment.