test_valve.py 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. """Tests for the valve entity"""
  2. from unittest.mock import AsyncMock, Mock
  3. import pytest
  4. from homeassistant.components.valve import (
  5. ValveEntityFeature,
  6. ValveState,
  7. )
  8. from homeassistant.components.valve.const import ValveEntityStateAttribute
  9. from pytest_homeassistant_custom_component.common import MockConfigEntry
  10. from custom_components.tuya_local.const import (
  11. CONF_DEVICE_ID,
  12. CONF_PROTOCOL_VERSION,
  13. CONF_TYPE,
  14. DOMAIN,
  15. )
  16. from custom_components.tuya_local.helpers.device_config import get_config
  17. from custom_components.tuya_local.valve import TuyaLocalValve, async_setup_entry
  18. from .helpers import assert_device_properties_set, mock_device
  19. FRANKEVER_DPS = {
  20. "1": True,
  21. "9": 0,
  22. "38": "memory",
  23. "101": 100,
  24. "102": 100,
  25. }
  26. def _make_frankever_valve(mocker, dps=None):
  27. """Create a valve using the FrankEver position-controlled profile."""
  28. config = get_config("frankever_watervalve")
  29. entity_config = next(
  30. entity for entity in config.all_entities() if entity.entity == "valve"
  31. )
  32. device = mock_device(dps or FRANKEVER_DPS, mocker)
  33. return TuyaLocalValve(device, entity_config), device
  34. @pytest.mark.asyncio
  35. async def test_init_entry(hass):
  36. """Test initialisation"""
  37. entry = MockConfigEntry(
  38. domain=DOMAIN,
  39. data={
  40. CONF_TYPE: "ble_water_valve",
  41. CONF_DEVICE_ID: "dummy",
  42. CONF_PROTOCOL_VERSION: "auto",
  43. },
  44. )
  45. m_add_entities = Mock()
  46. m_device = AsyncMock()
  47. hass.data[DOMAIN] = {
  48. "dummy": {
  49. "device": m_device,
  50. },
  51. }
  52. await async_setup_entry(hass, entry, m_add_entities)
  53. assert type(hass.data[DOMAIN]["dummy"]["valve_water"]) is TuyaLocalValve
  54. m_add_entities.assert_called_once()
  55. @pytest.mark.asyncio
  56. async def test_init_entry_fails_if_device_has_no_valve(hass):
  57. """Test initialisation when device has no matching entity"""
  58. entry = MockConfigEntry(
  59. domain=DOMAIN,
  60. data={
  61. CONF_TYPE: "kogan_heater",
  62. CONF_DEVICE_ID: "dummy",
  63. CONF_PROTOCOL_VERSION: "auto",
  64. },
  65. )
  66. m_add_entities = Mock()
  67. m_device = AsyncMock()
  68. hass.data[DOMAIN] = {
  69. "dummy": {
  70. "device": m_device,
  71. },
  72. }
  73. try:
  74. await async_setup_entry(hass, entry, m_add_entities)
  75. assert False
  76. except ValueError:
  77. pass
  78. m_add_entities.assert_not_called()
  79. @pytest.mark.parametrize(
  80. ("position", "expected_state"),
  81. [
  82. (0, ValveState.CLOSED),
  83. (20, ValveState.OPEN),
  84. (100, ValveState.OPEN),
  85. ],
  86. )
  87. def test_separate_current_position(mocker, position, expected_state):
  88. """Current position is reported by the read-only feedback DP."""
  89. dps = {**FRANKEVER_DPS, "101": position, "102": position}
  90. valve, _ = _make_frankever_valve(mocker, dps)
  91. assert valve.current_valve_position == position
  92. assert valve.is_closed is (position == 0)
  93. assert valve.state == expected_state
  94. assert valve.state_attributes == {
  95. ValveEntityStateAttribute.IS_CLOSED: position == 0,
  96. ValveEntityStateAttribute.CURRENT_POSITION: position,
  97. }
  98. assert valve.supported_features == (
  99. ValveEntityFeature.OPEN
  100. | ValveEntityFeature.CLOSE
  101. | ValveEntityFeature.SET_POSITION
  102. )
  103. @pytest.mark.asyncio
  104. async def test_set_position_writes_target_only(mocker):
  105. """Setting a target position does not change the separate switch."""
  106. valve, device = _make_frankever_valve(mocker)
  107. async with assert_device_properties_set(device, {"101": 50}):
  108. await valve.async_set_valve_position(50)
  109. @pytest.mark.asyncio
  110. async def test_set_zero_writes_target_only(mocker):
  111. """Setting a zero target position does not change the separate switch."""
  112. valve, device = _make_frankever_valve(mocker)
  113. async with assert_device_properties_set(device, {"101": 0}):
  114. await valve.async_set_valve_position(0)
  115. @pytest.mark.asyncio
  116. async def test_open_uses_switch(mocker):
  117. """Opening uses the switch without changing a non-zero target position."""
  118. valve, device = _make_frankever_valve(mocker)
  119. async with assert_device_properties_set(device, {"1": True}):
  120. await valve.async_open_valve()
  121. @pytest.mark.asyncio
  122. async def test_close_uses_switch(mocker):
  123. """Closing uses the switch without changing the target position."""
  124. valve, device = _make_frankever_valve(mocker)
  125. async with assert_device_properties_set(device, {"1": False}):
  126. await valve.async_close_valve()
  127. def test_frankever_product_match_is_preferred():
  128. """The explicit product match is stronger than the Tellur DPS match."""
  129. frankever = get_config("frankever_watervalve")
  130. tellur = get_config("tellur_tll331501_watervalve")
  131. product_ids = ["nzx0kku9d6eq59nt"]
  132. assert frankever.matches_product(product_ids[0])
  133. assert frankever.match_quality(FRANKEVER_DPS, product_ids) == 101
  134. assert tellur.match_quality(FRANKEVER_DPS) == 100
  135. def test_frankever_auxiliary_entity_values(mocker):
  136. """Countdown and initial-state mappings retain their observed values."""
  137. config = get_config("frankever_watervalve")
  138. entities = {entity.entity: entity for entity in config.all_entities()}
  139. device = mock_device(FRANKEVER_DPS, mocker)
  140. assert entities["time"].find_dps("second").get_value(device) == 0
  141. initial_state = entities["select"].find_dps("option")
  142. assert initial_state.get_value(device) == "memory"
  143. assert initial_state.values(device) == ["off", "on", "memory"]
  144. @pytest.mark.asyncio
  145. async def test_init_entry_fails_if_config_is_missing(hass):
  146. """Test initialisation when device has no matching entity"""
  147. entry = MockConfigEntry(
  148. domain=DOMAIN,
  149. data={
  150. CONF_TYPE: "non_existing",
  151. CONF_DEVICE_ID: "dummy",
  152. CONF_PROTOCOL_VERSION: "auto",
  153. },
  154. )
  155. # although async, the async_add_entities function passed to
  156. # async_setup_entry is called truly asynchronously. If we use
  157. # AsyncMock, it expects us to await the result.
  158. m_add_entities = Mock()
  159. m_device = AsyncMock()
  160. hass.data[DOMAIN] = {}
  161. hass.data[DOMAIN]["dummy"] = {}
  162. hass.data[DOMAIN]["dummy"]["device"] = m_device
  163. try:
  164. await async_setup_entry(hass, entry, m_add_entities)
  165. assert False
  166. except ValueError:
  167. pass
  168. m_add_entities.assert_not_called()