Disable update polling for Wemo when devices can push updates (#46806)

This commit is contained in:
Eric Severance 2021-02-20 11:37:48 -08:00 committed by GitHub
parent 806369ddc1
commit fe4cf611f7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -39,6 +39,7 @@ class WemoEntity(Entity):
self._state = None self._state = None
self._available = True self._available = True
self._update_lock = None self._update_lock = None
self._has_polled = False
@property @property
def name(self) -> str: def name(self) -> str:
@ -103,6 +104,7 @@ class WemoEntity(Entity):
"""Try updating within an async lock.""" """Try updating within an async lock."""
async with self._update_lock: async with self._update_lock:
await self.hass.async_add_executor_job(self._update, force_update) await self.hass.async_add_executor_job(self._update, force_update)
self._has_polled = True
# When the timeout expires HomeAssistant is no longer waiting for an # When the timeout expires HomeAssistant is no longer waiting for an
# update from the device. Instead, the state needs to be updated # update from the device. Instead, the state needs to be updated
# asynchronously. This also handles the case where an update came # asynchronously. This also handles the case where an update came
@ -136,6 +138,24 @@ class WemoSubscriptionEntity(WemoEntity):
"""Return true if the state is on. Standby is on.""" """Return true if the state is on. Standby is on."""
return self._state return self._state
@property
def should_poll(self) -> bool:
"""Return True if the the device requires local polling, False otherwise.
Polling can be disabled if three conditions are met:
1. The device has polled to get the initial state (self._has_polled).
2. The polling was successful and the device is in a healthy state
(self.available).
3. The pywemo subscription registry reports that there is an active
subscription and the subscription has been confirmed by receiving an
initial event. This confirms that device push notifications are
working correctly (registry.is_subscribed - this method is async safe).
"""
registry = self.hass.data[WEMO_DOMAIN]["registry"]
return not (
self.available and self._has_polled and registry.is_subscribed(self.wemo)
)
async def async_added_to_hass(self) -> None: async def async_added_to_hass(self) -> None:
"""Wemo device added to Home Assistant.""" """Wemo device added to Home Assistant."""
await super().async_added_to_hass() await super().async_added_to_hass()