Address requested code changes in Tesla (#38680)
* Address requested code changes * Address ternary operator in sensor
This commit is contained in:
parent
ac1f431f91
commit
ca3842b150
3 changed files with 16 additions and 30 deletions
|
@ -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()
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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]:
|
||||
|
|
Loading…
Add table
Reference in a new issue