* S allius/issue117 (#118) * add shutdown flag * add more register definitions * add start commando for client side connections * add first support for port 8899 * fix shutdown * add client_mode configuration * read client_mode config to setup inverter connections * add client_mode connections over port 8899 * add preview build * Update README.md describe the new client-mode over port 8899 for GEN3PLUS * MODBUS: the last digit of the inverter version is a hexadecimal number (#121) * S allius/issue117 (#122) * add shutdown flag * add more register definitions * add start commando for client side connections * add first support for port 8899 * fix shutdown * add client_mode configuration * read client_mode config to setup inverter connections * add client_mode connections over port 8899 * add preview build * add documentation for client_mode * catch os error and log thme with DEBUG level * update changelog * make the maximum output coefficient configurable (#124) * S allius/issue120 (#126) * add config option to disable the modbus polling * read more modbus regs in polling mode * extend connection timeouts if polling mode is disabled * update changelog * S allius/issue125 (#127) * fix linter warning * move sequence diagramm to wiki * catch asyncio.CancelledError * S allius/issue128 (#130) * set Register.NO_INPUTS fix to 4 for GEN3PLUS * don't set Register.NO_INPUTS per MODBUS * fix unit tests * register OUTPUT_COEFFICIENT at HA * update changelog * - Home Assistant: improve inverter status value texts * - GEN3: add inverter status * on closing send outstanding MQTT data to the broker * force MQTT publish on every conn open and close * reset inverter state on close - workaround which reset the inverter status to offline when the inverter has a very low output power on connection close * improve client modified - reduce the polling cadence to 30s - set controller statistics for HA * client mode set controller IP for HA * S allius/issue131 (#132) * Make __publish_outstanding_mqtt public * update proxy counter - on client mode connection establishment or disconnecting update tje counection counter * Update README.md (#133) * reset inverter state on close - workaround which reset the inverter status to offline when the inverter has a very low output power on connection close * S allius/issue134 (#135) * add polling invertval and method ha_remove() * add client_mode arg to constructors - add PollingInvervall * hide some topics in client mode - we hide topics in HA by sending an empty register MQTT topic during HA auto configuration * add client_mode value * update class diagram * fix modbus close handler - fix empty call and cleanup que - add unit test * don't sent an initial 1710 msg in client mode * change HA icon for inverter status * increase test coverage * accelerate timer tests * bump aiomqtt and schema to latest release (#137) * MQTT timestamps and protocol improvements (#140) * add TS_INPUT, TS_GRID and TS_TOTAL * prepare MQTT timestamps - add _set_mqtt_timestamp method - fix hexdump printing * push dev and debug images to docker.io * add unix epoche timestamp for MQTT pakets * set timezone for unit tests * set name für setting timezone step * trigger new action * GEN3 and GEN3PLUS: handle multiple message - read: iterate over the receive buffer - forward: append messages to the forward buffer - _update_header: iterate over the forward buffer * GEN3: optimize timeout handling - longer timeout in state init and reveived - got to state pending only from state up * update changelog * cleanup * print coloured logs * Create sonarcloud.yml (#143) * Update sonarcloud.yml * Update sonarcloud.yml * Update sonarcloud.yml * Update sonarcloud.yml * Update sonarcloud.yml * build multi arch images with sboms (#146) * don't send MODBUS request when state is not up (#147) * adapt timings * don't send MODBUS request when state is note up * adapt unit test * make test code more clean (#148) * Make test code more clean (#149) * cleanup * Code coverage for SonarCloud (#150) * cleanup code and unit tests * add test coverage for SonarCloud * configure SonarCloud * update changelog * Do no build on *.yml changes * prepare release 0.10.0 * disable MODBUS_POLLING for GEN§PLUS in example config * bump aiohttp to version 3.10.2 * code cleanup * Fetch all history for all tags and branches
140 lines
10 KiB
Python
140 lines
10 KiB
Python
# test_with_pytest.py
|
|
import tomllib
|
|
from schema import SchemaMissingKeyError
|
|
from app.src.config import Config
|
|
|
|
class TstConfig(Config):
|
|
|
|
@classmethod
|
|
def set(cls, cnf):
|
|
cls.config = cnf
|
|
|
|
@classmethod
|
|
def _read_config_file(cls) -> dict:
|
|
return cls.config
|
|
|
|
|
|
def test_empty_config():
|
|
cnf = {}
|
|
try:
|
|
Config.conf_schema.validate(cnf)
|
|
assert False
|
|
except SchemaMissingKeyError:
|
|
pass
|
|
|
|
def test_default_config():
|
|
with open("app/config/default_config.toml", "rb") as f:
|
|
cnf = tomllib.load(f)
|
|
|
|
try:
|
|
validated = Config.conf_schema.validate(cnf)
|
|
except Exception:
|
|
assert False
|
|
assert validated == {'gen3plus': {'at_acl': {'mqtt': {'allow': ['AT+'], 'block': []}, 'tsun': {'allow': ['AT+Z', 'AT+UPURL', 'AT+SUPDATE'], 'block': []}}}, 'tsun': {'enabled': True, 'host': 'logger.talent-monitoring.com', 'port': 5005}, 'solarman': {'enabled': True, 'host': 'iot.talent-monitoring.com', 'port': 10000}, 'mqtt': {'host': 'mqtt', 'port': 1883, 'user': None, 'passwd': None}, 'ha': {'auto_conf_prefix': 'homeassistant', 'discovery_prefix': 'homeassistant', 'entity_prefix': 'tsun', 'proxy_node_id': 'proxy', 'proxy_unique_id': 'P170000000000001'}, 'inverters': {'allow_all': True, 'R170000000000001': {'node_id': '', 'modbus_polling': False, 'monitor_sn': 0, 'suggested_area': ''}, 'Y170000000000001': {'modbus_polling': True, 'monitor_sn': 2000000000, 'node_id': '', 'suggested_area': ''}}}
|
|
|
|
def test_full_config():
|
|
cnf = {'tsun': {'enabled': True, 'host': 'logger.talent-monitoring.com', 'port': 5005},
|
|
'gen3plus': {'at_acl': {'mqtt': {'allow': ['AT+'], 'block': []},
|
|
'tsun': {'allow': ['AT+Z', 'AT+UPURL', 'AT+SUPDATE'], 'block': []}}},
|
|
'solarman': {'enabled': True, 'host': 'iot.talent-monitoring.com', 'port': 10000},
|
|
'mqtt': {'host': 'mqtt', 'port': 1883, 'user': '', 'passwd': ''},
|
|
'ha': {'auto_conf_prefix': 'homeassistant', 'discovery_prefix': 'homeassistant', 'entity_prefix': 'tsun', 'proxy_node_id': 'proxy', 'proxy_unique_id': 'P170000000000001'},
|
|
'inverters': {'allow_all': True,
|
|
'R170000000000001': {'modbus_polling': True, 'node_id': '', 'suggested_area': '', 'pv1': {'type': 'type1', 'manufacturer': 'man1'}, 'pv2': {'type': 'type2', 'manufacturer': 'man2'}, 'pv3': {'type': 'type3', 'manufacturer': 'man3'}},
|
|
'Y170000000000001': {'modbus_polling': True, 'monitor_sn': 2000000000, 'node_id': '', 'suggested_area': ''}}}
|
|
try:
|
|
validated = Config.conf_schema.validate(cnf)
|
|
except Exception:
|
|
assert False
|
|
assert validated == {'gen3plus': {'at_acl': {'mqtt': {'allow': ['AT+'], 'block': []}, 'tsun': {'allow': ['AT+Z', 'AT+UPURL', 'AT+SUPDATE'], 'block': []}}}, 'tsun': {'enabled': True, 'host': 'logger.talent-monitoring.com', 'port': 5005}, 'solarman': {'enabled': True, 'host': 'iot.talent-monitoring.com', 'port': 10000}, 'mqtt': {'host': 'mqtt', 'port': 1883, 'user': None, 'passwd': None}, 'ha': {'auto_conf_prefix': 'homeassistant', 'discovery_prefix': 'homeassistant', 'entity_prefix': 'tsun', 'proxy_node_id': 'proxy', 'proxy_unique_id': 'P170000000000001'}, 'inverters': {'allow_all': True, 'R170000000000001': {'node_id': '', 'modbus_polling': True, 'monitor_sn': 0, 'pv1': {'manufacturer': 'man1','type': 'type1'},'pv2': {'manufacturer': 'man2','type': 'type2'},'pv3': {'manufacturer': 'man3','type': 'type3'}, 'suggested_area': ''}, 'Y170000000000001': {'modbus_polling': True, 'monitor_sn': 2000000000, 'node_id': '', 'suggested_area': ''}}}
|
|
|
|
def test_mininum_config():
|
|
cnf = {'tsun': {'enabled': True, 'host': 'logger.talent-monitoring.com', 'port': 5005},
|
|
'gen3plus': {'at_acl': {'mqtt': {'allow': ['AT+']},
|
|
'tsun': {'allow': ['AT+Z', 'AT+UPURL', 'AT+SUPDATE']}}},
|
|
'solarman': {'enabled': True, 'host': 'iot.talent-monitoring.com', 'port': 10000},
|
|
'mqtt': {'host': 'mqtt', 'port': 1883, 'user': '', 'passwd': ''},
|
|
'ha': {'auto_conf_prefix': 'homeassistant', 'discovery_prefix': 'homeassistant', 'entity_prefix': 'tsun', 'proxy_node_id': 'proxy', 'proxy_unique_id': 'P170000000000001'},
|
|
'inverters': {'allow_all': True,
|
|
'R170000000000001': {}}
|
|
}
|
|
|
|
try:
|
|
validated = Config.conf_schema.validate(cnf)
|
|
except Exception:
|
|
assert False
|
|
assert validated == {'gen3plus': {'at_acl': {'mqtt': {'allow': ['AT+'], 'block': []}, 'tsun': {'allow': ['AT+Z', 'AT+UPURL', 'AT+SUPDATE'], 'block': []}}}, 'tsun': {'enabled': True, 'host': 'logger.talent-monitoring.com', 'port': 5005}, 'solarman': {'enabled': True, 'host': 'iot.talent-monitoring.com', 'port': 10000}, 'mqtt': {'host': 'mqtt', 'port': 1883, 'user': None, 'passwd': None}, 'ha': {'auto_conf_prefix': 'homeassistant', 'discovery_prefix': 'homeassistant', 'entity_prefix': 'tsun', 'proxy_node_id': 'proxy', 'proxy_unique_id': 'P170000000000001'}, 'inverters': {'allow_all': True, 'R170000000000001': {'node_id': '', 'modbus_polling': True, 'monitor_sn': 0, 'suggested_area': ''}}}
|
|
|
|
def test_read_empty():
|
|
cnf = {}
|
|
TstConfig.set(cnf)
|
|
err = TstConfig.read('app/config/')
|
|
assert err == None
|
|
cnf = TstConfig.get()
|
|
assert cnf == {'gen3plus': {'at_acl': {'mqtt': {'allow': ['AT+'], 'block': []}, 'tsun': {'allow': ['AT+Z', 'AT+UPURL', 'AT+SUPDATE'], 'block': []}}}, 'tsun': {'enabled': True, 'host': 'logger.talent-monitoring.com', 'port': 5005}, 'solarman': {'enabled': True, 'host': 'iot.talent-monitoring.com', 'port': 10000}, 'mqtt': {'host': 'mqtt', 'port': 1883, 'user': None, 'passwd': None}, 'ha': {'auto_conf_prefix': 'homeassistant', 'discovery_prefix': 'homeassistant', 'entity_prefix': 'tsun', 'proxy_node_id': 'proxy', 'proxy_unique_id': 'P170000000000001'}, 'inverters': {'allow_all': True, 'R170000000000001': {'suggested_area': '', 'modbus_polling': False, 'monitor_sn': 0, 'node_id': ''}, 'Y170000000000001': {'modbus_polling': True, 'monitor_sn': 2000000000, 'suggested_area': '', 'node_id': ''}}}
|
|
|
|
defcnf = TstConfig.def_config.get('solarman')
|
|
assert defcnf == {'enabled': True, 'host': 'iot.talent-monitoring.com', 'port': 10000}
|
|
assert True == TstConfig.is_default('solarman')
|
|
|
|
def test_no_file():
|
|
cnf = {}
|
|
TstConfig.set(cnf)
|
|
err = TstConfig.read('')
|
|
assert err == "Config.read: [Errno 2] No such file or directory: 'default_config.toml'"
|
|
cnf = TstConfig.get()
|
|
assert cnf == {}
|
|
defcnf = TstConfig.def_config.get('solarman')
|
|
assert defcnf == None
|
|
|
|
def test_read_cnf1():
|
|
cnf = {'solarman' : {'enabled': False}}
|
|
TstConfig.set(cnf)
|
|
err = TstConfig.read('app/config/')
|
|
assert err == None
|
|
cnf = TstConfig.get()
|
|
assert cnf == {'gen3plus': {'at_acl': {'mqtt': {'allow': ['AT+'], 'block': []}, 'tsun': {'allow': ['AT+Z', 'AT+UPURL', 'AT+SUPDATE'], 'block': []}}}, 'tsun': {'enabled': True, 'host': 'logger.talent-monitoring.com', 'port': 5005}, 'solarman': {'enabled': False, 'host': 'iot.talent-monitoring.com', 'port': 10000}, 'mqtt': {'host': 'mqtt', 'port': 1883, 'user': None, 'passwd': None}, 'ha': {'auto_conf_prefix': 'homeassistant', 'discovery_prefix': 'homeassistant', 'entity_prefix': 'tsun', 'proxy_node_id': 'proxy', 'proxy_unique_id': 'P170000000000001'}, 'inverters': {'allow_all': True, 'R170000000000001': {'suggested_area': '', 'modbus_polling': False, 'monitor_sn': 0, 'node_id': ''}, 'Y170000000000001': {'modbus_polling': True, 'monitor_sn': 2000000000, 'suggested_area': '', 'node_id': ''}}}
|
|
cnf = TstConfig.get('solarman')
|
|
assert cnf == {'enabled': False, 'host': 'iot.talent-monitoring.com', 'port': 10000}
|
|
defcnf = TstConfig.def_config.get('solarman')
|
|
assert defcnf == {'enabled': True, 'host': 'iot.talent-monitoring.com', 'port': 10000}
|
|
assert False == TstConfig.is_default('solarman')
|
|
|
|
def test_read_cnf2():
|
|
cnf = {'solarman' : {'enabled': 'FALSE'}}
|
|
TstConfig.set(cnf)
|
|
err = TstConfig.read('app/config/')
|
|
assert err == None
|
|
cnf = TstConfig.get()
|
|
assert cnf == {'gen3plus': {'at_acl': {'mqtt': {'allow': ['AT+'], 'block': []}, 'tsun': {'allow': ['AT+Z', 'AT+UPURL', 'AT+SUPDATE'], 'block': []}}}, 'tsun': {'enabled': True, 'host': 'logger.talent-monitoring.com', 'port': 5005}, 'solarman': {'enabled': True, 'host': 'iot.talent-monitoring.com', 'port': 10000}, 'mqtt': {'host': 'mqtt', 'port': 1883, 'user': None, 'passwd': None}, 'ha': {'auto_conf_prefix': 'homeassistant', 'discovery_prefix': 'homeassistant', 'entity_prefix': 'tsun', 'proxy_node_id': 'proxy', 'proxy_unique_id': 'P170000000000001'}, 'inverters': {'allow_all': True, 'R170000000000001': {'suggested_area': '', 'modbus_polling': False, 'monitor_sn': 0, 'node_id': ''}, 'Y170000000000001': {'modbus_polling': True, 'monitor_sn': 2000000000, 'suggested_area': '', 'node_id': ''}}}
|
|
assert True == TstConfig.is_default('solarman')
|
|
|
|
def test_read_cnf3():
|
|
cnf = {'solarman' : {'port': 'FALSE'}}
|
|
TstConfig.set(cnf)
|
|
err = TstConfig.read('app/config/')
|
|
assert err == 'Config.read: Key \'solarman\' error:\nKey \'port\' error:\nint(\'FALSE\') raised ValueError("invalid literal for int() with base 10: \'FALSE\'")'
|
|
cnf = TstConfig.get()
|
|
assert cnf == {'solarman': {'port': 'FALSE'}}
|
|
|
|
def test_read_cnf4():
|
|
cnf = {'solarman' : {'port': 5000}}
|
|
TstConfig.set(cnf)
|
|
err = TstConfig.read('app/config/')
|
|
assert err == None
|
|
cnf = TstConfig.get()
|
|
assert cnf == {'gen3plus': {'at_acl': {'mqtt': {'allow': ['AT+'], 'block': []}, 'tsun': {'allow': ['AT+Z', 'AT+UPURL', 'AT+SUPDATE'], 'block': []}}}, 'tsun': {'enabled': True, 'host': 'logger.talent-monitoring.com', 'port': 5005}, 'solarman': {'enabled': True, 'host': 'iot.talent-monitoring.com', 'port': 5000}, 'mqtt': {'host': 'mqtt', 'port': 1883, 'user': None, 'passwd': None}, 'ha': {'auto_conf_prefix': 'homeassistant', 'discovery_prefix': 'homeassistant', 'entity_prefix': 'tsun', 'proxy_node_id': 'proxy', 'proxy_unique_id': 'P170000000000001'}, 'inverters': {'allow_all': True, 'R170000000000001': {'suggested_area': '', 'modbus_polling': False, 'monitor_sn': 0, 'node_id': ''}, 'Y170000000000001': {'modbus_polling': True, 'monitor_sn': 2000000000, 'suggested_area': '', 'node_id': ''}}}
|
|
assert False == TstConfig.is_default('solarman')
|
|
|
|
def test_read_cnf5():
|
|
cnf = {'solarman' : {'port': 1023}}
|
|
TstConfig.set(cnf)
|
|
err = TstConfig.read('app/config/')
|
|
assert err != None
|
|
|
|
def test_read_cnf6():
|
|
cnf = {'solarman' : {'port': 65536}}
|
|
TstConfig.set(cnf)
|
|
err = TstConfig.read('app/config/')
|
|
assert err != None
|