|
| 1 | +from bleak.backends.device import BLEDevice |
| 2 | +from bleak.backends.scanner import AdvertisementData |
| 3 | + |
| 4 | +from ..connection import ConnectionState |
1 | 5 | from ..devicebase import DeviceBase |
2 | 6 | from ..entity import controls |
3 | 7 | from ..entity.base import dynamic |
@@ -86,6 +90,26 @@ class Delta2Base(DeviceBase, RawDataProps): |
86 | 90 | dc12v_output_voltage = raw_field(pb_mppt.car_out_vol, pdiv(1000, 2)) |
87 | 91 | dc12v_output_current = raw_field(pb_mppt.car_out_amp, pdiv(1000, 2)) |
88 | 92 |
|
| 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 | + |
89 | 113 | @property |
90 | 114 | def pd_heart_type(self): |
91 | 115 | return BasePdHeart |
|
0 commit comments