Prechádzať zdrojové kódy

Partial revert of 7c971be75 Get HA to call async_refresh once...

Despite the name, Entity.async_schedule_update_ha_state is not async.
Remove the parts of the change that modified the API to make
everything involved in calling it async.
Jason Rumney 3 rokov pred
rodič
commit
dbab00a949

+ 3 - 3
custom_components/tuya_local/device.py

@@ -141,7 +141,7 @@ class TuyaLocalDevice(object):
         _LOGGER.debug(f"Monitor loop for {self.name} stopped")
         self._refresh_task = None
 
-    async def async_register_entity(self, entity):
+    def register_entity(self, entity):
         # If this is the first child entity to register, refresh the device
         # state
         should_poll = len(self._children) == 0
@@ -150,9 +150,9 @@ class TuyaLocalDevice(object):
         if not self._running and not self._startup_listener:
             self.start()
         if self.has_returned_state:
-            await entity.async_schedule_update_ha_state()
+            entity.async_schedule_update_ha_state()
         elif should_poll:
-            await entity.async_schedule_update_ha_state(True)
+            entity.async_schedule_update_ha_state(True)
 
     async def async_unregister_entity(self, entity):
         self._children.remove(entity)

+ 1 - 1
custom_components/tuya_local/helpers/mixin.py

@@ -85,7 +85,7 @@ class TuyaLocalEntity:
         await self._device.async_refresh()
 
     async def async_added_to_hass(self):
-        await self._device.async_register_entity(self)
+        self._device.register_entity(self)
 
     async def async_will_remove_from_hass(self):
         await self._device.async_unregister_entity(self)

+ 1 - 1
tests/test_config_flow.py

@@ -29,7 +29,7 @@ def auto_enable_custom_integrations(enable_custom_integrations):
 @pytest.fixture(autouse=True)
 def prevent_task_creation():
     with patch(
-        "custom_components.tuya_local.device.TuyaLocalDevice.async_register_entity",
+        "custom_components.tuya_local.device.TuyaLocalDevice.register_entity",
     ):
         yield
 

+ 6 - 6
tests/test_device.py

@@ -466,7 +466,7 @@ class TestDevice(IsolatedAsyncioTestCase):
         # Was the refresh task left empty?
         self.assertIsNone(self.subject._refresh_task)
 
-    async def test_register_first_entity_ha_running(self):
+    def test_register_first_entity_ha_running(self):
         # Set up preconditions
         self.subject._children = []
         self.subject._running = False
@@ -475,7 +475,7 @@ class TestDevice(IsolatedAsyncioTestCase):
         entity = AsyncMock()
 
         # Call the function under test
-        await self.subject.async_register_entity(entity)
+        self.subject.register_entity(entity)
 
         # Was the entity added to the list?
         self.assertEqual(self.subject._children, [entity])
@@ -483,7 +483,7 @@ class TestDevice(IsolatedAsyncioTestCase):
         # Did we start the loop?
         self.subject.start.assert_called_once()
 
-    async def test_register_subsequent_entity_ha_running(self):
+    def test_register_subsequent_entity_ha_running(self):
         # Set up preconditions
         first = AsyncMock()
         second = AsyncMock()
@@ -493,7 +493,7 @@ class TestDevice(IsolatedAsyncioTestCase):
         self.subject.start = Mock()
 
         # Call the function under test
-        await self.subject.async_register_entity(second)
+        self.subject.register_entity(second)
 
         # Was the entity added to the list?
         self.assertCountEqual(self.subject._children, [first, second])
@@ -501,7 +501,7 @@ class TestDevice(IsolatedAsyncioTestCase):
         # Did we avoid restarting the loop?
         self.subject.start.assert_not_called()
 
-    async def test_register_subsequent_entity_ha_starting(self):
+    def test_register_subsequent_entity_ha_starting(self):
         # Set up preconditions
         first = AsyncMock()
         second = AsyncMock()
@@ -511,7 +511,7 @@ class TestDevice(IsolatedAsyncioTestCase):
         self.subject.start = Mock()
 
         # Call the function under test
-        await self.subject.async_register_entity(second)
+        self.subject.register_entity(second)
 
         # Was the entity added to the list?
         self.assertCountEqual(self.subject._children, [first, second])