Handle invalid utf-8 from the ESPHome dashboard (#95743)

If the yaml file has invalid utf-8, the config flow would raise an
unhandled exception. Allow the encryption key to be entered manually
in this case instead of a hard failure

fixes #92772
This commit is contained in:
J. Nick Koston 2023-07-02 21:47:25 -05:00 committed by GitHub
parent 75bdb03363
commit 7bdd64a3f7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 13 additions and 1 deletions

View File

@ -3,6 +3,7 @@ from __future__ import annotations
from collections import OrderedDict from collections import OrderedDict
from collections.abc import Mapping from collections.abc import Mapping
import json
import logging import logging
from typing import Any from typing import Any
@ -408,6 +409,11 @@ class EsphomeFlowHandler(ConfigFlow, domain=DOMAIN):
except aiohttp.ClientError as err: except aiohttp.ClientError as err:
_LOGGER.error("Error talking to the dashboard: %s", err) _LOGGER.error("Error talking to the dashboard: %s", err)
return False return False
except json.JSONDecodeError as err:
_LOGGER.error(
"Error parsing response from dashboard: %s", err, exc_info=True
)
return False
self._noise_psk = noise_psk self._noise_psk = noise_psk
return True return True

View File

@ -1,5 +1,6 @@
"""Test config flow.""" """Test config flow."""
import asyncio import asyncio
import json
from unittest.mock import AsyncMock, MagicMock, patch from unittest.mock import AsyncMock, MagicMock, patch
from aioesphomeapi import ( from aioesphomeapi import (
@ -414,8 +415,13 @@ async def test_user_discovers_name_and_gets_key_from_dashboard(
assert mock_client.noise_psk == VALID_NOISE_PSK assert mock_client.noise_psk == VALID_NOISE_PSK
@pytest.mark.parametrize(
"dashboard_exception",
[aiohttp.ClientError(), json.JSONDecodeError("test", "test", 0)],
)
async def test_user_discovers_name_and_gets_key_from_dashboard_fails( async def test_user_discovers_name_and_gets_key_from_dashboard_fails(
hass: HomeAssistant, hass: HomeAssistant,
dashboard_exception: Exception,
mock_client, mock_client,
mock_dashboard, mock_dashboard,
mock_zeroconf: None, mock_zeroconf: None,
@ -442,7 +448,7 @@ async def test_user_discovers_name_and_gets_key_from_dashboard_fails(
with patch( with patch(
"homeassistant.components.esphome.dashboard.ESPHomeDashboardAPI.get_encryption_key", "homeassistant.components.esphome.dashboard.ESPHomeDashboardAPI.get_encryption_key",
side_effect=aiohttp.ClientError, side_effect=dashboard_exception,
): ):
result = await hass.config_entries.flow.async_init( result = await hass.config_entries.flow.async_init(
"esphome", "esphome",