Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Motion Blinds dhcp discovery #68809

Merged
merged 17 commits into from Mar 28, 2022
Merged
17 changes: 15 additions & 2 deletions homeassistant/components/motion_blinds/config_flow.py
Expand Up @@ -5,9 +5,11 @@
import voluptuous as vol

from homeassistant import config_entries
from homeassistant.components import network
from homeassistant.components import dhcp, network
from homeassistant.const import CONF_API_KEY, CONF_HOST
from homeassistant.core import callback
from homeassistant.data_entry_flow import FlowResult
from homeassistant.helpers.device_registry import format_mac

from .const import (
CONF_INTERFACE,
Expand Down Expand Up @@ -72,6 +74,17 @@ def async_get_options_flow(config_entry) -> OptionsFlowHandler:
"""Get the options flow."""
return OptionsFlowHandler(config_entry)

async def async_step_dhcp(self, discovery_info: dhcp.DhcpServiceInfo) -> FlowResult:
"""Handle discovery via dhcp."""
mac_address = format_mac(discovery_info.macaddress).replace(":", "")
await self.async_set_unique_id(mac_address)
self._abort_if_unique_id_configured(updates={CONF_HOST: discovery_info.ip})

self.context.update({"title_placeholders": {"name": discovery_info.ip}})
starkillerOG marked this conversation as resolved.
Show resolved Hide resolved

self._host = discovery_info.ip
starkillerOG marked this conversation as resolved.
Show resolved Hide resolved
return await self.async_step_connect()

async def async_step_user(self, user_input=None):
"""Handle a flow initialized by the user."""
errors = {}
Expand Down Expand Up @@ -137,7 +150,7 @@ async def async_step_connect(self, user_input=None):

mac_address = motion_gateway.mac

await self.async_set_unique_id(mac_address)
await self.async_set_unique_id(mac_address, raise_on_progress=False)
self._abort_if_unique_id_configured(
updates={
CONF_HOST: self._host,
Expand Down
6 changes: 6 additions & 0 deletions homeassistant/components/motion_blinds/manifest.json
Expand Up @@ -5,6 +5,12 @@
"documentation": "https://www.home-assistant.io/integrations/motion_blinds",
"requirements": ["motionblinds==0.6.2"],
"dependencies": ["network"],
"dhcp": [
{"registered_devices": true},
{
"hostname": "motion_*"
}
],
"codeowners": ["@starkillerOG"],
"iot_class": "local_push",
"loggers": ["motionblinds"]
Expand Down
1 change: 1 addition & 0 deletions homeassistant/components/motion_blinds/strings.json
@@ -1,5 +1,6 @@
{
"config": {
"flow_title": "{name}",
starkillerOG marked this conversation as resolved.
Show resolved Hide resolved
"step": {
"user": {
"title": "Motion Blinds",
Expand Down
2 changes: 2 additions & 0 deletions homeassistant/generated/dhcp.py
Expand Up @@ -55,6 +55,8 @@
{'domain': 'lyric', 'hostname': 'lyric-*', 'macaddress': '48A2E6*'},
{'domain': 'lyric', 'hostname': 'lyric-*', 'macaddress': 'B82CA0*'},
{'domain': 'lyric', 'hostname': 'lyric-*', 'macaddress': '00D02D*'},
{'domain': 'motion_blinds', 'registered_devices': True},
{'domain': 'motion_blinds', 'hostname': 'motion_*'},
{'domain': 'myq', 'macaddress': '645299*'},
{'domain': 'nest', 'macaddress': '18B430*'},
{'domain': 'nest', 'macaddress': '641666*'},
Expand Down
31 changes: 31 additions & 0 deletions tests/components/motion_blinds/test_config_flow.py
Expand Up @@ -5,6 +5,7 @@
import pytest

from homeassistant import config_entries, data_entry_flow
from homeassistant.components import dhcp
from homeassistant.components.motion_blinds import const
from homeassistant.components.motion_blinds.config_flow import DEFAULT_GATEWAY_NAME
from homeassistant.const import CONF_API_KEY, CONF_HOST
Expand Down Expand Up @@ -337,6 +338,36 @@ async def test_config_flow_invalid_interface(hass):
assert result["errors"] == {const.CONF_INTERFACE: "invalid_interface"}


async def test_dhcp_flow(hass):
"""Successful flow from DHCP discovery."""
dhcp_data = dhcp.DhcpServiceInfo(
ip=TEST_HOST,
hostname="MOTION_abcdef",
macaddress=TEST_MAC,
)

result = await hass.config_entries.flow.async_init(
const.DOMAIN, context={"source": config_entries.SOURCE_DHCP}, data=dhcp_data
)

assert result["type"] == "form"
assert result["step_id"] == "connect"
assert result["errors"] == {}

result = await hass.config_entries.flow.async_configure(
result["flow_id"],
{CONF_API_KEY: TEST_API_KEY},
)

assert result["type"] == "create_entry"
assert result["title"] == DEFAULT_GATEWAY_NAME
assert result["data"] == {
CONF_HOST: TEST_HOST,
CONF_API_KEY: TEST_API_KEY,
const.CONF_INTERFACE: TEST_HOST_HA,
}


async def test_options_flow(hass):
"""Test specifying non default settings using options flow."""
config_entry = MockConfigEntry(
Expand Down