Reload ESPHome config entries when dashboard info received (#86174)

This commit is contained in:
Paulus Schoutsen
2023-01-18 17:59:55 +01:00
committed by GitHub
parent c40c37e9ee
commit 29337bc6eb
10 changed files with 106 additions and 70 deletions
+11
View File
@@ -98,3 +98,14 @@ def mock_client(mock_device_info):
"homeassistant.components.esphome.config_flow.APIClient", mock_client
):
yield mock_client
@pytest.fixture
def mock_dashboard():
"""Mock dashboard."""
data = {"configured": [], "importable": []}
with patch(
"esphome_dashboard_api.ESPHomeDashboardAPI.get_devices",
return_value=data,
):
yield data
+29 -34
View File
@@ -471,9 +471,10 @@ async def test_reauth_confirm_valid(hass, mock_client, mock_zeroconf):
assert entry.data[CONF_NOISE_PSK] == VALID_NOISE_PSK
async def test_reauth_fixed_via_dashboard(hass, mock_client, mock_zeroconf):
async def test_reauth_fixed_via_dashboard(
hass, mock_client, mock_zeroconf, mock_dashboard
):
"""Test reauth fixed automatically via dashboard."""
dashboard.async_set_dashboard_info(hass, "mock-slug", "mock-host", 6052)
entry = MockConfigEntry(
domain=DOMAIN,
@@ -488,17 +489,16 @@ async def test_reauth_fixed_via_dashboard(hass, mock_client, mock_zeroconf):
mock_client.device_info.return_value = DeviceInfo(uses_password=False, name="test")
mock_dashboard["configured"].append(
{
"name": "test",
"configuration": "test.yaml",
}
)
await dashboard.async_set_dashboard_info(hass, "mock-slug", "mock-host", 6052)
with patch(
"homeassistant.components.esphome.dashboard.ESPHomeDashboardAPI.get_devices",
return_value={
"configured": [
{
"name": "test",
"configuration": "test.yaml",
}
]
},
), patch(
"homeassistant.components.esphome.dashboard.ESPHomeDashboardAPI.get_encryption_key",
return_value=VALID_NOISE_PSK,
) as mock_get_encryption_key:
@@ -511,7 +511,7 @@ async def test_reauth_fixed_via_dashboard(hass, mock_client, mock_zeroconf):
},
)
assert result["type"] == FlowResultType.ABORT
assert result["type"] == FlowResultType.ABORT, result
assert result["reason"] == "reauth_successful"
assert entry.data[CONF_NOISE_PSK] == VALID_NOISE_PSK
@@ -672,7 +672,9 @@ async def test_discovery_hassio(hass):
assert dash.addon_slug == "mock-slug"
async def test_zeroconf_encryption_key_via_dashboard(hass, mock_client, mock_zeroconf):
async def test_zeroconf_encryption_key_via_dashboard(
hass, mock_client, mock_zeroconf, mock_dashboard
):
"""Test encryption key retrieved from dashboard."""
service_info = zeroconf.ZeroconfServiceInfo(
host="192.168.43.183",
@@ -692,7 +694,14 @@ async def test_zeroconf_encryption_key_via_dashboard(hass, mock_client, mock_zer
assert flow["type"] == FlowResultType.FORM
assert flow["step_id"] == "discovery_confirm"
dashboard.async_set_dashboard_info(hass, "mock-slug", "mock-host", 6052)
mock_dashboard["configured"].append(
{
"name": "test8266",
"configuration": "test8266.yaml",
}
)
await dashboard.async_set_dashboard_info(hass, "mock-slug", "mock-host", 6052)
mock_client.device_info.side_effect = [
RequiresEncryptionAPIError,
@@ -704,16 +713,6 @@ async def test_zeroconf_encryption_key_via_dashboard(hass, mock_client, mock_zer
]
with patch(
"homeassistant.components.esphome.dashboard.ESPHomeDashboardAPI.get_devices",
return_value={
"configured": [
{
"name": "test8266",
"configuration": "test8266.yaml",
}
]
},
), patch(
"homeassistant.components.esphome.dashboard.ESPHomeDashboardAPI.get_encryption_key",
return_value=VALID_NOISE_PSK,
) as mock_get_encryption_key:
@@ -736,7 +735,7 @@ async def test_zeroconf_encryption_key_via_dashboard(hass, mock_client, mock_zer
async def test_zeroconf_no_encryption_key_via_dashboard(
hass, mock_client, mock_zeroconf
hass, mock_client, mock_zeroconf, mock_dashboard
):
"""Test encryption key not retrieved from dashboard."""
service_info = zeroconf.ZeroconfServiceInfo(
@@ -757,17 +756,13 @@ async def test_zeroconf_no_encryption_key_via_dashboard(
assert flow["type"] == FlowResultType.FORM
assert flow["step_id"] == "discovery_confirm"
dashboard.async_set_dashboard_info(hass, "mock-slug", "mock-host", 6052)
await dashboard.async_set_dashboard_info(hass, "mock-slug", "mock-host", 6052)
mock_client.device_info.side_effect = RequiresEncryptionAPIError
with patch(
"homeassistant.components.esphome.dashboard.ESPHomeDashboardAPI.get_devices",
return_value={"configured": []},
):
result = await hass.config_entries.flow.async_configure(
flow["flow_id"], user_input={}
)
result = await hass.config_entries.flow.async_configure(
flow["flow_id"], user_input={}
)
assert result["type"] == FlowResultType.FORM
assert result["step_id"] == "encryption_key"
@@ -0,0 +1,22 @@
"""Test ESPHome dashboard features."""
from unittest.mock import patch
from homeassistant.components.esphome import dashboard
from homeassistant.config_entries import ConfigEntryState
async def test_new_info_reload_config_entries(hass, init_integration, mock_dashboard):
"""Test config entries are reloaded when new info is set."""
assert init_integration.state == ConfigEntryState.LOADED
with patch("homeassistant.components.esphome.async_setup_entry") as mock_setup:
await dashboard.async_set_dashboard_info(hass, "test-slug", "test-host", 6052)
assert len(mock_setup.mock_calls) == 1
assert mock_setup.mock_calls[0][1][1] == init_integration
# Test it's a no-op when the same info is set
with patch("homeassistant.components.esphome.async_setup_entry") as mock_setup:
await dashboard.async_set_dashboard_info(hass, "test-slug", "test-host", 6052)
assert len(mock_setup.mock_calls) == 0
+3 -4
View File
@@ -37,21 +37,20 @@ async def test_update_entity(
hass,
mock_config_entry,
mock_device_info,
mock_dashboard,
devices_payload,
expected_state,
expected_attributes,
):
"""Test ESPHome update entity."""
async_set_dashboard_info(hass, "mock-addon-slug", "mock-addon-host", 1234)
mock_dashboard["configured"] = devices_payload
await async_set_dashboard_info(hass, "mock-addon-slug", "mock-addon-host", 1234)
mock_config_entry.add_to_hass(hass)
with patch(
"homeassistant.components.esphome.update.DomainData.get_entry_data",
return_value=Mock(available=True, device_info=mock_device_info),
), patch(
"homeassistant.components.esphome.dashboard.ESPHomeDashboardAPI.get_devices",
return_value={"configured": devices_payload},
):
assert await hass.config_entries.async_forward_entry_setup(
mock_config_entry, "update"