remove indentation level in _maybe_cancel

This commit is contained in:
Bas Nijholt 2020-09-27 20:54:38 +02:00
commit 7575d4d09d

View file

@ -572,57 +572,56 @@ class AdaptiveSwitch(SwitchEntity, RestoreEntity):
delay = TURNING_OFF_DELAY
delta_time = now_ts - ts_on_to_off
if delta_time < delay:
delay -= delta_time # already been delta_time since the event
brightness_going_down = True # this might not be the case
_LOGGER.debug(
"%s: Waiting with adjusting '%s' for %s.", self._name, entity_id, delay
if delta_time > delay:
return False
delay -= delta_time # delta_time has passed since the 'off' → 'on' event
_LOGGER.debug(
"%s: Waiting with adjusting '%s' for %s.", self._name, entity_id, delay
)
current_state = self.hass.states.get(entity_id)
_LOGGER.debug(
"%s: '%s' state before sleep is '%s'",
self._name,
entity_id,
current_state,
)
for _ in range(3):
# It can happen that the actual transition time is longer than the
# specified time in the 'turn_off' service, so we check whether the
# brightness is still going down, if so, we wait a little longer.
await asyncio.sleep(delay)
await self.hass.services.async_call(
HA_DOMAIN,
SERVICE_UPDATE_ENTITY,
{ATTR_ENTITY_ID: entity_id},
blocking=True,
)
old_state = current_state
current_state = self.hass.states.get(entity_id)
if current_state.state == "off":
return True
old_brightness = old_state.attributes.get(ATTR_BRIGHTNESS, 0)
current_brightness = current_state.attributes.get(ATTR_BRIGHTNESS, 0)
brightness_going_down = old_brightness > current_brightness
_LOGGER.debug(
"%s: '%s' state before sleep is '%s'",
"%s: '%s' state after sleep is '%s'",
self._name,
entity_id,
current_state,
)
for _ in range(3):
# It can happen that the actual transition time is longer than
# the specified time in the 'turn_off' service, so we check
# whether the brightness is still going down, if so, we wait a
# little longer.
await asyncio.sleep(delay)
await self.hass.services.async_call(
HA_DOMAIN,
SERVICE_UPDATE_ENTITY,
{ATTR_ENTITY_ID: entity_id},
blocking=True,
)
old_state = current_state
current_state = self.hass.states.get(entity_id)
old_brightness = old_state.attributes.get(ATTR_BRIGHTNESS, 0)
current_brightness = current_state.attributes.get(ATTR_BRIGHTNESS, 0)
brightness_going_down = old_brightness > current_brightness
_LOGGER.debug(
"%s: '%s' state after sleep is '%s'",
self._name,
entity_id,
current_state,
)
if not brightness_going_down:
break
delay = TURNING_OFF_DELAY
if not brightness_going_down:
break
delay = TURNING_OFF_DELAY # next time only wait this long
if transition is not None:
# Always ignore when there's a transition
# TODO: I am doing this because it seems like HA cannot detect
# whether a light is transitioning into 'off'. Because in my
# tests `brightness_going_down == False` even when it is actually
# still going down... Needs some discussion.
return True
if transition is not None:
# Always ignore when there's a transition and light is still on.
# TODO: I am doing this because it seems like HA cannot detect
# whether a light is transitioning into 'off'. Because in my
# tests `brightness_going_down == False` even when it is actually
# still going down... Needs some discussion.
return True
if not is_on(self.hass, entity_id):
return True
return False
return current_state.state == "off"
async def _turn_off_event_listener(self, event):
"""Track 'light.turn_off(..., transition=...)' service calls."""