diff --git a/custom_components/smarthashtag/climate.py b/custom_components/smarthashtag/climate.py index 7ded24f..90f4bc0 100644 --- a/custom_components/smarthashtag/climate.py +++ b/custom_components/smarthashtag/climate.py @@ -8,7 +8,6 @@ from homeassistant.helpers.entity import EntityCategory from homeassistant.const import ( ATTR_TEMPERATURE, - CONF_SCAN_INTERVAL, UnitOfTemperature, ) from .coordinator import SmartHashtagDataUpdateCoordinator @@ -17,7 +16,6 @@ CONF_CONDITIONING_TEMP, CONF_VEHICLE, DEFAULT_CONDITIONING_TEMP, - DEFAULT_SCAN_INTERVAL, DOMAIN, FAST_INTERVAL, ) @@ -68,15 +66,9 @@ def hvac_mode(self) -> HVACMode: ) # value is true, last setting is off -> keep on requesting - 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 - ) - ) + if current_mode == self._last_mode: + self.coordinator.reset_update_interval("climate") + return current_mode @property @@ -120,7 +112,9 @@ async def async_turn_on(self) -> None: self._temperature, True ) self._last_mode = HVACMode.HEAT_COOL - self.coordinator.update_interval = timedelta(seconds=FAST_INTERVAL) + self.coordinator.set_update_interval( + "climate", timedelta(seconds=FAST_INTERVAL) + ) await self.coordinator.async_request_refresh() async def async_turn_off(self) -> None: @@ -129,7 +123,9 @@ async def async_turn_off(self) -> None: self._temperature, False ) self._last_mode = HVACMode.OFF - self.coordinator.update_interval = timedelta(seconds=FAST_INTERVAL) + self.coordinator.set_update_interval( + "climate", timedelta(seconds=FAST_INTERVAL) + ) await self.coordinator.async_request_refresh() async def async_set_temperature(self, **kwargs: Any) -> None: diff --git a/custom_components/smarthashtag/coordinator.py b/custom_components/smarthashtag/coordinator.py index e3d2e54..ca0d566 100644 --- a/custom_components/smarthashtag/coordinator.py +++ b/custom_components/smarthashtag/coordinator.py @@ -8,11 +8,12 @@ from homeassistant.exceptions import ConfigEntryAuthFailed from homeassistant.helpers.update_coordinator import DataUpdateCoordinator from homeassistant.helpers.update_coordinator import UpdateFailed +from homeassistant.const import CONF_SCAN_INTERVAL from pysmarthashtag.account import SmartAccount from pysmarthashtag.models import SmartAuthError, SmartAPIError from pysmarthashtag.models import SmartRemoteServiceError -from .const import DOMAIN +from .const import DEFAULT_SCAN_INTERVAL, DOMAIN from .const import LOGGER @@ -35,6 +36,7 @@ def __init__( name=DOMAIN, update_interval=timedelta(minutes=5), ) + self._update_intervals = {} async def _async_update_data(self): """Update data via library.""" @@ -46,3 +48,23 @@ async def _async_update_data(self): raise UpdateFailed(exception) from exception except SmartAPIError as exception: LOGGER.info(f"API access failed with: {exception}") + + def set_update_interval(self, key: str, deltatime: timedelta) -> None: + """Update intervals by key and select the shortest""" + LOGGER.info(f"Updatefrequency set for {key}: {deltatime}") + self._update_intervals[key] = deltatime + sorted_intervals = list(self._update_intervals.values()) + sorted_intervals.sort() + if sorted_intervals: + self.update_interval = sorted_intervals[0] + + def reset_update_interval(self, key: str): + """Reset interval for this key to default""" + self.set_update_interval( + key, + timedelta( + seconds=self.config_entry.options.get( + CONF_SCAN_INTERVAL, DEFAULT_SCAN_INTERVAL + ) + ), + ) diff --git a/custom_components/smarthashtag/sensor.py b/custom_components/smarthashtag/sensor.py index 0cdd80a..9b596d3 100644 --- a/custom_components/smarthashtag/sensor.py +++ b/custom_components/smarthashtag/sensor.py @@ -10,9 +10,6 @@ SensorDeviceClass, SensorStateClass, ) -from homeassistant.const import ( - CONF_SCAN_INTERVAL, -) from pysmarthashtag.models import ValueWithUnit from .const import ( @@ -21,7 +18,6 @@ CONF_VEHICLE, DEFAULT_CHARGING_INTERVAL, DEFAULT_DRIVING_INTERVAL, - DEFAULT_SCAN_INTERVAL, DOMAIN, ) from .coordinator import SmartHashtagDataUpdateCoordinator @@ -1124,23 +1120,18 @@ def native_value(self) -> str | int | float: ) 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: - if self.coordinator.update_interval.seconds == scan_interval: - self.coordinator.update_interval = timedelta( - seconds=charging_interval - ) - self.hass.async_create_task( - self.coordinator.async_request_refresh() - ) + self.coordinator.set_update_interval( + "charging", + timedelta( + seconds=self.coordinator.config_entry.options.get( + CONF_CHARGING_INTERVAL, DEFAULT_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.coordinator.reset_update_interval("charging") if "charging_power" in self.entity_description.key: if data.value == -0.0: @@ -1246,23 +1237,20 @@ def native_value(self) -> float: remove_vin_from_key(self.entity_description.key), ) - 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": - if self.coordinator.update_interval.seconds == scan_interval: - self.coordinator.update_interval = timedelta( - seconds=driving_interval - ) + self.coordinator.set_update_interval( + "driving", + timedelta( + seconds=self.coordinator.config_entry.options.get( + CONF_DRIVING_INTERVAL, DEFAULT_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.coordinator.reset_update_interval("driving") self.icon = "mdi:engine-off" if isinstance(data, ValueWithUnit):