Skip to content

Commit

Permalink
Add entity_id to check_back
Browse files Browse the repository at this point in the history
  • Loading branch information
rgc99 committed Oct 23, 2023
1 parent 516c0e6 commit 6d2d4b1
Show file tree
Hide file tree
Showing 4 changed files with 250 additions and 68 deletions.
50 changes: 32 additions & 18 deletions custom_components/irrigation_unlimited/irrigation_unlimited.py
Original file line number Diff line number Diff line change
Expand Up @@ -1352,6 +1352,7 @@ def __init__(
self._check_back_resync: bool = True
self._state_on = STATE_ON
self._state_off = STATE_OFF
self._check_back_entity_id: str = None
# private variables
self._state: bool = None # This parameter should mirror IUZone._is_on
self._check_back_time: timedelta = None
Expand Down Expand Up @@ -1413,6 +1414,9 @@ def load_params(config: OrderedDict) -> None:
self._state_off = config.get(CONF_STATE_OFF, self._state_off)
delay = config.get(CONF_DELAY, self._check_back_delay.total_seconds())
self._check_back_delay = wash_td(timedelta(seconds=delay))
self._check_back_entity_id = config.get(
CONF_ENTITY_ID, self._check_back_entity_id
)

self.clear()
self._switch_entity_id = config.get(CONF_ENTITY_ID)
Expand All @@ -1429,26 +1433,36 @@ def muster(self, stime: datetime) -> int:
def check_switch(self, stime: datetime, resync: bool, log: bool) -> list[str]:
"""Check the linked entity is in sync. Returns a list of entities
that are not in sync"""

result: list[str] = []

def _check_entity(entity_id: str, expected: str) -> bool:
is_valid = self._hass.states.is_state(entity_id, expected)
if not is_valid:
result.append(entity_id)
if log:
self._coordinator.logger.log_sync_error(stime, expected, entity_id)
self._coordinator.notify_switch(
EVENT_SYNC_ERROR,
expected,
[entity_id],
self._controller,
self._zone,
)

return is_valid

if self._switch_entity_id is not None:
for entity_id in self._switch_entity_id:
expected = self._state_on if self._state else self._state_off
is_valid = self._hass.states.is_state(entity_id, expected)
if not is_valid:
result.append(entity_id)
if log:
self._coordinator.logger.log_sync_error(
stime, expected, entity_id
)
self._coordinator.notify_switch(
EVENT_SYNC_ERROR,
expected,
[entity_id],
self._controller,
self._zone,
)
if resync:
self._set_switch(entity_id, self._state)
expected = self._state_on if self._state else self._state_off
if self._check_back_entity_id is None:
for entity_id in self._switch_entity_id:
if not _check_entity(entity_id, expected):
if resync:
self._set_switch(entity_id, self._state)
else:
if not _check_entity(self._check_back_entity_id, expected):
if resync and len(self._switch_entity_id) == 1:
self._set_switch(self._switch_entity_id, self._state)
return result

def call_switch(self, state: bool, stime: datetime = None) -> None:
Expand Down
1 change: 1 addition & 0 deletions custom_components/irrigation_unlimited/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ def _parse_dd_mmm(value: str) -> date | None:
vol.Optional(CONF_RESYNC): cv.boolean,
vol.Optional(CONF_STATE_ON): cv.string,
vol.Optional(CONF_STATE_OFF): cv.string,
vol.Optional(CONF_ENTITY_ID): cv.entity_id,
}
)

Expand Down
98 changes: 83 additions & 15 deletions tests/configs/test_check_back.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,35 @@ default_config:

# Dummy switches
input_boolean:
dummy_switch_c1_m:
dummy_switch_m:
name: Dummy Master Switch
initial: false

dummy_switch_c1_z1:
dummy_switch_z1:
name: Dummy Zone Switch 1
initial: false

dummy_switch_c1_z2:
dummy_switch_z2:
name: Dummy Zone Switch 2
initial: false

dummy_switch_c1_z3:
dummy_switch_z3:
name: Dummy Zone Switch 3
initial: false

dummy_switch_c1_z4:
dummy_switch_z4:
name: Dummy Zone Switch 4
initial: false

dummy_check_back_switch:
name: Dummy Check Back Switch
initial: false

irrigation_unlimited:
refresh_interval: 2000
controllers:
- name: "Test controller 1"
entity_id: input_boolean.dummy_switch_c1_m
entity_id: input_boolean.dummy_switch_m
all_zones_config:
check_back:
states: all
Expand All @@ -37,9 +41,9 @@ irrigation_unlimited:
state_off: "off"
zones:
- name: "Zone 1"
entity_id: input_boolean.dummy_switch_c1_z1
entity_id: input_boolean.dummy_switch_z1
- name: "Zone 2"
entity_id: input_boolean.dummy_switch_c1_z2,input_boolean.dummy_switch_c1_z3,input_boolean.dummy_switch_c1_z4
entity_id: input_boolean.dummy_switch_z2,input_boolean.dummy_switch_z3,input_boolean.dummy_switch_z4
check_back:
states: all
delay: 30
Expand All @@ -58,12 +62,12 @@ irrigation_unlimited:
- zone_id: 2
duration: "0:12:00"
- name: "Test controller 2"
entity_id: input_boolean.dummy_switch_c1_m
entity_id: input_boolean.dummy_switch_m
zones:
- name: "Zone 1"
entity_id: input_boolean.dummy_switch_c1_z1
entity_id: input_boolean.dummy_switch_z1
- name: "Zone 2"
entity_id: input_boolean.dummy_switch_c1_z2,input_boolean.dummy_switch_c1_z3,input_boolean.dummy_switch_c1_z4
entity_id: input_boolean.dummy_switch_z2,input_boolean.dummy_switch_z3,input_boolean.dummy_switch_z4
sequences:
- name: "Sequence 1"
delay: "0:01:00"
Expand All @@ -74,6 +78,34 @@ irrigation_unlimited:
duration: "0:00:15"
- zone_id: 2
duration: "0:00:15"
- name: "Test controller 3"
entity_id: input_boolean.dummy_switch_m
check_back:
states: none
all_zones_config:
check_back:
entity_id: input_boolean.dummy_check_back_switch
states: all
delay: 15
retries: 5
resync: true
state_on: "on"
state_off: "off"
zones:
- name: "Zone 1"
entity_id: input_boolean.dummy_switch_z1
- name: "Zone 2"
entity_id: input_boolean.dummy_switch_z2,input_boolean.dummy_switch_z3,input_boolean.dummy_switch_z4
sequences:
- name: "Sequence 1"
delay: "0:01:00"
schedules:
- time: "20:05"
zones:
- zone_id: 1
duration: "0:06:00"
- zone_id: 2
duration: "0:12:00"
testing:
enabled: true
speed: 600.0
Expand All @@ -93,7 +125,19 @@ irrigation_unlimited:
- {t: '2021-01-04 07:12:00', c: 1, z: 2, s: 1}
- {t: '2021-01-04 07:24:00', c: 1, z: 2, s: 0}
- {t: '2021-01-04 07:24:00', c: 1, z: 0, s: 0}
- name: "2-Faux partial communications error on zone 2"
- name: "2-Check entity is corrected"
start: "2021-01-04 07:00"
end: "2021-01-04 08:00"
results:
- {t: '2021-01-04 07:05:00', c: 1, z: 0, s: 1}
- {t: '2021-01-04 07:05:00', c: 1, z: 1, s: 1}
- {t: '2021-01-04 07:11:00', c: 1, z: 1, s: 0}
- {t: '2021-01-04 07:11:00', c: 1, z: 0, s: 0}
- {t: '2021-01-04 07:12:00', c: 1, z: 0, s: 1}
- {t: '2021-01-04 07:12:00', c: 1, z: 2, s: 1}
- {t: '2021-01-04 07:24:00', c: 1, z: 2, s: 0}
- {t: '2021-01-04 07:24:00', c: 1, z: 0, s: 0}
- name: "3-Faux partial communications error on zone 2"
start: "2021-01-04 07:00"
end: "2021-01-04 08:00"
results:
Expand All @@ -105,7 +149,7 @@ irrigation_unlimited:
- {t: '2021-01-04 07:12:00', c: 1, z: 2, s: 1}
- {t: '2021-01-04 07:24:00', c: 1, z: 2, s: 0}
- {t: '2021-01-04 07:24:00', c: 1, z: 0, s: 0}
- name: "3-Faux full communications error on zone 2"
- name: "4-Faux full communications error on zone 2"
start: "2021-01-04 07:00"
end: "2021-01-04 08:00"
results:
Expand All @@ -117,7 +161,7 @@ irrigation_unlimited:
- {t: '2021-01-04 07:12:00', c: 1, z: 2, s: 1}
- {t: '2021-01-04 07:24:00', c: 1, z: 2, s: 0}
- {t: '2021-01-04 07:24:00', c: 1, z: 0, s: 0}
- name: "4-Faux full communications error"
- name: "5-Faux full communications error"
start: "2021-01-04 07:00"
end: "2021-01-04 08:00"
results:
Expand All @@ -129,7 +173,7 @@ irrigation_unlimited:
- {t: '2021-01-04 07:12:00', c: 1, z: 2, s: 1}
- {t: '2021-01-04 07:24:00', c: 1, z: 2, s: 0}
- {t: '2021-01-04 07:24:00', c: 1, z: 0, s: 0}
- name: "5-State change before chack back"
- name: "6-State change before check back"
start: "2021-01-04 19:00"
end: "2021-01-04 20:00"
results:
Expand All @@ -141,3 +185,27 @@ irrigation_unlimited:
- {t: '2021-01-04 19:06:15', c: 2, z: 2, s: 1}
- {t: '2021-01-04 19:06:30', c: 2, z: 2, s: 0}
- {t: '2021-01-04 19:06:30', c: 2, z: 0, s: 0}
- name: "7-Check back from other entity, no errors"
start: "2021-01-04 20:00"
end: "2021-01-04 21:00"
results:
- {t: '2021-01-04 20:05:00', c: 3, z: 0, s: 1}
- {t: '2021-01-04 20:05:00', c: 3, z: 1, s: 1}
- {t: '2021-01-04 20:11:00', c: 3, z: 1, s: 0}
- {t: '2021-01-04 20:11:00', c: 3, z: 0, s: 0}
- {t: '2021-01-04 20:12:00', c: 3, z: 0, s: 1}
- {t: '2021-01-04 20:12:00', c: 3, z: 2, s: 1}
- {t: '2021-01-04 20:24:00', c: 3, z: 2, s: 0}
- {t: '2021-01-04 20:24:00', c: 3, z: 0, s: 0}
- name: "8-Check back from other entity - check restore"
start: "2021-01-04 20:00"
end: "2021-01-04 21:00"
results:
- {t: '2021-01-04 20:05:00', c: 3, z: 0, s: 1}
- {t: '2021-01-04 20:05:00', c: 3, z: 1, s: 1}
- {t: '2021-01-04 20:11:00', c: 3, z: 1, s: 0}
- {t: '2021-01-04 20:11:00', c: 3, z: 0, s: 0}
- {t: '2021-01-04 20:12:00', c: 3, z: 0, s: 1}
- {t: '2021-01-04 20:12:00', c: 3, z: 2, s: 1}
- {t: '2021-01-04 20:24:00', c: 3, z: 2, s: 0}
- {t: '2021-01-04 20:24:00', c: 3, z: 0, s: 0}
Loading

0 comments on commit 6d2d4b1

Please sign in to comment.