|
111 | 111 |
|
112 | 112 | _LOGGER = logging.getLogger(__name__)
|
113 | 113 |
|
| 114 | +# Configure delay before starting to monitor state change events |
| 115 | +STARTUP_DELAY = 70 |
| 116 | + |
114 | 117 | devices = []
|
115 | 118 | MODE_SCHEMA = vol.Schema(
|
116 | 119 | {
|
@@ -485,6 +488,8 @@ class Model:
|
485 | 488 | """ Represents the transitions state machine model """
|
486 | 489 |
|
487 | 490 | def __init__(self, hass, config, machine, entity):
|
| 491 | + self.ec_startup_time = datetime.now() |
| 492 | + |
488 | 493 | self.hass = hass # backwards reference to hass object
|
489 | 494 | self.entity = entity # backwards reference to entity containing this model
|
490 | 495 |
|
@@ -581,6 +586,11 @@ def sensor_state_change(self, entity, old, new):
|
581 | 586 | self.log.debug("sensor_state_change :: old NoneType")
|
582 | 587 | pass
|
583 | 588 |
|
| 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 | + |
584 | 594 | if self.matches(new.state, self.SENSOR_ON_STATE) and (
|
585 | 595 | self.is_idle() or self.is_active_timer() or self.is_blocked()
|
586 | 596 | ):
|
@@ -644,6 +654,11 @@ def state_entity_state_change(self, entity, old, new):
|
644 | 654 | self.log.debug("state_entity_state_change :: Ignoring this state change because it came from %s" % (new.context.id))
|
645 | 655 | return
|
646 | 656 |
|
| 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 | + |
647 | 662 | # 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).
|
648 | 663 | try:
|
649 | 664 | if not old or not new or old == 'off' or new == 'off':
|
@@ -1137,6 +1152,17 @@ def config_override_entities(self, config):
|
1137 | 1152 | event.async_track_state_change(
|
1138 | 1153 | self.hass, self.overrideEntities, self.override_state_change
|
1139 | 1154 | )
|
| 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())) |
1140 | 1166 |
|
1141 | 1167 | def config_other(self, config):
|
1142 | 1168 | self.do_draw = config.get("draw", False)
|
|
0 commit comments