1
0

test_action.py 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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.login_manager.get_login_manager_proxy"
  13. ), unittest.mock.patch(
  14. "systemctl_mqtt._dbus.login_manager.schedule_shutdown"
  15. ) as schedule_shutdown_mock:
  16. action.trigger(
  17. state=systemctl_mqtt._State(
  18. mqtt_topic_prefix="systemctl/hostname",
  19. homeassistant_discovery_prefix="homeassistant",
  20. homeassistant_discovery_object_id="node",
  21. poweroff_delay=delay,
  22. monitored_system_unit_names=[],
  23. )
  24. )
  25. schedule_shutdown_mock.assert_called_once_with(action="poweroff", delay=delay)
  26. @pytest.mark.parametrize(
  27. ("topic_suffix", "expected_action_arg"), [("poweroff", "poweroff")]
  28. )
  29. def test_mqtt_topic_suffix_action_mapping_poweroff(topic_suffix, expected_action_arg):
  30. mqtt_action = systemctl_mqtt._MQTT_TOPIC_SUFFIX_ACTION_MAPPING[topic_suffix]
  31. login_manager_mock = unittest.mock.MagicMock()
  32. with unittest.mock.patch(
  33. "systemctl_mqtt._dbus.login_manager.get_login_manager_proxy",
  34. return_value=login_manager_mock,
  35. ):
  36. mqtt_action.trigger(
  37. state=systemctl_mqtt._State(
  38. mqtt_topic_prefix="systemctl/hostname",
  39. homeassistant_discovery_prefix="homeassistant",
  40. homeassistant_discovery_object_id="node",
  41. poweroff_delay=datetime.timedelta(),
  42. monitored_system_unit_names=[],
  43. )
  44. )
  45. login_manager_mock.ScheduleShutdown.assert_called_once()
  46. schedule_args, schedule_kwargs = login_manager_mock.ScheduleShutdown.call_args
  47. assert not schedule_args
  48. assert schedule_kwargs.pop("action") == expected_action_arg
  49. assert abs(
  50. datetime.datetime.now() - schedule_kwargs.pop("time")
  51. ) < datetime.timedelta(seconds=2)
  52. assert not schedule_kwargs
  53. def test_mqtt_topic_suffix_action_mapping_lock():
  54. mqtt_action = systemctl_mqtt._MQTT_TOPIC_SUFFIX_ACTION_MAPPING["lock-all-sessions"]
  55. login_manager_mock = unittest.mock.MagicMock()
  56. with unittest.mock.patch(
  57. "systemctl_mqtt._dbus.login_manager.get_login_manager_proxy",
  58. return_value=login_manager_mock,
  59. ):
  60. mqtt_action.trigger(state="dummy")
  61. login_manager_mock.LockSessions.assert_called_once_with()
  62. def test_mqtt_topic_suffix_action_mapping_suspend():
  63. mqtt_action = systemctl_mqtt._MQTT_TOPIC_SUFFIX_ACTION_MAPPING["suspend"]
  64. login_manager_mock = unittest.mock.MagicMock()
  65. with unittest.mock.patch(
  66. "systemctl_mqtt._dbus.login_manager.get_login_manager_proxy",
  67. return_value=login_manager_mock,
  68. ):
  69. mqtt_action.trigger(state="dummy")
  70. login_manager_mock.Suspend.assert_called_once_with(interactive=False)
  71. def test_poweroff_str():
  72. assert (
  73. str(systemctl_mqtt._MQTTActionSchedulePoweroff())
  74. == "_MQTTActionSchedulePoweroff"
  75. )