Skip to content

Commit

Permalink
Add retry if any lights are not ready (e.g., unavailable) to be turne…
Browse files Browse the repository at this point in the history
…d off
  • Loading branch information
mkotler committed Mar 11, 2024
1 parent d6f0906 commit 67dbf06
Showing 1 changed file with 25 additions and 7 deletions.
32 changes: 25 additions & 7 deletions apps/automoli/automoli.py
Original file line number Diff line number Diff line change
Expand Up @@ -1567,13 +1567,11 @@ def lights_off(self, kwargs: dict[str, Any]) -> None:
level=logging.DEBUG,
)

# if any([self.get_state(entity) == "on" for entity in self.lights]):
if all([self.get_state(entity, copy=False) == "off" for entity in self.lights]):
return

at_least_one_turned_off = False
at_least_one_turned_off = kwargs.get("one_turned_off_already", False)
at_least_one_error = False
for entity in self.lights:
if self.get_state(entity, copy=False) == "on":
state = self.get_state(entity, copy=False)
if state == "on":
if self.only_own_events:
if entity in self._switched_on_by_automoli:
self.call_service(
Expand All @@ -1593,7 +1591,15 @@ def lights_off(self, kwargs: dict[str, Any]) -> None:
if entity not in self._switched_off_by_automoli:
self._switched_off_by_automoli.add(entity)
at_least_one_turned_off = True
if at_least_one_turned_off:
elif state in NOT_READY_STATES:
at_least_one_error = True
self.lg(
f"{entity} is not ready to be turned off with state '{state}'",
icon=ALERT_ICON,
)

# only run if there were no errors
if at_least_one_turned_off and not at_least_one_error:
delay = kwargs.get("timeDelay", 0)
daytimeChange = kwargs.get("daytimeChange", False)
self.run_in(
Expand Down Expand Up @@ -1621,6 +1627,18 @@ def lights_off(self, kwargs: dict[str, Any]) -> None:
),
)

if at_least_one_error:
self.lg(
f"Since at least one light may not have been turned off, retrying in 60 seconds.",
icon=ALERT_ICON,
)
# Retry again in 60 seconds. Pass in current state if already turned one off so can finish
# actions after turning lights off, once there are no errors.
handle = self.run_in(
self.lights_off, 60, one_turned_off_already=at_least_one_turned_off
)
self.room.handles_automoli.add(handle)

# Global lock ensures that multiple log writes occur together after turning off lights
@ad.global_lock
def turned_off(self, kwargs: dict[str, Any] | None = None) -> None:
Expand Down

0 comments on commit 67dbf06

Please sign in to comment.