Skip to content

Commit

Permalink
feat: use name from bridge as added integration name
Browse files Browse the repository at this point in the history
  • Loading branch information
andersevenrud committed Jan 1, 2023
1 parent a866d54 commit 3a67d1f
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 20 deletions.
28 changes: 9 additions & 19 deletions custom_components/nexa_bridge_x/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,38 +32,27 @@
)


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):
"""Handle a config flow for Nexa Bridge X."""

VERSION = 1

_discovered_name: str | None = None
_discovered_host: str | None = None
_discovered_username: str | None = None
_discovered_password: str | None = None
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
)

Expand Down
4 changes: 3 additions & 1 deletion custom_components/nexa_bridge_x/nexa.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand All @@ -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")
Expand Down

0 comments on commit 3a67d1f

Please sign in to comment.