From b4faeac869eae04ca74fc3255b10bc2f58ae7b12 Mon Sep 17 00:00:00 2001 From: Sam Stenvall Date: Fri, 13 Sep 2024 09:44:56 +0300 Subject: [PATCH] Specify a unique ID for config flows to prevent adding the same device twice --- custom_components/extron/config_flow.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/custom_components/extron/config_flow.py b/custom_components/extron/config_flow.py index 03cc454..9050733 100644 --- a/custom_components/extron/config_flow.py +++ b/custom_components/extron/config_flow.py @@ -7,6 +7,7 @@ import voluptuous as vol from homeassistant.config_entries import ConfigEntry, ConfigFlow, OptionsFlow +from homeassistant.helpers.device_registry import format_mac from homeassistant.helpers.selector import selector from .const import CONF_DEVICE_TYPE, CONF_HOST, CONF_PASSWORD, CONF_PORT, DOMAIN, OPTION_INPUT_NAMES @@ -51,6 +52,11 @@ async def async_step_user(self, user_input: dict[str, Any] | None = None): model_name = await extron_device.query_model_name() title = f"Extron {model_name}" + # Make a unique ID for the entry, prevent adding the same device twice + unique_id = format_mac(await extron_device.query_mac_address()) + await self.async_set_unique_id(unique_id) + self._abort_if_unique_id_configured() + # Disconnect, we'll connect again later, this was just for validation await extron_device.disconnect() except AuthenticationError: