Skip to content

Commit

Permalink
Add configuration option for interval between data collection. (#105)
Browse files Browse the repository at this point in the history
  • Loading branch information
catsmanac authored Jul 16, 2023
1 parent 17b7786 commit 9898132
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 4 deletions.
7 changes: 5 additions & 2 deletions custom_components/enphase_envoy_custom/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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(
Expand Down Expand Up @@ -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:
Expand Down
33 changes: 32 additions & 1 deletion custom_components/enphase_envoy_custom/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__)

Expand Down Expand Up @@ -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."""
Expand Down
2 changes: 2 additions & 0 deletions custom_components/enphase_envoy_custom/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
COORDINATOR = "coordinator"
NAME = "name"

DEFAULT_SCAN_INTERVAL = 60 # default in seconds

CONF_SERIAL = "serial"
CONF_USE_ENLIGHTEN = "use_enlighten"

Expand Down
15 changes: 14 additions & 1 deletion custom_components/enphase_envoy_custom/strings.json
Original file line number Diff line number Diff line change
Expand Up @@ -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%]",
Expand All @@ -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"
}
}
}
}
}
14 changes: 14 additions & 0 deletions custom_components/enphase_envoy_custom/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
}
}

}

0 comments on commit 9898132

Please sign in to comment.