Skip to content

Commit

Permalink
improve handling of callback handles
Browse files Browse the repository at this point in the history
  • Loading branch information
benleb committed Apr 4, 2021
1 parent 401cc3a commit b9565d7
Showing 1 changed file with 34 additions and 24 deletions.
58 changes: 34 additions & 24 deletions apps/automoli/automoli.py
Original file line number Diff line number Diff line change
Expand Up @@ -373,9 +373,6 @@ async def initialize(self) -> None:
)
)

# callback handles to switch lights off
self.handles: Set[str] = set()

self.args.update(
{
"room": self.room_name.capitalize(),
Expand All @@ -384,14 +381,16 @@ async def initialize(self) -> None:
"daytimes": daytimes,
"lights": self.lights,
"dim": self.dim,
"threshold": self.thresholds,
"sensors": self.sensors,
"disable_hue_groups": self.disable_hue_groups,
"only_own_events": self.only_own_events,
"loglevel": self.loglevel,
}
)

if self.thresholds:
self.args.update({"thresholds": self.thresholds})

# add night mode to config if enabled
if self.night_mode:
self.args.update({"night_mode": self.night_mode})
Expand Down Expand Up @@ -471,7 +470,7 @@ async def motion_cleared(
await self.refresh_timer()
else:
# cancel scheduled callbacks
await self.clear_handles(deepcopy(self.handles))
await self.clear_handles()

async def motion_detected(
self, entity: str, attribute: str, old: str, new: str, kwargs: Dict[str, Any]
Expand All @@ -489,7 +488,7 @@ async def motion_detected(
)

# cancel scheduled callbacks
await self.clear_handles(deepcopy(self.handles))
await self.clear_handles()

self.lg(
f"{stack()[0][3]}() handles cleared and cancelled all scheduled timers"
Expand Down Expand Up @@ -536,11 +535,16 @@ def has_min_ad_version(self, required_version: str) -> bool:
required_version = required_version if required_version else "4.0.7"
return bool(StrictVersion(self.get_ad_version()) >= StrictVersion(required_version))

async def clear_handles(self, handles: Set[str] = None) -> None:
"""clear scheduled timers/callbacks."""
self.handles.clear()

if not handles:
handles = deepcopy(self.room.handles_automoli)

self.room.handles_automoli.clear()

self.lg(
f"{stack()[0][3]}() {self.handles = } cleared, canceling {handles = }",
level=logging.DEBUG,
f"{stack()[0][3]}() {self.room.handles_automoli = } | {handles = }", level=logging.DEBUG
)

if self.has_min_ad_version("4.0.7"):
Expand All @@ -560,28 +564,34 @@ async def refresh_timer(self) -> None:
self.dimming = False

# cancel scheduled callbacks
await self.clear_handles(deepcopy(self.handles))
self.lg(f"{stack()[0][3]}() handles cleared → {self.handles = }", level=logging.DEBUG)
await self.clear_handles()

# if no delay is set or delay = 0, lights will not switched off by AutoMoLi
if delay := self.active.get("delay"):

self.lg(f"{stack()[0][3]}() {self.active = }", level=logging.DEBUG)
self.lg(
f"{stack()[0][3]}() {self.active = } | {self.dim = } | {delay = }",
level=logging.DEBUG,
)

if self.dim:
dim_in_sec = int(delay) - self.dim["seconds_before"] + 2
self.lg(
f"{stack()[0][3]}() {self.dim = }, dimming in {dim_in_sec}", level=logging.DEBUG
)
dim_in_sec = int(delay) - self.dim["seconds_before"]
self.lg(f"{stack()[0][3]}() {self.dim = } | {dim_in_sec}", level=logging.DEBUG)

dim_handle = await self.run_in(self.dim_lights, (dim_in_sec))
self.handles.add(dim_handle)
self.lg(f"{stack()[0][3]}() {dim_handle = } -> {self.handles}", level=logging.DEBUG)
handle = await self.run_in(self.dim_lights, (dim_in_sec))

# schedule "turn off" callback
off_handle = await self.run_in(self.lights_off, delay)
self.handles.add(off_handle)
self.lg(f"{stack()[0][3]}() {off_handle = } -> {self.handles}", level=logging.DEBUG)
else:
handle = await self.run_in(self.lights_off, delay)

self.room.handles_automoli.add(handle)

self.lg(
f"{stack()[0][3]}() {handle = } -> {self.room.handles_automoli}",
level=logging.DEBUG,
)

async def night_mode_active(self) -> bool:
return bool(self.night_mode and await self.get_state(self.night_mode["entity"]) == "on")

async def is_disabled(self) -> bool:
"""check if automoli is disabled via home assistant entity"""
Expand Down Expand Up @@ -619,7 +629,7 @@ async def is_blocked(self) -> bool:
)

if current_humidity >= humidity_threshold:
# blocker.append(sensor)

await self.refresh_timer()
self.lg(
f"🛁 no motion in {hl(self.room.capitalize())} since "
Expand Down

0 comments on commit b9565d7

Please sign in to comment.