From 57d008b551d5ec0d3609309bf904e6bf5ef18927 Mon Sep 17 00:00:00 2001 From: rgc99 Date: Fri, 24 Nov 2023 02:17:30 +0000 Subject: [PATCH] Fix issue #152 --- .../irrigation_unlimited.py | 29 ++++++++++--------- tests/configs/test_template.yaml | 24 +++++++++++++++ tests/test_template.py | 17 +++++++++++ 3 files changed, 56 insertions(+), 14 deletions(-) diff --git a/custom_components/irrigation_unlimited/irrigation_unlimited.py b/custom_components/irrigation_unlimited/irrigation_unlimited.py index 070c035..fe820a6 100644 --- a/custom_components/irrigation_unlimited/irrigation_unlimited.py +++ b/custom_components/irrigation_unlimited/irrigation_unlimited.py @@ -20,7 +20,7 @@ Event as HAEvent, ) from homeassistant.helpers.entity import Entity -from homeassistant.helpers.template import Template +from homeassistant.helpers.template import Template, render_complex from homeassistant.helpers.event import ( async_track_point_in_utc_time, async_call_later, @@ -306,13 +306,12 @@ def utc_eot() -> datetime: return datetime.max.replace(tzinfo=timezone.utc) -def render_positive_time_period(hass: HomeAssistant, data: dict, key: str) -> None: +def render_positive_time_period(data: dict, key: str) -> None: """Resolve a template that specifies a timedelta""" - if isinstance(data.get(key), Template): - template: Template = data[key] - template.hass = hass + if key in data: schema = vol.Schema({key: cv.positive_time_period}) - data[key] = schema({key: template.async_render()})[key] + rendered = render_complex(data[key]) + data[key] = schema({key: rendered})[key] def render_positive_float(hass: HomeAssistant, data: dict, key: str) -> None: @@ -5766,6 +5765,8 @@ def service_call( changed = True stime = self.service_time() + self._logger.log_service_call(service, stime, controller, zone, data, DEBUG) + data1 = dict(data) if service in [SERVICE_ENABLE, SERVICE_DISABLE, SERVICE_TOGGLE]: @@ -5775,7 +5776,7 @@ def service_call( else: changed = controller.service_call(data1, stime, service) elif service == SERVICE_SUSPEND: - render_positive_time_period(self._hass, data1, CONF_FOR) + render_positive_time_period(data1, CONF_FOR) if zone is not None: if changed := zone.service_suspend(data1, stime): controller.clear_zone_runs(zone) @@ -5788,11 +5789,11 @@ def service_call( else: controller.service_cancel(data1, stime) elif service == SERVICE_TIME_ADJUST: - render_positive_time_period(self._hass, data1, CONF_ACTUAL) - render_positive_time_period(self._hass, data1, CONF_INCREASE) - render_positive_time_period(self._hass, data1, CONF_DECREASE) - render_positive_time_period(self._hass, data1, CONF_MINIMUM) - render_positive_time_period(self._hass, data1, CONF_MAXIMUM) + render_positive_time_period(data1, CONF_ACTUAL) + render_positive_time_period(data1, CONF_INCREASE) + render_positive_time_period(data1, CONF_DECREASE) + render_positive_time_period(data1, CONF_MINIMUM) + render_positive_time_period(data1, CONF_MAXIMUM) render_positive_float(self._hass, data1, CONF_PERCENTAGE) if zone is not None: if changed := zone.service_adjust_time(data1, stime): @@ -5800,13 +5801,13 @@ def service_call( else: changed = controller.service_adjust_time(data1, stime) elif service == SERVICE_MANUAL_RUN: - render_positive_time_period(self._hass, data1, CONF_TIME) + render_positive_time_period(data1, CONF_TIME) if zone is not None: zone.service_manual_run(data1, stime) else: controller.service_manual_run(data1, stime) elif service == SERVICE_LOAD_SCHEDULE: - render_positive_time_period(self._hass, data1, CONF_DURATION) + render_positive_time_period(data1, CONF_DURATION) self.service_load_schedule(data1) else: return diff --git a/tests/configs/test_template.yaml b/tests/configs/test_template.yaml index 465a9b1..884e7da 100644 --- a/tests/configs/test_template.yaml +++ b/tests/configs/test_template.yaml @@ -133,3 +133,27 @@ irrigation_unlimited: - {t: '2021-01-04 15:40:00', c: 2, z: 4, s: 1} - {t: '2021-01-04 15:49:00', c: 2, z: 4, s: 0} - {t: '2021-01-04 15:49:00', c: 2, z: 0, s: 0} + - name: '7-Manual run sequence complex' + start: '2021-01-04 06:00' + end: '2021-01-04 07:00' + results: + - {t: '2021-01-04 06:00:30', c: 2, z: 0, s: 1} + - {t: '2021-01-04 06:00:30', c: 2, z: 1, s: 1} + - {t: '2021-01-04 06:05:00', c: 2, z: 1, s: 0} + - {t: '2021-01-04 06:05:00', c: 2, z: 0, s: 0} + - {t: '2021-01-04 06:06:00', c: 2, z: 0, s: 1} + - {t: '2021-01-04 06:06:00', c: 2, z: 2, s: 1} + - {t: '2021-01-04 06:10:00', c: 1, z: 0, s: 1} + - {t: '2021-01-04 06:10:00', c: 1, z: 2, s: 1} + - {t: '2021-01-04 06:10:30', c: 2, z: 2, s: 0} + - {t: '2021-01-04 06:10:30', c: 2, z: 0, s: 0} + - {t: '2021-01-04 06:11:30', c: 2, z: 0, s: 1} + - {t: '2021-01-04 06:11:30', c: 2, z: 3, s: 1} + - {t: '2021-01-04 06:16:00', c: 2, z: 3, s: 0} + - {t: '2021-01-04 06:16:00', c: 2, z: 0, s: 0} + - {t: '2021-01-04 06:17:00', c: 2, z: 0, s: 1} + - {t: '2021-01-04 06:17:00', c: 2, z: 4, s: 1} + - {t: '2021-01-04 06:20:00', c: 1, z: 2, s: 0} + - {t: '2021-01-04 06:20:00', c: 1, z: 0, s: 0} + - {t: '2021-01-04 06:21:30', c: 2, z: 4, s: 0} + - {t: '2021-01-04 06:21:30', c: 2, z: 0, s: 0} diff --git a/tests/test_template.py b/tests/test_template.py index 28f7f10..4c4b2d3 100644 --- a/tests/test_template.py +++ b/tests/test_template.py @@ -111,4 +111,21 @@ async def set_dummy_value(value: str): ) await exam.finish_test() + await set_dummy_value("21") + await exam.begin_test(7) + await exam.run_until("2021-01-04 06:00:29") + await exam.call( + SERVICE_MANUAL_RUN, + { + "entity_id": "binary_sensor.irrigation_unlimited_c2_m", + "time": { + "hours": 0, + "minutes": "{{ states('input_text.dummy_text_1') }}", + "seconds": 0, + }, + "sequence_id": 1, + }, + ) + await exam.finish_test() + exam.check_summary()