Add name and slug to supervisor discovery info (#80094)

This commit is contained in:
Martin Hjelmare 2022-10-11 16:56:45 +02:00 committed by GitHub
parent 918243b7c8
commit 62c4cd3c26
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 160 additions and 50 deletions

View File

@ -17,7 +17,7 @@ from homeassistant.data_entry_flow import BaseServiceInfo
from homeassistant.helpers import discovery_flow
from .const import ATTR_ADDON, ATTR_CONFIG, ATTR_DISCOVERY, ATTR_UUID
from .handler import HassioAPIError
from .handler import HassIO, HassioAPIError
_LOGGER = logging.getLogger(__name__)
@ -27,6 +27,8 @@ class HassioServiceInfo(BaseServiceInfo):
"""Prepared info from hassio entries."""
config: dict[str, Any]
name: str
slug: str
@callback
@ -62,7 +64,7 @@ class HassIODiscovery(HomeAssistantView):
name = "api:hassio_push:discovery"
url = "/api/hassio_push/discovery/{uuid}"
def __init__(self, hass: HomeAssistant, hassio):
def __init__(self, hass: HomeAssistant, hassio: HassIO) -> None:
"""Initialize WebView."""
self.hass = hass
self.hassio = hassio
@ -86,25 +88,28 @@ class HassIODiscovery(HomeAssistantView):
await self.async_process_del(data)
return web.Response()
async def async_process_new(self, data):
async def async_process_new(self, data: dict[str, Any]) -> None:
"""Process add discovery entry."""
service = data[ATTR_SERVICE]
config_data = data[ATTR_CONFIG]
service: str = data[ATTR_SERVICE]
config_data: dict[str, Any] = data[ATTR_CONFIG]
slug: str = data[ATTR_ADDON]
# Read additional Add-on info
try:
addon_info = await self.hassio.get_addon_info(data[ATTR_ADDON])
addon_info = await self.hassio.get_addon_info(slug)
except HassioAPIError as err:
_LOGGER.error("Can't read add-on info: %s", err)
return
config_data[ATTR_ADDON] = addon_info[ATTR_NAME]
name: str = addon_info[ATTR_NAME]
config_data[ATTR_ADDON] = name
# Use config flow
discovery_flow.async_create_flow(
self.hass,
service,
context={"source": config_entries.SOURCE_HASSIO},
data=HassioServiceInfo(config=config_data),
data=HassioServiceInfo(config=config_data, name=name, slug=slug),
)
async def async_process_del(self, data):

View File

@ -127,7 +127,9 @@ async def test_hassio_already_configured(hass: HomeAssistant) -> None:
"addon": "AdGuard Home Addon",
"host": "mock-adguard",
"port": "3000",
}
},
name="AdGuard Home Addon",
slug="adguard",
),
context={"source": config_entries.SOURCE_HASSIO},
)
@ -149,7 +151,9 @@ async def test_hassio_ignored(hass: HomeAssistant) -> None:
"addon": "AdGuard Home Addon",
"host": "mock-adguard",
"port": "3000",
}
},
name="AdGuard Home Addon",
slug="adguard",
),
context={"source": config_entries.SOURCE_HASSIO},
)
@ -171,7 +175,13 @@ async def test_hassio_confirm(
result = await hass.config_entries.flow.async_init(
DOMAIN,
data=HassioServiceInfo(
config={"addon": "AdGuard Home Addon", "host": "mock-adguard", "port": 3000}
config={
"addon": "AdGuard Home Addon",
"host": "mock-adguard",
"port": 3000,
},
name="AdGuard Home Addon",
slug="adguard",
),
context={"source": config_entries.SOURCE_HASSIO},
)
@ -207,7 +217,13 @@ async def test_hassio_connection_error(
result = await hass.config_entries.flow.async_init(
DOMAIN,
data=HassioServiceInfo(
config={"addon": "AdGuard Home Addon", "host": "mock-adguard", "port": 3000}
config={
"addon": "AdGuard Home Addon",
"host": "mock-adguard",
"port": 3000,
},
name="AdGuard Home Addon",
slug="adguard",
),
context={"source": config_entries.SOURCE_HASSIO},
)

View File

@ -53,7 +53,9 @@ async def test_hassio(hass):
DOMAIN,
context={"source": config_entries.SOURCE_HASSIO},
data=HassioServiceInfo(
config={"addon": "Almond add-on", "host": "almond-addon", "port": "1234"}
config={"addon": "Almond add-on", "host": "almond-addon", "port": "1234"},
name="Almond add-on",
slug="almond",
),
)
@ -90,7 +92,9 @@ async def test_abort_if_existing_entry(hass):
assert result["type"] == data_entry_flow.FlowResultType.ABORT
assert result["reason"] == "single_instance_allowed"
result = await flow.async_step_hassio(HassioServiceInfo(config={}))
result = await flow.async_step_hassio(
HassioServiceInfo(config={}, name="Almond add-on", slug="almond")
)
assert result["type"] == data_entry_flow.FlowResultType.ABORT
assert result["reason"] == "single_instance_allowed"

View File

@ -545,7 +545,9 @@ async def test_flow_hassio_discovery(hass):
CONF_PORT: 80,
CONF_SERIAL: BRIDGEID,
CONF_API_KEY: API_KEY,
}
},
name="Mock Addon",
slug="deconz",
),
context={"source": SOURCE_HASSIO},
)
@ -593,7 +595,9 @@ async def test_hassio_discovery_update_configuration(hass, aioclient_mock):
CONF_PORT: 8080,
CONF_API_KEY: "updated",
CONF_SERIAL: BRIDGEID,
}
},
name="Mock Addon",
slug="deconz",
),
context={"source": SOURCE_HASSIO},
)
@ -619,7 +623,9 @@ async def test_hassio_discovery_dont_update_configuration(hass, aioclient_mock):
CONF_PORT: 80,
CONF_API_KEY: API_KEY,
CONF_SERIAL: BRIDGEID,
}
},
name="Mock Addon",
slug="deconz",
),
context={"source": SOURCE_HASSIO},
)

View File

@ -5,7 +5,7 @@ from unittest.mock import AsyncMock, Mock, patch
import pytest
from homeassistant import config_entries
from homeassistant.components.hassio import HassioServiceInfo
from homeassistant.components.hassio.discovery import HassioServiceInfo
from homeassistant.components.hassio.handler import HassioAPIError
from homeassistant.components.mqtt import DOMAIN as MQTT_DOMAIN
from homeassistant.const import EVENT_HOMEASSISTANT_START, EVENT_HOMEASSISTANT_STARTED
@ -14,8 +14,8 @@ from homeassistant.setup import async_setup_component
from tests.common import MockModule, mock_entity_platform, mock_integration
@pytest.fixture
async def mock_mqtt(hass):
@pytest.fixture(name="mock_mqtt")
async def mock_mqtt_fixture(hass):
"""Mock the MQTT integration's config flow."""
mock_integration(hass, MockModule(MQTT_DOMAIN))
mock_entity_platform(hass, f"config_flow.{MQTT_DOMAIN}", None)
@ -78,7 +78,9 @@ async def test_hassio_discovery_startup(hass, aioclient_mock, hassio_client, moc
"password": "mock-pass",
"protocol": "3.1.1",
"addon": "Mosquitto Test",
}
},
name="Mosquitto Test",
slug="mosquitto",
)
)
@ -140,7 +142,9 @@ async def test_hassio_discovery_startup_done(
"password": "mock-pass",
"protocol": "3.1.1",
"addon": "Mosquitto Test",
}
},
name="Mosquitto Test",
slug="mosquitto",
)
)
@ -190,6 +194,8 @@ async def test_hassio_discovery_webhook(hass, aioclient_mock, hassio_client, moc
"password": "mock-pass",
"protocol": "3.1.1",
"addon": "Mosquitto Test",
}
},
name="Mosquitto Test",
slug="mosquitto",
)
)

View File

@ -75,7 +75,11 @@ async def test_hassio_success(hass: HomeAssistant) -> None:
result = await hass.config_entries.flow.async_init(
DOMAIN,
data=HassioServiceInfo(config={"addon": "motionEye", "url": TEST_URL}),
data=HassioServiceInfo(
config={"addon": "motionEye", "url": TEST_URL},
name="motionEye",
slug="motioneye",
),
context={"source": config_entries.SOURCE_HASSIO},
)
@ -351,7 +355,11 @@ async def test_hassio_already_configured(hass: HomeAssistant) -> None:
result = await hass.config_entries.flow.async_init(
DOMAIN,
data=HassioServiceInfo(config={"addon": "motionEye", "url": TEST_URL}),
data=HassioServiceInfo(
config={"addon": "motionEye", "url": TEST_URL},
name="motionEye",
slug="motioneye",
),
context={"source": config_entries.SOURCE_HASSIO},
)
assert result.get("type") == data_entry_flow.FlowResultType.ABORT
@ -366,7 +374,11 @@ async def test_hassio_ignored(hass: HomeAssistant) -> None:
result = await hass.config_entries.flow.async_init(
DOMAIN,
data=HassioServiceInfo(config={"addon": "motionEye", "url": TEST_URL}),
data=HassioServiceInfo(
config={"addon": "motionEye", "url": TEST_URL},
name="motionEye",
slug="motioneye",
),
context={"source": config_entries.SOURCE_HASSIO},
)
assert result.get("type") == data_entry_flow.FlowResultType.ABORT
@ -382,7 +394,11 @@ async def test_hassio_abort_if_already_in_progress(hass: HomeAssistant) -> None:
result2 = await hass.config_entries.flow.async_init(
DOMAIN,
data=HassioServiceInfo(config={"addon": "motionEye", "url": TEST_URL}),
data=HassioServiceInfo(
config={"addon": "motionEye", "url": TEST_URL},
name="motionEye",
slug="motioneye",
),
context={"source": config_entries.SOURCE_HASSIO},
)
assert result2.get("type") == data_entry_flow.FlowResultType.ABORT
@ -394,7 +410,11 @@ async def test_hassio_clean_up_on_user_flow(hass: HomeAssistant) -> None:
result = await hass.config_entries.flow.async_init(
DOMAIN,
data=HassioServiceInfo(config={"addon": "motionEye", "url": TEST_URL}),
data=HassioServiceInfo(
config={"addon": "motionEye", "url": TEST_URL},
name="motionEye",
slug="motioneye",
),
context={"source": config_entries.SOURCE_HASSIO},
)
assert result.get("type") == data_entry_flow.FlowResultType.FORM

View File

@ -237,7 +237,9 @@ async def test_hassio_ignored(hass: HomeAssistant) -> None:
"host": "mock-mosquitto",
"port": "1883",
"protocol": "3.1.1",
}
},
name="Mosquitto",
slug="mosquitto",
),
context={"source": config_entries.SOURCE_HASSIO},
)
@ -261,7 +263,9 @@ async def test_hassio_confirm(hass, mock_try_connection_success, mock_finish_set
"password": "mock-pass",
"protocol": "3.1.1", # Set by the addon's discovery, ignored by HA
"ssl": False, # Set by the addon's discovery, ignored by HA
}
},
name="Mock Addon",
slug="mosquitto",
),
context={"source": config_entries.SOURCE_HASSIO},
)
@ -305,7 +309,9 @@ async def test_hassio_cannot_connect(
"password": "mock-pass",
"protocol": "3.1.1", # Set by the addon's discovery, ignored by HA
"ssl": False, # Set by the addon's discovery, ignored by HA
}
},
name="Mock Addon",
slug="mosquitto",
),
context={"source": config_entries.SOURCE_HASSIO},
)

View File

@ -124,7 +124,9 @@ async def test_hassio_discovery(hass):
"addon": "RTSPtoWebRTC",
"host": "fake-server",
"port": 8083,
}
},
name="RTSPtoWebRTC",
slug="rtsp-to-webrtc",
),
context={"source": config_entries.SOURCE_HASSIO},
)
@ -161,7 +163,9 @@ async def test_hassio_single_config_entry(hass: HomeAssistant) -> None:
"addon": "RTSPtoWebRTC",
"host": "fake-server",
"port": 8083,
}
},
name="RTSPtoWebRTC",
slug="rtsp-to-webrtc",
),
context={"source": config_entries.SOURCE_HASSIO},
)
@ -181,7 +185,9 @@ async def test_hassio_ignored(hass: HomeAssistant) -> None:
"addon": "RTSPtoWebRTC",
"host": "fake-server",
"port": 8083,
}
},
name="RTSPtoWebRTC",
slug="rtsp-to-webrtc",
),
context={"source": config_entries.SOURCE_HASSIO},
)
@ -198,7 +204,9 @@ async def test_hassio_discovery_server_failure(hass: HomeAssistant) -> None:
"addon": "RTSPtoWebRTC",
"host": "fake-server",
"port": 8083,
}
},
name="RTSPtoWebRTC",
slug="rtsp-to-webrtc",
),
context={"source": config_entries.SOURCE_HASSIO},
)

View File

@ -246,8 +246,10 @@ async def test_hassio_flow(hass: HomeAssistant) -> None:
"host": "1.1.1.1",
"port": 8888,
"name": "custom name",
"addon": "vlc",
}
"addon": "VLC",
},
name="VLC",
slug="vlc",
)
result = await hass.config_entries.flow.async_init(
@ -284,7 +286,7 @@ async def test_hassio_already_configured(hass: HomeAssistant) -> None:
result = await hass.config_entries.flow.async_init(
DOMAIN,
context={"source": config_entries.SOURCE_HASSIO},
data=HassioServiceInfo(config=entry_data),
data=HassioServiceInfo(config=entry_data, name="VLC", slug="vlc"),
)
await hass.async_block_till_done()
@ -324,8 +326,10 @@ async def test_hassio_errors(
"host": "1.1.1.1",
"port": 8888,
"name": "custom name",
"addon": "vlc",
}
"addon": "VLC",
},
name="VLC",
slug="vlc",
),
)
await hass.async_block_till_done()

View File

@ -15,7 +15,7 @@ from homeassistant.components.hassio import HassioServiceInfo
from homeassistant.components.hassio.handler import HassioAPIError
from homeassistant.components.zeroconf import ZeroconfServiceInfo
from homeassistant.components.zwave_js.config_flow import SERVER_VERSION_TIMEOUT, TITLE
from homeassistant.components.zwave_js.const import DOMAIN
from homeassistant.components.zwave_js.const import ADDON_SLUG, DOMAIN
from tests.common import MockConfigEntry
@ -326,7 +326,11 @@ async def test_supervisor_discovery(
result = await hass.config_entries.flow.async_init(
DOMAIN,
context={"source": config_entries.SOURCE_HASSIO},
data=HassioServiceInfo(config=ADDON_DISCOVERY_INFO),
data=HassioServiceInfo(
config=ADDON_DISCOVERY_INFO,
name="Z-Wave JS",
slug=ADDON_SLUG,
),
)
with patch(
@ -366,7 +370,11 @@ async def test_supervisor_discovery_cannot_connect(
result = await hass.config_entries.flow.async_init(
DOMAIN,
context={"source": config_entries.SOURCE_HASSIO},
data=HassioServiceInfo(config=ADDON_DISCOVERY_INFO),
data=HassioServiceInfo(
config=ADDON_DISCOVERY_INFO,
name="Z-Wave JS",
slug=ADDON_SLUG,
),
)
assert result["type"] == "abort"
@ -388,7 +396,11 @@ async def test_clean_discovery_on_user_create(
result = await hass.config_entries.flow.async_init(
DOMAIN,
context={"source": config_entries.SOURCE_HASSIO},
data=HassioServiceInfo(config=ADDON_DISCOVERY_INFO),
data=HassioServiceInfo(
config=ADDON_DISCOVERY_INFO,
name="Z-Wave JS",
slug=ADDON_SLUG,
),
)
assert result["type"] == "form"
@ -454,7 +466,11 @@ async def test_abort_discovery_with_existing_entry(
result = await hass.config_entries.flow.async_init(
DOMAIN,
context={"source": config_entries.SOURCE_HASSIO},
data=HassioServiceInfo(config=ADDON_DISCOVERY_INFO),
data=HassioServiceInfo(
config=ADDON_DISCOVERY_INFO,
name="Z-Wave JS",
slug=ADDON_SLUG,
),
)
assert result["type"] == "abort"
@ -478,7 +494,11 @@ async def test_abort_hassio_discovery_with_existing_flow(
result2 = await hass.config_entries.flow.async_init(
DOMAIN,
context={"source": config_entries.SOURCE_HASSIO},
data=HassioServiceInfo(config=ADDON_DISCOVERY_INFO),
data=HassioServiceInfo(
config=ADDON_DISCOVERY_INFO,
name="Z-Wave JS",
slug=ADDON_SLUG,
),
)
assert result2["type"] == "abort"
@ -673,7 +693,11 @@ async def test_discovery_addon_not_running(
result = await hass.config_entries.flow.async_init(
DOMAIN,
context={"source": config_entries.SOURCE_HASSIO},
data=HassioServiceInfo(config=ADDON_DISCOVERY_INFO),
data=HassioServiceInfo(
config=ADDON_DISCOVERY_INFO,
name="Z-Wave JS",
slug=ADDON_SLUG,
),
)
assert result["step_id"] == "hassio_confirm"
@ -753,7 +777,11 @@ async def test_discovery_addon_not_installed(
result = await hass.config_entries.flow.async_init(
DOMAIN,
context={"source": config_entries.SOURCE_HASSIO},
data=HassioServiceInfo(config=ADDON_DISCOVERY_INFO),
data=HassioServiceInfo(
config=ADDON_DISCOVERY_INFO,
name="Z-Wave JS",
slug=ADDON_SLUG,
),
)
assert result["step_id"] == "hassio_confirm"
@ -834,7 +862,11 @@ async def test_abort_usb_discovery_with_existing_flow(hass, supervisor, addon_op
result = await hass.config_entries.flow.async_init(
DOMAIN,
context={"source": config_entries.SOURCE_HASSIO},
data=HassioServiceInfo(config=ADDON_DISCOVERY_INFO),
data=HassioServiceInfo(
config=ADDON_DISCOVERY_INFO,
name="Z-Wave JS",
slug=ADDON_SLUG,
),
)
assert result["type"] == "form"

View File

@ -2507,7 +2507,10 @@ async def test_async_setup_update_entry(hass):
(config_entries.SOURCE_HOMEKIT, BaseServiceInfo()),
(config_entries.SOURCE_DHCP, BaseServiceInfo()),
(config_entries.SOURCE_ZEROCONF, BaseServiceInfo()),
(config_entries.SOURCE_HASSIO, HassioServiceInfo(config={})),
(
config_entries.SOURCE_HASSIO,
HassioServiceInfo(config={}, name="Test", slug="test"),
),
),
)
async def test_flow_with_default_discovery(hass, manager, discovery_source):