test_sensor.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. """Tests for the sensor entity."""
  2. from unittest.mock import AsyncMock, Mock
  3. import pytest
  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.sensor import TuyaLocalSensor, async_setup_entry
  13. @pytest.mark.asyncio
  14. async def test_init_entry(hass):
  15. """Test the initialisation."""
  16. entry = MockConfigEntry(
  17. domain=DOMAIN,
  18. data={
  19. CONF_TYPE: "goldair_dehumidifier",
  20. CONF_DEVICE_ID: "dummy",
  21. CONF_PROTOCOL_VERSION: "auto",
  22. },
  23. )
  24. m_add_entities = Mock()
  25. m_device = AsyncMock()
  26. hass.data[DOMAIN] = {
  27. "dummy": {"device": m_device},
  28. }
  29. await async_setup_entry(hass, entry, m_add_entities)
  30. assert (
  31. type(hass.data[DOMAIN]["dummy"]["sensor_current_temperature"])
  32. == TuyaLocalSensor
  33. )
  34. m_add_entities.assert_called_once()
  35. @pytest.mark.asyncio
  36. async def test_init_entry_fails_if_device_has_no_sensor(hass):
  37. """Test initialisation when device has no matching entity"""
  38. entry = MockConfigEntry(
  39. domain=DOMAIN,
  40. data={
  41. CONF_TYPE: "mirabella_genio_usb",
  42. CONF_DEVICE_ID: "dummy",
  43. CONF_PROTOCOL_VERSION: "auto",
  44. },
  45. )
  46. m_add_entities = Mock()
  47. m_device = AsyncMock()
  48. hass.data[DOMAIN] = {
  49. "dummy": {"device": m_device},
  50. }
  51. try:
  52. await async_setup_entry(hass, entry, m_add_entities)
  53. assert False
  54. except ValueError:
  55. pass
  56. m_add_entities.assert_not_called()
  57. @pytest.mark.asyncio
  58. async def test_init_entry_fails_if_config_is_missing(hass):
  59. """Test initialisation when device has no matching entity"""
  60. entry = MockConfigEntry(
  61. domain=DOMAIN,
  62. data={
  63. CONF_TYPE: "non_existing",
  64. CONF_DEVICE_ID: "dummy",
  65. CONF_PROTOCOL_VERSION: "auto",
  66. },
  67. )
  68. m_add_entities = Mock()
  69. m_device = AsyncMock()
  70. hass.data[DOMAIN] = {
  71. "dummy": {"device": m_device},
  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. def test_sensor_suggested_display_precision():
  80. mock_device = Mock()
  81. config = TuyaEntityConfig(
  82. mock_device,
  83. {
  84. "entity": "sensor",
  85. "dps": [
  86. {
  87. "id": 1,
  88. "name": "sensor",
  89. "type": "integer",
  90. "precision": 1,
  91. }
  92. ],
  93. },
  94. )
  95. sensor = TuyaLocalSensor(mock_device, config)
  96. assert sensor.suggested_display_precision == 1
  97. config = TuyaEntityConfig(
  98. mock_device,
  99. {
  100. "entity": "sensor",
  101. "dps": [{"id": 1, "name": "sensor", "type": "integer"}],
  102. },
  103. )
  104. sensor = TuyaLocalSensor(mock_device, config)
  105. assert sensor.suggested_display_precision is None