Update tesla to use CoordinatorEntity ()

This commit is contained in:
springstan 2020-08-30 17:48:15 +02:00 committed by GitHub
parent e701001c5f
commit 22b8d43bf5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -21,8 +21,11 @@ from homeassistant.const import (
from homeassistant.core import callback
from homeassistant.exceptions import ConfigEntryNotReady
from homeassistant.helpers import aiohttp_client, config_validation as cv
from homeassistant.helpers.entity import Entity
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
from homeassistant.helpers.update_coordinator import (
CoordinatorEntity,
DataUpdateCoordinator,
UpdateFailed,
)
from homeassistant.util import slugify
from .config_flow import (
@ -247,13 +250,13 @@ class TeslaDataUpdateCoordinator(DataUpdateCoordinator):
raise UpdateFailed(f"Error communicating with API: {err}") from err
class TeslaDevice(Entity):
class TeslaDevice(CoordinatorEntity):
"""Representation of a Tesla device."""
def __init__(self, tesla_device, coordinator):
"""Initialise the Tesla device."""
super().__init__(coordinator)
self.tesla_device = tesla_device
self.coordinator = coordinator
self._name = self.tesla_device.name
self._unique_id = slugify(self.tesla_device.uniq_name)
self._attributes = self.tesla_device.attrs.copy()
@ -276,16 +279,6 @@ class TeslaDevice(Entity):
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
def device_state_attributes(self):
"""Return the state attributes of the device."""
@ -310,14 +303,6 @@ class TeslaDevice(Entity):
"""Register state update callback."""
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
def refresh(self) -> None:
"""Refresh the state of the device.