# Path: test/settings/mqttkit.py
# PROCESS_DELAY_MQTT = 0.3
# PROCESS_DELAY_HTTP = 0.3
# class TestSettings:
# 
# Path: test/util.py
# def mqtt_json_sensor(topic, data):
#     payload = json.dumps(data)
#     return mqtt_sensor(topic, payload)
# 
# def sleep(secs):
#     # https://gist.github.com/jhorman/891717
#     return deferLater(reactor, secs, lambda: None)


import logging
import pytest
import pytest_twisted
from twisted.internet import threads
from test.settings.mqttkit import settings, influx_sensors, PROCESS_DELAY_MQTT
from test.util import mqtt_json_sensor, sleep
# -*- coding: utf-8 -*-
# (c) 2020-2021 Andreas Motl <andreas@getkotori.org>



logger = logging.getLogger(__name__)


@pytest_twisted.inlineCallbacks
@pytest.mark.mqtt
@pytest.mark.homie
def test_mqtt_homie(machinery, create_influxdb, reset_influxdb):
    """
    Publish reading like a Homie device in JSON format to MQTT broker
    and proof it is stored in the InfluxDB database.
    """

    # Submit a single measurement, without timestamp.
    data = {
        'temperature': 42.84,
        'humidity': 83.1,
    }
    yield threads.deferToThread(mqtt_json_sensor, settings.mqtt_topic_homie, data)

    # Wait for some time to process the message.

# Path: test/settings/mqttkit.py
# PROCESS_DELAY_MQTT = 0.3
# PROCESS_DELAY_HTTP = 0.3
# class TestSettings:
# 
# Path: test/util.py
# def mqtt_json_sensor(topic, data):
#     payload = json.dumps(data)
#     return mqtt_sensor(topic, payload)
# 
# def sleep(secs):
#     # https://gist.github.com/jhorman/891717
#     return deferLater(reactor, secs, lambda: None)


# Path: test/test_homie.py
import logging
import pytest
import pytest_twisted
from twisted.internet import threads
from test.settings.mqttkit import settings, influx_sensors, PROCESS_DELAY_MQTT
from test.util import mqtt_json_sensor, sleep

# -*- coding: utf-8 -*-
# (c) 2020-2021 Andreas Motl <andreas@getkotori.org>



logger = logging.getLogger(__name__)


@pytest_twisted.inlineCallbacks
@pytest.mark.mqtt
@pytest.mark.homie
def test_mqtt_homie(machinery, create_influxdb, reset_influxdb):
    """
    Publish reading like a Homie device in JSON format to MQTT broker
    and proof it is stored in the InfluxDB database.
    """

    # Submit a single measurement, without timestamp.
    data = {
        'temperature': 42.84,
        'humidity': 83.1,
    }
    yield threads.deferToThread(mqtt_json_sensor, settings.mqtt_topic_homie, data)

    # Wait for some time to process the message.

    yield sleep(PROCESS_DELAY_MQTT)