Update tesla to use CoordinatorEntity (#39436)
This commit is contained in:
parent
e701001c5f
commit
22b8d43bf5
1 changed files with 7 additions and 22 deletions
|
@ -21,8 +21,11 @@ from homeassistant.const import (
|
||||||
from homeassistant.core import callback
|
from homeassistant.core import callback
|
||||||
from homeassistant.exceptions import ConfigEntryNotReady
|
from homeassistant.exceptions import ConfigEntryNotReady
|
||||||
from homeassistant.helpers import aiohttp_client, config_validation as cv
|
from homeassistant.helpers import aiohttp_client, config_validation as cv
|
||||||
from homeassistant.helpers.entity import Entity
|
from homeassistant.helpers.update_coordinator import (
|
||||||
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
|
CoordinatorEntity,
|
||||||
|
DataUpdateCoordinator,
|
||||||
|
UpdateFailed,
|
||||||
|
)
|
||||||
from homeassistant.util import slugify
|
from homeassistant.util import slugify
|
||||||
|
|
||||||
from .config_flow import (
|
from .config_flow import (
|
||||||
|
@ -247,13 +250,13 @@ class TeslaDataUpdateCoordinator(DataUpdateCoordinator):
|
||||||
raise UpdateFailed(f"Error communicating with API: {err}") from err
|
raise UpdateFailed(f"Error communicating with API: {err}") from err
|
||||||
|
|
||||||
|
|
||||||
class TeslaDevice(Entity):
|
class TeslaDevice(CoordinatorEntity):
|
||||||
"""Representation of a Tesla device."""
|
"""Representation of a Tesla device."""
|
||||||
|
|
||||||
def __init__(self, tesla_device, coordinator):
|
def __init__(self, tesla_device, coordinator):
|
||||||
"""Initialise the Tesla device."""
|
"""Initialise the Tesla device."""
|
||||||
|
super().__init__(coordinator)
|
||||||
self.tesla_device = tesla_device
|
self.tesla_device = tesla_device
|
||||||
self.coordinator = coordinator
|
|
||||||
self._name = self.tesla_device.name
|
self._name = self.tesla_device.name
|
||||||
self._unique_id = slugify(self.tesla_device.uniq_name)
|
self._unique_id = slugify(self.tesla_device.uniq_name)
|
||||||
self._attributes = self.tesla_device.attrs.copy()
|
self._attributes = self.tesla_device.attrs.copy()
|
||||||
|
@ -276,16 +279,6 @@ class TeslaDevice(Entity):
|
||||||
|
|
||||||
return ICONS.get(self.tesla_device.type)
|
return ICONS.get(self.tesla_device.type)
|
||||||
|
|
||||||
@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
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def device_state_attributes(self):
|
def device_state_attributes(self):
|
||||||
"""Return the state attributes of the device."""
|
"""Return the state attributes of the device."""
|
||||||
|
@ -310,14 +303,6 @@ class TeslaDevice(Entity):
|
||||||
"""Register state update callback."""
|
"""Register state update callback."""
|
||||||
self.async_on_remove(self.coordinator.async_add_listener(self.refresh))
|
self.async_on_remove(self.coordinator.async_add_listener(self.refresh))
|
||||||
|
|
||||||
async def async_will_remove_from_hass(self):
|
|
||||||
"""Prepare for unload."""
|
|
||||||
|
|
||||||
async def async_update(self):
|
|
||||||
"""Update the state of the device."""
|
|
||||||
_LOGGER.debug("Updating state for: %s", self.name)
|
|
||||||
await self.coordinator.async_request_refresh()
|
|
||||||
|
|
||||||
@callback
|
@callback
|
||||||
def refresh(self) -> None:
|
def refresh(self) -> None:
|
||||||
"""Refresh the state of the device.
|
"""Refresh the state of the device.
|
||||||
|
|
Loading…
Add table
Reference in a new issue