From 6c2a419900a906bf777cd044daa9cc2a8220862d Mon Sep 17 00:00:00 2001 From: Bastian Neumann Date: Tue, 6 Aug 2024 18:52:25 +0000 Subject: [PATCH 1/4] Climate: do not mess with other intervals when resetting --- custom_components/smarthashtag/climate.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/custom_components/smarthashtag/climate.py b/custom_components/smarthashtag/climate.py index 24da777..0429cb0 100644 --- a/custom_components/smarthashtag/climate.py +++ b/custom_components/smarthashtag/climate.py @@ -68,7 +68,10 @@ def hvac_mode(self) -> HVACMode: ) # value is true, last setting is off -> keep on requesting - if current_mode == self._last_mode: + if ( + current_mode == self._last_mode + and self.coordinator.update_interval.seconds == FAST_INTERVAL + ): self.coordinator.update_interval = timedelta( seconds=self.coordinator.config_entry.options.get( CONF_SCAN_INTERVAL, DEFAULT_SCAN_INTERVAL From 835ab84f6ba57283642d5ceff1c7b9f7480e63cb Mon Sep 17 00:00:00 2001 From: Bastian Neumann Date: Tue, 6 Aug 2024 18:58:45 +0000 Subject: [PATCH 2/4] Sensor: do not mess with other update intervals than your own --- custom_components/smarthashtag/sensor.py | 51 +++++++++++------------- 1 file changed, 24 insertions(+), 27 deletions(-) diff --git a/custom_components/smarthashtag/sensor.py b/custom_components/smarthashtag/sensor.py index 8461b82..83f76fd 100644 --- a/custom_components/smarthashtag/sensor.py +++ b/custom_components/smarthashtag/sensor.py @@ -23,7 +23,6 @@ DEFAULT_DRIVING_INTERVAL, DEFAULT_SCAN_INTERVAL, DOMAIN, - FAST_INTERVAL, ) from .coordinator import SmartHashtagDataUpdateCoordinator from .entity import SmartHashtagEntity @@ -1027,22 +1026,21 @@ def native_value(self) -> str | int | float: remove_vin_from_key(self.entity_description.key), ) - if ( - "charging_current" in self.entity_description.key - and self.coordinator.update_interval.seconds != FAST_INTERVAL - ): + if "charging_current" in self.entity_description.key: + scan_interval = self.coordinator.config_entry.options.get( + CONF_SCAN_INTERVAL, DEFAULT_SCAN_INTERVAL + ) + charging_interval = self.coordinator.config_entry.options.get( + CONF_CHARGING_INTERVAL, DEFAULT_CHARGING_INTERVAL + ) if data.value != 0: - self.coordinator.update_interval = timedelta( - seconds=self.coordinator.config_entry.options.get( - CONF_CHARGING_INTERVAL, DEFAULT_CHARGING_INTERVAL + if self.coordinator.update_interval.seconds == scan_interval: + self.coordinator.update_interval = timedelta( + seconds=charging_interval ) - ) else: - self.coordinator.update_interval = timedelta( - seconds=self.coordinator.config_entry.options.get( - CONF_SCAN_INTERVAL, DEFAULT_SCAN_INTERVAL - ) - ) + if self.coordinator.update_interval.seconds == charging_interval: + self.coordinator.update_interval = timedelta(seconds=scan_interval) self.hass.async_create_task(self.coordinator.async_request_refresh()) if "charging_power" in self.entity_description.key: @@ -1149,23 +1147,22 @@ def native_value(self) -> float: remove_vin_from_key(self.entity_description.key), ) - if ( - key == "engine_state" - and self.coordinator.update_interval.seconds != FAST_INTERVAL - ): + scan_interval = self.coordinator.config_entry.options.get( + CONF_SCAN_INTERVAL, DEFAULT_SCAN_INTERVAL + ) + driving_interval = self.coordinator.config_entry.options.get( + CONF_DRIVING_INTERVAL, DEFAULT_DRIVING_INTERVAL + ) + if key == "engine_state": if data == "engine_running": - self.coordinator.update_interval = timedelta( - seconds=self.coordinator.config_entry.options.get( - CONF_DRIVING_INTERVAL, DEFAULT_DRIVING_INTERVAL + if self.coordinator.update_interval.seconds == scan_interval: + self.coordinator.update_interval = timedelta( + seconds=driving_interval ) - ) self.icon = "mdi:engine" else: - self.coordinator.update_interval = timedelta( - seconds=self.coordinator.config_entry.options.get( - CONF_SCAN_INTERVAL, DEFAULT_SCAN_INTERVAL - ) - ) + if self.coordinator.update_interval.seconds == driving_interval: + self.coordinator.update_interval = timedelta(seconds=scan_interval) self.icon = "mdi:engine-off" self.hass.async_create_task(self.coordinator.async_request_refresh()) From de8e8fcf9d9ed04bcd82949970d251d5ece15a64 Mon Sep 17 00:00:00 2001 From: Bastian Neumann Date: Tue, 6 Aug 2024 19:11:52 +0000 Subject: [PATCH 3/4] Climate: request refresh instead of forced --- custom_components/smarthashtag/climate.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/custom_components/smarthashtag/climate.py b/custom_components/smarthashtag/climate.py index 0429cb0..7ded24f 100644 --- a/custom_components/smarthashtag/climate.py +++ b/custom_components/smarthashtag/climate.py @@ -121,7 +121,7 @@ async def async_turn_on(self) -> None: ) self._last_mode = HVACMode.HEAT_COOL self.coordinator.update_interval = timedelta(seconds=FAST_INTERVAL) - await self.coordinator.async_refresh() + await self.coordinator.async_request_refresh() async def async_turn_off(self) -> None: """Turn off the climate system.""" @@ -130,7 +130,7 @@ async def async_turn_off(self) -> None: ) self._last_mode = HVACMode.OFF self.coordinator.update_interval = timedelta(seconds=FAST_INTERVAL) - await self.coordinator.async_refresh() + await self.coordinator.async_request_refresh() async def async_set_temperature(self, **kwargs: Any) -> None: """Set new target temperature for the vehicle.""" From f346878bb3da3885424aaf921d4f1950d2f9fdc3 Mon Sep 17 00:00:00 2001 From: Bastian Neumann Date: Tue, 6 Aug 2024 19:12:58 +0000 Subject: [PATCH 4/4] Sensor: only request refresh if interval changed --- custom_components/smarthashtag/sensor.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/custom_components/smarthashtag/sensor.py b/custom_components/smarthashtag/sensor.py index 83f76fd..f299c3b 100644 --- a/custom_components/smarthashtag/sensor.py +++ b/custom_components/smarthashtag/sensor.py @@ -1038,10 +1038,12 @@ def native_value(self) -> str | int | float: self.coordinator.update_interval = timedelta( seconds=charging_interval ) + self.hass.async_create_task( + self.coordinator.async_request_refresh() + ) else: if self.coordinator.update_interval.seconds == charging_interval: self.coordinator.update_interval = timedelta(seconds=scan_interval) - self.hass.async_create_task(self.coordinator.async_request_refresh()) if "charging_power" in self.entity_description.key: if data.value == -0.0: @@ -1159,12 +1161,12 @@ def native_value(self) -> float: self.coordinator.update_interval = timedelta( seconds=driving_interval ) + self.hass.async_create_task(self.coordinator.async_request_refresh()) self.icon = "mdi:engine" else: if self.coordinator.update_interval.seconds == driving_interval: self.coordinator.update_interval = timedelta(seconds=scan_interval) self.icon = "mdi:engine-off" - self.hass.async_create_task(self.coordinator.async_request_refresh()) if isinstance(data, ValueWithUnit): return data.value