test_infrared.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. """Tests for the infrared entity."""
  2. import pytest
  3. from infrared_protocols.commands import NECCommand
  4. from pytest_homeassistant_custom_component.common import MockConfigEntry
  5. from custom_components.tuya_local.const import (
  6. CONF_DEVICE_ID,
  7. CONF_PROTOCOL_VERSION,
  8. CONF_TYPE,
  9. DOMAIN,
  10. )
  11. from custom_components.tuya_local.helpers.device_config import TuyaEntityConfig
  12. from custom_components.tuya_local.infrared import TuyaLocalInfrared, async_setup_entry
  13. from .helpers import assert_device_properties_set, mock_device
  14. @pytest.mark.asyncio
  15. async def test_init_entry(hass, mocker):
  16. """Test the initialisation."""
  17. entry = MockConfigEntry(
  18. domain=DOMAIN,
  19. data={
  20. CONF_TYPE: "ir_remote_sensors",
  21. CONF_DEVICE_ID: "dummy",
  22. CONF_PROTOCOL_VERSION: "auto",
  23. },
  24. )
  25. # although async, the async_add_entities function passed to
  26. # async_setup_entry is called truly asynchronously. If we use
  27. # AsyncMock, it expects us to await the result.
  28. m_add_entities = mocker.Mock()
  29. m_device = mocker.AsyncMock()
  30. hass.data[DOMAIN] = {}
  31. hass.data[DOMAIN]["dummy"] = {}
  32. hass.data[DOMAIN]["dummy"]["device"] = m_device
  33. await async_setup_entry(hass, entry, m_add_entities)
  34. assert type(hass.data[DOMAIN]["dummy"]["infrared"]) is TuyaLocalInfrared
  35. m_add_entities.assert_called_once()
  36. @pytest.mark.asyncio
  37. async def test_init_entry_fails_if_device_has_no_infrared(hass, mocker):
  38. """Test initialisation when device has no matching entity"""
  39. entry = MockConfigEntry(
  40. domain=DOMAIN,
  41. data={
  42. CONF_TYPE: "smartplugv1",
  43. CONF_DEVICE_ID: "dummy",
  44. CONF_PROTOCOL_VERSION: "auto",
  45. },
  46. )
  47. # although async, the async_add_entities function passed to
  48. # async_setup_entry is called truly asynchronously. If we use
  49. # AsyncMock, it expects us to await the result.
  50. m_add_entities = mocker.Mock()
  51. m_device = mocker.AsyncMock()
  52. hass.data[DOMAIN] = {}
  53. hass.data[DOMAIN]["dummy"] = {}
  54. hass.data[DOMAIN]["dummy"]["device"] = m_device
  55. try:
  56. await async_setup_entry(hass, entry, m_add_entities)
  57. assert False
  58. except ValueError:
  59. pass
  60. m_add_entities.assert_not_called()
  61. @pytest.mark.asyncio
  62. async def test_init_entry_fails_if_config_is_missing(hass, mocker):
  63. """Test initialisation when device has no matching entity"""
  64. entry = MockConfigEntry(
  65. domain=DOMAIN,
  66. data={
  67. CONF_TYPE: "non_existing",
  68. CONF_DEVICE_ID: "dummy",
  69. CONF_PROTOCOL_VERSION: "auto",
  70. },
  71. )
  72. # although async, the async_add_entities function passed to
  73. # async_setup_entry is called truly asynchronously. If we use
  74. # AsyncMock, it expects us to await the result.
  75. m_add_entities = mocker.Mock()
  76. m_device = mocker.AsyncMock()
  77. hass.data[DOMAIN] = {}
  78. hass.data[DOMAIN]["dummy"] = {}
  79. hass.data[DOMAIN]["dummy"]["device"] = m_device
  80. try:
  81. await async_setup_entry(hass, entry, m_add_entities)
  82. assert False
  83. except ValueError:
  84. pass
  85. m_add_entities.assert_not_called()
  86. @pytest.mark.asyncio
  87. async def test_async_send_command(mocker):
  88. """Test that infrared encodes commands as expected."""
  89. config = {
  90. "entity": "infrared",
  91. "dps": [
  92. {
  93. "id": "201",
  94. "name": "send",
  95. "type": "base64",
  96. }
  97. ],
  98. }
  99. tuyadevice = mocker.MagicMock()
  100. dps = {"201": ""}
  101. device = mock_device(dps, mocker)
  102. infrared = TuyaLocalInfrared(
  103. device,
  104. TuyaEntityConfig(tuyadevice, config),
  105. )
  106. async with assert_device_properties_set(
  107. device,
  108. {
  109. "201": (
  110. "{'control': 'send_ir', 'type': 0, 'head': '', 'key1': '1KCOUETICMgIyAjI"
  111. "CMgKXBjICMgIyAjICMgIyAjICMgIyApcGMgIyAjIClwYyAjICMgIyAjIClwYyAjICMgKXBj"
  112. "ICMgIyAjICMgKXBjICMgIyAjICMgKXBjIClwYyAjICMgIyAjIClwYyAjICMgKXBjIClwYyA"
  113. "jICMgIyAjIClwYyApcGMgIAAA=='}"
  114. )
  115. },
  116. ):
  117. await infrared.async_send_command(
  118. NECCommand(address=0x5284, command=0x32, modulation=38000)
  119. )