Skip to content

Commit

Permalink
Cleaning up state method
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
valleedelisle committed Dec 7, 2023
1 parent 04406bc commit 27dd9d4
Showing 1 changed file with 9 additions and 21 deletions.
30 changes: 9 additions & 21 deletions custom_components/hilo/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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"
Expand Down

0 comments on commit 27dd9d4

Please sign in to comment.