test_number.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. """Tests for the number entity."""
  2. import pytest
  3. from pytest_homeassistant_custom_component.common import MockConfigEntry
  4. from custom_components.tuya_local.const import (
  5. CONF_DEVICE_ID,
  6. CONF_PROTOCOL_VERSION,
  7. CONF_TYPE,
  8. DOMAIN,
  9. )
  10. from custom_components.tuya_local.helpers.device_config import TuyaEntityConfig
  11. from custom_components.tuya_local.number import TuyaLocalNumber, async_setup_entry
  12. from .helpers import assert_device_properties_set, mock_device
  13. @pytest.mark.asyncio
  14. async def test_init_entry(hass, mocker):
  15. """Test the initialisation."""
  16. entry = MockConfigEntry(
  17. domain=DOMAIN,
  18. data={
  19. CONF_TYPE: "anko_fan",
  20. CONF_DEVICE_ID: "dummy",
  21. CONF_PROTOCOL_VERSION: "auto",
  22. },
  23. )
  24. m_add_entities = mocker.Mock()
  25. m_device = mocker.AsyncMock()
  26. hass.data[DOMAIN] = {
  27. "dummy": {"device": m_device},
  28. }
  29. await async_setup_entry(hass, entry, m_add_entities)
  30. assert type(hass.data[DOMAIN]["dummy"]["number_timer"]) is TuyaLocalNumber
  31. m_add_entities.assert_called_once()
  32. @pytest.mark.asyncio
  33. async def test_init_entry_fails_if_device_has_no_number(hass, mocker):
  34. """Test initialisation when device has no matching entity"""
  35. entry = MockConfigEntry(
  36. domain=DOMAIN,
  37. data={
  38. CONF_TYPE: "simple_switch",
  39. CONF_DEVICE_ID: "dummy",
  40. CONF_PROTOCOL_VERSION: "auto",
  41. },
  42. )
  43. m_add_entities = mocker.Mock()
  44. m_device = mocker.AsyncMock()
  45. hass.data[DOMAIN] = {
  46. "dummy": {"device": m_device},
  47. }
  48. try:
  49. await async_setup_entry(hass, entry, m_add_entities)
  50. assert False
  51. except ValueError:
  52. pass
  53. m_add_entities.assert_not_called()
  54. @pytest.mark.asyncio
  55. async def test_init_entry_fails_if_config_is_missing(hass, mocker):
  56. """Test initialisation when device has no matching entity"""
  57. entry = MockConfigEntry(
  58. domain=DOMAIN,
  59. data={
  60. CONF_TYPE: "non_existing",
  61. CONF_DEVICE_ID: "dummy",
  62. CONF_PROTOCOL_VERSION: "auto",
  63. },
  64. )
  65. m_add_entities = mocker.Mock()
  66. m_device = mocker.AsyncMock()
  67. hass.data[DOMAIN] = {
  68. "dummy": {"device": m_device},
  69. }
  70. try:
  71. await async_setup_entry(hass, entry, m_add_entities)
  72. assert False
  73. except ValueError:
  74. pass
  75. m_add_entities.assert_not_called()
  76. def test_decimal(mocker):
  77. """Test the decimal property."""
  78. config = {
  79. "entity": "number",
  80. "dps": [
  81. {
  82. "id": "1",
  83. "type": "integer",
  84. "name": "value",
  85. "range": {
  86. "min": 0,
  87. "max": 100,
  88. },
  89. },
  90. {
  91. "id": "2",
  92. "type": "integer",
  93. "name": "decimal",
  94. "mapping": [
  95. {
  96. "scale": 10,
  97. }
  98. ],
  99. "range": {
  100. "min": 0,
  101. "max": 9,
  102. },
  103. },
  104. ],
  105. }
  106. tuyadevice = mocker.AsyncMock()
  107. dps = {"1": 12, "2": 3}
  108. device = mock_device(dps, mocker)
  109. number = TuyaLocalNumber(device, TuyaEntityConfig(tuyadevice, config))
  110. assert number.native_value == 12.3
  111. @pytest.mark.asyncio
  112. async def test_set_decimal(mocker):
  113. """Test the decimal property."""
  114. config = {
  115. "entity": "number",
  116. "dps": [
  117. {
  118. "id": "1",
  119. "type": "integer",
  120. "name": "value",
  121. "range": {
  122. "min": 0,
  123. "max": 100,
  124. },
  125. },
  126. {
  127. "id": "2",
  128. "type": "integer",
  129. "name": "decimal",
  130. "mapping": [
  131. {
  132. "scale": 10,
  133. }
  134. ],
  135. "range": {
  136. "min": 0,
  137. "max": 9,
  138. },
  139. },
  140. ],
  141. }
  142. tuyadevice = mocker.AsyncMock()
  143. dps = {"1": 1, "2": 1}
  144. device = mock_device(dps, mocker)
  145. number = TuyaLocalNumber(device, TuyaEntityConfig(tuyadevice, config))
  146. async with assert_device_properties_set(
  147. device,
  148. {"1": 10, "2": 5},
  149. ):
  150. await number.async_set_native_value(10.5)