test_binary_sensor.py 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. """Tests for the binary_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.binary_sensor import (
  6. TuyaLocalBinarySensor,
  7. async_setup_entry,
  8. )
  9. from custom_components.tuya_local.const import (
  10. CONF_DEVICE_ID,
  11. CONF_PROTOCOL_VERSION,
  12. CONF_TYPE,
  13. DOMAIN,
  14. )
  15. @pytest.mark.asyncio
  16. async def test_init_entry(hass):
  17. """Test the initialisation."""
  18. entry = MockConfigEntry(
  19. domain=DOMAIN,
  20. data={
  21. CONF_TYPE: "goldair_dehumidifier",
  22. CONF_DEVICE_ID: "dummy",
  23. CONF_PROTOCOL_VERSION: "auto",
  24. },
  25. )
  26. m_add_entities = Mock()
  27. m_device = AsyncMock()
  28. hass.data[DOMAIN] = {
  29. "dummy": {"device": m_device},
  30. }
  31. await async_setup_entry(hass, entry, m_add_entities)
  32. assert (
  33. type(hass.data[DOMAIN]["dummy"]["binary_sensor_tank_full"])
  34. is TuyaLocalBinarySensor
  35. )
  36. m_add_entities.assert_called_once()
  37. @pytest.mark.asyncio
  38. async def test_init_entry_fails_if_device_has_no_binary_sensor(hass):
  39. """Test initialisation when device has no matching entity"""
  40. entry = MockConfigEntry(
  41. domain=DOMAIN,
  42. data={
  43. CONF_TYPE: "mirabella_genio_usb",
  44. CONF_DEVICE_ID: "dummy",
  45. CONF_PROTOCOL_VERSION: "auto",
  46. },
  47. )
  48. m_add_entities = Mock()
  49. m_device = AsyncMock()
  50. hass.data[DOMAIN] = {
  51. "dummy": {"device": m_device},
  52. }
  53. try:
  54. await async_setup_entry(hass, entry, m_add_entities)
  55. assert False
  56. except ValueError:
  57. pass
  58. m_add_entities.assert_not_called()
  59. @pytest.mark.asyncio
  60. async def test_init_entry_fails_if_config_is_missing(hass):
  61. """Test initialisation when device has no matching entity"""
  62. entry = MockConfigEntry(
  63. domain=DOMAIN,
  64. data={
  65. CONF_TYPE: "non_existing",
  66. CONF_DEVICE_ID: "dummy",
  67. CONF_PROTOCOL_VERSION: "auto",
  68. },
  69. )
  70. m_add_entities = Mock()
  71. m_device = AsyncMock()
  72. hass.data[DOMAIN] = {
  73. "dummy": {"device": m_device},
  74. }
  75. try:
  76. await async_setup_entry(hass, entry, m_add_entities)
  77. assert False
  78. except ValueError:
  79. pass
  80. m_add_entities.assert_not_called()