test_sensor.py 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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.generic.sensor import TuyaLocalSensor
  12. from custom_components.tuya_local.sensor import 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()