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
pull/95660/head^2
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.abc import Mapping
import json
import logging
from typing import Any
@ -408,6 +409,11 @@ class EsphomeFlowHandler(ConfigFlow, domain=DOMAIN):
except aiohttp.ClientError as err:
_LOGGER.error("Error talking to the dashboard: %s", err)
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
return True

View File

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