test_binary_sensor.py 2.3 KB

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