Skip to content

Commit

Permalink
Implement Zeroconf auto-discovery
Browse files Browse the repository at this point in the history
Untested since I'm not able to access the network
  • Loading branch information
Jalle19 committed Sep 10, 2024
1 parent af7ac9b commit 8995c57
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
20 changes: 20 additions & 0 deletions custom_components/vinx/config_flow.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
import logging

from typing import Any

import voluptuous as vol

from homeassistant.components.zeroconf import ZeroconfServiceInfo
from homeassistant.config_entries import ConfigFlow, ConfigFlowResult
from homeassistant.helpers.device_registry import format_mac

from .const import CONF_HOST, CONF_PORT, DOMAIN
from .lw3 import LW3

_LOGGER = logging.getLogger(__name__)


class VinxConfigFlow(ConfigFlow, domain=DOMAIN):
VERSION = 1
Expand Down Expand Up @@ -52,3 +57,18 @@ async def async_step_user(self, user_input: dict[str, Any] | None = None) -> Con
return self.async_create_entry(title=title, data=user_input)

return self.async_show_form(step_id="user", data_schema=self.schema, errors=errors)

async def async_step_zeroconf(self, discovery_info: ZeroconfServiceInfo) -> ConfigFlowResult:
_LOGGER.info(f"Zeroconf discovery info: {discovery_info}")

# Abort if the device is already configured
unique_id = format_mac(discovery_info.properties.get("mac"))
await self.async_set_unique_id(unique_id)
self._abort_if_unique_id_configured()

# Pre-populate the form
self.host = discovery_info.ip_address
self.port = discovery_info.port

# Trigger the user configuration flow
return await self.async_step_user()
2 changes: 1 addition & 1 deletion custom_components/vinx/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@
"iot_class": "local_polling",
"requirements": [],
"ssdp": [],
"zeroconf": []
"zeroconf": ["_lwr3._tcp.local."]
}

0 comments on commit 8995c57

Please sign in to comment.