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

fix: reduce force refreshing when not needed #153

Closed
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
5 changes: 1 addition & 4 deletions custom_components/kia_uvo/KiaUvoAPIUSA.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,7 @@

from .const import (
DOMAIN,
BRANDS,
BRAND_HYUNDAI,
BRAND_KIA,
DATE_FORMAT,
VEHICLE_LOCK_ACTION,
)
from .KiaUvoApiImpl import KiaUvoApiImpl
from .Token import Token
Expand Down Expand Up @@ -91,6 +87,7 @@ def __init__(
super().__init__(
username, password, region, brand, use_email_with_geocode_api, pin
)
self.synchronous_actions = True

# Randomly generate a plausible device id on startup
self.device_id = (
Expand Down
7 changes: 1 addition & 6 deletions custom_components/kia_uvo/KiaUvoApiImpl.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
import logging

from datetime import datetime
from datetime import tzinfo
import push_receiver
import random
import requests
import uuid
from urllib.parse import parse_qs, urlparse

from homeassistant.util import dt as dt_util

Expand All @@ -33,6 +27,7 @@ def __init__(
self.stamps = None
self.region = region
self.brand = brand
self.synchronous_actions = False

def login(self) -> Token:
pass
Expand Down
36 changes: 14 additions & 22 deletions custom_components/kia_uvo/Vehicle.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

from datetime import datetime
import re
import requests
import traceback

from homeassistant.config_entries import ConfigEntry
Expand All @@ -12,7 +11,6 @@
async_dispatcher_send,
)
from homeassistant.helpers.event import async_call_later
import homeassistant.util.dt as dt_util

from .const import *
from .KiaUvoApiImpl import KiaUvoApiImpl
Expand Down Expand Up @@ -92,6 +90,15 @@ async def force_update(self):
)
await self.update()

async def force_update_loop_start(self):
if self.kia_uvo_api.synchronous_actions:
await self.update()
else:
self.force_update_try_count = 0
self.force_update_try_caller = async_call_later(
self.hass, START_FORCE_UPDATE_AFTER_COMMAND, self.force_update_loop
)

async def force_update_loop(self, _):
_LOGGER.debug(
f"{DOMAIN} - force_update_loop start {self.force_update_try_count} {COUNT_FORCE_UPDATE_AFTER_COMMAND}"
Expand Down Expand Up @@ -136,10 +143,7 @@ async def lock_action(self, action: VEHICLE_LOCK_ACTION):
await self.hass.async_add_executor_job(
self.kia_uvo_api.lock_action, self.token, action.value
)
self.force_update_try_count = 0
self.force_update_try_caller = async_call_later(
self.hass, START_FORCE_UPDATE_AFTER_COMMAND, self.force_update_loop
)
await self.force_update_loop_start()

async def refresh_token(self):
_LOGGER.debug(
Expand Down Expand Up @@ -182,10 +186,7 @@ async def start_climate(self, set_temp, duration, defrost, climate, heating):
climate,
heating,
)
self.force_update_try_count = 0
self.force_update_try_caller = async_call_later(
self.hass, START_FORCE_UPDATE_AFTER_COMMAND, self.force_update_loop
)
await self.force_update_loop_start()

async def stop_climate(self):
if self.engine_type == VEHICLE_ENGINE_TYPE.EV and self.region == REGION_CANADA:
Expand All @@ -196,26 +197,17 @@ async def stop_climate(self):
await self.hass.async_add_executor_job(
self.kia_uvo_api.stop_climate, self.token
)
self.force_update_try_count = 0
self.force_update_try_caller = async_call_later(
self.hass, START_FORCE_UPDATE_AFTER_COMMAND, self.force_update_loop
)
await self.force_update_loop_start()

async def start_charge(self):
await self.hass.async_add_executor_job(
self.kia_uvo_api.start_charge, self.token
)
self.force_update_try_count = 0
self.force_update_try_caller = async_call_later(
self.hass, START_FORCE_UPDATE_AFTER_COMMAND, self.force_update_loop
)
await self.force_update_loop_start()

async def stop_charge(self):
await self.hass.async_add_executor_job(self.kia_uvo_api.stop_charge, self.token)
self.force_update_try_count = 0
self.force_update_try_caller = async_call_later(
self.hass, START_FORCE_UPDATE_AFTER_COMMAND, self.force_update_loop
)
await self.force_update_loop_start()

def login(self):
self.token = self.kia_uvo_api.login()
Expand Down