Skip to content
This repository was archived by the owner on Mar 5, 2025. It is now read-only.

Commit 3deeccf

Browse files
authored
Refactor target percent/time logic and fix pending approval bug
1 parent 3675b08 commit 3deeccf

File tree

5 files changed

+34
-22
lines changed

5 files changed

+34
-22
lines changed

custom_components/ohme/api_client.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ async def async_max_charge(self):
194194
result = await self._put_request(f"/v1/chargeSessions/{self._serial}/rule?maxCharge=true")
195195
return bool(result)
196196

197-
async def async_apply_charge_rule(self, max_price=None, target_time=None, target_percent=None, pre_condition=None, pre_condition_length=None):
197+
async def async_apply_session_rule(self, max_price=None, target_time=None, target_percent=None, pre_condition=None, pre_condition_length=None):
198198
"""Apply rule to ongoing charge/stop max charge."""
199199
# Check every property. If we've provided it, use that. If not, use the existing.
200200
if max_price is None:

custom_components/ohme/number.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from homeassistant.helpers.entity import generate_entity_id
55
from homeassistant.core import callback, HomeAssistant
66
from .const import DOMAIN, DATA_CLIENT, DATA_COORDINATORS, COORDINATOR_CHARGESESSIONS, COORDINATOR_SCHEDULES
7-
7+
from .utils import session_in_progress
88

99
async def async_setup_entry(
1010
hass: HomeAssistant,
@@ -50,15 +50,15 @@ def unique_id(self):
5050

5151
async def async_set_native_value(self, value: float) -> None:
5252
"""Update the current value."""
53-
# If disconnected, update top rule. If not, apply rule to current session
54-
if self.coordinator.data and self.coordinator.data['mode'] == "DISCONNECTED":
55-
await self._client.async_update_schedule(target_percent=int(value))
53+
# If session in progress, update this session, if not update the first schedule
54+
if session_in_progress(self.coordinator.data):
55+
await self._client.async_apply_session_rule(target_percent=int(value))
5656
await asyncio.sleep(1)
57-
await self.coordinator_schedules.async_refresh()
57+
await self.coordinator.async_refresh()
5858
else:
59-
await self._client.async_apply_charge_rule(target_percent=int(value))
59+
await self._client.async_update_schedule(target_percent=int(value))
6060
await asyncio.sleep(1)
61-
await self.coordinator.async_refresh()
61+
await self.coordinator_schedules.async_refresh()
6262

6363
@property
6464
def icon(self):
@@ -68,9 +68,9 @@ def icon(self):
6868
@property
6969
def native_value(self):
7070
"""Get value from data returned from API by coordinator"""
71-
if self.coordinator.data and self.coordinator.data['appliedRule'] and self.coordinator.data['mode'] != "PENDING_APPROVAL" and self.coordinator.data['mode'] != "DISCONNECTED":
72-
target = round(
73-
self.coordinator.data['appliedRule']['targetPercent'])
71+
# Set with the same logic as reading
72+
if session_in_progress(self.coordinator.data):
73+
target = round(self.coordinator.data['appliedRule']['targetPercent'])
7474
elif self.coordinator_schedules.data:
7575
target = round(self.coordinator_schedules.data['targetPercent'])
7676

custom_components/ohme/switch.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ async def async_turn_on(self):
161161
async def async_turn_off(self):
162162
"""Stop max charging.
163163
We are not changing anything, just applying the last rule. No need to supply anything."""
164-
await self._client.async_apply_charge_rule()
164+
await self._client.async_apply_session_rule()
165165

166166
await asyncio.sleep(1)
167167
await self.coordinator.async_refresh()

custom_components/ohme/time.py

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from homeassistant.helpers.entity import generate_entity_id
66
from homeassistant.core import callback, HomeAssistant
77
from .const import DOMAIN, DATA_CLIENT, DATA_COORDINATORS, COORDINATOR_CHARGESESSIONS, COORDINATOR_SCHEDULES
8+
from .utils import session_in_progress
89
from datetime import time as dt_time
910

1011
_LOGGER = logging.getLogger(__name__)
@@ -52,17 +53,15 @@ def unique_id(self):
5253

5354
async def async_set_value(self, value: dt_time) -> None:
5455
"""Update the current value."""
55-
# If disconnected, update top rule. If not, apply rule to current session
56-
if self.coordinator.data and self.coordinator.data['mode'] == "DISCONNECTED":
57-
await self._client.async_update_schedule(target_time=(int(value.hour), int(value.minute)))
56+
# If session in progress, update this session, if not update the first schedule
57+
if session_in_progress(self.coordinator.data):
58+
await self._client.async_apply_session_rule(target_time=(int(value.hour), int(value.minute)))
5859
await asyncio.sleep(1)
59-
await self.coordinator_schedules.async_refresh()
60+
await self.coordinator.async_refresh()
6061
else:
61-
await self._client.async_apply_charge_rule(target_time=(int(value.hour), int(value.minute)))
62+
await self._client.async_update_schedule(target_time=(int(value.hour), int(value.minute)))
6263
await asyncio.sleep(1)
63-
await self.coordinator.async_refresh()
64-
65-
64+
await self.coordinator_schedules.async_refresh()
6665

6766
@property
6867
def icon(self):
@@ -72,9 +71,9 @@ def icon(self):
7271
@property
7372
def native_value(self):
7473
"""Get value from data returned from API by coordinator"""
75-
# If we are not pending approval or disconnected, return in progress charge rule
74+
# Set with the same logic as reading
7675
target = None
77-
if self.coordinator.data and self.coordinator.data['appliedRule'] and self.coordinator.data['mode'] != "PENDING_APPROVAL" and self.coordinator.data['mode'] != "DISCONNECTED":
76+
if session_in_progress(self.coordinator.data):
7877
target = self.coordinator.data['appliedRule']['targetTime']
7978
elif self.coordinator_schedules.data:
8079
target = self.coordinator_schedules.data['targetTime']

custom_components/ohme/utils.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,3 +73,16 @@ def time_next_occurs(hour, minute):
7373
target = target + timedelta(days=1)
7474

7575
return target
76+
77+
def session_in_progress(data):
78+
"""Is there a session in progress?
79+
Used to check if we should update the current session rather than the first schedule."""
80+
# Default to False with no data
81+
if not data:
82+
return False
83+
84+
# Car disconnected or pending approval, we should update the schedule
85+
if data['mode'] == "DISCONNECTED" or data['mode'] == "PENDING_APPROVAL":
86+
return False
87+
88+
return True

0 commit comments

Comments
 (0)