Address requested code changes in Tesla (#38680)

* Address requested code changes

* Address ternary operator in sensor
This commit is contained in:
Alan Tse 2020-08-09 07:16:39 -07:00 committed by GitHub
parent ac1f431f91
commit ca3842b150
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 16 additions and 30 deletions

View file

@ -251,17 +251,19 @@ class TeslaDevice(Entity):
"""Initialise the Tesla device.""" """Initialise the Tesla device."""
self.tesla_device = tesla_device self.tesla_device = tesla_device
self.coordinator = coordinator 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() self._attributes = self.tesla_device.attrs.copy()
@property @property
def name(self): def name(self):
"""Return the name of the device.""" """Return the name of the device."""
return self.tesla_device.name return self._name
@property @property
def unique_id(self) -> str: def unique_id(self) -> str:
"""Return a unique ID.""" """Return a unique ID."""
return slugify(self.tesla_device.uniq_name) return self._unique_id
@property @property
def icon(self): def icon(self):
@ -312,12 +314,12 @@ class TeslaDevice(Entity):
"""Update the state of the device.""" """Update the state of the device."""
_LOGGER.debug("Updating state for: %s", self.name) _LOGGER.debug("Updating state for: %s", self.name)
await self.coordinator.async_request_refresh() await self.coordinator.async_request_refresh()
self.refresh()
@callback
def refresh(self) -> None: def refresh(self) -> None:
"""Refresh the state of the device. """Refresh the state of the device.
This assumes the coordinator has updated the controller. This assumes the coordinator has updated the controller.
""" """
self.tesla_device.refresh() self.tesla_device.refresh()
self.schedule_update_ha_state() self.async_write_ha_state()

View file

@ -26,11 +26,6 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
class TeslaDeviceEntity(TeslaDevice, TrackerEntity): class TeslaDeviceEntity(TeslaDevice, TrackerEntity):
"""A class representing a Tesla device.""" """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 @property
def latitude(self) -> Optional[float]: def latitude(self) -> Optional[float]:
"""Return latitude value of the device.""" """Return latitude value of the device."""
@ -54,9 +49,11 @@ class TeslaDeviceEntity(TeslaDevice, TrackerEntity):
attr = super().device_state_attributes.copy() attr = super().device_state_attributes.copy()
location = self.tesla_device.get_location() location = self.tesla_device.get_location()
if location: if location:
self._attributes = { attr.update(
"trackr_id": self.unique_id, {
"heading": location["heading"], "trackr_id": self.unique_id,
"speed": location["speed"], "heading": location["heading"],
} "speed": location["speed"],
}
)
return attr return attr

View file

@ -37,22 +37,9 @@ class TeslaSensor(TeslaDevice, Entity):
"""Initialize of the sensor.""" """Initialize of the sensor."""
super().__init__(tesla_device, coordinator) super().__init__(tesla_device, coordinator)
self.type = sensor_type self.type = sensor_type
if self.type:
@property self._name = f"{super().name} ({self.type})"
def name(self) -> str: self._unique_id = f"{super().unique_id}_{self.type}"
"""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}"
)
@property @property
def state(self) -> Optional[float]: def state(self) -> Optional[float]: