test_mqtt.py 1.1 KB

123456789101112131415161718192021222324252627
  1. import unittest.mock
  2. import pytest
  3. import switchbot_mqtt
  4. @pytest.mark.parametrize("mqtt_host", ["mqtt-broker.local"])
  5. @pytest.mark.parametrize("mqtt_port", [1833])
  6. def test__run(mqtt_host, mqtt_port):
  7. with unittest.mock.patch(
  8. "paho.mqtt.client.Client"
  9. ) as mqtt_client_mock, unittest.mock.patch(
  10. "switchbot_mqtt._mqtt_on_message"
  11. ) as message_handler_mock:
  12. # pylint: disable=protected-access
  13. switchbot_mqtt._run(mqtt_host=mqtt_host, mqtt_port=mqtt_port)
  14. mqtt_client_mock.assert_called_once_with()
  15. mqtt_client_mock().connect.assert_called_once_with(host=mqtt_host, port=mqtt_port)
  16. mqtt_client_mock().socket().getpeername.return_value = (mqtt_host, mqtt_port)
  17. mqtt_client_mock().on_connect(mqtt_client_mock(), None, {}, 0)
  18. mqtt_client_mock().subscribe.assert_called_once_with(
  19. "homeassistant/switch/switchbot/+/set"
  20. )
  21. mqtt_client_mock().on_message(mqtt_client_mock(), None, "message")
  22. message_handler_mock.assert_called_once()
  23. mqtt_client_mock().loop_forever.assert_called_once_with()