Use DhcpServiceInfo in roomba (#60056)

This commit is contained in:
epenet 2021-11-21 14:54:39 +01:00 committed by GitHub
parent 25e5263954
commit 435eb97495
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 63 additions and 62 deletions

View File

@ -8,9 +8,10 @@ from roombapy.getpassword import RoombaPassword
import voluptuous as vol import voluptuous as vol
from homeassistant import config_entries, core from homeassistant import config_entries, core
from homeassistant.components.dhcp import HOSTNAME, IP_ADDRESS from homeassistant.components import dhcp
from homeassistant.const import CONF_DELAY, CONF_HOST, CONF_NAME, CONF_PASSWORD from homeassistant.const import CONF_DELAY, CONF_HOST, CONF_NAME, CONF_PASSWORD
from homeassistant.core import callback from homeassistant.core import callback
from homeassistant.data_entry_flow import FlowResult
from . import CannotConnect, async_connect_or_timeout, async_disconnect_or_timeout from . import CannotConnect, async_connect_or_timeout, async_disconnect_or_timeout
from .const import ( from .const import (
@ -77,15 +78,15 @@ class RoombaConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
"""Get the options flow for this handler.""" """Get the options flow for this handler."""
return OptionsFlowHandler(config_entry) return OptionsFlowHandler(config_entry)
async def async_step_dhcp(self, discovery_info): async def async_step_dhcp(self, discovery_info: dhcp.DhcpServiceInfo) -> FlowResult:
"""Handle dhcp discovery.""" """Handle dhcp discovery."""
self._async_abort_entries_match({CONF_HOST: discovery_info[IP_ADDRESS]}) self._async_abort_entries_match({CONF_HOST: discovery_info[dhcp.IP_ADDRESS]})
if not discovery_info[HOSTNAME].startswith(("irobot-", "roomba-")): if not discovery_info[dhcp.HOSTNAME].startswith(("irobot-", "roomba-")):
return self.async_abort(reason="not_irobot_device") return self.async_abort(reason="not_irobot_device")
self.host = discovery_info[IP_ADDRESS] self.host = discovery_info[dhcp.IP_ADDRESS]
self.blid = _async_blid_from_hostname(discovery_info[HOSTNAME]) self.blid = _async_blid_from_hostname(discovery_info[dhcp.HOSTNAME])
await self.async_set_unique_id(self.blid) await self.async_set_unique_id(self.blid)
self._abort_if_unique_id_configured(updates={CONF_HOST: self.host}) self._abort_if_unique_id_configured(updates={CONF_HOST: self.host})

View File

@ -5,7 +5,7 @@ import pytest
from roombapy import RoombaConnectionError, RoombaInfo from roombapy import RoombaConnectionError, RoombaInfo
from homeassistant import config_entries, data_entry_flow from homeassistant import config_entries, data_entry_flow
from homeassistant.components.dhcp import HOSTNAME, IP_ADDRESS, MAC_ADDRESS from homeassistant.components import dhcp
from homeassistant.components.roomba import config_flow from homeassistant.components.roomba import config_flow
from homeassistant.components.roomba.const import CONF_BLID, CONF_CONTINUOUS, DOMAIN from homeassistant.components.roomba.const import CONF_BLID, CONF_CONTINUOUS, DOMAIN
from homeassistant.const import CONF_DELAY, CONF_HOST, CONF_PASSWORD from homeassistant.const import CONF_DELAY, CONF_HOST, CONF_PASSWORD
@ -16,30 +16,30 @@ MOCK_IP = "1.2.3.4"
VALID_CONFIG = {CONF_HOST: MOCK_IP, CONF_BLID: "BLID", CONF_PASSWORD: "password"} VALID_CONFIG = {CONF_HOST: MOCK_IP, CONF_BLID: "BLID", CONF_PASSWORD: "password"}
DHCP_DISCOVERY_DEVICES = [ DHCP_DISCOVERY_DEVICES = [
{ dhcp.DhcpServiceInfo(
IP_ADDRESS: MOCK_IP, ip=MOCK_IP,
MAC_ADDRESS: "50:14:79:DD:EE:FF", macaddress="50:14:79:DD:EE:FF",
HOSTNAME: "irobot-blid", hostname="irobot-blid",
}, ),
{ dhcp.DhcpServiceInfo(
IP_ADDRESS: MOCK_IP, ip=MOCK_IP,
MAC_ADDRESS: "80:A5:89:DD:EE:FF", macaddress="80:A5:89:DD:EE:FF",
HOSTNAME: "roomba-blid", hostname="roomba-blid",
}, ),
] ]
DHCP_DISCOVERY_DEVICES_WITHOUT_MATCHING_IP = [ DHCP_DISCOVERY_DEVICES_WITHOUT_MATCHING_IP = [
{ dhcp.DhcpServiceInfo(
IP_ADDRESS: "4.4.4.4", ip="4.4.4.4",
MAC_ADDRESS: "50:14:79:DD:EE:FF", macaddress="50:14:79:DD:EE:FF",
HOSTNAME: "irobot-blid", hostname="irobot-blid",
}, ),
{ dhcp.DhcpServiceInfo(
IP_ADDRESS: "5.5.5.5", ip="5.5.5.5",
MAC_ADDRESS: "80:A5:89:DD:EE:FF", macaddress="80:A5:89:DD:EE:FF",
HOSTNAME: "roomba-blid", hostname="roomba-blid",
}, ),
] ]
@ -815,11 +815,11 @@ async def test_dhcp_discovery_with_ignored(hass):
result = await hass.config_entries.flow.async_init( result = await hass.config_entries.flow.async_init(
DOMAIN, DOMAIN,
context={"source": config_entries.SOURCE_DHCP}, context={"source": config_entries.SOURCE_DHCP},
data={ data=dhcp.DhcpServiceInfo(
IP_ADDRESS: MOCK_IP, ip=MOCK_IP,
MAC_ADDRESS: "AA:BB:CC:DD:EE:FF", macaddress="AA:BB:CC:DD:EE:FF",
HOSTNAME: "irobot-blid", hostname="irobot-blid",
}, ),
) )
await hass.async_block_till_done() await hass.async_block_till_done()
@ -838,11 +838,11 @@ async def test_dhcp_discovery_already_configured_host(hass):
result = await hass.config_entries.flow.async_init( result = await hass.config_entries.flow.async_init(
DOMAIN, DOMAIN,
context={"source": config_entries.SOURCE_DHCP}, context={"source": config_entries.SOURCE_DHCP},
data={ data=dhcp.DhcpServiceInfo(
IP_ADDRESS: MOCK_IP, ip=MOCK_IP,
MAC_ADDRESS: "AA:BB:CC:DD:EE:FF", macaddress="AA:BB:CC:DD:EE:FF",
HOSTNAME: "irobot-blid", hostname="irobot-blid",
}, ),
) )
await hass.async_block_till_done() await hass.async_block_till_done()
@ -864,11 +864,11 @@ async def test_dhcp_discovery_already_configured_blid(hass):
result = await hass.config_entries.flow.async_init( result = await hass.config_entries.flow.async_init(
DOMAIN, DOMAIN,
context={"source": config_entries.SOURCE_DHCP}, context={"source": config_entries.SOURCE_DHCP},
data={ data=dhcp.DhcpServiceInfo(
IP_ADDRESS: MOCK_IP, ip=MOCK_IP,
MAC_ADDRESS: "AA:BB:CC:DD:EE:FF", macaddress="AA:BB:CC:DD:EE:FF",
HOSTNAME: "irobot-blid", hostname="irobot-blid",
}, ),
) )
await hass.async_block_till_done() await hass.async_block_till_done()
@ -890,11 +890,11 @@ async def test_dhcp_discovery_not_irobot(hass):
result = await hass.config_entries.flow.async_init( result = await hass.config_entries.flow.async_init(
DOMAIN, DOMAIN,
context={"source": config_entries.SOURCE_DHCP}, context={"source": config_entries.SOURCE_DHCP},
data={ data=dhcp.DhcpServiceInfo(
IP_ADDRESS: MOCK_IP, ip=MOCK_IP,
MAC_ADDRESS: "AA:BB:CC:DD:EE:FF", macaddress="AA:BB:CC:DD:EE:FF",
HOSTNAME: "Notirobot-blid", hostname="Notirobot-blid",
}, ),
) )
await hass.async_block_till_done() await hass.async_block_till_done()
@ -911,11 +911,11 @@ async def test_dhcp_discovery_partial_hostname(hass):
result = await hass.config_entries.flow.async_init( result = await hass.config_entries.flow.async_init(
DOMAIN, DOMAIN,
context={"source": config_entries.SOURCE_DHCP}, context={"source": config_entries.SOURCE_DHCP},
data={ data=dhcp.DhcpServiceInfo(
IP_ADDRESS: MOCK_IP, ip=MOCK_IP,
MAC_ADDRESS: "AA:BB:CC:DD:EE:FF", macaddress="AA:BB:CC:DD:EE:FF",
HOSTNAME: "irobot-blid", hostname="irobot-blid",
}, ),
) )
await hass.async_block_till_done() await hass.async_block_till_done()
@ -928,11 +928,11 @@ async def test_dhcp_discovery_partial_hostname(hass):
result2 = await hass.config_entries.flow.async_init( result2 = await hass.config_entries.flow.async_init(
DOMAIN, DOMAIN,
context={"source": config_entries.SOURCE_DHCP}, context={"source": config_entries.SOURCE_DHCP},
data={ data=dhcp.DhcpServiceInfo(
IP_ADDRESS: MOCK_IP, ip=MOCK_IP,
MAC_ADDRESS: "AA:BB:CC:DD:EE:FF", macaddress="AA:BB:CC:DD:EE:FF",
HOSTNAME: "irobot-blidthatislonger", hostname="irobot-blidthatislonger",
}, ),
) )
await hass.async_block_till_done() await hass.async_block_till_done()
@ -949,11 +949,11 @@ async def test_dhcp_discovery_partial_hostname(hass):
result3 = await hass.config_entries.flow.async_init( result3 = await hass.config_entries.flow.async_init(
DOMAIN, DOMAIN,
context={"source": config_entries.SOURCE_DHCP}, context={"source": config_entries.SOURCE_DHCP},
data={ data=dhcp.DhcpServiceInfo(
IP_ADDRESS: MOCK_IP, ip=MOCK_IP,
MAC_ADDRESS: "AA:BB:CC:DD:EE:FF", macaddress="AA:BB:CC:DD:EE:FF",
HOSTNAME: "irobot-bl", hostname="irobot-bl",
}, ),
) )
await hass.async_block_till_done() await hass.async_block_till_done()