44from homeassistant .components .time import TimeEntity
55from homeassistant .helpers .entity import generate_entity_id
66from homeassistant .core import callback , HomeAssistant
7- from .const import DOMAIN , DATA_CLIENT , DATA_COORDINATORS , COORDINATOR_CHARGESESSIONS , COORDINATOR_ACCOUNTINFO
7+ from .const import DOMAIN , DATA_CLIENT , DATA_COORDINATORS , COORDINATOR_CHARGESESSIONS , COORDINATOR_SCHEDULES
88from datetime import time as dt_time
99
1010_LOGGER = logging .getLogger (__name__ )
@@ -18,10 +18,10 @@ async def async_setup_entry(
1818 """Setup switches and configure coordinator."""
1919 coordinators = hass .data [DOMAIN ][DATA_COORDINATORS ]
2020
21- coordinator = coordinators [COORDINATOR_CHARGESESSIONS ]
2221 client = hass .data [DOMAIN ][DATA_CLIENT ]
2322
24- numbers = [TargetTime (coordinator , hass , client )]
23+ numbers = [TargetTime (coordinators [COORDINATOR_CHARGESESSIONS ],
24+ coordinators [COORDINATOR_SCHEDULES ], hass , client )]
2525
2626 async_add_entities (numbers , update_before_add = True )
2727
@@ -30,8 +30,9 @@ class TargetTime(TimeEntity):
3030 """Target time sensor."""
3131 _attr_name = "Target Time"
3232
33- def __init__ (self , coordinator , hass : HomeAssistant , client ):
33+ def __init__ (self , coordinator , coordinator_schedules , hass : HomeAssistant , client ):
3434 self .coordinator = coordinator
35+ self .coordinator_schedules = coordinator_schedules
3536
3637 self ._client = client
3738
@@ -51,10 +52,17 @@ def unique_id(self):
5152
5253 async def async_set_value (self , value : dt_time ) -> None :
5354 """Update the current value."""
54- await self ._client .async_apply_charge_rule (target_time = (int (value .hour ), int (value .minute )))
55-
56- await asyncio .sleep (1 )
57- await self .coordinator .async_refresh ()
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 )))
58+ await asyncio .sleep (1 )
59+ await self .coordinator_schedules .async_refresh ()
60+ else :
61+ await self ._client .async_apply_charge_rule (target_time = (int (value .hour ), int (value .minute )))
62+ await asyncio .sleep (1 )
63+ await self .coordinator .async_refresh ()
64+
65+
5866
5967 @property
6068 def icon (self ):
@@ -64,9 +72,14 @@ def icon(self):
6472 @property
6573 def native_value (self ):
6674 """Get value from data returned from API by coordinator"""
67- # Make sure we're not pending approval, as this sets the target time to now
68- if self .coordinator .data and self .coordinator .data ['appliedRule' ] and self .coordinator .data ['mode' ] != "PENDING_APPROVAL" :
75+ # If we are not pending approval or disconnected, return in progress charge rule
76+ 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" :
6978 target = self .coordinator .data ['appliedRule' ]['targetTime' ]
79+ elif self .coordinator_schedules .data :
80+ target = self .coordinator_schedules .data ['targetTime' ]
81+
82+ if target :
7083 self ._state = dt_time (
7184 hour = target // 3600 ,
7285 minute = (target % 3600 ) // 60 ,
0 commit comments