Skip to content

Commit

Permalink
Merge pull request #219 from alandtse/dev
Browse files Browse the repository at this point in the history
2022-05-28
  • Loading branch information
alandtse authored May 29, 2022
2 parents 3f3680e + bf94f3b commit c5a89db
Show file tree
Hide file tree
Showing 6 changed files with 318 additions and 305 deletions.
23 changes: 20 additions & 3 deletions custom_components/tesla_custom/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import logging

import async_timeout
from homeassistant.config_entries import SOURCE_IMPORT
from homeassistant.config_entries import SOURCE_IMPORT, ConfigEntry
from homeassistant.const import (
CONF_ACCESS_TOKEN,
CONF_DOMAIN,
Expand All @@ -15,8 +15,9 @@
CONF_USERNAME,
EVENT_HOMEASSISTANT_CLOSE,
)
from homeassistant.core import callback
from homeassistant.core import HomeAssistant, callback
from homeassistant.exceptions import ConfigEntryAuthFailed, ConfigEntryNotReady
from homeassistant.helpers import device_registry as dr
from homeassistant.helpers.httpx_client import SERVER_SOFTWARE, USER_AGENT
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
import httpx
Expand All @@ -38,6 +39,7 @@
PLATFORMS,
)
from .services import async_setup_services, async_unload_services
from .tesla_device import device_identifier

_LOGGER = logging.getLogger(__name__)

Expand Down Expand Up @@ -238,7 +240,9 @@ async def update_listener(hass, config_entry):
"""Update when config_entry options update."""
controller = hass.data[DOMAIN][config_entry.entry_id]["coordinator"].controller
old_update_interval = controller.update_interval
controller.update_interval = config_entry.options.get(CONF_SCAN_INTERVAL)
controller.update_interval = config_entry.options.get(
CONF_SCAN_INTERVAL, DEFAULT_SCAN_INTERVAL
)
if old_update_interval != controller.update_interval:
_LOGGER.debug(
"Changing scan_interval from %s to %s",
Expand Down Expand Up @@ -286,3 +290,16 @@ async def _async_update_data(self):
await self.hass.config_entries.async_reload(self.config_entry.entry_id)
except TeslaException as err:
raise UpdateFailed(f"Error communicating with API: {err}") from err


async def async_remove_config_entry_device(
hass: HomeAssistant, config_entry: ConfigEntry, device_entry: dr.DeviceEntry
) -> bool:
"""Remove tesla_custom config entry from a device."""
controller: TeslaAPI = hass.data[DOMAIN][config_entry.entry_id][
"coordinator"
].controller
return not device_entry.identifiers.intersection(
device_identifier(telsa_device)
for telsa_device in controller.get_homeassistant_components()
)
2 changes: 1 addition & 1 deletion custom_components/tesla_custom/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"config_flow": true,
"documentation": "https://github.com/alandtse/tesla/wiki",
"issue_tracker": "https://github.com/alandtse/tesla/issues",
"requirements": ["teslajsonpy==2.1.0"],
"requirements": ["teslajsonpy==2.2.1"],
"codeowners": ["@alandtse"],
"dependencies": ["http"],
"dhcp": [
Expand Down
18 changes: 13 additions & 5 deletions custom_components/tesla_custom/tesla_device.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
"""Support for Tesla cars."""
from functools import wraps
import logging
from typing import Any, Optional
from typing import Any, Optional, Tuple

from homeassistant.const import ATTR_BATTERY_CHARGING, ATTR_BATTERY_LEVEL
from homeassistant.core import callback
from homeassistant.helpers.entity_registry import async_get_registry
from homeassistant.helpers import entity_registry as er
from homeassistant.helpers.update_coordinator import CoordinatorEntity
from homeassistant.util import slugify
from teslajsonpy.exceptions import IncompleteCredentials
Expand All @@ -15,6 +15,14 @@
_LOGGER = logging.getLogger(__name__)


def device_identifier(tesla_device) -> Tuple[str, int]:
"""Return the identifier for a tesla device."""
# Note that Home Assistant types this to be
# tuple[str, str] but since that would involve
# migrating, it is not changed here.
return (DOMAIN, tesla_device.id())


class TeslaDevice(CoordinatorEntity):
"""Representation of a Tesla device."""

Expand Down Expand Up @@ -92,15 +100,15 @@ def device_info(self):
"""Return the device_info of the device."""
if hasattr(self.tesla_device, "car_name"):
return {
"identifiers": {(DOMAIN, self.tesla_device.id())},
"identifiers": {device_identifier(self.tesla_device)},
"name": self.tesla_device.car_name(),
"manufacturer": "Tesla",
"model": self.tesla_device.car_type,
"sw_version": self.tesla_device.car_version,
}
elif hasattr(self.tesla_device, "site_name"):
return {
"identifiers": {(DOMAIN, self.tesla_device.id())},
"identifiers": {device_identifier(self.tesla_device)},
"name": self.tesla_device.site_name(),
"manufacturer": "Tesla",
}
Expand All @@ -109,7 +117,7 @@ def device_info(self):
async def async_added_to_hass(self):
"""Register state update callback."""
self.async_on_remove(self.coordinator.async_add_listener(self.refresh))
registry = await async_get_registry(self.hass)
registry = er.async_get(self.hass)
self.config_entry_id = registry.entities.get(self.entity_id).config_entry_id

@callback
Expand Down
10 changes: 0 additions & 10 deletions hacs.json
Original file line number Diff line number Diff line change
@@ -1,16 +1,6 @@
{
"name": "Tesla",
"hacs": "1.6.0",
"domains": [
"sensor",
"lock",
"climate",
"binary_sensor",
"device_tracker",
"switch",
"button"
],
"iot_class": "Cloud Polling",
"homeassistant": "2021.9.0",
"zip_release": true,
"filename": "tesla_custom.zip"
Expand Down
Loading

0 comments on commit c5a89db

Please sign in to comment.