From b57f33c41ac5b41f02733b86f4cb602ad0f3e5c9 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sun, 30 Aug 2020 13:27:46 -0500 Subject: [PATCH] Update control4 to use CoordinatorEntity (#39466) --- homeassistant/components/control4/__init__.py | 31 +++++-------------- homeassistant/components/control4/light.py | 8 ++--- 2 files changed, 11 insertions(+), 28 deletions(-) diff --git a/homeassistant/components/control4/__init__.py b/homeassistant/components/control4/__init__.py index 41b71162d6c..a45cdc4006a 100644 --- a/homeassistant/components/control4/__init__.py +++ b/homeassistant/components/control4/__init__.py @@ -18,8 +18,11 @@ from homeassistant.const import ( ) from homeassistant.core import HomeAssistant from homeassistant.exceptions import ConfigEntryNotReady -from homeassistant.helpers import aiohttp_client, device_registry as dr, entity -from homeassistant.helpers.update_coordinator import DataUpdateCoordinator +from homeassistant.helpers import aiohttp_client, device_registry as dr +from homeassistant.helpers.update_coordinator import ( + CoordinatorEntity, + DataUpdateCoordinator, +) from .const import ( CONF_ACCOUNT, @@ -154,7 +157,7 @@ async def get_items_of_category(hass: HomeAssistant, entry: ConfigEntry, categor return return_list -class Control4Entity(entity.Entity): +class Control4Entity(CoordinatorEntity): """Base entity for Control4.""" def __init__( @@ -170,11 +173,11 @@ class Control4Entity(entity.Entity): device_id: int, ): """Initialize a Control4 entity.""" + super().__init__(coordinator) self.entry = entry self.entry_data = entry_data self._name = name self._idx = idx - self._coordinator = coordinator self._controller_unique_id = entry_data[CONF_CONTROLLER_UNIQUE_ID] self._device_name = device_name self._device_manufacturer = device_manufacturer @@ -202,23 +205,3 @@ class Control4Entity(entity.Entity): "model": self._device_model, "via_device": (DOMAIN, self._controller_unique_id), } - - @property - def should_poll(self): - """No need to poll. Coordinator notifies entity of updates.""" - return False - - @property - def available(self): - """Return if entity is available.""" - return self._coordinator.last_update_success - - async def async_added_to_hass(self): - """When entity is added to hass.""" - self.async_on_remove( - self._coordinator.async_add_listener(self.async_write_ha_state) - ) - - async def async_update(self): - """Update the state of the device.""" - await self._coordinator.async_request_refresh() diff --git a/homeassistant/components/control4/light.py b/homeassistant/components/control4/light.py index 09e05791169..08ac23e40e2 100644 --- a/homeassistant/components/control4/light.py +++ b/homeassistant/components/control4/light.py @@ -175,13 +175,13 @@ class Control4Light(Control4Entity, LightEntity): @property def is_on(self): """Return whether this light is on or off.""" - return self._coordinator.data[self._idx]["value"] > 0 + return self.coordinator.data[self._idx]["value"] > 0 @property def brightness(self): """Return the brightness of this light between 0..255.""" if self._is_dimmer: - return round(self._coordinator.data[self._idx]["value"] * 2.55) + return round(self.coordinator.data[self._idx]["value"] * 2.55) return None @property @@ -213,7 +213,7 @@ class Control4Light(Control4Entity, LightEntity): delay_time = (transition_length / 1000) + 0.7 _LOGGER.debug("Delaying light update by %s seconds", delay_time) await asyncio.sleep(delay_time) - await self._coordinator.async_request_refresh() + await self.coordinator.async_request_refresh() async def async_turn_off(self, **kwargs) -> None: """Turn the entity off.""" @@ -232,4 +232,4 @@ class Control4Light(Control4Entity, LightEntity): delay_time = (transition_length / 1000) + 0.7 _LOGGER.debug("Delaying light update by %s seconds", delay_time) await asyncio.sleep(delay_time) - await self._coordinator.async_request_refresh() + await self.coordinator.async_request_refresh()