From a65a0b5903dfcdef9489ce07c3c43d0162e36e45 Mon Sep 17 00:00:00 2001 From: Otto Winter Date: Mon, 24 Jan 2022 08:29:29 +0100 Subject: [PATCH] ESPHome prevent connecting to different hosts when IP reassigned (#64559) --- homeassistant/components/esphome/__init__.py | 9 +++++++++ homeassistant/components/esphome/manifest.json | 2 +- requirements_all.txt | 2 +- requirements_test_all.txt | 2 +- 4 files changed, 12 insertions(+), 3 deletions(-) diff --git a/homeassistant/components/esphome/__init__.py b/homeassistant/components/esphome/__init__.py index a50c1e8c944..7e799a83ee2 100644 --- a/homeassistant/components/esphome/__init__.py +++ b/homeassistant/components/esphome/__init__.py @@ -13,6 +13,7 @@ from aioesphomeapi import ( APIConnectionError, APIIntEnum, APIVersion, + BadNameAPIError, DeviceInfo as EsphomeDeviceInfo, EntityCategory as EsphomeEntityCategory, EntityInfo, @@ -269,6 +270,7 @@ async def async_setup_entry( # noqa: C901 entry_data.api_version = cli.api_version entry_data.available = True if entry_data.device_info.name: + cli.expected_name = entry_data.device_info.name reconnect_logic.name = entry_data.device_info.name device_id = _async_setup_device_registry( hass, entry, entry_data.device_info @@ -300,6 +302,12 @@ async def async_setup_entry( # noqa: C901 """Start reauth flow if appropriate connect error type.""" if isinstance(err, (RequiresEncryptionAPIError, InvalidEncryptionKeyAPIError)): entry.async_start_reauth(hass) + if isinstance(err, BadNameAPIError): + _LOGGER.warning( + "Name of device %s changed to %s, potentially due to IP reassignment", + cli.expected_name, + err.received_name, + ) reconnect_logic = ReconnectLogic( client=cli, @@ -317,6 +325,7 @@ async def async_setup_entry( # noqa: C901 await _setup_services(hass, entry_data, services) if entry_data.device_info is not None and entry_data.device_info.name: + cli.expected_name = entry_data.device_info.name reconnect_logic.name = entry_data.device_info.name await reconnect_logic.start() diff --git a/homeassistant/components/esphome/manifest.json b/homeassistant/components/esphome/manifest.json index 0cb0fb546b7..042bf930d0e 100644 --- a/homeassistant/components/esphome/manifest.json +++ b/homeassistant/components/esphome/manifest.json @@ -3,7 +3,7 @@ "name": "ESPHome", "config_flow": true, "documentation": "https://www.home-assistant.io/integrations/esphome", - "requirements": ["aioesphomeapi==10.7.0"], + "requirements": ["aioesphomeapi==10.8.1"], "zeroconf": ["_esphomelib._tcp.local."], "codeowners": ["@OttoWinter", "@jesserockz"], "after_dependencies": ["zeroconf", "tag"], diff --git a/requirements_all.txt b/requirements_all.txt index 6b31008c514..64fd43d25f9 100644 --- a/requirements_all.txt +++ b/requirements_all.txt @@ -166,7 +166,7 @@ aioeagle==1.1.0 aioemonitor==1.0.5 # homeassistant.components.esphome -aioesphomeapi==10.7.0 +aioesphomeapi==10.8.1 # homeassistant.components.flo aioflo==2021.11.0 diff --git a/requirements_test_all.txt b/requirements_test_all.txt index b6c091c6935..8fe696a8aef 100644 --- a/requirements_test_all.txt +++ b/requirements_test_all.txt @@ -119,7 +119,7 @@ aioeagle==1.1.0 aioemonitor==1.0.5 # homeassistant.components.esphome -aioesphomeapi==10.7.0 +aioesphomeapi==10.8.1 # homeassistant.components.flo aioflo==2021.11.0