summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorEvgeny Zinoviev <me@ch1p.io>2021-11-03 19:17:04 +0300
committerEvgeny Zinoviev <me@ch1p.io>2021-11-03 19:17:16 +0300
commita96a90fcc2df7a6bb2aa02705c3539e33b89026d (patch)
tree046fa7b6e778623e344dba59790cf44282eb5a3b /src
parent3887262236935c9f113d5e086d437bdea4e9cbf8 (diff)
shit, i'm not ready for python 3.10, so rewrite match-case blocks with if-else
Diffstat (limited to 'src')
-rwxr-xr-xsrc/inverter-bot51
-rw-r--r--src/monitor.py113
2 files changed, 77 insertions, 87 deletions
diff --git a/src/inverter-bot b/src/inverter-bot
index 4fa18e0..66f4fb2 100755
--- a/src/inverter-bot
+++ b/src/inverter-bot
@@ -378,25 +378,21 @@ def on_button(update: Update, context: CallbackContext) -> None:
#
def monitor_charging_event_handler(event: ChargingEvent, **kwargs) -> None:
- key = None
args = []
-
- match event:
- case ChargingEvent.AC_CHARGING_STARTED:
- key = 'started'
- case ChargingEvent.AC_CHARGING_FINISHED:
- key = 'finished'
- case ChargingEvent.AC_DISCONNECTED:
- key = 'disconnected'
- case ChargingEvent.AC_NOT_CHARGING:
- key = 'not_charging'
- case ChargingEvent.AC_CURRENT_CHANGED:
- key = 'current_changed'
- args.append(kwargs['current'])
- case ChargingEvent.AC_CHARGING_UNAVAILABLE_BECAUSE_SOLAR:
- key = 'na_solar'
-
- if key is None:
+ if event == ChargingEvent.AC_CHARGING_STARTED:
+ key = 'started'
+ elif event == ChargingEvent.AC_CHARGING_FINISHED:
+ key = 'finished'
+ elif event == ChargingEvent.AC_DISCONNECTED:
+ key = 'disconnected'
+ elif event == ChargingEvent.AC_NOT_CHARGING:
+ key = 'not_charging'
+ elif event == ChargingEvent.AC_CURRENT_CHANGED:
+ key = 'current_changed'
+ args.append(kwargs['current'])
+ elif event == ChargingEvent.AC_CHARGING_UNAVAILABLE_BECAUSE_SOLAR:
+ key = 'na_solar'
+ else:
logger.error('unknown charging event:', event)
return
@@ -404,16 +400,15 @@ def monitor_charging_event_handler(event: ChargingEvent, **kwargs) -> None:
def monitor_battery_event_handler(state: BatteryState, v: float, load_watts: int) -> None:
- match state:
- case BatteryState.NORMAL:
- label = '✅ Normal'
- case BatteryState.LOW:
- label = '⚠️ Low'
- case BatteryState.CRITICAL:
- label = '‼️ Critical'
- case _:
- logger.error('unknown battery state:', state)
- return
+ if state == BatteryState.NORMAL:
+ label = '✅ Normal'
+ elif state == BatteryState.LOW:
+ label = '⚠️ Low'
+ elif state == BatteryState.CRITICAL:
+ label = '‼️ Critical'
+ else:
+ logger.error('unknown battery state:', state)
+ return
notify_all(_('battery_level_changed', label, v, load_watts))
diff --git a/src/monitor.py b/src/monitor.py
index ddbbdd7..1efe88a 100644
--- a/src/monitor.py
+++ b/src/monitor.py
@@ -40,15 +40,14 @@ class BatteryState(Enum):
def _pd_from_string(pd: str) -> BatteryPowerDirection:
- match pd:
- case 'Discharge':
- return BatteryPowerDirection.DISCHARGING
- case 'Charge':
- return BatteryPowerDirection.CHARGING
- case 'Do nothing':
- return BatteryPowerDirection.DO_NOTHING
- case _:
- raise ValueError(f'invalid power direction: {pd}')
+ if pd == 'Discharge':
+ return BatteryPowerDirection.DISCHARGING
+ elif pd == 'Charge':
+ return BatteryPowerDirection.CHARGING
+ elif pd == 'Do nothing':
+ return BatteryPowerDirection.DO_NOTHING
+ else:
+ raise ValueError(f'invalid power direction: {pd}')
class InverterMonitor(Thread):
@@ -140,47 +139,46 @@ class InverterMonitor(Thread):
sleep(2)
def ac_charging_program(self, ac: bool, solar: bool, v: float, pd: BatteryPowerDirection):
- match self.charging_state:
- case ChargingState.NOT_CHARGING:
- if ac and solar:
- self.charging_state = ChargingState.AC_BUT_SOLAR
- self.charging_event_handler(ChargingEvent.AC_CHARGING_UNAVAILABLE_BECAUSE_SOLAR)
- _logger.info('entering AC_BUT_SOLAR state')
- elif ac:
- self.ac_charging_start(pd)
-
- case ChargingState.AC_BUT_SOLAR:
- if not ac:
- self.ac_charging_stop(ChargingState.NOT_CHARGING)
- elif not solar:
- self.ac_charging_start(pd)
-
- case ChargingState.AC_OK | ChargingState.AC_WAITING:
- if not ac:
- self.ac_charging_stop(ChargingState.NOT_CHARGING)
- return
-
- if solar:
- self.charging_state = ChargingState.AC_BUT_SOLAR
- self.charging_event_handler(ChargingEvent.AC_CHARGING_UNAVAILABLE_BECAUSE_SOLAR)
- _logger.info('solar power connected during charging, entering AC_BUT_SOLAR state')
-
- state = ChargingState.AC_OK if pd == BatteryPowerDirection.CHARGING else ChargingState.AC_WAITING
- if state != self.charging_state:
- self.charging_state = state
-
- evt = ChargingEvent.AC_CHARGING_STARTED if state == ChargingState.AC_OK else ChargingEvent.AC_NOT_CHARGING
- self.charging_event_handler(evt)
-
- # if currently charging, monitor battery voltage dynamics here
- if self.active_current is not None:
- upper_bound = 56.6 if self.active_current > 10 else 54
- if v >= upper_bound:
- self.ac_charging_next_current()
-
- case ChargingState.AC_DONE:
- if not ac:
- self.ac_charging_stop(ChargingState.NOT_CHARGING)
+ if self.charging_state == ChargingState.NOT_CHARGING:
+ if ac and solar:
+ self.charging_state = ChargingState.AC_BUT_SOLAR
+ self.charging_event_handler(ChargingEvent.AC_CHARGING_UNAVAILABLE_BECAUSE_SOLAR)
+ _logger.info('entering AC_BUT_SOLAR state')
+ elif ac:
+ self.ac_charging_start(pd)
+
+ elif self.charging_state == ChargingState.AC_BUT_SOLAR:
+ if not ac:
+ self.ac_charging_stop(ChargingState.NOT_CHARGING)
+ elif not solar:
+ self.ac_charging_start(pd)
+
+ elif self.charging_state in (ChargingState.AC_OK, ChargingState.AC_WAITING):
+ if not ac:
+ self.ac_charging_stop(ChargingState.NOT_CHARGING)
+ return
+
+ if solar:
+ self.charging_state = ChargingState.AC_BUT_SOLAR
+ self.charging_event_handler(ChargingEvent.AC_CHARGING_UNAVAILABLE_BECAUSE_SOLAR)
+ _logger.info('solar power connected during charging, entering AC_BUT_SOLAR state')
+
+ state = ChargingState.AC_OK if pd == BatteryPowerDirection.CHARGING else ChargingState.AC_WAITING
+ if state != self.charging_state:
+ self.charging_state = state
+
+ evt = ChargingEvent.AC_CHARGING_STARTED if state == ChargingState.AC_OK else ChargingEvent.AC_NOT_CHARGING
+ self.charging_event_handler(evt)
+
+ # if currently charging, monitor battery voltage dynamics here
+ if self.active_current is not None:
+ upper_bound = 56.6 if self.active_current > 10 else 54
+ if v >= upper_bound:
+ self.ac_charging_next_current()
+
+ elif self.charging_state == ChargingState.AC_DONE:
+ if not ac:
+ self.ac_charging_stop(ChargingState.NOT_CHARGING)
def ac_charging_start(self, pd: BatteryPowerDirection):
if pd == BatteryPowerDirection.CHARGING:
@@ -204,15 +202,12 @@ class InverterMonitor(Thread):
def ac_charging_stop(self, reason: ChargingState):
self.charging_state = reason
- match reason:
- case ChargingState.AC_DONE:
- event = ChargingEvent.AC_CHARGING_FINISHED
-
- case ChargingState.NOT_CHARGING:
- event = ChargingEvent.AC_DISCONNECTED
-
- case _:
- raise ValueError(f'ac_charging_stop: unexpected reason {reason}')
+ if reason == ChargingState.AC_DONE:
+ event = ChargingEvent.AC_CHARGING_FINISHED
+ elif reason == ChargingState.NOT_CHARGING:
+ event = ChargingEvent.AC_DISCONNECTED
+ else:
+ raise ValueError(f'ac_charging_stop: unexpected reason {reason}')
_logger.info(f'charging is finished, entering {reason} state')
self.charging_event_handler(event)