Skip to content

Commit

Permalink
Optimise template render routines
Browse files Browse the repository at this point in the history
  • Loading branch information
rgc99 committed Sep 21, 2024
1 parent 8af301f commit 191c022
Showing 1 changed file with 6 additions and 12 deletions.
18 changes: 6 additions & 12 deletions custom_components/irrigation_unlimited/irrigation_unlimited.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import json
from decimal import Decimal
from enum import Enum, Flag, auto
import voluptuous as vol
from crontab import CronTab
from homeassistant.core import (
HomeAssistant,
Expand Down Expand Up @@ -328,19 +327,14 @@ def utc_eot() -> datetime:
def render_positive_time_period(data: dict, key: str) -> None:
"""Resolve a template that specifies a timedelta"""
if key in data:
schema = vol.Schema({key: cv.positive_time_period})
rendered = render_complex(data[key])
data[key] = schema({key: rendered})[key]
data[key] = cv.positive_time_period(render_complex(data[key]))


def render_positive_float(hass: HomeAssistant, data: dict, key: str) -> None:
def render_positive_float(data: dict, key: str) -> None:
"""Resolve a template that specifies a float"""
if isinstance(data.get(key), Template):
template: Template = data[key]
template.hass = hass
schema = vol.Schema({key: cv.positive_float})
data[key] = schema({key: template.async_render()})[key]

template: Template = data.get(key)
if isinstance(template, Template):
data[key] = cv.positive_float(template.async_render())

def check_item(index: int, items: list[int] | None) -> bool:
"""If items is None or contains only a 0 (match all) then
Expand Down Expand Up @@ -6645,7 +6639,7 @@ def service_call(
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)
render_positive_float(data1, CONF_PERCENTAGE)
if sequence is not None:
changed = sequence.service_adjust_time(data1, stime)
elif zone is not None:
Expand Down

0 comments on commit 191c022

Please sign in to comment.