Skip to content

Commit

Permalink
fix(endless loop):
Browse files Browse the repository at this point in the history
Edge case caused an endless loop
  • Loading branch information
Xavi committed Nov 17, 2019
1 parent 90f0fe2 commit b110399
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions apps/z2m_ikea_controller/z2m_ikea_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def state(self, entity, attribute, old, new, kwargs):
self.turn_on_light(attribute, value, sign, self.manual_steps)
elif action == "hold":
self.on_hold = True
while self.on_hold and min_ <= value <= max_:
while self.on_hold and value is not None:
value = self.turn_on_light(
attribute, value, sign, self.automatic_steps
)
Expand All @@ -69,14 +69,20 @@ def state(self, entity, attribute, old, new, kwargs):
time.sleep(self.delay / 1000)

def turn_on_light(self, attribute, old, sign, steps):
"""
It returns the new value if it didn't reached min or max. Otherwise None.
"""
max_ = attribute_minmax[attribute]["max"]
min_ = attribute_minmax[attribute]["min"]
step = (max_ - min_) // steps
new_state_attribute = old + sign * step
new_state_attribute = max(min_, min(new_state_attribute, max_))
self.turn_on(self.light, **{attribute: new_state_attribute})
return new_state_attribute

if min_ <= new_state_attribute <= max_:
self.turn_on(self.light, **{attribute: new_state_attribute})
return new_state_attribute
else:
new_state_attribute = max(min_, min(new_state_attribute, max_))
self.turn_on(self.light, **{attribute: new_state_attribute})
return None

class E1810Controller(IkeaController):
# Different states reported from the controller:
Expand Down

0 comments on commit b110399

Please sign in to comment.