From ac84cda5bff40bae1b9f728e3ddb480e8153079f Mon Sep 17 00:00:00 2001 From: Evgeny Zinoviev Date: Wed, 3 Nov 2021 13:03:50 +0300 Subject: monitor: update battery level notifications --- src/inverter-bot | 14 +++++++------- src/monitor.py | 11 ++++++----- 2 files changed, 13 insertions(+), 12 deletions(-) diff --git a/src/inverter-bot b/src/inverter-bot index cb50b65..7f5e5db 100755 --- a/src/inverter-bot +++ b/src/inverter-bot @@ -63,7 +63,7 @@ _strings = { 'chrg_evt_disconnected': 'AC line disconnected.', 'chrg_evt_current_changed': 'AC charging current set to %dA.', 'chrg_evt_na_solar': 'AC line detected, but battery charging is unavailable due to active solar power line.', - 'battery_state_changed': 'Battery voltage state changed to %s (%0.1f V)' + 'battery_level_changed': 'Battery level: %s (%0.1f V under %d W load)' } logger = logging.getLogger(__name__) @@ -395,18 +395,18 @@ def monitor_charging_event_handler(event: ChargingEvent, **kwargs) -> None: notify_all(_(f'chrg_evt_{key}', *args)) -def monitor_battery_event_handler(state: BatteryState, v: float) -> None: +def monitor_battery_event_handler(state: BatteryState, v: float, load_watts: int) -> None: if state == BatteryState.NORMAL: - label = 'normal' - elif state == BatteryState.WARNING: - label = 'warning' + label = '✅ Normal' + elif state == BatteryState.LOW: + label = '⚠️ Low' elif state == BatteryState.CRITICAL: - label = 'critical' + label = '‼️ Critical' else: logger.error('unknown battery state:', state) return - notify_all(_('battery_state_changed', label, v)) + notify_all(_('battery_level_changed', label, v, load_watts)) if __name__ == '__main__': diff --git a/src/monitor.py b/src/monitor.py index 6f6d5a4..e87b2f8 100644 --- a/src/monitor.py +++ b/src/monitor.py @@ -28,7 +28,7 @@ class ChargingState(Enum): class BatteryState(Enum): NORMAL = auto() - WARNING = auto() + LOW = auto() CRITICAL = auto() @@ -81,13 +81,14 @@ class InverterMonitor(Thread): ac = gs['grid_voltage']['value'] > 0 or gs['grid_freq']['value'] > 0 solar = gs['pv1_input_power']['value'] > 0 v = float(gs['battery_voltage']['value']) + load_watts = int(gs['ac_output_active_power']['value']) _logger.debug(f'got status: ac={ac}, solar={solar}, v={v}') self.ac_charging_program(ac, solar, v) if not ac: - self.low_voltage_program(v) + self.low_voltage_program(v, load_watts) elif self.battery_state != BatteryState.NORMAL: self.battery_state = BatteryState.NORMAL @@ -181,17 +182,17 @@ class InverterMonitor(Thread): except InverterError as e: _logger.exception(e) - def low_voltage_program(self, v: float): + def low_voltage_program(self, v: float, load_watts: int): if v < 45: state = BatteryState.CRITICAL elif v < 47: - state = BatteryState.WARNING + state = BatteryState.LOW else: state = BatteryState.NORMAL if state != self.battery_state: self.battery_state = state - self.battery_event_handler(state, v=v) + self.battery_event_handler(state, v, load_watts) def set_charging_event_handler(self, handler: Callable): self.charging_event_handler = handler -- cgit v1.2.3