Skip to content

Commit

Permalink
feat: Add support for async_remove_config_entry_device (#218)
Browse files Browse the repository at this point in the history
  • Loading branch information
bdraco authored May 28, 2022
1 parent f82e030 commit 562c1b0
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 5 deletions.
19 changes: 17 additions & 2 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 @@ -288,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()
)
14 changes: 11 additions & 3 deletions custom_components/tesla_custom/tesla_device.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""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
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 Down

0 comments on commit 562c1b0

Please sign in to comment.