From 27dd9d4d3a6b8f394578bc8b18c67b1f0801106e Mon Sep 17 00:00:00 2001 From: David Vallee Delisle Date: Sat, 2 Dec 2023 12:47:21 -0500 Subject: [PATCH] Cleaning up state method It's easier to read when we predefine some tmp variables first. In python, unless we deepcopy an object, when we create a variable like this it will just "link" to it, so it's a nocost optimization for human readability. --- custom_components/hilo/sensor.py | 30 +++++++++--------------------- 1 file changed, 9 insertions(+), 21 deletions(-) diff --git a/custom_components/hilo/sensor.py b/custom_components/hilo/sensor.py index 8019edc..90ccb14 100755 --- a/custom_components/hilo/sensor.py +++ b/custom_components/hilo/sensor.py @@ -36,7 +36,6 @@ CONF_ENERGY_METER_PERIOD, CONF_GENERATE_ENERGY_METERS, CONF_HQ_PLAN_NAME, - CONF_PRE_COLD_PHASE, CONF_TARIFF, CONF_UNTARIFICATED_DEVICES, DEFAULT_ENERGY_METER_PERIOD, @@ -653,30 +652,19 @@ def __init__(self, hilo, device, scan_interval): @property def state(self): if len(self._next_events) > 0: - if datetime.now(timezone.utc) > self._next_events[0].phases.recovery_end: - if len(self._next_events) > 1: - # another challenge is scheduled after this one - return "scheduled" - else: - return "off" - elif ( - datetime.now(timezone.utc) > self._next_events[0].phases.recovery_start - ): + phases = self._next_events[0].phases + now = datetime.now(timezone.utc) + if now > phases.recovery_end: + return "scheduled" + elif now > phases.recovery_start: return "recovery" - elif ( - datetime.now(timezone.utc) > self._next_events[0].phases.reduction_start - ): + elif now > phases.reduction_start: return "reduction" - elif datetime.now(timezone.utc) > self._next_events[0].phases.preheat_start: + elif now > phases.preheat_start: return "pre_heat" - elif ( - datetime.now(timezone.utc) - > self._next_events[0].phases.appreciation_start - ): + elif now > phases.appreciation_start: return "appreciation" - elif datetime.now(timezone.utc) > self._next_events[ - 0 - ].phases.appreciation_start - timedelta(hours=CONF_PRE_COLD_PHASE): + elif now > phases.pre_cold_start: return "pre_cold" else: return "scheduled"