Add id constant for homekit discovery (#59986)

pull/59904/head
epenet 2021-11-20 11:07:59 +01:00 committed by GitHub
parent 59f10373ad
commit d0ff8a9b76
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 28 additions and 15 deletions

View File

@ -39,7 +39,9 @@ class Gogogate2FlowHandler(ConfigFlow, domain=DOMAIN):
self, discovery_info: zeroconf.ZeroconfServiceInfo
) -> data_entry_flow.FlowResult:
"""Handle homekit discovery."""
await self.async_set_unique_id(discovery_info[zeroconf.ATTR_PROPERTIES]["id"])
await self.async_set_unique_id(
discovery_info[zeroconf.ATTR_PROPERTIES][zeroconf.ATTR_PROPERTIES_ID]
)
return await self._async_discovery_handler(discovery_info[zeroconf.ATTR_HOST])
async def async_step_dhcp(

View File

@ -167,7 +167,7 @@ class HomekitControllerFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
properties={
"md": record["md"],
"pv": record["pv"],
"id": unique_id,
zeroconf.ATTR_PROPERTIES_ID: unique_id,
"c#": record["c#"],
"s#": record["s#"],
"ff": record["ff"],
@ -212,7 +212,7 @@ class HomekitControllerFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
for (key, value) in discovery_info[zeroconf.ATTR_PROPERTIES].items()
}
if "id" not in properties:
if zeroconf.ATTR_PROPERTIES_ID not in properties:
# This can happen if the TXT record is received after the PTR record
# we will wait for the next update in this case
_LOGGER.debug(
@ -223,7 +223,7 @@ class HomekitControllerFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
# The hkid is a unique random number that looks like a pairing code.
# It changes if a device is factory reset.
hkid = properties["id"]
hkid = properties[zeroconf.ATTR_PROPERTIES_ID]
model = properties["md"]
name = discovery_info[zeroconf.ATTR_NAME].replace("._hap._tcp.local.", "")
status_flags = int(properties["sf"])

View File

@ -68,6 +68,9 @@ ATTR_PORT: Final = "port"
ATTR_PROPERTIES: Final = "properties"
ATTR_TYPE: Final = "type"
# Attributes for ZeroconfServiceInfo[ATTR_PROPERTIES]
ATTR_PROPERTIES_ID: Final = "id"
CONFIG_SCHEMA = vol.Schema(
{

View File

@ -108,7 +108,7 @@ async def test_form_homekit_unique_id_already_setup(hass):
DOMAIN,
context={"source": config_entries.SOURCE_HOMEKIT},
data=zeroconf.ZeroconfServiceInfo(
host="1.2.3.4", properties={"id": MOCK_MAC_ADDR}
host="1.2.3.4", properties={zeroconf.ATTR_PROPERTIES_ID: MOCK_MAC_ADDR}
),
)
assert result["type"] == RESULT_TYPE_FORM
@ -130,7 +130,7 @@ async def test_form_homekit_unique_id_already_setup(hass):
DOMAIN,
context={"source": config_entries.SOURCE_HOMEKIT},
data=zeroconf.ZeroconfServiceInfo(
host="1.2.3.4", properties={"id": MOCK_MAC_ADDR}
host="1.2.3.4", properties={zeroconf.ATTR_PROPERTIES_ID: MOCK_MAC_ADDR}
),
)
assert result["type"] == RESULT_TYPE_ABORT
@ -149,7 +149,7 @@ async def test_form_homekit_ip_address_already_setup(hass):
DOMAIN,
context={"source": config_entries.SOURCE_HOMEKIT},
data=zeroconf.ZeroconfServiceInfo(
host="1.2.3.4", properties={"id": MOCK_MAC_ADDR}
host="1.2.3.4", properties={zeroconf.ATTR_PROPERTIES_ID: MOCK_MAC_ADDR}
),
)
assert result["type"] == RESULT_TYPE_ABORT
@ -162,7 +162,7 @@ async def test_form_homekit_ip_address(hass):
DOMAIN,
context={"source": config_entries.SOURCE_HOMEKIT},
data=zeroconf.ZeroconfServiceInfo(
host="1.2.3.4", properties={"id": MOCK_MAC_ADDR}
host="1.2.3.4", properties={zeroconf.ATTR_PROPERTIES_ID: MOCK_MAC_ADDR}
),
)
assert result["type"] == RESULT_TYPE_FORM
@ -237,7 +237,7 @@ async def test_discovered_by_homekit_and_dhcp(hass):
DOMAIN,
context={"source": config_entries.SOURCE_HOMEKIT},
data=zeroconf.ZeroconfServiceInfo(
host="1.2.3.4", properties={"id": MOCK_MAC_ADDR}
host="1.2.3.4", properties={zeroconf.ATTR_PROPERTIES_ID: MOCK_MAC_ADDR}
),
)
assert result["type"] == RESULT_TYPE_FORM

View File

@ -148,7 +148,7 @@ def get_device_discovery_info(
properties={
"md": record["md"],
"pv": record["pv"],
"id": device.device_id,
zeroconf.ATTR_PROPERTIES_ID: device.device_id,
"c#": record["c#"],
"s#": record["s#"],
"ff": record["ff"],
@ -273,7 +273,7 @@ async def test_id_missing(hass, controller):
discovery_info = get_device_discovery_info(device)
# Remove id from device
del discovery_info["properties"]["id"]
del discovery_info[zeroconf.ATTR_PROPERTIES][zeroconf.ATTR_PROPERTIES_ID]
# Device is discovered
result = await hass.config_entries.flow.async_init(
@ -289,7 +289,9 @@ async def test_discovery_ignored_model(hass, controller):
"""Already paired."""
device = setup_mock_accessory(controller)
discovery_info = get_device_discovery_info(device)
discovery_info[zeroconf.ATTR_PROPERTIES]["id"] = "AA:BB:CC:DD:EE:FF"
discovery_info[zeroconf.ATTR_PROPERTIES][
zeroconf.ATTR_PROPERTIES_ID
] = "AA:BB:CC:DD:EE:FF"
discovery_info[zeroconf.ATTR_PROPERTIES]["md"] = "HHKBridge1,1"
# Device is discovered
@ -317,7 +319,9 @@ async def test_discovery_ignored_hk_bridge(hass, controller):
connections={(device_registry.CONNECTION_NETWORK_MAC, formatted_mac)},
)
discovery_info[zeroconf.ATTR_PROPERTIES]["id"] = "AA:BB:CC:DD:EE:FF"
discovery_info[zeroconf.ATTR_PROPERTIES][
zeroconf.ATTR_PROPERTIES_ID
] = "AA:BB:CC:DD:EE:FF"
# Device is discovered
result = await hass.config_entries.flow.async_init(
@ -344,7 +348,9 @@ async def test_discovery_does_not_ignore_non_homekit(hass, controller):
connections={(device_registry.CONNECTION_NETWORK_MAC, formatted_mac)},
)
discovery_info[zeroconf.ATTR_PROPERTIES]["id"] = "AA:BB:CC:DD:EE:FF"
discovery_info[zeroconf.ATTR_PROPERTIES][
zeroconf.ATTR_PROPERTIES_ID
] = "AA:BB:CC:DD:EE:FF"
# Device is discovered
result = await hass.config_entries.flow.async_init(
@ -486,7 +492,9 @@ async def test_discovery_already_configured_update_csharp(hass, controller):
# Set device as already paired
discovery_info[zeroconf.ATTR_PROPERTIES]["sf"] = 0x00
discovery_info[zeroconf.ATTR_PROPERTIES]["c#"] = 99999
discovery_info[zeroconf.ATTR_PROPERTIES]["id"] = "AA:BB:CC:DD:EE:FF"
discovery_info[zeroconf.ATTR_PROPERTIES][
zeroconf.ATTR_PROPERTIES_ID
] = "AA:BB:CC:DD:EE:FF"
# Device is discovered
result = await hass.config_entries.flow.async_init(