From a05cece674f53069bcf8b0d9f7e6a32453448f7f Mon Sep 17 00:00:00 2001 From: dougiteixeira <31328123+dougiteixeira@users.noreply.github.com> Date: Wed, 26 Apr 2023 14:29:21 -0300 Subject: [PATCH 1/2] Protects error integration in case the vehicle is deleted in the app --- custom_components/drivvo/__init__.py | 2 +- custom_components/drivvo/sensor.py | 12 ++++++++++++ custom_components/drivvo/translations/en.json | 4 ++++ custom_components/drivvo/translations/pt-BR.json | 4 ++++ 4 files changed, 21 insertions(+), 1 deletion(-) diff --git a/custom_components/drivvo/__init__.py b/custom_components/drivvo/__init__.py index aa09d7e..711a977 100644 --- a/custom_components/drivvo/__init__.py +++ b/custom_components/drivvo/__init__.py @@ -154,4 +154,4 @@ def sort_by_key(list): return data _LOGGER.debug("API Response Data Vehicle - %s: %s", info, response) - return False + return None diff --git a/custom_components/drivvo/sensor.py b/custom_components/drivvo/sensor.py index 9b015c8..287f465 100755 --- a/custom_components/drivvo/sensor.py +++ b/custom_components/drivvo/sensor.py @@ -75,6 +75,18 @@ async def async_setup_entry( ], update_before_add=True, ) + else: + async_create_issue( + hass, + DOMAIN, + f"{vehicle}_vehicle_non_existent", + is_fixable=False, + severity=IssueSeverity.WARNING, + translation_key="vehicle_non_existent", + translation_placeholders={ + "vehicle": vehicle, + }, + ) async def async_setup_platform( diff --git a/custom_components/drivvo/translations/en.json b/custom_components/drivvo/translations/en.json index d215d06..1d51003 100644 --- a/custom_components/drivvo/translations/en.json +++ b/custom_components/drivvo/translations/en.json @@ -37,6 +37,10 @@ "import_already_configured": { "title": "Account {email} already exists", "description": "Account {email} already exists and therefore was not imported.\n\nYou can remove the configuration from your `configuration.yaml` file." + }, + "vehicle_non_existent": { + "title": "The vehicle ID {vehicle} does not exist in the Drivvo app", + "description": "The vehicle ID {vehicle} does not exist, it may have been deleted.\n\nGo to the integration configuration options and remove the vehicle." } }, "options": { diff --git a/custom_components/drivvo/translations/pt-BR.json b/custom_components/drivvo/translations/pt-BR.json index 0b65cd6..2d2688f 100644 --- a/custom_components/drivvo/translations/pt-BR.json +++ b/custom_components/drivvo/translations/pt-BR.json @@ -37,6 +37,10 @@ "import_already_configured": { "title": "A configuração da conta {email} já existe", "description": "A configuração da conta {email} já existe e por isso não foi importada.\n\nVocê pode remover a configuração do seu arquivo `configuration.yaml`." + }, + "vehicle_non_existent": { + "title": "O veículo ID {vehicle} não existe no aplicativo Drivvo", + "description": "O veículo {vehicle} não existe, ele pode ter sido excluído.\n\nAcesse as opções de configuração da integração e remova o veículo." } }, "options": { From d25500f0de2cad705e438e118362520a04405da2 Mon Sep 17 00:00:00 2001 From: dougiteixeira <31328123+dougiteixeira@users.noreply.github.com> Date: Wed, 26 Apr 2023 14:36:06 -0300 Subject: [PATCH 2/2] Update config_flow.py --- custom_components/drivvo/config_flow.py | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/custom_components/drivvo/config_flow.py b/custom_components/drivvo/config_flow.py index 44d0ed7..057df2b 100644 --- a/custom_components/drivvo/config_flow.py +++ b/custom_components/drivvo/config_flow.py @@ -9,7 +9,11 @@ from homeassistant.data_entry_flow import FlowResult import homeassistant.helpers.config_validation as cv from homeassistant.helpers import device_registry as dr -from homeassistant.helpers.issue_registry import IssueSeverity, async_create_issue +from homeassistant.helpers.issue_registry import ( + IssueSeverity, + async_create_issue, + async_delete_issue, +) from . import auth, get_vehicles from .const import ( @@ -63,6 +67,11 @@ async def async_step_init(self, user_input: dict[str, Any]) -> FlowResult: device_id=device.id, remove_config_entry_id=self.config_entry.entry_id, ) + async_delete_issue( + self.hass, + DOMAIN, + f"{vehicle}_vehicle_non_existent", + ) _LOGGER.debug("Device %s (%s) removed", device.name, device.id) self.hass.config_entries.async_update_entry(