summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rwxr-xr-xsrc/inverter-bot37
-rw-r--r--src/monitor.py17
2 files changed, 39 insertions, 15 deletions
diff --git a/src/inverter-bot b/src/inverter-bot
index f4691f7..cb50b65 100755
--- a/src/inverter-bot
+++ b/src/inverter-bot
@@ -7,8 +7,8 @@ import json
from typing import Optional, Tuple
from argparse import ArgumentParser
from html import escape
-from inverter_wrapper import InverterClientWrapper, wrapper_instance as inverter
-from monitor import InverterMonitor, ChargingEvent
+from inverter_wrapper import wrapper_instance as inverter
+from monitor import InverterMonitor, ChargingEvent, BatteryState
from inverterd import Format, InverterError
from telegram import (
Update,
@@ -62,7 +62,8 @@ _strings = {
'chrg_evt_finished': 'Finished charging from AC.',
'chrg_evt_disconnected': 'AC line disconnected.',
'chrg_evt_current_changed': 'AC charging current set to <b>%dA</b>.',
- 'chrg_evt_na_solar': 'AC line detected, but battery charging is unavailable due to active solar power line.'
+ '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 <b>%s</b> (%0.1f V)'
}
logger = logging.getLogger(__name__)
@@ -122,6 +123,14 @@ def reply(update: Update, text: str, reply_markup=None) -> None:
parse_mode=ParseMode.HTML)
+def notify_all(text: str) -> None:
+ for chat_id in notify_to:
+ updater.bot.send_message(chat_id=chat_id,
+ text=text,
+ parse_mode='HTML',
+ reply_markup=get_markup())
+
+
def handle_exc(update: Update, e) -> None:
logging.exception(str(e))
@@ -363,7 +372,7 @@ def on_button(update: Update, context: CallbackContext) -> None:
query.answer('unexpected callback data')
-def monitor_charging_event_handler(event: ChargingEvent, **kwargs):
+def monitor_charging_event_handler(event: ChargingEvent, **kwargs) -> None:
key = None
args = []
@@ -383,17 +392,21 @@ def monitor_charging_event_handler(event: ChargingEvent, **kwargs):
logger.error('unknown charging event:', event)
return
- text = _(f'chrg_evt_{key}', *args)
+ notify_all(_(f'chrg_evt_{key}', *args))
- for chat_id in notify_to:
- updater.bot.send_message(chat_id=chat_id,
- text=text,
- parse_mode='HTML',
- reply_markup=get_markup())
+def monitor_battery_event_handler(state: BatteryState, v: float) -> None:
+ if state == BatteryState.NORMAL:
+ label = 'normal'
+ elif state == BatteryState.WARNING:
+ label = 'warning'
+ elif state == BatteryState.CRITICAL:
+ label = 'critical'
+ else:
+ logger.error('unknown battery state:', state)
+ return
-def monitor_battery_event_handler(event):
- pass
+ notify_all(_('battery_state_changed', label, v))
if __name__ == '__main__':
diff --git a/src/monitor.py b/src/monitor.py
index e8c24e7..6f6d5a4 100644
--- a/src/monitor.py
+++ b/src/monitor.py
@@ -41,7 +41,7 @@ class InverterMonitor(Thread):
self.allowed_currents = []
self.battery_under_voltage = None
self.charging_event_handler = None
- self.battery_voltage_handler = None
+ self.battery_event_handler = None
self.currents = []
self.active_current = None
@@ -88,6 +88,8 @@ class InverterMonitor(Thread):
if not ac:
self.low_voltage_program(v)
+ elif self.battery_state != BatteryState.NORMAL:
+ self.battery_state = BatteryState.NORMAL
except InverterError as e:
_logger.exception(e)
@@ -180,13 +182,22 @@ class InverterMonitor(Thread):
_logger.exception(e)
def low_voltage_program(self, v: float):
- pass
+ if v < 45:
+ state = BatteryState.CRITICAL
+ elif v < 47:
+ state = BatteryState.WARNING
+ else:
+ state = BatteryState.NORMAL
+
+ if state != self.battery_state:
+ self.battery_state = state
+ self.battery_event_handler(state, v=v)
def set_charging_event_handler(self, handler: Callable):
self.charging_event_handler = handler
def set_battery_event_handler(self, handler: Callable):
- self.battery_voltage_handler = handler
+ self.battery_event_handler = handler
def stop(self):
self.interrupted = True