|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""Test the app config process""" |
| 3 | + |
| 4 | +from subprocess import PIPE, Popen |
| 5 | +import json |
| 6 | + |
| 7 | +CONFIG_SERVICE_EMONCMS = 1 << 0 |
| 8 | +CONFIG_SERVICE_MQTT = 1 << 1 |
| 9 | +CONFIG_SERVICE_OHM = 1 << 2 |
| 10 | +CONFIG_SERVICE_SNTP = 1 << 3 |
| 11 | +CONFIG_MQTT_PROTOCOL = 7 << 4 |
| 12 | +CONFIG_MQTT_ALLOW_ANY_CERT = 1 << 7 |
| 13 | +CONFIG_SERVICE_TESLA = 1 << 8 |
| 14 | +CONFIG_SERVICE_DIVERT = 1 << 9 |
| 15 | +CONFIG_CHARGE_MODE = 7 << 10 |
| 16 | +CONFIG_PAUSE_USES_DISABLED = 1 << 13 |
| 17 | +CONFIG_SERVICE_OCPP = 1 << 14 |
| 18 | +CONFIG_OCPP_ACCESS_SUSPEND = 1 << 15 |
| 19 | +CONFIG_OCPP_ACCESS_ENERGIZE = 1 << 16 |
| 20 | +CONFIG_VEHICLE_RANGE_MILES = 1 << 17 |
| 21 | +CONFIG_RFID = 1 << 18 |
| 22 | +CONFIG_SERVICE_CUR_SHAPER = 1 << 19 |
| 23 | +CONFIG_MQTT_RETAINED = 1 << 20 |
| 24 | +CONFIG_FACTORY_WRITE_LOCK = 1 << 21 |
| 25 | +CONFIG_OCPP_AUTO_AUTH = 1 << 22 |
| 26 | +CONFIG_OCPP_OFFLINE_AUTH = 1 << 23 |
| 27 | +CONFIG_THREEPHASE = 1 << 24 |
| 28 | +CONFIG_WIZARD = 1 << 25 |
| 29 | +CONFIG_DEFAULT_STATE = 1 << 26 |
| 30 | + |
| 31 | +def check_config(config: bool = False, load: bool = False, commit: bool = False): |
| 32 | + command = ["./divert_sim", "--config-check"] |
| 33 | + if config: |
| 34 | + command.append("-c") |
| 35 | + command.append(json.dumps(config)) |
| 36 | + if load: |
| 37 | + command.append("--config-load") |
| 38 | + if commit: |
| 39 | + command.append("--config-commit") |
| 40 | + |
| 41 | + print(f"{' '.join(command)}") |
| 42 | + |
| 43 | + divert_process = Popen(command, stdout=PIPE, stderr=PIPE, universal_newlines=True) |
| 44 | + output = divert_process.communicate() |
| 45 | + |
| 46 | + if output[1]: |
| 47 | + print(output[1]) |
| 48 | + |
| 49 | + return json.loads(output[0]) |
| 50 | + |
| 51 | +def test_config_defaults() -> None: |
| 52 | + """Test the default config""" |
| 53 | + config = check_config() |
| 54 | + assert config["ssid"] == "" |
| 55 | + assert config["pass"] == "" |
| 56 | + assert config["ap_ssid"] == "" |
| 57 | + assert config["ap_pass"] == "" |
| 58 | + assert config["lang"] == "" |
| 59 | + assert config["www_username"] == "" |
| 60 | + assert config["www_password"] == "" |
| 61 | + assert config["hostname"] == "openevse-7856" |
| 62 | + assert config["sntp_hostname"] == "pool.ntp.org" |
| 63 | + assert config["time_zone"] == "Europe/London|GMT0BST,M3.5.0/1,M10.5.0" |
| 64 | + assert config["limit_default_type"] == "" |
| 65 | + assert config["limit_default_value"] == 0 |
| 66 | + assert config["emoncms_server"] == "https://data.openevse.com/emoncms" |
| 67 | + assert config["emoncms_node"] == "" |
| 68 | + assert config["emoncms_apikey"] == "" |
| 69 | + assert config["emoncms_fingerprint"] == "" |
| 70 | + assert config["mqtt_server"] == "emonpi" |
| 71 | + assert config["mqtt_port"] == 1883 |
| 72 | + assert config["mqtt_topic"] == "" |
| 73 | + assert config["mqtt_user"] == "emonpi" |
| 74 | + assert config["mqtt_pass"] == "emonpimqtt2016" |
| 75 | + assert config["mqtt_solar"] == "" |
| 76 | + assert config["mqtt_grid_ie"] == "emon/emonpi/power1" |
| 77 | + assert config["mqtt_vrms"] == "emon/emonpi/vrms" |
| 78 | + assert config["mqtt_live_pwr"] == "" |
| 79 | + assert config["mqtt_vehicle_soc"] == "" |
| 80 | + assert config["mqtt_vehicle_range"] == "" |
| 81 | + assert config["mqtt_vehicle_eta"] == "" |
| 82 | + assert config["mqtt_announce_topic"] == "openevse/announce/7856" |
| 83 | + assert config["ocpp_server"] == "" |
| 84 | + assert config["ocpp_chargeBoxId"] == "" |
| 85 | + assert config["ocpp_authkey"] == "" |
| 86 | + assert config["ocpp_idtag"] == "DefaultIdTag" |
| 87 | + assert config["ohm"] == "" |
| 88 | + assert config["divert_type"] == -1 |
| 89 | + assert config["divert_PV_ratio"] == 1.1 |
| 90 | + assert config["divert_attack_smoothing_time"] == 20 |
| 91 | + assert config["divert_decay_smoothing_time"] == 600 |
| 92 | + assert config["divert_min_charge_time"] == 600 |
| 93 | + assert config["current_shaper_max_pwr"] == 0 |
| 94 | + assert config["current_shaper_smoothing_time"] == 60 |
| 95 | + assert config["current_shaper_min_pause_time"] == 300 |
| 96 | + assert config["current_shaper_data_maxinterval"] == 120 |
| 97 | + assert config["vehicle_data_src"] == 0 |
| 98 | + assert config["tesla_access_token"] == "" |
| 99 | + assert config["tesla_refresh_token"] == "" |
| 100 | + #assert config["tesla_created_at"] == 18446744073709552000 |
| 101 | + #assert config["tesla_expires_in"] == 18446744073709552000 |
| 102 | + assert config["tesla_vehicle_id"] == "" |
| 103 | + assert config["rfid_storage"] == "" |
| 104 | + assert config["scheduler_start_window"] == 600 |
| 105 | + assert config["flags"] == 79691784 |
| 106 | + assert config["flags_changed"] == 0 |
| 107 | + assert config["emoncms_enabled"] == False |
| 108 | + assert config["mqtt_enabled"] == False |
| 109 | + assert config["mqtt_reject_unauthorized"] == True |
| 110 | + assert config["mqtt_retained"] == False |
| 111 | + assert config["ohm_enabled"] == False |
| 112 | + assert config["sntp_enabled"] == True |
| 113 | + assert config["tesla_enabled"] == False |
| 114 | + assert config["divert_enabled"] == False |
| 115 | + assert config["current_shaper_enabled"] == False |
| 116 | + assert config["pause_uses_disabled"] == False |
| 117 | + assert config["mqtt_vehicle_range_miles"] == False |
| 118 | + assert config["ocpp_enabled"] == False |
| 119 | + assert config["ocpp_auth_auto"] == True |
| 120 | + assert config["ocpp_auth_offline"] == True |
| 121 | + assert config["ocpp_suspend_evse"] == False |
| 122 | + assert config["ocpp_energize_plug"] == False |
| 123 | + assert config["rfid_enabled"] == False |
| 124 | + assert config["factory_write_lock"] == False |
| 125 | + assert config["is_threephase"] == False |
| 126 | + assert config["wizard_passed"] == False |
| 127 | + assert config["default_state"] == True |
| 128 | + assert config["mqtt_protocol"] == "mqtt" |
| 129 | + assert config["charge_mode"] == "fast" |
| 130 | + |
| 131 | +def test_flags_changed_bits() -> None: |
| 132 | + """Test the flags changed bits are set correctly""" |
| 133 | + config = check_config({ |
| 134 | + "mqtt_enabled": "true" |
| 135 | + }) |
| 136 | + assert config["flags_changed"] == CONFIG_SERVICE_MQTT |
| 137 | + |
| 138 | +def test_saving_and_loading() -> None: |
| 139 | + """Test the config is saved and loaded correctly""" |
| 140 | + check_config({ |
| 141 | + "mqtt_enabled": "true" |
| 142 | + }, commit=True) |
| 143 | + config = check_config(load=True) |
| 144 | + assert config["mqtt_enabled"] == True |
| 145 | + |
| 146 | +def test_flags_changed_bits() -> None: |
| 147 | + """Test the flags changed bits are set correctly""" |
| 148 | + config = check_config({ |
| 149 | + "mqtt_enabled": "true" |
| 150 | + }) |
| 151 | + assert config["flags_changed"] == CONFIG_SERVICE_MQTT |
| 152 | + |
| 153 | +def test_default_charging_mode() -> None: |
| 154 | + """Test the default chage mode is set correctly""" |
| 155 | + |
| 156 | + # When initially added the default charge mode was set to false (disabled) |
| 157 | + # This test ensures that the default is now set to true (active) and that |
| 158 | + # the option is still able to be set to false (disabled) |
| 159 | + |
| 160 | + check_config(commit=True) |
| 161 | + config = check_config(load=True) |
| 162 | + assert config["default_state"] == True |
| 163 | + |
| 164 | + # Check the previous defaults (that were set to false) result in true |
| 165 | + config = check_config({ |
| 166 | + "flags": CONFIG_SERVICE_SNTP | |
| 167 | + CONFIG_OCPP_AUTO_AUTH | |
| 168 | + CONFIG_OCPP_OFFLINE_AUTH |
| 169 | + }, commit=True) |
| 170 | + assert config["default_state"] == False |
| 171 | + assert config["flags_changed"] == CONFIG_FACTORY_WRITE_LOCK |
| 172 | + |
| 173 | + config = check_config(load=True) |
| 174 | + assert config["default_state"] == True |
| 175 | + assert config["flags_changed"] == CONFIG_FACTORY_WRITE_LOCK |
| 176 | + |
| 177 | + # Check setting the default state to false works |
| 178 | + config = check_config({ |
| 179 | + "default_state": False |
| 180 | + }, commit=True) |
| 181 | + assert config["default_state"] == False |
| 182 | + assert config["flags_changed"] == CONFIG_FACTORY_WRITE_LOCK | CONFIG_DEFAULT_STATE |
| 183 | + |
| 184 | + config = check_config(load=True) |
| 185 | + assert config["default_state"] == False |
| 186 | + assert config["flags_changed"] == CONFIG_FACTORY_WRITE_LOCK | CONFIG_DEFAULT_STATE |
| 187 | + |
| 188 | + # Check setting the default state to true works |
| 189 | + config = check_config({ |
| 190 | + "default_state": True |
| 191 | + }, commit=True, load=True) |
| 192 | + assert config["default_state"] == True |
| 193 | + assert config["flags_changed"] == CONFIG_FACTORY_WRITE_LOCK | CONFIG_DEFAULT_STATE |
| 194 | + |
| 195 | + config = check_config(load=True) |
| 196 | + assert config["default_state"] == True |
| 197 | + assert config["flags_changed"] == CONFIG_FACTORY_WRITE_LOCK | CONFIG_DEFAULT_STATE |
| 198 | + |
| 199 | + |
| 200 | +def test_wizard_flag(): |
| 201 | + """Test the wizard flag is set correctly""" |
| 202 | + |
| 203 | + config = check_config({ |
| 204 | + "wizard_passed": True |
| 205 | + }, commit=True) |
| 206 | + assert config["wizard_passed"] == True |
| 207 | + assert config["flags_changed"] == CONFIG_FACTORY_WRITE_LOCK | CONFIG_WIZARD |
| 208 | + |
| 209 | +def test_three_phase_flag(): |
| 210 | + """Test the three phase flag is set correctly""" |
| 211 | + |
| 212 | + config = check_config({ |
| 213 | + "is_threephase": True |
| 214 | + }) |
| 215 | + assert config["is_threephase"] == True |
| 216 | + assert config["flags_changed"] == CONFIG_THREEPHASE |
| 217 | + |
| 218 | + config = check_config({ |
| 219 | + "is_threephase": True |
| 220 | + }, commit=True) |
| 221 | + assert config["is_threephase"] == True |
| 222 | + assert config["flags_changed"] == CONFIG_FACTORY_WRITE_LOCK | CONFIG_THREEPHASE |
0 commit comments