def test_default_config(self): """ test the default config causes the right values to be logged """ with open( path.join(path.dirname(path.realpath(__file__)), "..", "config", "remeha.conf")) as json_file: with mock.patch('mqtt_logger.mqttClient') as mqtt_mock: mqtt_mock.Client.return_value = mqtt_mock sut = LogToMQtt(json.load(json_file), 5) TestLogToMQtt.raw_test_data[13] = 10 TestLogToMQtt.raw_test_data[14] = 0 # update checksum TestLogToMQtt.raw_test_data[71] = 0x61 TestLogToMQtt.raw_test_data[72] = 0xed sut.log(Frame(frame_data=TestLogToMQtt.raw_test_data), 0) calls = [ call('boiler/outside_temp', '0.1', retain=True), call('boiler/flow_temp', '47.7', retain=True), call('boiler/return_temp', '25.6', retain=True), call('boiler/calorifier_temp', '48.6', retain=True), call('boiler/airflow_actual', '83.1', retain=True), call('boiler/status', 'Burning CH (0s)', retain=True), call('boiler/substatus', 'Normal internal setpoint (0s)', retain=True), call('boiler/locking', 'No locking (0s)', retain=True), call('boiler/blocking', 'No Blocking (0s)', retain=True) ] mqtt_mock.publish.assert_has_calls(calls)
def test_log_single_value__scale_from_config(self): """ test case where the value to post with mqtt is increases the "not changed since" """ with mock.patch('mqtt_logger.mqttClient') as mqtt_mock: mqtt_mock.Client.return_value = mqtt_mock sut = LogToMQtt( json.loads( """{ "mqtt_logger": { "enabled": true, "host": "localhost", "port": 1883, "topic": "boiler", "log_values": ["outside_temp"], "scale_to_percent": [ { "value_name": "outside_temp", "lower_limit": 0, "upper_limit": 1 } ] }} """), 5) TestLogToMQtt.raw_test_data[13] = 10 TestLogToMQtt.raw_test_data[14] = 0 # update checksum TestLogToMQtt.raw_test_data[71] = 0x61 TestLogToMQtt.raw_test_data[72] = 0xed sut.log(Frame(frame_data=TestLogToMQtt.raw_test_data), 0) mqtt_mock.publish.assert_called_once_with('boiler/outside_temp', '10.0', retain=True)
def test_log(self): """ test case where the value to post with mqtt is a string """ with mock.patch('mqtt_logger.mqttClient') as mqtt_mock: mqtt_mock.Client.return_value = mqtt_mock sut = LogToMQtt(self.default_config, 5) sut.log(Frame(frame_data=TestLogToMQtt.raw_test_data), 0) mqtt_mock.publish.assert_called()
def test_log_single_value_when_no_config(self): """ test case where the value to post with mqtt is increases the "not changed since" """ with mock.patch('mqtt_logger.mqttClient') as mqtt_mock: mqtt_mock.Client.return_value = mqtt_mock sut = LogToMQtt(json.loads("""{}"""), 5) sut.log(Frame(frame_data=TestLogToMQtt.raw_test_data), 0) mqtt_mock.publish.assert_not_called()
def log_remeha(source_serial, destination_filename, mqtt_freq, config): ser = serial.Serial(source_serial, 9600, timeout=10, parity='N', bytesize=8, stopbits=1 ) if not ser.isOpen(): sys.exit("Could not open serial: " + source_serial) log_db = DatabaseLogger(config) log_mqtt = LogToMQtt(config, mqtt_freq, lambda message: log_db.log_manual(message)) log_file = FileLogger(config.get('file_logger'), destination_filename) clean_up_handler = atexit.register(clean_up, [log_db, log_mqtt, log_file]) sample_data_request = bytes([0x02, 0xFE, 0x01, 0x05, 0x08, 0x02, 0x01, 0x69, 0xAB, 0x03]) last_frame_was_valid = True runtime_seconds = 0 try: while True: # sys.stdout.flush() ser.write(sample_data_request) frame = Frame(io_source=ser) if frame.isValid: last_frame_was_valid = True log_file.log_data(frame) log_mqtt.log(frame, runtime_seconds) log_db.log_data(frame) while ser.inWaiting(): unknown_data = ser.read(ser.inWaiting()) print("Error unknown data: " + unknown_data.hex()) time.sleep(1) else: if not last_frame_was_valid: sys.exit("Two consecutive read errors") print("Sleep and retry") time.sleep(10) ser.close() time.sleep(10) ser.open() last_frame_was_valid = False runtime_seconds += 1 time.sleep(1) finally: log_db.close() log_file.close() atexit.unregister(clean_up_handler)