1
0

test_action.py 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. import datetime
  2. import unittest.mock
  3. import pytest
  4. import systemctl_mqtt
  5. # pylint: disable=protected-access
  6. @pytest.mark.parametrize(
  7. "delay", [datetime.timedelta(seconds=4), datetime.timedelta(hours=21)]
  8. )
  9. def test_poweroff_trigger(delay):
  10. action = systemctl_mqtt._MQTTActionSchedulePoweroff()
  11. with unittest.mock.patch(
  12. "systemctl_mqtt._dbus.schedule_shutdown"
  13. ) as schedule_shutdown_mock:
  14. action.trigger(
  15. state=systemctl_mqtt._State(
  16. mqtt_topic_prefix="systemctl/hostname",
  17. homeassistant_discovery_prefix="homeassistant",
  18. homeassistant_node_id="node",
  19. poweroff_delay=delay,
  20. )
  21. )
  22. schedule_shutdown_mock.assert_called_once_with(action="poweroff", delay=delay)
  23. @pytest.mark.parametrize(
  24. ("topic_suffix", "expected_action_arg"), [("poweroff", "poweroff")]
  25. )
  26. def test_mqtt_topic_suffix_action_mapping_poweroff(topic_suffix, expected_action_arg):
  27. mqtt_action = systemctl_mqtt._MQTT_TOPIC_SUFFIX_ACTION_MAPPING[topic_suffix]
  28. login_manager_mock = unittest.mock.MagicMock()
  29. with unittest.mock.patch(
  30. "systemctl_mqtt._dbus.get_login_manager", return_value=login_manager_mock
  31. ):
  32. mqtt_action.trigger(
  33. state=systemctl_mqtt._State(
  34. mqtt_topic_prefix="systemctl/hostname",
  35. homeassistant_discovery_prefix="homeassistant",
  36. homeassistant_node_id="node",
  37. poweroff_delay=datetime.timedelta(),
  38. )
  39. )
  40. login_manager_mock.ScheduleShutdown.assert_called_once()
  41. schedule_args, schedule_kwargs = login_manager_mock.ScheduleShutdown.call_args
  42. assert len(schedule_args) == 2
  43. assert schedule_args[0] == expected_action_arg
  44. assert not schedule_kwargs
  45. def test_mqtt_topic_suffix_action_mapping_lock():
  46. mqtt_action = systemctl_mqtt._MQTT_TOPIC_SUFFIX_ACTION_MAPPING["lock-all-sessions"]
  47. login_manager_mock = unittest.mock.MagicMock()
  48. with unittest.mock.patch(
  49. "systemctl_mqtt._dbus.get_login_manager", return_value=login_manager_mock
  50. ):
  51. mqtt_action.trigger(state="dummy")
  52. login_manager_mock.LockSessions.assert_called_once_with()