Skip to content

Commit

Permalink
Fix issue #152
Browse files Browse the repository at this point in the history
  • Loading branch information
rgc99 committed Nov 24, 2023
1 parent 95c2a4e commit 57d008b
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 14 deletions.
29 changes: 15 additions & 14 deletions custom_components/irrigation_unlimited/irrigation_unlimited.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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]:
Expand All @@ -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)
Expand All @@ -5788,25 +5789,25 @@ 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):
controller.clear_zone_runs(zone)
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
Expand Down
24 changes: 24 additions & 0 deletions tests/configs/test_template.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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}
17 changes: 17 additions & 0 deletions tests/test_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

0 comments on commit 57d008b

Please sign in to comment.