|
| 1 | +import logging |
1 | 2 | from configparser import ConfigParser |
2 | 3 |
|
| 4 | +logger = logging.getLogger() |
3 | 5 |
|
4 | 6 | class ConfigProvider: |
5 | 7 |
|
@@ -101,3 +103,194 @@ def get_reduce_wattage(self, inverter_idx): |
101 | 103 |
|
102 | 104 | def get_battery_priority(self, inverter_idx): |
103 | 105 | return self.config.getint('INVERTER_' + str(inverter_idx + 1), 'HOY_BATTERY_PRIORITY') |
| 106 | + |
| 107 | + |
| 108 | +class ConfigProviderChain(ConfigProvider): |
| 109 | + """ |
| 110 | + This class is a chain of config providers. It will call all the providers in the order they are given and return the |
| 111 | + first non-None value. |
| 112 | +
|
| 113 | + This is useful if you want to combine multiple config sources, e.g. a config file and a MQTT topic. |
| 114 | + """ |
| 115 | + def __init__(self, providers): |
| 116 | + self.providers = providers |
| 117 | + |
| 118 | + def update(self): |
| 119 | + for provider in self.providers: |
| 120 | + provider.update() |
| 121 | + |
| 122 | + def __getattribute__(self, name): |
| 123 | + if name in ['update', 'providers']: |
| 124 | + return object.__getattribute__(self, name) |
| 125 | + |
| 126 | + def method(*args, **kwargs): |
| 127 | + for provider in self.providers: |
| 128 | + f = getattr(provider, name) |
| 129 | + if callable(f): |
| 130 | + value = f(*args, **kwargs) |
| 131 | + if value is not None: |
| 132 | + return value |
| 133 | + return None |
| 134 | + return method |
| 135 | + |
| 136 | +class OverridingConfigProvider(ConfigProvider): |
| 137 | + """ |
| 138 | + This class is a config provider that allows to override the config values from code. |
| 139 | +
|
| 140 | + This can be used as a base class for config providers that allow to change the configuration |
| 141 | + using a push mechanism, e.g. MQTT or a REST API. |
| 142 | + """ |
| 143 | + def __init__(self): |
| 144 | + self.common_config = {} |
| 145 | + self.inverter_config = [] |
| 146 | + |
| 147 | + @staticmethod |
| 148 | + def cast_value(is_inverter_value, key, value): |
| 149 | + if is_inverter_value: |
| 150 | + if key in ['min_watt_in_percent', 'normal_watt', 'reduce_watt', 'battery_priority']: |
| 151 | + return int(value) |
| 152 | + else: |
| 153 | + logger.error(f"Unknown inverter key {key}") |
| 154 | + else: |
| 155 | + if key in ['powermeter_target_point', 'powermeter_max_point', 'powermeter_tolerance', 'on_grid_usage_jump_to_limit_percent']: |
| 156 | + return int(value) |
| 157 | + else: |
| 158 | + logger.error(f"Unknown common key {key}") |
| 159 | + |
| 160 | + def set_common_value(self, name, value): |
| 161 | + if value is None: |
| 162 | + if name in self.common_config: |
| 163 | + del self.common_config[name] |
| 164 | + logger.info(f"Unset common config value {name}") |
| 165 | + else: |
| 166 | + cast_value = self.cast_value(False, name, value) |
| 167 | + self.common_config[name] = cast_value |
| 168 | + logger.info(f"Set common config value {name} to {cast_value}") |
| 169 | + |
| 170 | + def set_inverter_value(self, inverter_idx: int, name: str, value): |
| 171 | + if value is None: |
| 172 | + if inverter_idx < len(self.inverter_config) and name in self.inverter_config[inverter_idx]: |
| 173 | + del self.inverter_config[inverter_idx][name] |
| 174 | + logger.info(f"Unset inverter {inverter_idx} config value {name}") |
| 175 | + else: |
| 176 | + while len(self.inverter_config) <= inverter_idx: |
| 177 | + self.inverter_config.append({}) |
| 178 | + cast_value = self.cast_value(True, name, value) |
| 179 | + self.inverter_config[inverter_idx][name] = cast_value |
| 180 | + logger.info(f"Set inverter {inverter_idx} config value {name} to {cast_value}") |
| 181 | + |
| 182 | + def get_powermeter_target_point(self): |
| 183 | + return self.common_config.get('powermeter_target_point') |
| 184 | + |
| 185 | + def get_powermeter_max_point(self): |
| 186 | + return self.common_config.get('powermeter_max_point') |
| 187 | + |
| 188 | + def get_powermeter_tolerance(self): |
| 189 | + return self.common_config.get('powermeter_tolerance') |
| 190 | + |
| 191 | + def on_grid_usage_jump_to_limit_percent(self): |
| 192 | + return self.common_config.get('on_grid_usage_jump_to_limit_percent') |
| 193 | + |
| 194 | + def get_min_wattage_in_percent(self, inverter_idx): |
| 195 | + if inverter_idx >= len(self.inverter_config): |
| 196 | + return None |
| 197 | + return self.inverter_config[inverter_idx].get('min_watt_in_percent') |
| 198 | + |
| 199 | + def get_normal_wattage(self, inverter_idx): |
| 200 | + if inverter_idx >= len(self.inverter_config): |
| 201 | + return None |
| 202 | + return self.inverter_config[inverter_idx].get('normal_watt') |
| 203 | + |
| 204 | + def get_reduce_wattage(self, inverter_idx): |
| 205 | + if inverter_idx >= len(self.inverter_config): |
| 206 | + return None |
| 207 | + return self.inverter_config[inverter_idx].get('reduce_watt') |
| 208 | + |
| 209 | + def get_battery_priority(self, inverter_idx): |
| 210 | + if inverter_idx >= len(self.inverter_config): |
| 211 | + return None |
| 212 | + return self.inverter_config[inverter_idx].get('battery_priority') |
| 213 | + |
| 214 | + |
| 215 | +class MqttConfigProvider(OverridingConfigProvider): |
| 216 | + """ |
| 217 | + Config provider that subscribes to a MQTT topic and updates the configuration from the messages. |
| 218 | + """ |
| 219 | + def __init__(self, mqtt_broker, mqtt_port, client_id, mqtt_username, mqtt_password, set_topic, reset_topic): |
| 220 | + super().__init__() |
| 221 | + self.mqtt_broker = mqtt_broker |
| 222 | + self.mqtt_port = mqtt_port |
| 223 | + self.mqtt_username = mqtt_username |
| 224 | + self.mqtt_password = mqtt_password |
| 225 | + self.set_topic = set_topic |
| 226 | + self.reset_topic = reset_topic |
| 227 | + self.target_point = None |
| 228 | + self.max_point = None |
| 229 | + self.tolerance = None |
| 230 | + self.on_grid_usage_jump_to_limit_percent = None |
| 231 | + self.min_wattage_in_percent = [] |
| 232 | + self.normal_wattage = [] |
| 233 | + self.reduce_wattage = [] |
| 234 | + self.battery_priority = [] |
| 235 | + |
| 236 | + import paho.mqtt.client as mqtt |
| 237 | + self.mqtt_client = mqtt.Client(mqtt.CallbackAPIVersion.VERSION2, client_id=client_id) |
| 238 | + self.mqtt_client.on_connect = self.on_connect |
| 239 | + self.mqtt_client.on_message = self.on_message |
| 240 | + if self.mqtt_username is not None: |
| 241 | + self.mqtt_client.username_pw_set(self.mqtt_username, self.mqtt_password) |
| 242 | + self.mqtt_client.connect(self.mqtt_broker, self.mqtt_port) |
| 243 | + self.mqtt_client.loop_start() |
| 244 | + |
| 245 | + def on_connect(self, client, userdata, flags, reason_code, properties): |
| 246 | + print("Connected with result code " + str(reason_code)) |
| 247 | + client.subscribe(f"{self.set_topic}/#") |
| 248 | + client.subscribe(f"{self.reset_topic}/#") |
| 249 | + |
| 250 | + def on_message(self, client, userdata, msg): |
| 251 | + try: |
| 252 | + self.handle_message(msg) |
| 253 | + except Exception as e: |
| 254 | + logger.error(f"Error handling message {msg.topic}: {e}") |
| 255 | + |
| 256 | + def handle_message(self, msg): |
| 257 | + if msg.topic.startswith(self.set_topic): |
| 258 | + topic_suffix = msg.topic[len(self.set_topic) + 1:] |
| 259 | + logger.info(f"Received set message for config value {topic_suffix} with payload {msg.payload}") |
| 260 | + |
| 261 | + def set_common_value(name): |
| 262 | + self.set_common_value(name, msg.payload) |
| 263 | + |
| 264 | + def set_inverter_value(inverter_idx, name): |
| 265 | + self.set_inverter_value(inverter_idx, name, msg.payload) |
| 266 | + |
| 267 | + elif msg.topic.startswith(self.reset_topic): |
| 268 | + topic_suffix = msg.topic[len(self.reset_topic) + 1:] |
| 269 | + logger.info(f"Received reset message for config value {topic_suffix}") |
| 270 | + |
| 271 | + def set_common_value(name): |
| 272 | + self.set_common_value(name, None) |
| 273 | + |
| 274 | + def set_inverter_value(inverter_idx, name): |
| 275 | + self.set_inverter_value(inverter_idx, name, None) |
| 276 | + else: |
| 277 | + logger.error(f"Invalid topic {msg.topic}") |
| 278 | + return |
| 279 | + |
| 280 | + if topic_suffix.startswith("inverter/"): |
| 281 | + inverter_topic_suffix = topic_suffix[len("inverter/"):] |
| 282 | + |
| 283 | + index_config_start_pos = inverter_topic_suffix.index("/") |
| 284 | + if index_config_start_pos == -1: |
| 285 | + logger.error(f"Invalid inverter config topic {msg.topic}") |
| 286 | + return |
| 287 | + |
| 288 | + inverter = int(inverter_topic_suffix[:index_config_start_pos]) |
| 289 | + key = inverter_topic_suffix[index_config_start_pos + 1:] |
| 290 | + set_inverter_value(inverter, key) |
| 291 | + else: |
| 292 | + set_common_value(topic_suffix) |
| 293 | + |
| 294 | + def __del__(self): |
| 295 | + logger.info("Disconnecting MQTT client") |
| 296 | + self.mqtt_client.disconnect() |
0 commit comments