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

Climate: request API data on state switch until switch is confirmed #139

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
30 changes: 25 additions & 5 deletions custom_components/smarthashtag/climate.py
Original file line number Diff line number Diff line change
@@ -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,
)


Expand Down Expand Up @@ -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):
Expand All @@ -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
Expand Down Expand Up @@ -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."""
Expand Down
1 change: 1 addition & 0 deletions custom_components/smarthashtag/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 12 additions & 2 deletions custom_components/smarthashtag/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
DEFAULT_DRIVING_INTERVAL,
DEFAULT_SCAN_INTERVAL,
DOMAIN,
FAST_INTERVAL,
)
from .coordinator import SmartHashtagDataUpdateCoordinator
from .entity import SmartHashtagEntity
Expand Down Expand Up @@ -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:
Expand All @@ -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(
Expand All @@ -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:
Expand Down Expand Up @@ -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(
Expand All @@ -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
Expand Down
Loading