Skip to content

Commit

Permalink
Add get_grid_profiles and set_grid_profile services
Browse files Browse the repository at this point in the history
  • Loading branch information
vincentwolsink committed Nov 2, 2023
1 parent e5261b6 commit 72b7fd1
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 1 deletion.
34 changes: 33 additions & 1 deletion custom_components/enphase_envoy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,15 @@
CONF_USERNAME,
EVENT_HOMEASSISTANT_STOP,
)
from homeassistant.core import HomeAssistant, callback, CoreState, Event
from homeassistant.core import (
HomeAssistant,
callback,
CoreState,
Event,
ServiceCall,
ServiceResponse,
SupportsResponse,
)
from homeassistant.exceptions import ConfigEntryAuthFailed
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
from homeassistant.helpers.storage import Store
Expand Down Expand Up @@ -137,6 +145,30 @@ async def async_update_data():
),
)

async def get_grid_profiles(call: ServiceCall) -> ServiceResponse:
return {
"selected_profile": coordinator.data.get("grid_profile"),
"available_profiles": [
k["profile_id"] for k in coordinator.data.get("grid_profiles_available")
],
}

hass.services.async_register(
DOMAIN,
"get_grid_profiles",
get_grid_profiles,
supports_response=SupportsResponse.ONLY,
)

async def set_grid_profile(call: ServiceCall):
await envoy_reader.set_grid_profile(call.data["profile"])

hass.services.async_register(
DOMAIN,
"set_grid_profile",
set_grid_profile,
)

@Throttle(time_between_realtime_updates)
def update_production_meters(streamdata: StreamData):
new_data = {}
Expand Down
15 changes: 15 additions & 0 deletions custom_components/enphase_envoy/envoy_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
ENDPOINT_URL_PRODUCTION_REPORT = "https://{}/ivp/meters/reports/production"
ENDPOINT_URL_PDM_ENERGY = "https://{}/ivp/pdm/energy"
ENDPOINT_URL_INSTALLER_AGF = "https://{}/installer/agf/index.json"
ENDPOINT_URL_INSTALLER_AGF_SET_PROFILE = "https://{}/installer/agf/set_profile.json"

ENVOY_MODEL_M = "Metered"
ENVOY_MODEL_S = "Standard"
Expand Down Expand Up @@ -377,6 +378,7 @@ class EnvoyStandard(EnvoyData):
envoy_update_status_value = "endpoint_home_json_results.update_status"
serial_number_value = "endpoint_info_results.envoy_info.device.sn"
grid_profile_value = "endpoint_installer_agf.selected_profile"
grid_profiles_available_value = "endpoint_installer_agf.profiles"

@envoy_property()
def envoy_info(self):
Expand Down Expand Up @@ -1331,6 +1333,19 @@ async def set_production_power(self, power_on):
# Make sure the next poll will update the endpoint.
self._clear_endpoint_cache("endpoint_production_power")

async def set_grid_profile(self, profile_id):
if self.endpoint_installer_agf is not None:
formatted_url = ENDPOINT_URL_INSTALLER_AGF_SET_PROFILE.format(self.host)
resp = await self._async_put(
formatted_url, data={"selected_profile": profile_id}
)

if "accepted" not in resp.text:
raise EnvoyError(f"Failed setting grid profile: {resp.json().get('message')} - {resp.json().get('reason')}")

self._clear_endpoint_cache("endpoint_installer_agf")
return resp

def run_stream(self):
print("Reading stream...")
loop = asyncio.get_event_loop()
Expand Down
8 changes: 8 additions & 0 deletions custom_components/enphase_envoy/services.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
get_grid_profiles:
set_grid_profile:
fields:
profile:
required: true
example: "EN 50549-1:2019 RfG E02 Netherlands:1.2.4"
selector:
text:

0 comments on commit 72b7fd1

Please sign in to comment.