From 9898132b1c92ddd6accdcf4e882f157c725a3355 Mon Sep 17 00:00:00 2001 From: Arie Catsman <120491684+catsmanac@users.noreply.github.com> Date: Sun, 16 Jul 2023 14:17:42 +0200 Subject: [PATCH] Add configuration option for interval between data collection. (#105) --- .../enphase_envoy_custom/__init__.py | 7 ++-- .../enphase_envoy_custom/config_flow.py | 33 ++++++++++++++++++- .../enphase_envoy_custom/const.py | 2 ++ .../enphase_envoy_custom/strings.json | 15 ++++++++- .../enphase_envoy_custom/translations/en.json | 14 ++++++++ 5 files changed, 67 insertions(+), 4 deletions(-) diff --git a/custom_components/enphase_envoy_custom/__init__.py b/custom_components/enphase_envoy_custom/__init__.py index 711ec67..c328f2d 100644 --- a/custom_components/enphase_envoy_custom/__init__.py +++ b/custom_components/enphase_envoy_custom/__init__.py @@ -15,7 +15,7 @@ from homeassistant.exceptions import ConfigEntryAuthFailed from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed -from .const import COORDINATOR, DOMAIN, NAME, PLATFORMS, SENSORS, CONF_USE_ENLIGHTEN, CONF_SERIAL, PHASE_SENSORS +from .const import COORDINATOR, DOMAIN, NAME, PLATFORMS, SENSORS, CONF_USE_ENLIGHTEN, CONF_SERIAL, PHASE_SENSORS, DEFAULT_SCAN_INTERVAL SCAN_INTERVAL = timedelta(seconds=60) @@ -26,6 +26,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: """Set up Enphase Envoy from a config entry.""" config = entry.data + options = entry.options name = config[CONF_NAME] envoy_reader = EnvoyReader( @@ -109,7 +110,9 @@ async def async_update_data(): _LOGGER, name=f"envoy {name}", update_method=async_update_data, - update_interval=SCAN_INTERVAL, + update_interval=timedelta( + seconds=options.get("data_interval", DEFAULT_SCAN_INTERVAL) + ) ) try: diff --git a/custom_components/enphase_envoy_custom/config_flow.py b/custom_components/enphase_envoy_custom/config_flow.py index 32c3513..a537e5c 100644 --- a/custom_components/enphase_envoy_custom/config_flow.py +++ b/custom_components/enphase_envoy_custom/config_flow.py @@ -16,7 +16,7 @@ from homeassistant.data_entry_flow import FlowResult from homeassistant.exceptions import HomeAssistantError -from .const import DOMAIN, CONF_SERIAL, CONF_USE_ENLIGHTEN +from .const import DOMAIN, CONF_SERIAL, CONF_USE_ENLIGHTEN, DEFAULT_SCAN_INTERVAL _LOGGER = logging.getLogger(__name__) @@ -196,6 +196,37 @@ async def async_step_user( errors=errors, ) + @staticmethod + @callback + def async_get_options_flow(config_entry): + return EnvoyOptionsFlowHandler(config_entry) + +class EnvoyOptionsFlowHandler(config_entries.OptionsFlow): + """Envoy config flow options handler.""" + + def __init__(self, config_entry): + """Initialize Envoy options flow.""" + self.config_entry = config_entry + + async def async_step_init(self, _user_input=None): + """Manage the options.""" + return await self.async_step_user() + + async def async_step_user(self, user_input=None): + """Handle a flow initialized by the user.""" + + if user_input is not None: + return self.async_create_entry(title="", data=user_input) + + schema = { + vol.Optional( + "data_interval", + default=self.config_entry.options.get( + "data_interval", DEFAULT_SCAN_INTERVAL + ), + ): vol.All(vol.Coerce(int), vol.Range(min=5)), + } + return self.async_show_form(step_id="user", data_schema=vol.Schema(schema)) class CannotConnect(HomeAssistantError): """Error to indicate we cannot connect.""" diff --git a/custom_components/enphase_envoy_custom/const.py b/custom_components/enphase_envoy_custom/const.py index cc63faa..6a84169 100644 --- a/custom_components/enphase_envoy_custom/const.py +++ b/custom_components/enphase_envoy_custom/const.py @@ -26,6 +26,8 @@ COORDINATOR = "coordinator" NAME = "name" +DEFAULT_SCAN_INTERVAL = 60 # default in seconds + CONF_SERIAL = "serial" CONF_USE_ENLIGHTEN = "use_enlighten" diff --git a/custom_components/enphase_envoy_custom/strings.json b/custom_components/enphase_envoy_custom/strings.json index 01a19ff..c6477c8 100644 --- a/custom_components/enphase_envoy_custom/strings.json +++ b/custom_components/enphase_envoy_custom/strings.json @@ -3,7 +3,7 @@ "flow_title": "{serial} ({host})", "step": { "user": { - "description": "- If your Envoy is on Firmware 7.x or later, validate or enter the Host IP address of your Envoy, your Enphase website username, password, Envoy serial number, and tick the `Use Enlighten` box.\n- For older models do not tick `Use Enlighten` and use either usernames `envoy` without a password, `installer` without a password or a valid username and password and validate or enter the Host ip address." + "description": "- If your Envoy is on Firmware 7.x or later, validate or enter the Host IP address of your Envoy, your Enphase website username, password, Envoy serial number, and tick the `Use Enlighten` box.\n- For older models do not tick `Use Enlighten` and use either usernames `envoy` without a password, `installer` without a password or a valid username and password and validate or enter the Host ip address.", "data": { "host": "[%key:common::config_flow::data::host%]", "username": "[%key:common::config_flow::data::username%]", @@ -22,5 +22,18 @@ "already_configured": "[%key:common::config_flow::abort::already_configured_device%]", "reauth_successful": "[%key:common::config_flow::abort::reauth_successful%]" } + }, + "options": { + "step": { + "user": { + "title": "Envoy options", + "data": { + "data_interval": "Time between entity updates [s]" + }, + "data_description": { + "data_interval": "Time between data updates, minimum 5 sec. After changes reload the envoy" + } + } + } } } diff --git a/custom_components/enphase_envoy_custom/translations/en.json b/custom_components/enphase_envoy_custom/translations/en.json index 0ddfa45..0fd7754 100644 --- a/custom_components/enphase_envoy_custom/translations/en.json +++ b/custom_components/enphase_envoy_custom/translations/en.json @@ -22,5 +22,19 @@ "description": "- If your Envoy is on Firmware 7.x or later, validate or enter the Host IP address of your Envoy, your Enphase website username, password, Envoy serial number, and tick the `Use Enlighten` box.\n- For older models do not tick `Use Enlighten` and use either usernames `envoy` without a password, `installer` without a password or a valid username and password and validate or enter the Host ip address." } } + }, + "options": { + "step": { + "user": { + "title": "Envoy options", + "data": { + "data_interval": "Time between entity updates [s]" + }, + "data_description": { + "data_interval": "Time between data updates, minimum 5 sec. After changes reload the envoy" + } + } + } } + }