Skip to content

Commit

Permalink
Update occupancy sync.
Browse files Browse the repository at this point in the history
  • Loading branch information
rlutes committed May 29, 2024
1 parent b0761ca commit a63ca6b
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 19 deletions.
6 changes: 3 additions & 3 deletions aems-edge/Manager/manager/data_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ def get_current_oat(self) -> tuple[dt | None, float | None]:
:rtype: tuple[dt | None, float | None]
"""
if not self.df.empty:
if Points.outdoorairtemperature.name in self.df.columns:
df = self.df[self.df[Points.outdoorairtemperature.name].notna()]
return df.index[-1], df[Points.outdoorairtemperature.name].iloc[-1]
if Points.outdoortemperature.name in self.df.columns:
df = self.df[self.df[Points.outdoortemperature.name].notna()]
return df.index[-1], df[Points.outdoortemperature.name].iloc[-1]
return None, None
40 changes: 24 additions & 16 deletions aems-edge/Manager/manager/main_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -678,6 +678,15 @@ def sync_occupancy_state(self):
:rtype:
"""
is_occupied = self.is_system_occupied()
if isinstance(is_occupied, str):
_log.debug(f'{self.identity} - Error getting occupancy state: {is_occupied}')
_log.debug(f'{self.identity} - cannot sync to occupancy state')
return

if is_occupied is None:
_log.debug(f'{self.identity} - Unknown occupancy state!')
_log.debug(f'{self.identity} - attempting to sync to correct occupancy state')

_log.debug(f'{self.identity} - Syncing occupancy state override is {self.occupancy_override.current_id}')
if self.occupancy_override.current_id is not None:
if not is_occupied:
Expand All @@ -699,19 +708,19 @@ def sync_occupancy_state(self):
current_time = dt.time(dt.now()).replace(microsecond=0, second=0)
is_holiday = self.holiday_manager.is_holiday(dt.now())
_log.debug(f'{self.identity} - current day is holiday: {is_holiday}')
if is_holiday:
if is_holiday and (is_occupied or is_occupied is None):
self.change_occupancy(OccupancyTypes.UNOCCUPIED)
return
if current_schedule.is_always_off() and is_occupied:
if current_schedule.is_always_off() and (is_occupied or is_occupied is None):
_log.debug('Unit is in occupied mode but should be unoccupied! -- always_off')
self.change_occupancy(OccupancyTypes.UNOCCUPIED)
return
if current_schedule.is_always_on() and not is_occupied:
if current_schedule.is_always_on() and (not is_occupied or is_occupied is None):
_log.debug('Unit is in unoccupied mode but should be occupied! -- always_on')
self.change_occupancy(OccupancyTypes.OCCUPIED)
return

# TODO We need to deal with temporary occupancy becasuse this will undo it.
# TODO We need to deal with temporary occupancy because this will undo it.
if current_schedule.is_always_on() or current_schedule.is_always_off():
_log.debug('Current schedule is always off or always on')
return
Expand All @@ -721,17 +730,17 @@ def sync_occupancy_state(self):
_earliest = current_schedule.earliest_start

# Change current control to match occupancy schedule
if not is_occupied and _start < current_time < _end:
if _start < current_time < _end and (not is_occupied or is_occupied is None):
_log.debug('Unit is in unoccupied mode but should be occupied! -- _start < current_time < _end')
self.change_occupancy(OccupancyTypes.OCCUPIED)
e_hour = _end.hour
e_minute = _end.minute
unoccupied_time = dt.now().replace(hour=e_hour, minute=e_minute)
self.end_obj = self.core.schedule(unoccupied_time, self.change_occupancy, OccupancyTypes.UNOCCUPIED)
if is_occupied and current_time >= _end:
if current_time >= _end and (is_occupied or is_occupied is None):
_log.debug('Unit is in occupied mode but should be unoccupied! -- current_time >= _end')
self.change_occupancy(OccupancyTypes.UNOCCUPIED)
if is_occupied and current_time < _earliest:
if current_time < _earliest and (is_occupied or is_occupied is None):
_log.debug('Unit is in occupied mode but should be unoccupied! -- current_time < _earliest')
self.change_occupancy(OccupancyTypes.UNOCCUPIED)

Expand Down Expand Up @@ -788,8 +797,8 @@ def update_custom_data(self, peer, sender, bus, topic, header, message):
_log.debug(f'Update data : {topic}')
payload = {}
data, meta = message
if Points.outdoorairtemperature.value in data:
payload[Points.outdoorairtemperature.value] = data[Points.outdoorairtemperature.value]
if Points.outdoortemperature.value in data:
payload[Points.outdoortemperature.value] = data[Points.outdoortemperature.value]
self.data_handler.update_data(payload, header)

def is_system_occupied(self):
Expand All @@ -798,19 +807,18 @@ def is_system_occupied(self):
:return:
:rtype:
"""
result = None
try:
result = self.rpc_get_point(Points.occupancy.value)

occ_value = self.cfg.occupancy_values[self.cfg.occupancy_current]
if result == occ_value:
occupied_value = self.cfg.occupancy_values[OccupancyTypes.OCCUPIED.value]
unoccupied_value = self.cfg.occupancy_values[OccupancyTypes.UNOCCUPIED.value]
if result == occupied_value:
return True
else:
if result == unoccupied_value:
return False
except (RemoteError, gevent.Timeout) as ex:
_log.warning('Failed to get {}: {}'.format(self.cfg.system_rpc_path, str(ex)))

return result
return str(ex)
return None

def do_zone_control(self, point, value):
"""
Expand Down

0 comments on commit a63ca6b

Please sign in to comment.