55from datetime import datetime , timedelta
66from homeassistant .helpers .entity import DeviceInfo
77from .const import DOMAIN
8+ from .utils import time_next_occurs
89
910_LOGGER = logging .getLogger (__name__ )
1011
@@ -36,6 +37,9 @@ def __init__(self, email, password):
3637 self ._user_id = ""
3738 self ._serial = ""
3839
40+ # Cache the last rule to use when we disable max charge or change schedule
41+ self ._last_rule = {}
42+
3943 # Sessions
4044 self ._session = aiohttp .ClientSession (
4145 base_url = "https://api.ohme.io" )
@@ -156,7 +160,7 @@ async def _get_request(self, url):
156160 def ct_connected (self ):
157161 """Is CT clamp connected."""
158162 return self ._ct_connected
159-
163+
160164 def is_capable (self , capability ):
161165 """Return whether or not this model has a given capability."""
162166 return bool (self ._capabilities [capability ])
@@ -189,9 +193,33 @@ async def async_max_charge(self):
189193 result = await self ._put_request (f"/v1/chargeSessions/{ self ._serial } /rule?maxCharge=true" )
190194 return bool (result )
191195
192- async def async_apply_charge_rule (self , max_price = False , target_ts = 0 , target_percent = 100 , pre_condition = False , pre_condition_length = 0 ):
196+ async def async_apply_charge_rule (self , max_price = None , target_time = None , target_percent = None , pre_condition = None , pre_condition_length = None ):
193197 """Apply charge rule/stop max charge."""
198+ # Check every property. If we've provided it, use that. If not, use the existing.
199+ if max_price is None :
200+ max_price = self ._last_rule ['settings' ][0 ]['enabled' ] if 'settings' in self ._last_rule and len (
201+ self ._last_rule ['settings' ]) > 1 else False
202+
203+ if target_percent is None :
204+ target_percent = self ._last_rule ['targetPercent' ] if 'targetPercent' in self ._last_rule else 80
205+
206+ if pre_condition is None :
207+ pre_condition = self ._last_rule ['preconditioningEnabled' ] if 'preconditioningEnabled' in self ._last_rule else False
208+
209+ if pre_condition_length is None :
210+ pre_condition_length = self ._last_rule [
211+ 'preconditionLengthMins' ] if 'preconditionLengthMins' in self ._last_rule else 30
194212
213+ if target_time is None :
214+ # Default to 9am
215+ target_time = self ._last_rule ['targetTime' ] if 'targetTime' in self ._last_rule else 32400
216+ target_time = (target_time // 3600 ,
217+ (target_time % 3600 ) // 60 )
218+
219+ target_ts = int (time_next_occurs (
220+ target_time [0 ], target_time [1 ]).timestamp () * 1000 )
221+
222+ # Convert these to string form
195223 max_price = 'true' if max_price else 'false'
196224 pre_condition = 'true' if pre_condition else 'false'
197225
@@ -209,8 +237,13 @@ async def async_get_charge_sessions(self, is_retry=False):
209237 """Try to fetch charge sessions endpoint.
210238 If we get a non 200 response, refresh auth token and try again"""
211239 resp = await self ._get_request ('/v1/chargeSessions' )
240+ resp = resp [0 ]
212241
213- return resp [0 ]
242+ # Cache the current rule if we are given it
243+ if resp ["mode" ] == "SMART_CHARGE" and 'appliedRule' in resp :
244+ self ._last_rule = resp ["appliedRule" ]
245+
246+ return resp
214247
215248 async def async_get_account_info (self ):
216249 resp = await self ._get_request ('/v1/users/me/account' )
@@ -249,7 +282,7 @@ async def async_get_charge_statistics(self):
249282 async def async_get_ct_reading (self ):
250283 """Get CT clamp reading."""
251284 resp = await self ._get_request (f"/v1/chargeDevices/{ self ._serial } /advancedSettings" )
252-
285+
253286 # If we ever get a reading above 0, assume CT connected
254287 if resp ['clampAmps' ] and resp ['clampAmps' ] > 0 :
255288 self ._ct_connected = True
0 commit comments