test_sensor.py 3.2 KB

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