diff --git a/custom_components/smarthashtag/climate.py b/custom_components/smarthashtag/climate.py index 568f8e7..24da777 100644 --- a/custom_components/smarthashtag/climate.py +++ b/custom_components/smarthashtag/climate.py @@ -1,19 +1,25 @@ """Support for Smart #1 / #3 switches.""" +from datetime import timedelta from typing import Any from homeassistant.components.climate import ClimateEntity, ClimateEntityFeature from homeassistant.components.climate.const import HVACMode from homeassistant.core import HomeAssistant from homeassistant.helpers.entity import EntityCategory -from homeassistant.const import ATTR_TEMPERATURE, UnitOfTemperature - +from homeassistant.const import ( + ATTR_TEMPERATURE, + CONF_SCAN_INTERVAL, + UnitOfTemperature, +) from .coordinator import SmartHashtagDataUpdateCoordinator from .const import ( CONF_CONDITIONING_TEMP, CONF_VEHICLE, DEFAULT_CONDITIONING_TEMP, + DEFAULT_SCAN_INTERVAL, DOMAIN, + FAST_INTERVAL, ) @@ -46,6 +52,7 @@ class SmartConditioningMode(ClimateEntity): _attr_has_entity_name = True _attr_icon = "mdi:thermostat-auto" _enable_turn_on_off_backwards_compatibility = False + _last_mode = HVACMode.OFF @property def translation_key(self): @@ -54,12 +61,21 @@ def translation_key(self): @property def hvac_mode(self) -> HVACMode: """Return hvac operating mode: heat, cool""" - return ( + current_mode = ( HVACMode.HEAT_COOL if self._vehicle.climate.pre_climate_active else HVACMode.OFF ) + # value is true, last setting is off -> keep on requesting + if current_mode == self._last_mode: + self.coordinator.update_interval = timedelta( + seconds=self.coordinator.config_entry.options.get( + CONF_SCAN_INTERVAL, DEFAULT_SCAN_INTERVAL + ) + ) + return current_mode + @property def temperature_unit(self): return UnitOfTemperature.CELSIUS @@ -100,14 +116,18 @@ async def async_turn_on(self) -> None: await self._vehicle.climate_control.set_climate_conditioning( self._temperature, True ) - self.coordinator.async_request_refresh() + self._last_mode = HVACMode.HEAT_COOL + self.coordinator.update_interval = timedelta(seconds=FAST_INTERVAL) + await self.coordinator.async_refresh() async def async_turn_off(self) -> None: """Turn off the climate system.""" await self._vehicle.climate_control.set_climate_conditioning( self._temperature, False ) - self.coordinator.async_request_refresh() + self._last_mode = HVACMode.OFF + self.coordinator.update_interval = timedelta(seconds=FAST_INTERVAL) + await self.coordinator.async_refresh() async def async_set_temperature(self, **kwargs: Any) -> None: """Set new target temperature for the vehicle.""" diff --git a/custom_components/smarthashtag/const.py b/custom_components/smarthashtag/const.py index 44d652c..0bf1d7e 100644 --- a/custom_components/smarthashtag/const.py +++ b/custom_components/smarthashtag/const.py @@ -39,6 +39,7 @@ DEFAULT_SCAN_INTERVAL = 300 DEFAULT_CHARGING_INTERVAL = 30 DEFAULT_DRIVING_INTERVAL = 60 +FAST_INTERVAL = 5 MIN_SCAN_INTERVAL = 10 DEFAULT_CONDITIONING_TEMP = 21 DEFAULT_SEATHEATING_LEVEL = 3 diff --git a/custom_components/smarthashtag/sensor.py b/custom_components/smarthashtag/sensor.py index 393859e..8461b82 100644 --- a/custom_components/smarthashtag/sensor.py +++ b/custom_components/smarthashtag/sensor.py @@ -23,6 +23,7 @@ DEFAULT_DRIVING_INTERVAL, DEFAULT_SCAN_INTERVAL, DOMAIN, + FAST_INTERVAL, ) from .coordinator import SmartHashtagDataUpdateCoordinator from .entity import SmartHashtagEntity @@ -1014,6 +1015,7 @@ def __init__( super().__init__(coordinator) self._attr_unique_id = f"{self._attr_unique_id}_{entity_description.key}" self.entity_description = entity_description + self._last_value = None @property def native_value(self) -> str | int | float: @@ -1025,7 +1027,10 @@ def native_value(self) -> str | int | float: remove_vin_from_key(self.entity_description.key), ) - if "charging_current" in self.entity_description.key: + if ( + "charging_current" in self.entity_description.key + and self.coordinator.update_interval.seconds != FAST_INTERVAL + ): if data.value != 0: self.coordinator.update_interval = timedelta( seconds=self.coordinator.config_entry.options.get( @@ -1038,6 +1043,7 @@ def native_value(self) -> str | int | float: CONF_SCAN_INTERVAL, DEFAULT_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: @@ -1143,7 +1149,10 @@ def native_value(self) -> float: remove_vin_from_key(self.entity_description.key), ) - if key == "engine_state": + if ( + key == "engine_state" + and self.coordinator.update_interval.seconds != FAST_INTERVAL + ): if data == "engine_running": self.coordinator.update_interval = timedelta( seconds=self.coordinator.config_entry.options.get( @@ -1158,6 +1167,7 @@ def native_value(self) -> float: ) ) self.icon = "mdi:engine-off" + self.hass.async_create_task(self.coordinator.async_request_refresh()) if isinstance(data, ValueWithUnit): return data.value