test_eurom_600_heater.py 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. from unittest import IsolatedAsyncioTestCase, skip
  2. from unittest.mock import AsyncMock, patch
  3. from homeassistant.components.climate.const import (
  4. HVAC_MODE_HEAT,
  5. HVAC_MODE_OFF,
  6. SUPPORT_PRESET_MODE,
  7. SUPPORT_TARGET_TEMPERATURE,
  8. )
  9. from homeassistant.const import STATE_UNAVAILABLE
  10. from custom_components.tuya_local.generic.climate import TuyaLocalClimate
  11. from custom_components.tuya_local.helpers.device_config import TuyaDeviceConfig
  12. from ..const import EUROM_600_HEATER_PAYLOAD
  13. from ..helpers import assert_device_properties_set
  14. HVACMODE_DPS = "1"
  15. TEMPERATURE_DPS = "2"
  16. CURRENTTEMP_DPS = "5"
  17. ERROR_DPS = "6"
  18. class TestEurom600Heater(IsolatedAsyncioTestCase):
  19. def setUp(self):
  20. device_patcher = patch("custom_components.tuya_local.device.TuyaLocalDevice")
  21. self.addCleanup(device_patcher.stop)
  22. self.mock_device = device_patcher.start()
  23. cfg = TuyaDeviceConfig("eurom_600_heater.yaml")
  24. climate = cfg.primary_entity
  25. self.climate_name = climate.name
  26. self.subject = TuyaLocalClimate(self.mock_device, climate)
  27. self.dps = EUROM_600_HEATER_PAYLOAD.copy()
  28. self.subject._device.get_property.side_effect = lambda id: self.dps[id]
  29. def test_supported_features(self):
  30. self.assertEqual(
  31. self.subject.supported_features,
  32. SUPPORT_TARGET_TEMPERATURE,
  33. )
  34. def test_should_poll(self):
  35. self.assertTrue(self.subject.should_poll)
  36. def test_name_returns_device_name(self):
  37. self.assertEqual(self.subject.name, self.subject._device.name)
  38. def test_unique_id_returns_device_unique_id(self):
  39. self.assertEqual(self.subject.unique_id, self.subject._device.unique_id)
  40. def test_device_info_returns_device_info_from_device(self):
  41. self.assertEqual(self.subject.device_info, self.subject._device.device_info)
  42. def test_icon(self):
  43. self.dps[HVACMODE_DPS] = True
  44. self.assertEqual(self.subject.icon, "mdi:radiator")
  45. self.dps[HVACMODE_DPS] = False
  46. self.assertEqual(self.subject.icon, "mdi:radiator-disabled")
  47. def test_temperature_unit_returns_device_temperature_unit(self):
  48. self.assertEqual(
  49. self.subject.temperature_unit, self.subject._device.temperature_unit
  50. )
  51. def test_target_temperature(self):
  52. self.dps[TEMPERATURE_DPS] = 25
  53. self.assertEqual(self.subject.target_temperature, 25)
  54. def test_target_temperature_step(self):
  55. self.assertEqual(self.subject.target_temperature_step, 1)
  56. def test_minimum_target_temperature(self):
  57. self.assertEqual(self.subject.min_temp, 15)
  58. def test_maximum_target_temperature(self):
  59. self.assertEqual(self.subject.max_temp, 35)
  60. async def test_legacy_set_temperature_with_temperature(self):
  61. async with assert_device_properties_set(
  62. self.subject._device, {TEMPERATURE_DPS: 24}
  63. ):
  64. await self.subject.async_set_temperature(temperature=24)
  65. async def test_legacy_set_temperature_with_no_valid_properties(self):
  66. await self.subject.async_set_temperature(something="else")
  67. self.subject._device.async_set_property.assert_not_called
  68. async def test_set_target_temperature_succeeds_within_valid_range(self):
  69. async with assert_device_properties_set(
  70. self.subject._device,
  71. {TEMPERATURE_DPS: 25},
  72. ):
  73. await self.subject.async_set_target_temperature(25)
  74. async def test_set_target_temperature_rounds_value_to_closest_integer(self):
  75. async with assert_device_properties_set(
  76. self.subject._device, {TEMPERATURE_DPS: 23}
  77. ):
  78. await self.subject.async_set_target_temperature(22.6)
  79. async def test_set_target_temperature_fails_outside_valid_range(self):
  80. with self.assertRaisesRegex(
  81. ValueError, "temperature \\(14\\) must be between 15 and 35"
  82. ):
  83. await self.subject.async_set_target_temperature(14)
  84. with self.assertRaisesRegex(
  85. ValueError, "temperature \\(36\\) must be between 15 and 35"
  86. ):
  87. await self.subject.async_set_target_temperature(36)
  88. def test_current_temperature(self):
  89. self.dps[CURRENTTEMP_DPS] = 25
  90. self.assertEqual(self.subject.current_temperature, 25)
  91. def test_hvac_mode(self):
  92. self.dps[HVACMODE_DPS] = True
  93. self.assertEqual(self.subject.hvac_mode, HVAC_MODE_HEAT)
  94. self.dps[HVACMODE_DPS] = False
  95. self.assertEqual(self.subject.hvac_mode, HVAC_MODE_OFF)
  96. self.dps[HVACMODE_DPS] = None
  97. self.assertEqual(self.subject.hvac_mode, STATE_UNAVAILABLE)
  98. def test_hvac_modes(self):
  99. self.assertCountEqual(self.subject.hvac_modes, [HVAC_MODE_OFF, HVAC_MODE_HEAT])
  100. async def test_turn_on(self):
  101. async with assert_device_properties_set(
  102. self.subject._device, {HVACMODE_DPS: True}
  103. ):
  104. await self.subject.async_set_hvac_mode(HVAC_MODE_HEAT)
  105. async def test_turn_off(self):
  106. async with assert_device_properties_set(
  107. self.subject._device, {HVACMODE_DPS: False}
  108. ):
  109. await self.subject.async_set_hvac_mode(HVAC_MODE_OFF)
  110. def test_error_state(self):
  111. # There are currently no known error states; update this as
  112. # they are discovered
  113. self.dps[ERROR_DPS] = "something"
  114. self.assertEqual(self.subject.device_state_attributes, {"error": "something"})
  115. self.dps[ERROR_DPS] = "0"
  116. self.assertEqual(self.subject.device_state_attributes, {"error": "OK"})
  117. async def test_update(self):
  118. result = AsyncMock()
  119. self.subject._device.async_refresh.return_value = result()
  120. await self.subject.async_update()
  121. self.subject._device.async_refresh.assert_called_once()
  122. result.assert_awaited()