Enable config flow for Tesla (#28744)

* 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>
This commit is contained in:
Alan Tse 2019-12-23 12:54:25 -08:00 committed by Martin Hjelmare
parent edce497a0d
commit 3aa2ae1700
17 changed files with 671 additions and 122 deletions

View file

@ -1,45 +1,70 @@
"""Support for tracking Tesla cars."""
import logging
from homeassistant.helpers.event import async_track_utc_time_change
from homeassistant.util import slugify
from homeassistant.components.device_tracker import SOURCE_TYPE_GPS
from homeassistant.components.device_tracker.config_entry import TrackerEntity
from . import DOMAIN as TESLA_DOMAIN
from . import DOMAIN as TESLA_DOMAIN, TeslaDevice
_LOGGER = logging.getLogger(__name__)
async def async_setup_scanner(hass, config, async_see, discovery_info=None):
"""Set up the Tesla tracker."""
tracker = TeslaDeviceTracker(
hass, config, async_see, hass.data[TESLA_DOMAIN]["devices"]["devices_tracker"]
)
await tracker.update_info()
async_track_utc_time_change(hass, tracker.update_info, second=range(0, 60, 30))
return True
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 TeslaDeviceTracker:
class TeslaDeviceEntity(TeslaDevice, TrackerEntity):
"""A class representing a Tesla device."""
def __init__(self, hass, config, see, tesla_devices):
def __init__(self, tesla_device, controller, config_entry):
"""Initialize the Tesla device scanner."""
self.hass = hass
self.see = see
self.devices = tesla_devices
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 update_info(self, now=None):
async def async_update(self):
"""Update the device info."""
for device in self.devices:
await device.async_update()
name = device.name
_LOGGER.debug("Updating device position: %s", name)
dev_id = slugify(device.uniq_name)
location = device.get_location()
if location:
lat = location["latitude"]
lon = location["longitude"]
attrs = {"trackr_id": dev_id, "id": dev_id, "name": name}
await self.see(
dev_id=dev_id, host_name=name, gps=(lat, lon), attributes=attrs
)
_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