Update Tesla to use DataUpdateCoordinator (#38306)

* Update Tesla to use DataUpdateCoordinator

* Update Tesla to use DataUpdateCoordinator

* Fix linting errors

* Apply suggestions from code review

Co-authored-by: Chris Talkington <chris@talkingtontech.com>

* Address requested changes

* Apply suggestions from code review

Co-authored-by: Chris Talkington <chris@talkingtontech.com>

* Fix lint errors

* Remove controller from hass.data

Co-authored-by: Chris Talkington <chris@talkingtontech.com>
This commit is contained in:
Alan TseandChris Talkington authored and GitHub committed 2020-08-07 22:16:28 -05:00
1 parent 0d5e279509
commit 94b6d09b51
10 files changed
+247 -257

No files matched your search

@@ -13,9 +13,7 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
async_add_entities(
[
TeslaBinarySensor(
device,
hass.data[TESLA_DOMAIN][config_entry.entry_id]["controller"],
config_entry,
device, hass.data[TESLA_DOMAIN][config_entry.entry_id]["coordinator"],
)
for device in hass.data[TESLA_DOMAIN][config_entry.entry_id]["devices"][
"binary_sensor"
@@ -28,27 +26,16 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
class TeslaBinarySensor(TeslaDevice, BinarySensorEntity):
"""Implement an Tesla binary sensor for parking and charger."""
def __init__(self, tesla_device, controller, config_entry):
"""Initialise of a Tesla binary sensor."""
super().__init__(tesla_device, controller, config_entry)
self._state = None
self._sensor_type = None
if tesla_device.sensor_type in DEVICE_CLASSES:
self._sensor_type = tesla_device.sensor_type
@property
def device_class(self):
"""Return the class of this binary sensor."""
return self._sensor_type
return (
self.tesla_device.sensor_type
if self.tesla_device.sensor_type in DEVICE_CLASSES
else None
)
@property
def is_on(self):
"""Return the state of the binary sensor."""
return self._state
async def async_update(self):
"""Update the state of the device."""
_LOGGER.debug("Updating sensor: %s", self._name)
await super().async_update()
self._state = self.tesla_device.get_value()
self._attributes = self.tesla_device.attrs
return self.tesla_device.get_value()