test_action.py 3.0 KB

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