From ca3842b1502eb3e83c72fcf040b60f36d35464c4 Mon Sep 17 00:00:00 2001 From: Alan Tse Date: Sun, 9 Aug 2020 07:16:39 -0700 Subject: [PATCH] Address requested code changes in Tesla (#38680) * Address requested code changes * Address ternary operator in sensor --- homeassistant/components/tesla/__init__.py | 10 ++++++---- .../components/tesla/device_tracker.py | 17 +++++++---------- homeassistant/components/tesla/sensor.py | 19 +++---------------- 3 files changed, 16 insertions(+), 30 deletions(-) diff --git a/homeassistant/components/tesla/__init__.py b/homeassistant/components/tesla/__init__.py index 1dc6bce01de..fefe980fbd1 100644 --- a/homeassistant/components/tesla/__init__.py +++ b/homeassistant/components/tesla/__init__.py @@ -251,17 +251,19 @@ class TeslaDevice(Entity): """Initialise the Tesla device.""" 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() @property def name(self): """Return the name of the device.""" - return self.tesla_device.name + return self._name @property def unique_id(self) -> str: """Return a unique ID.""" - return slugify(self.tesla_device.uniq_name) + return self._unique_id @property def icon(self): @@ -312,12 +314,12 @@ class TeslaDevice(Entity): """Update the state of the device.""" _LOGGER.debug("Updating state for: %s", self.name) await self.coordinator.async_request_refresh() - self.refresh() + @callback def refresh(self) -> None: """Refresh the state of the device. This assumes the coordinator has updated the controller. """ self.tesla_device.refresh() - self.schedule_update_ha_state() + self.async_write_ha_state() diff --git a/homeassistant/components/tesla/device_tracker.py b/homeassistant/components/tesla/device_tracker.py index ce5ea5a2a8a..c3c82a708bf 100644 --- a/homeassistant/components/tesla/device_tracker.py +++ b/homeassistant/components/tesla/device_tracker.py @@ -26,11 +26,6 @@ async def async_setup_entry(hass, config_entry, async_add_entities): class TeslaDeviceEntity(TeslaDevice, TrackerEntity): """A class representing a Tesla device.""" - def __init__(self, tesla_device, coordinator): - """Initialize the Tesla device scanner.""" - super().__init__(tesla_device, coordinator) - self._attributes = {"trackr_id": self.unique_id} - @property def latitude(self) -> Optional[float]: """Return latitude value of the device.""" @@ -54,9 +49,11 @@ class TeslaDeviceEntity(TeslaDevice, TrackerEntity): attr = super().device_state_attributes.copy() location = self.tesla_device.get_location() if location: - self._attributes = { - "trackr_id": self.unique_id, - "heading": location["heading"], - "speed": location["speed"], - } + attr.update( + { + "trackr_id": self.unique_id, + "heading": location["heading"], + "speed": location["speed"], + } + ) return attr diff --git a/homeassistant/components/tesla/sensor.py b/homeassistant/components/tesla/sensor.py index 93cc03cd718..50be1edc87d 100644 --- a/homeassistant/components/tesla/sensor.py +++ b/homeassistant/components/tesla/sensor.py @@ -37,22 +37,9 @@ class TeslaSensor(TeslaDevice, Entity): """Initialize of the sensor.""" super().__init__(tesla_device, coordinator) self.type = sensor_type - - @property - def name(self) -> str: - """Return the name of the device.""" - return ( - self.tesla_device.name - if not self.type - else f"{self.tesla_device.name} ({self.type})" - ) - - @property - def unique_id(self) -> str: - """Return a unique ID.""" - return ( - super().unique_id if not self.type else f"{super().unique_id}_{self.type}" - ) + if self.type: + self._name = f"{super().name} ({self.type})" + self._unique_id = f"{super().unique_id}_{self.type}" @property def state(self) -> Optional[float]: