Skip to content

Commit

Permalink
Refactor target percent/time logic and fix pending approval bug
Browse files Browse the repository at this point in the history
  • Loading branch information
dan-r authored Jan 3, 2024
1 parent 3675b08 commit 3deeccf
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 22 deletions.
2 changes: 1 addition & 1 deletion custom_components/ohme/api_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ async def async_max_charge(self):
result = await self._put_request(f"/v1/chargeSessions/{self._serial}/rule?maxCharge=true")
return bool(result)

async def async_apply_charge_rule(self, max_price=None, target_time=None, target_percent=None, pre_condition=None, pre_condition_length=None):
async def async_apply_session_rule(self, max_price=None, target_time=None, target_percent=None, pre_condition=None, pre_condition_length=None):
"""Apply rule to ongoing charge/stop max charge."""
# Check every property. If we've provided it, use that. If not, use the existing.
if max_price is None:
Expand Down
20 changes: 10 additions & 10 deletions custom_components/ohme/number.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from homeassistant.helpers.entity import generate_entity_id
from homeassistant.core import callback, HomeAssistant
from .const import DOMAIN, DATA_CLIENT, DATA_COORDINATORS, COORDINATOR_CHARGESESSIONS, COORDINATOR_SCHEDULES

from .utils import session_in_progress

async def async_setup_entry(
hass: HomeAssistant,
Expand Down Expand Up @@ -50,15 +50,15 @@ def unique_id(self):

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

@property
def icon(self):
Expand All @@ -68,9 +68,9 @@ def icon(self):
@property
def native_value(self):
"""Get value from data returned from API by coordinator"""
if self.coordinator.data and self.coordinator.data['appliedRule'] and self.coordinator.data['mode'] != "PENDING_APPROVAL" and self.coordinator.data['mode'] != "DISCONNECTED":
target = round(
self.coordinator.data['appliedRule']['targetPercent'])
# Set with the same logic as reading
if session_in_progress(self.coordinator.data):
target = round(self.coordinator.data['appliedRule']['targetPercent'])
elif self.coordinator_schedules.data:
target = round(self.coordinator_schedules.data['targetPercent'])

Expand Down
2 changes: 1 addition & 1 deletion custom_components/ohme/switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ async def async_turn_on(self):
async def async_turn_off(self):
"""Stop max charging.
We are not changing anything, just applying the last rule. No need to supply anything."""
await self._client.async_apply_charge_rule()
await self._client.async_apply_session_rule()

await asyncio.sleep(1)
await self.coordinator.async_refresh()
Expand Down
19 changes: 9 additions & 10 deletions custom_components/ohme/time.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from homeassistant.helpers.entity import generate_entity_id
from homeassistant.core import callback, HomeAssistant
from .const import DOMAIN, DATA_CLIENT, DATA_COORDINATORS, COORDINATOR_CHARGESESSIONS, COORDINATOR_SCHEDULES
from .utils import session_in_progress
from datetime import time as dt_time

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

async def async_set_value(self, value: dt_time) -> None:
"""Update the current value."""
# If disconnected, update top rule. If not, apply rule to current session
if self.coordinator.data and self.coordinator.data['mode'] == "DISCONNECTED":
await self._client.async_update_schedule(target_time=(int(value.hour), int(value.minute)))
# If session in progress, update this session, if not update the first schedule
if session_in_progress(self.coordinator.data):
await self._client.async_apply_session_rule(target_time=(int(value.hour), int(value.minute)))
await asyncio.sleep(1)
await self.coordinator_schedules.async_refresh()
await self.coordinator.async_refresh()
else:
await self._client.async_apply_charge_rule(target_time=(int(value.hour), int(value.minute)))
await self._client.async_update_schedule(target_time=(int(value.hour), int(value.minute)))
await asyncio.sleep(1)
await self.coordinator.async_refresh()


await self.coordinator_schedules.async_refresh()

@property
def icon(self):
Expand All @@ -72,9 +71,9 @@ def icon(self):
@property
def native_value(self):
"""Get value from data returned from API by coordinator"""
# If we are not pending approval or disconnected, return in progress charge rule
# Set with the same logic as reading
target = None
if self.coordinator.data and self.coordinator.data['appliedRule'] and self.coordinator.data['mode'] != "PENDING_APPROVAL" and self.coordinator.data['mode'] != "DISCONNECTED":
if session_in_progress(self.coordinator.data):
target = self.coordinator.data['appliedRule']['targetTime']
elif self.coordinator_schedules.data:
target = self.coordinator_schedules.data['targetTime']
Expand Down
13 changes: 13 additions & 0 deletions custom_components/ohme/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,16 @@ def time_next_occurs(hour, minute):
target = target + timedelta(days=1)

return target

def session_in_progress(data):
"""Is there a session in progress?
Used to check if we should update the current session rather than the first schedule."""
# Default to False with no data
if not data:
return False

# Car disconnected or pending approval, we should update the schedule
if data['mode'] == "DISCONNECTED" or data['mode'] == "PENDING_APPROVAL":
return False

return True

0 comments on commit 3deeccf

Please sign in to comment.