Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

coordinator: update interval manager #149

Merged
merged 1 commit into from
Aug 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 9 additions & 13 deletions custom_components/smarthashtag/climate.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
from homeassistant.helpers.entity import EntityCategory
from homeassistant.const import (
ATTR_TEMPERATURE,
CONF_SCAN_INTERVAL,
UnitOfTemperature,
)
from .coordinator import SmartHashtagDataUpdateCoordinator
Expand All @@ -17,7 +16,6 @@
CONF_CONDITIONING_TEMP,
CONF_VEHICLE,
DEFAULT_CONDITIONING_TEMP,
DEFAULT_SCAN_INTERVAL,
DOMAIN,
FAST_INTERVAL,
)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand All @@ -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:
Expand Down
24 changes: 23 additions & 1 deletion custom_components/smarthashtag/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand All @@ -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."""
Expand All @@ -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
)
),
)
50 changes: 19 additions & 31 deletions custom_components/smarthashtag/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@
SensorDeviceClass,
SensorStateClass,
)
from homeassistant.const import (
CONF_SCAN_INTERVAL,
)
from pysmarthashtag.models import ValueWithUnit

from .const import (
Expand All @@ -21,7 +18,6 @@
CONF_VEHICLE,
DEFAULT_CHARGING_INTERVAL,
DEFAULT_DRIVING_INTERVAL,
DEFAULT_SCAN_INTERVAL,
DOMAIN,
)
from .coordinator import SmartHashtagDataUpdateCoordinator
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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):
Expand Down
Loading