Kaynağa Gözat

Fix test warning and Sonar issues.

Jason Rumney 4 yıl önce
ebeveyn
işleme
2d8f251c66
2 değiştirilmiş dosya ile 10 ekleme ve 10 silme
  1. 3 6
      custom_components/tuya_local/lock.py
  2. 7 4
      tests/test_lock.py

+ 3 - 6
custom_components/tuya_local/lock.py

@@ -38,13 +38,10 @@ async def async_setup_platform(hass, config, async_add_entities, discovery_info=
             raise ValueError(f"{device.name} does not support use as a lock device.")
 
     legacy_class = locate(ecfg.legacy_class)
-    if legacy_class is None:
-        raise TypeError(f"No legacy class {ecfg.legacy_class} exists.")
-
+    # Instantiate it: Sonarcloud thinks this is a blocker bug, and legacy_class
+    # is not callable, but the unit tests show the object is created...
     data[CONF_CHILD_LOCK] = legacy_class(device)
-
-    if CONF_CHILD_LOCK in data:
-        async_add_entities([data[CONF_CHILD_LOCK]])
+    async_add_entities([data[CONF_CHILD_LOCK]])
 
 
 async def async_setup_entry(hass, config_entry, async_add_entities):

+ 7 - 4
tests/test_lock.py

@@ -1,7 +1,7 @@
 """Tests for the lock entity."""
 import pytest
 from pytest_homeassistant_custom_component.common import MockConfigEntry
-from unittest.mock import AsyncMock
+from unittest.mock import AsyncMock, Mock
 
 from custom_components.tuya_local.const import (
     CONF_CHILD_LOCK,
@@ -15,13 +15,16 @@ from custom_components.tuya_local.heater.lock import GoldairHeaterChildLock
 from custom_components.tuya_local.lock import async_setup_entry
 
 
-@pytest.mark.asyncio
 async def test_init_entry(hass):
     """Test the initialisation."""
     entry = MockConfigEntry(
-        domain=DOMAIN, data={CONF_TYPE: CONF_TYPE_AUTO, CONF_DEVICE_ID: "dummy"},
+        domain=DOMAIN,
+        data={CONF_TYPE: CONF_TYPE_AUTO, CONF_DEVICE_ID: "dummy"},
     )
-    m_add_entities = AsyncMock()
+    # although async, the async_add_entities function passed to
+    # async_setup_entry is called truly asynchronously. If we use
+    # AsyncMock, it expects us to await the result.
+    m_add_entities = Mock()
     m_device = AsyncMock()
     m_device.async_inferred_type = AsyncMock(return_value=CONF_TYPE_GPPH_HEATER)