From 3a67d1f12572d0cb959415b99ed3fec96b44e9c4 Mon Sep 17 00:00:00 2001 From: Anders Evenrud Date: Sun, 1 Jan 2023 01:08:46 +0100 Subject: [PATCH] feat: use name from bridge as added integration name --- .../nexa_bridge_x/config_flow.py | 28 ++++++------------- custom_components/nexa_bridge_x/nexa.py | 4 ++- 2 files changed, 12 insertions(+), 20 deletions(-) diff --git a/custom_components/nexa_bridge_x/config_flow.py b/custom_components/nexa_bridge_x/config_flow.py index 23cd87a..a7e4200 100644 --- a/custom_components/nexa_bridge_x/config_flow.py +++ b/custom_components/nexa_bridge_x/config_flow.py @@ -32,31 +32,19 @@ ) -class PlaceholderHub: - def __init__(self, host: str) -> None: - self.host = host - - async def authenticate(self, username: str, password: str) -> bool: - try: - api = NexaApi(self.host, username, password) - await api.test_connection() - except Exception: # pylint: disable=broad-except - return False - - return True - - async def validate_input(hass: HomeAssistant, data: dict[str, Any]) -> dict[str, Any]: """Validate the user input allows us to connect. Data has the keys from STEP_USER_DATA_SCHEMA with values provided by the user. """ - hub = PlaceholderHub(data["host"]) - if not await hub.authenticate(data["username"], data["password"]): + try: + api = NexaApi(data["host"], data["username"], data["password"]) + info = await api.test_connection() + except Exception: raise InvalidAuth - return {"title": "Nexa Bridge X"} + return {"title": info["name"]} class NexaBridgeXFlowHandler(ConfigFlow, domain=DOMAIN): @@ -64,6 +52,7 @@ class NexaBridgeXFlowHandler(ConfigFlow, domain=DOMAIN): VERSION = 1 + _discovered_name: str | None = None _discovered_host: str | None = None _discovered_username: str | None = None _discovered_password: str | None = None @@ -118,10 +107,11 @@ async def async_step_zeroconf( try: api = NexaApi(host, 'nexa', 'nexa') - await api.test_connection() + info = await api.test_connection() except Exception: # pylint: disable=broad-except return self.async_abort(reason="unknown") + self._discovered_name = info["name"] self._discovered_host = host self._discovered_username = username self._discovered_password = password @@ -158,7 +148,7 @@ async def async_step_discovery_confirm( ) return self.async_create_entry( - title="Nexa Bridge X", + title=self._discovered_name, data=form ) diff --git a/custom_components/nexa_bridge_x/nexa.py b/custom_components/nexa_bridge_x/nexa.py index ae240b4..e865428 100644 --- a/custom_components/nexa_bridge_x/nexa.py +++ b/custom_components/nexa_bridge_x/nexa.py @@ -140,7 +140,7 @@ async def request( async with session.get(url, auth=auth) as response: return await self.handle_response(response) - async def test_connection(self) -> None: + async def test_connection(self) -> NexaInfoData: """See if the connection is valid""" result = await self.fetch_info() @@ -158,6 +158,8 @@ async def test_connection(self) -> None: if not str(result["version"]).startswith("2"): raise NexaApiNotCompatibleError("Endpoint not compatible") + return result + async def fetch_info(self) -> NexaInfoData: """Get information about bridge""" return await self.request("get", "info")