* build: bump teslajsonpy to 0.2.0 * Remove tests * feat: add config flow * feat: add async * perf: convert unnecessary async calls to sync * feat: add charger voltage and current sensor * feat: add options flow * build: bump teslajsonpy to 0.2.0 * Remove icon property * Revert climate mode change * Remove charger sensor * Simplify async_setup_platform * Update homeassistant/components/tesla/sensor.py Co-Authored-By: Paulus Schoutsen <paulus@home-assistant.io> * Update homeassistant/components/tesla/binary_sensor.py Co-Authored-By: Paulus Schoutsen <paulus@home-assistant.io> * Address requested changes * Fix pylint error * Address requested changes * Update codeowners * Fix pylint error * Address requested changes * Address requested change * Remove unnecessary check for existing config entry * Load scan_interval in async_setup_entry * Include coverage of config_flow * Add tests for full coverage * Address requested test changes * Remove unnecessary init lines * Remove unnecessary init Co-authored-by: Paulus Schoutsen <paulus@home-assistant.io>
70 lines
2.2 KiB
Python
70 lines
2.2 KiB
Python
"""Support for tracking Tesla cars."""
|
|
import logging
|
|
|
|
from homeassistant.components.device_tracker import SOURCE_TYPE_GPS
|
|
from homeassistant.components.device_tracker.config_entry import TrackerEntity
|
|
|
|
from . import DOMAIN as TESLA_DOMAIN, TeslaDevice
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
async def async_setup_entry(hass, config_entry, async_add_entities):
|
|
"""Set up the Tesla binary_sensors by config_entry."""
|
|
entities = [
|
|
TeslaDeviceEntity(
|
|
device,
|
|
hass.data[TESLA_DOMAIN][config_entry.entry_id]["controller"],
|
|
config_entry,
|
|
)
|
|
for device in hass.data[TESLA_DOMAIN][config_entry.entry_id]["devices"][
|
|
"devices_tracker"
|
|
]
|
|
]
|
|
async_add_entities(entities, True)
|
|
|
|
|
|
class TeslaDeviceEntity(TeslaDevice, TrackerEntity):
|
|
"""A class representing a Tesla device."""
|
|
|
|
def __init__(self, tesla_device, controller, config_entry):
|
|
"""Initialize the Tesla device scanner."""
|
|
super().__init__(tesla_device, controller, config_entry)
|
|
self._latitude = None
|
|
self._longitude = None
|
|
self._attributes = {"trackr_id": self.unique_id}
|
|
self._listener = None
|
|
|
|
async def async_update(self):
|
|
"""Update the device info."""
|
|
_LOGGER.debug("Updating device position: %s", self.name)
|
|
await super().async_update()
|
|
location = self.tesla_device.get_location()
|
|
if location:
|
|
self._latitude = location["latitude"]
|
|
self._longitude = location["longitude"]
|
|
self._attributes = {
|
|
"trackr_id": self.unique_id,
|
|
"heading": location["heading"],
|
|
"speed": location["speed"],
|
|
}
|
|
|
|
@property
|
|
def latitude(self) -> float:
|
|
"""Return latitude value of the device."""
|
|
return self._latitude
|
|
|
|
@property
|
|
def longitude(self) -> float:
|
|
"""Return longitude value of the device."""
|
|
return self._longitude
|
|
|
|
@property
|
|
def should_poll(self):
|
|
"""Return whether polling is needed."""
|
|
return True
|
|
|
|
@property
|
|
def source_type(self):
|
|
"""Return the source type, eg gps or router, of the device."""
|
|
return SOURCE_TYPE_GPS
|