Skip to content

Commit 2b7b9b7

Browse files
committed
Default Delta 2 ac_ports to off when inverter heartbeat is missing
1 parent 860073a commit 2b7b9b7

1 file changed

Lines changed: 24 additions & 0 deletions

File tree

custom_components/ef_ble/eflib/devices/_delta2_base.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
from bleak.backends.device import BLEDevice
2+
from bleak.backends.scanner import AdvertisementData
3+
4+
from ..connection import ConnectionState
15
from ..devicebase import DeviceBase
26
from ..entity import controls
37
from ..entity.base import dynamic
@@ -86,6 +90,26 @@ class Delta2Base(DeviceBase, RawDataProps):
8690
dc12v_output_voltage = raw_field(pb_mppt.car_out_vol, pdiv(1000, 2))
8791
dc12v_output_current = raw_field(pb_mppt.car_out_amp, pdiv(1000, 2))
8892

93+
def __init__(
94+
self, ble_dev: BLEDevice, adv_data: AdvertisementData, sn: str
95+
) -> None:
96+
super().__init__(ble_dev, adv_data, sn)
97+
self.on_connection_state_change(self._default_ac_ports_off_when_missing)
98+
99+
def _default_ac_ports_off_when_missing(self, state: ConnectionState) -> None:
100+
# The inverter stops sending its heartbeat entirely while AC output is off, so
101+
# `ac_ports` never gets a value after a fresh connect and the switch stays
102+
# `unavailable`. Default it to off if nothing arrives within 10s - a real
103+
# heartbeat afterwards still overrides this.
104+
if state != ConnectionState.AUTHENTICATED:
105+
return
106+
107+
def _set_off_if_unknown() -> None:
108+
if self.ac_ports is None:
109+
self.notify_field(Delta2Base.ac_ports, False)
110+
111+
self.call_later(10, _set_off_if_unknown, key="default_ac_ports_off")
112+
89113
@property
90114
def pd_heart_type(self):
91115
return BasePdHeart

0 commit comments

Comments
 (0)