예제 #1
0
    def test_log_single_value(self):
        with mock.patch('mqtt_logger.mqttClient') as mqtt_mock:
            mqtt_mock.Client.return_value = mqtt_mock
            sut = LogToMQtt(self.default_config, 5)

            sut.log_single_value('outside_temp', TestLogToMQtt.unpacked_test_data)
            mqtt_mock.publish.assert_called_once_with('boiler/outside_temp', '0.1', retain=True)
예제 #2
0
    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)
예제 #3
0
    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)
예제 #4
0
    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()
예제 #5
0
    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()
예제 #6
0
    def test_log_single_value__with_last_change_time_string(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_single_value('status', TestLogToMQtt.unpacked_test_data, datetime.datetime.now())
            mqtt_mock.publish.assert_called_once_with('boiler/status', 'Standby (0s)', retain=True)
예제 #7
0
    def test_log_single_value__with_last_change_time(self):
        """
        test a integer value with info of last change
        """
        with mock.patch('mqtt_logger.mqttClient') as mqtt_mock:
            mqtt_mock.Client.return_value = mqtt_mock
            sut = LogToMQtt(self.default_config, 5)

            sut.log_single_value('outside_temp', TestLogToMQtt.unpacked_test_data, datetime.datetime.now())
            mqtt_mock.publish.assert_called_once_with('boiler/outside_temp', '0.1 (0s)', retain=True)
예제 #8
0
    def test_connect_to_config_server(self):
        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": "myhost", "port": 1234, "topic": "boiler/",
                  "log_values": ["test_value"],
                  "Log_values_with_duration": []}}
                """), 5)

            sut.log_single_value('outside_temp', TestLogToMQtt.unpacked_test_data)
            mqtt_mock.connect.assert_called_once_with(host="myhost", port=1234)
예제 #9
0
    def test_log_duration_of_value_start(self):
        """
        test case where
        """
        with mock.patch('mqtt_logger.mqttClient') as mqtt_mock:
            mqtt_mock.Client.return_value = mqtt_mock
            sut = LogToMQtt(self.default_config, 5)

            current_date = datetime.datetime.now()
            sut.log_duration_of_value('status', 'Standby', TestLogToMQtt.unpacked_test_data, current_date)
            mqtt_mock.publish.assert_not_called()
예제 #10
0
    def test_log_single_value__scale(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(self.default_config, 5)

            TestLogToMQtt.unpacked_test_data[3] = 10  # 0.1
            sut.log_single_value('outside_temp', TestLogToMQtt.unpacked_test_data, scale_to_percent=[0, 1])
            mqtt_mock.publish.assert_called_once_with('boiler/outside_temp', '10.0', retain=True)
예제 #11
0
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)
예제 #12
0
    def test_log_duration_of_value_only_updates_after_a_full_cycle(self):
        """
        test case where
        """
        with mock.patch('mqtt_logger.mqttClient') as mqtt_mock:
            mqtt_mock.Client.return_value = mqtt_mock
            sut = LogToMQtt(self.default_config, 5)

            current_date = datetime.datetime.now()
            update_date_1 = current_date + datetime.timedelta(seconds=1)
            update_date_2 = update_date_1 + datetime.timedelta(seconds=1)
            # Set status to burning_dhw
            TestLogToMQtt.unpacked_test_data[26] = 4
            sut.log_duration_of_value('status', 'burning_dhw',
                                      TestLogToMQtt.unpacked_test_data,
                                      current_date)
            sut.log_duration_of_value('status', 'burning_dhw',
                                      TestLogToMQtt.unpacked_test_data,
                                      update_date_1)
            TestLogToMQtt.unpacked_test_data[26] = 0
            sut.log_duration_of_value('status', 'burning_dhw',
                                      TestLogToMQtt.unpacked_test_data,
                                      update_date_2)
            mqtt_mock.publish.assert_called_once_with(
                'boiler/status_burning_dhw_duration', '2s', retain=True)
예제 #13
0
    def test_log_single_value__with_last_change_time_string__long_updates_time(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(self.default_config, 5)

            current_date = datetime.datetime.now()
            update_date = current_date + datetime.timedelta(hours=10)
            sut.log_single_value('status', TestLogToMQtt.unpacked_test_data, current_date)
            mqtt_mock.publish.assert_called_once_with('boiler/status', 'Standby (0s)', retain=True)
            mqtt_mock.reset_mock()
            sut.log_single_value('status', TestLogToMQtt.unpacked_test_data, update_date)
            mqtt_mock.publish.assert_called_once_with('boiler/status', 'Standby (10h)', retain=True)