From d68005c0e8640847fd79ef27ae808a6732d4a13a Mon Sep 17 00:00:00 2001 From: Daniel Raper Date: Thu, 24 Oct 2024 18:03:45 +0100 Subject: [PATCH] Configurable refresh intervals --- custom_components/ohme/config_flow.py | 16 ++++++++++++++-- custom_components/ohme/const.py | 5 +++++ custom_components/ohme/coordinator.py | 20 ++++++++++++++------ custom_components/ohme/translations/en.json | 9 +++++++-- 4 files changed, 40 insertions(+), 10 deletions(-) diff --git a/custom_components/ohme/config_flow.py b/custom_components/ohme/config_flow.py index 41f74e8..3381658 100644 --- a/custom_components/ohme/config_flow.py +++ b/custom_components/ohme/config_flow.py @@ -1,6 +1,6 @@ import voluptuous as vol from homeassistant.config_entries import (ConfigFlow, OptionsFlow) -from .const import DOMAIN, CONFIG_VERSION +from .const import DOMAIN, CONFIG_VERSION, DEFAULT_INTERVAL_CHARGESESSIONS, DEFAULT_INTERVAL_ACCOUNTINFO, DEFAULT_INTERVAL_ADVANCED, DEFAULT_INTERVAL_SCHEDULES from .api_client import OhmeApiClient @@ -89,6 +89,18 @@ async def async_step_init(self, options): ) : bool, vol.Required( "never_collapse_slots", default=self._config_entry.options.get("never_collapse_slots", False) - ) : bool + ) : bool, + vol.Required( + "interval_chargesessions", default=self._config_entry.options.get("interval_chargesessions", DEFAULT_INTERVAL_CHARGESESSIONS) + ) : vol.All(vol.Coerce(float), vol.Clamp(min=DEFAULT_INTERVAL_CHARGESESSIONS)), + vol.Required( + "interval_accountinfo", default=self._config_entry.options.get("interval_accountinfo", DEFAULT_INTERVAL_ACCOUNTINFO) + ) : vol.All(vol.Coerce(float), vol.Clamp(min=DEFAULT_INTERVAL_ACCOUNTINFO)), + vol.Required( + "interval_advanced", default=self._config_entry.options.get("interval_advanced", DEFAULT_INTERVAL_ADVANCED) + ) : vol.All(vol.Coerce(float), vol.Clamp(min=DEFAULT_INTERVAL_ADVANCED)), + vol.Required( + "interval_schedules", default=self._config_entry.options.get("interval_schedules", DEFAULT_INTERVAL_SCHEDULES) + ) : vol.All(vol.Coerce(float), vol.Clamp(min=DEFAULT_INTERVAL_SCHEDULES)) }), errors=errors ) diff --git a/custom_components/ohme/const.py b/custom_components/ohme/const.py index 7670fbc..f332578 100644 --- a/custom_components/ohme/const.py +++ b/custom_components/ohme/const.py @@ -14,3 +14,8 @@ COORDINATOR_ACCOUNTINFO = 1 COORDINATOR_ADVANCED = 2 COORDINATOR_SCHEDULES = 3 + +DEFAULT_INTERVAL_CHARGESESSIONS = 0.5 +DEFAULT_INTERVAL_ACCOUNTINFO = 1 +DEFAULT_INTERVAL_ADVANCED = 1 +DEFAULT_INTERVAL_SCHEDULES = 10 diff --git a/custom_components/ohme/coordinator.py b/custom_components/ohme/coordinator.py index 57a6a68..ddb8b9d 100644 --- a/custom_components/ohme/coordinator.py +++ b/custom_components/ohme/coordinator.py @@ -6,7 +6,8 @@ UpdateFailed ) -from .const import DOMAIN, DATA_CLIENT +from .const import DOMAIN, DATA_CLIENT, DEFAULT_INTERVAL_CHARGESESSIONS, DEFAULT_INTERVAL_ACCOUNTINFO, DEFAULT_INTERVAL_ADVANCED, DEFAULT_INTERVAL_SCHEDULES +from .utils import get_option _LOGGER = logging.getLogger(__name__) @@ -20,7 +21,9 @@ def __init__(self, hass): hass, _LOGGER, name="Ohme Charge Sessions", - update_interval=timedelta(seconds=30), + update_interval=timedelta(minutes= + get_option(hass, "interval_chargesessions", DEFAULT_INTERVAL_CHARGESESSIONS) + ), ) self._client = hass.data[DOMAIN][DATA_CLIENT] @@ -42,7 +45,9 @@ def __init__(self, hass): hass, _LOGGER, name="Ohme Account Info", - update_interval=timedelta(minutes=1), + update_interval=timedelta(minutes= + get_option(hass, "interval_accountinfo", DEFAULT_INTERVAL_ACCOUNTINFO) + ), ) self._client = hass.data[DOMAIN][DATA_CLIENT] @@ -64,7 +69,9 @@ def __init__(self, hass): hass, _LOGGER, name="Ohme Advanced Settings", - update_interval=timedelta(minutes=1), + update_interval=timedelta(minutes= + get_option(hass, "interval_advanced", DEFAULT_INTERVAL_ADVANCED) + ), ) self._client = hass.data[DOMAIN][DATA_CLIENT] @@ -86,7 +93,9 @@ def __init__(self, hass): hass, _LOGGER, name="Ohme Charge Schedules", - update_interval=timedelta(minutes=10), + update_interval=timedelta(minutes= + get_option(hass, "interval_schedules", DEFAULT_INTERVAL_SCHEDULES) + ), ) self._client = hass.data[DOMAIN][DATA_CLIENT] @@ -97,4 +106,3 @@ async def _async_update_data(self): except BaseException: raise UpdateFailed("Error communicating with API") - diff --git a/custom_components/ohme/translations/en.json b/custom_components/ohme/translations/en.json index 090a916..a3c8001 100644 --- a/custom_components/ohme/translations/en.json +++ b/custom_components/ohme/translations/en.json @@ -23,12 +23,17 @@ "email": "Email address", "password": "Password", "never_session_specific": "Never update an ongoing session", - "never_collapse_slots": "Don't collapse charge slots" + "never_collapse_slots": "Don't collapse charge slots", + "interval_chargesessions": "Charge sessions refresh rate (minutes)", + "interval_accountinfo": "Account info refresh rate (minutes)", + "interval_advanced": "Advanced settings refresh rate (minutes)", + "interval_schedules": "Schedules refresh rate (minutes)" }, "data_description": { "password": "If you are not changing your credentials, leave the password field empty.", "never_session_specific": "When adjusting charge percentage, charge target or preconditioning settings, the schedule will always be updated even if a charge session is in progress.", - "never_collapse_slots": "By default, adjacent slots are merged into one. This option shows every slot, as shown in the Ohme app." + "never_collapse_slots": "By default, adjacent slots are merged into one. This option shows every slot, as shown in the Ohme app.", + "interval_schedules": "Details on which entities are updated by each coordinator are in the README." } } },