diff --git a/custom_components/bluetti_bt/__init__.py b/custom_components/bluetti_bt/__init__.py index 6c5a740..5b51176 100644 --- a/custom_components/bluetti_bt/__init__.py +++ b/custom_components/bluetti_bt/__init__.py @@ -12,7 +12,7 @@ from homeassistant.core import HomeAssistant from homeassistant.helpers.entity import DeviceInfo from homeassistant.exceptions import ConfigEntryNotReady -from .const import DATA_COORDINATOR, DOMAIN, MANUFACTURER, CONF_USE_CONTROLS, DATA_POLLING_RUNNING +from .const import CONF_POLLING_INTERVAL, DATA_COORDINATOR, DOMAIN, MANUFACTURER, CONF_USE_CONTROLS, DATA_POLLING_RUNNING from .coordinator import PollingCoordinator PLATFORMS: [Platform] = [Platform.BINARY_SENSOR, Platform.SENSOR] @@ -25,6 +25,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: address = entry.data.get(CONF_ADDRESS) device_name = entry.data.get(CONF_NAME) use_controls = entry.data.get(CONF_USE_CONTROLS) + polling_interval = entry.data.get(CONF_POLLING_INTERVAL, 20) if address is None: return False @@ -38,7 +39,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: hass.data[DOMAIN][entry.entry_id].setdefault(DATA_POLLING_RUNNING, False) # Create coordinator for polling - coordinator = PollingCoordinator(hass, address, device_name) + coordinator = PollingCoordinator(hass, address, device_name, polling_interval) await coordinator.async_config_entry_first_refresh() hass.data[DOMAIN][entry.entry_id].setdefault(DATA_COORDINATOR, coordinator) diff --git a/custom_components/bluetti_bt/config_flow.py b/custom_components/bluetti_bt/config_flow.py index 6e818e5..98a74ec 100644 --- a/custom_components/bluetti_bt/config_flow.py +++ b/custom_components/bluetti_bt/config_flow.py @@ -17,7 +17,7 @@ from homeassistant.data_entry_flow import FlowResult from homeassistant.helpers import selector from . import get_type_by_bt_name -from .const import DOMAIN, CONF_USE_CONTROLS +from .const import CONF_PERSISTANT_CONN, CONF_POLLING_INTERVAL, DOMAIN, CONF_USE_CONTROLS _LOGGER = logging.getLogger(__name__) @@ -114,6 +114,8 @@ class OptionsFlowHandler(config_entries.OptionsFlow): **self.config_entry.data, **{ CONF_USE_CONTROLS: user_input[CONF_USE_CONTROLS], + CONF_PERSISTANT_CONN: user_input[CONF_PERSISTANT_CONN], + CONF_POLLING_INTERVAL: user_input[CONF_POLLING_INTERVAL], }, }, ) @@ -126,6 +128,8 @@ class OptionsFlowHandler(config_entries.OptionsFlow): title="", data={ CONF_USE_CONTROLS: user_input[CONF_USE_CONTROLS], + CONF_PERSISTANT_CONN: user_input[CONF_PERSISTANT_CONN], + CONF_POLLING_INTERVAL: user_input[CONF_POLLING_INTERVAL], }, ) @@ -137,6 +141,14 @@ class OptionsFlowHandler(config_entries.OptionsFlow): CONF_USE_CONTROLS, default=self.config_entry.data.get(CONF_USE_CONTROLS, False), ): selector.BooleanSelector(), + vol.Required( + CONF_PERSISTANT_CONN, + default=self.config_entry.data.get(CONF_PERSISTANT_CONN, False), + ): selector.BooleanSelector(), + vol.Required( + CONF_POLLING_INTERVAL, + default=self.config_entry.data.get(CONF_POLLING_INTERVAL, 20), + ): int, } ), ) diff --git a/custom_components/bluetti_bt/const.py b/custom_components/bluetti_bt/const.py index 2fdacb7..dd7baae 100644 --- a/custom_components/bluetti_bt/const.py +++ b/custom_components/bluetti_bt/const.py @@ -7,6 +7,8 @@ MANUFACTURER = "Bluetti" CONF_OPTIONS = "options" CONF_USE_CONTROLS = "use_controls" +CONF_PERSISTANT_CONN = "persistant_conn" +CONF_POLLING_INTERVAL = "polling_interval" DATA_COORDINATOR = "coordinator" DATA_POLLING_RUNNING = "polling_running" diff --git a/custom_components/bluetti_bt/coordinator.py b/custom_components/bluetti_bt/coordinator.py index 0a62d41..b8e6694 100644 --- a/custom_components/bluetti_bt/coordinator.py +++ b/custom_components/bluetti_bt/coordinator.py @@ -129,13 +129,13 @@ class DummyDevice(BluettiDevice): class PollingCoordinator(DataUpdateCoordinator): """Polling coordinator.""" - def __init__(self, hass: HomeAssistant, address, device_name: str): + def __init__(self, hass: HomeAssistant, address, device_name: str, polling_interval: int): """Initialize coordinator.""" super().__init__( hass, _LOGGER, name="Bluetti polling coordinator", - update_interval=timedelta(seconds=20), + update_interval=timedelta(seconds=polling_interval), ) self._address = address self.notify_future = None diff --git a/custom_components/bluetti_bt/translations/de.json b/custom_components/bluetti_bt/translations/de.json index 8b1d377..850227c 100644 --- a/custom_components/bluetti_bt/translations/de.json +++ b/custom_components/bluetti_bt/translations/de.json @@ -3,7 +3,9 @@ "step": { "init": { "data": { - "use_controls": "Steuerung aktivieren (auf eigenes Risiko)" + "use_controls": "Steuerung aktivieren (auf eigenes Risiko)", + "persistant_conn": "Dauerhafte Verbindung", + "polling_interval": "Datenabruf-Intervall" } } } diff --git a/custom_components/bluetti_bt/translations/en.json b/custom_components/bluetti_bt/translations/en.json index d3fceda..e622734 100644 --- a/custom_components/bluetti_bt/translations/en.json +++ b/custom_components/bluetti_bt/translations/en.json @@ -3,7 +3,9 @@ "step": { "init": { "data": { - "use_controls": "Use controls (use at own risk)" + "use_controls": "Use controls (use at own risk)", + "persistant_conn": "Persistant connection", + "polling_interval": "Polling interval" } } }