diff --git a/custom_components/tesla_custom/__init__.py b/custom_components/tesla_custom/__init__.py index b97743c8..2de42264 100644 --- a/custom_components/tesla_custom/__init__.py +++ b/custom_components/tesla_custom/__init__.py @@ -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, @@ -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 @@ -38,6 +39,7 @@ PLATFORMS, ) from .services import async_setup_services, async_unload_services +from .tesla_device import device_identifier _LOGGER = logging.getLogger(__name__) @@ -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() + ) diff --git a/custom_components/tesla_custom/tesla_device.py b/custom_components/tesla_custom/tesla_device.py index 68a0da32..f8748ccf 100644 --- a/custom_components/tesla_custom/tesla_device.py +++ b/custom_components/tesla_custom/tesla_device.py @@ -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 @@ -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.""" @@ -92,7 +100,7 @@ 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, @@ -100,7 +108,7 @@ def device_info(self): } 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", }