import time from unittest.mock import patch import paho.mqtt.client as mqtt import power_mqtt1 class SMBus: def __init__(self, _bus): self.storage = {} def read_byte(self, _i2c_addr): time.sleep(5) # return random.randbytes(1)[0] return self.storage.get(_i2c_addr, -1) def write_byte(self, i2c_addr, data): self.storage[i2c_addr] = data class MqttClient(mqtt.Client): def connect(self, host, *args, **kwargs): return super().connect("127.0.0.1", *args, **kwargs) # return super().connect(host, *args, **kwargs) def publish(self, topic, payload=None, qos=0, retain=False, properties=None): print(f"publish topic={topic}, data={payload}") super().publish(topic, payload, qos, retain, properties) with patch('smbus.SMBus', new=SMBus) as SMBus_mock: with patch("paho.mqtt.client.Client", new=MqttClient) as connect_mock: power_mqtt1.main()