mirror of
https://github.com/home-assistant/core.git
synced 2025-07-15 09:17:10 +00:00
Update control4 to use CoordinatorEntity (#39466)
This commit is contained in:
parent
ab0b0dc51c
commit
b57f33c41a
@ -18,8 +18,11 @@ from homeassistant.const import (
|
|||||||
)
|
)
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
from homeassistant.exceptions import ConfigEntryNotReady
|
from homeassistant.exceptions import ConfigEntryNotReady
|
||||||
from homeassistant.helpers import aiohttp_client, device_registry as dr, entity
|
from homeassistant.helpers import aiohttp_client, device_registry as dr
|
||||||
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator
|
from homeassistant.helpers.update_coordinator import (
|
||||||
|
CoordinatorEntity,
|
||||||
|
DataUpdateCoordinator,
|
||||||
|
)
|
||||||
|
|
||||||
from .const import (
|
from .const import (
|
||||||
CONF_ACCOUNT,
|
CONF_ACCOUNT,
|
||||||
@ -154,7 +157,7 @@ async def get_items_of_category(hass: HomeAssistant, entry: ConfigEntry, categor
|
|||||||
return return_list
|
return return_list
|
||||||
|
|
||||||
|
|
||||||
class Control4Entity(entity.Entity):
|
class Control4Entity(CoordinatorEntity):
|
||||||
"""Base entity for Control4."""
|
"""Base entity for Control4."""
|
||||||
|
|
||||||
def __init__(
|
def __init__(
|
||||||
@ -170,11 +173,11 @@ class Control4Entity(entity.Entity):
|
|||||||
device_id: int,
|
device_id: int,
|
||||||
):
|
):
|
||||||
"""Initialize a Control4 entity."""
|
"""Initialize a Control4 entity."""
|
||||||
|
super().__init__(coordinator)
|
||||||
self.entry = entry
|
self.entry = entry
|
||||||
self.entry_data = entry_data
|
self.entry_data = entry_data
|
||||||
self._name = name
|
self._name = name
|
||||||
self._idx = idx
|
self._idx = idx
|
||||||
self._coordinator = coordinator
|
|
||||||
self._controller_unique_id = entry_data[CONF_CONTROLLER_UNIQUE_ID]
|
self._controller_unique_id = entry_data[CONF_CONTROLLER_UNIQUE_ID]
|
||||||
self._device_name = device_name
|
self._device_name = device_name
|
||||||
self._device_manufacturer = device_manufacturer
|
self._device_manufacturer = device_manufacturer
|
||||||
@ -202,23 +205,3 @@ class Control4Entity(entity.Entity):
|
|||||||
"model": self._device_model,
|
"model": self._device_model,
|
||||||
"via_device": (DOMAIN, self._controller_unique_id),
|
"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()
|
|
||||||
|
@ -175,13 +175,13 @@ class Control4Light(Control4Entity, LightEntity):
|
|||||||
@property
|
@property
|
||||||
def is_on(self):
|
def is_on(self):
|
||||||
"""Return whether this light is on or off."""
|
"""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
|
@property
|
||||||
def brightness(self):
|
def brightness(self):
|
||||||
"""Return the brightness of this light between 0..255."""
|
"""Return the brightness of this light between 0..255."""
|
||||||
if self._is_dimmer:
|
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
|
return None
|
||||||
|
|
||||||
@property
|
@property
|
||||||
@ -213,7 +213,7 @@ class Control4Light(Control4Entity, LightEntity):
|
|||||||
delay_time = (transition_length / 1000) + 0.7
|
delay_time = (transition_length / 1000) + 0.7
|
||||||
_LOGGER.debug("Delaying light update by %s seconds", delay_time)
|
_LOGGER.debug("Delaying light update by %s seconds", delay_time)
|
||||||
await asyncio.sleep(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:
|
async def async_turn_off(self, **kwargs) -> None:
|
||||||
"""Turn the entity off."""
|
"""Turn the entity off."""
|
||||||
@ -232,4 +232,4 @@ class Control4Light(Control4Entity, LightEntity):
|
|||||||
delay_time = (transition_length / 1000) + 0.7
|
delay_time = (transition_length / 1000) + 0.7
|
||||||
_LOGGER.debug("Delaying light update by %s seconds", delay_time)
|
_LOGGER.debug("Delaying light update by %s seconds", delay_time)
|
||||||
await asyncio.sleep(delay_time)
|
await asyncio.sleep(delay_time)
|
||||||
await self._coordinator.async_request_refresh()
|
await self.coordinator.async_request_refresh()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user