Skip to content

Commit 865882b

Browse files
committed
Ignore state change events for 70 seconds after starting. Check if controller needs to be overridden 65 seconds after starting (after entity states are ready)
1 parent d2fd0a7 commit 865882b

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

custom_components/entity_controller/__init__.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,9 @@
111111

112112
_LOGGER = logging.getLogger(__name__)
113113

114+
# Configure delay before starting to monitor state change events
115+
STARTUP_DELAY = 70
116+
114117
devices = []
115118
MODE_SCHEMA = vol.Schema(
116119
{
@@ -485,6 +488,8 @@ class Model:
485488
""" Represents the transitions state machine model """
486489

487490
def __init__(self, hass, config, machine, entity):
491+
self.ec_startup_time = datetime.now()
492+
488493
self.hass = hass # backwards reference to hass object
489494
self.entity = entity # backwards reference to entity containing this model
490495

@@ -581,6 +586,11 @@ def sensor_state_change(self, entity, old, new):
581586
self.log.debug("sensor_state_change :: old NoneType")
582587
pass
583588

589+
# Ignore state changes while entity states are being initialized (e.g. after HA restart)
590+
if (datetime.now() - self.ec_startup_time).total_seconds() < STARTUP_DELAY:
591+
self.log.debug("sensor_state_change :: Ignoring state change for %s seconds after starting" % STARTUP_DELAY)
592+
return
593+
584594
if self.matches(new.state, self.SENSOR_ON_STATE) and (
585595
self.is_idle() or self.is_active_timer() or self.is_blocked()
586596
):
@@ -644,6 +654,11 @@ def state_entity_state_change(self, entity, old, new):
644654
self.log.debug("state_entity_state_change :: Ignoring this state change because it came from %s" % (new.context.id))
645655
return
646656

657+
# Ignore state changes while entity states are being initialized (e.g. after HA restart)
658+
if (datetime.now() - self.ec_startup_time).total_seconds() < STARTUP_DELAY:
659+
self.log.debug("state_entity_state_change :: Ignoring state change for %s seconds after starting" % STARTUP_DELAY)
660+
return
661+
647662
# If the state changed, we definitely want to handle the transition. If only attributes changed, we'll check if the new attributes are significant (i.e., not being ignored).
648663
try:
649664
if not old or not new or old == 'off' or new == 'off':
@@ -1137,6 +1152,17 @@ def config_override_entities(self, config):
11371152
event.async_track_state_change(
11381153
self.hass, self.overrideEntities, self.override_state_change
11391154
)
1155+
# Check if controller needs to be overridden after delay (after entity states finished initializing)
1156+
check_override_time = datetime.now() + timedelta(seconds=(STARTUP_DELAY - 5))
1157+
self.log.debug("Scheduling check_override_callback for %s", check_override_time)
1158+
self.check_override_event_hook = event.async_track_point_in_time(
1159+
self.hass, self.check_override_callback, check_override_time
1160+
)
1161+
1162+
def check_override_callback(self, evt):
1163+
if self.is_override_state_on():
1164+
self.override()
1165+
self.update(overridden_at=str(datetime.now()))
11401166

11411167
def config_other(self, config):
11421168
self.do_draw = config.get("draw", False)

0 commit comments

Comments
 (0)