Fix Aranet failure when the Bluetooth proxy is not providing a device name (#115298)

Co-authored-by: J. Nick Koston <nick@koston.org>
This commit is contained in:
Shay Levy 2024-04-10 14:58:35 +03:00 committed by GitHub
parent 012509f683
commit a6b93ea8ac
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 40 additions and 14 deletions

View File

@ -2,10 +2,10 @@
from __future__ import annotations
import logging
from typing import Any
from aranet4.client import Aranet4Advertisement, Version as AranetVersion
from bluetooth_data_tools import human_readable_name
import voluptuous as vol
from homeassistant.components.bluetooth import (
@ -18,11 +18,15 @@ from homeassistant.data_entry_flow import AbortFlow
from .const import DOMAIN
_LOGGER = logging.getLogger(__name__)
MIN_VERSION = AranetVersion(1, 2, 0)
def _title(discovery_info: BluetoothServiceInfoBleak) -> str:
return discovery_info.device.name or human_readable_name(
None, "Aranet", discovery_info.address
)
class AranetConfigFlow(ConfigFlow, domain=DOMAIN):
"""Handle a config flow for Aranet."""
@ -61,11 +65,8 @@ class AranetConfigFlow(ConfigFlow, domain=DOMAIN):
self, user_input: dict[str, Any] | None = None
) -> ConfigFlowResult:
"""Confirm discovery."""
assert self._discovered_device is not None
adv = self._discovered_device
assert self._discovery_info is not None
discovery_info = self._discovery_info
title = adv.readings.name if adv.readings else discovery_info.name
title = _title(self._discovery_info)
if user_input is not None:
return self.async_create_entry(title=title, data={})
@ -101,10 +102,7 @@ class AranetConfigFlow(ConfigFlow, domain=DOMAIN):
discovery_info.device, discovery_info.advertisement
)
if adv.manufacturer_data:
self._discovered_devices[address] = (
adv.readings.name if adv.readings else discovery_info.name,
adv,
)
self._discovered_devices[address] = (_title(discovery_info), adv)
if not self._discovered_devices:
return self.async_abort(reason="no_devices_found")

View File

@ -19,5 +19,5 @@
"documentation": "https://www.home-assistant.io/integrations/aranet",
"integration_type": "device",
"iot_class": "local_push",
"requirements": ["aranet4==2.2.2"]
"requirements": ["aranet4==2.3.3"]
}

View File

@ -467,7 +467,7 @@ aprslib==0.7.2
aqualogic==2.6
# homeassistant.components.aranet
aranet4==2.2.2
aranet4==2.3.3
# homeassistant.components.arcam_fmj
arcam-fmj==1.4.0

View File

@ -428,7 +428,7 @@ apprise==1.7.4
aprslib==0.7.2
# homeassistant.components.aranet
aranet4==2.2.2
aranet4==2.3.3
# homeassistant.components.arcam_fmj
arcam-fmj==1.4.0

View File

@ -58,6 +58,14 @@ VALID_DATA_SERVICE_INFO = fake_service_info(
},
)
VALID_DATA_SERVICE_INFO_WITH_NO_NAME = fake_service_info(
None,
"0000fce0-0000-1000-8000-00805f9b34fb",
{
1794: b'\x21\x00\x02\x01\x00\x00\x00\x01\x8a\x02\xa5\x01\xb1&"Y\x01,\x01\xe8\x00\x88'
},
)
VALID_ARANET2_DATA_SERVICE_INFO = fake_service_info(
"Aranet2 12345",
"0000fce0-0000-1000-8000-00805f9b34fb",

View File

@ -12,6 +12,7 @@ from . import (
NOT_ARANET4_SERVICE_INFO,
OLD_FIRMWARE_SERVICE_INFO,
VALID_DATA_SERVICE_INFO,
VALID_DATA_SERVICE_INFO_WITH_NO_NAME,
)
from tests.common import MockConfigEntry
@ -36,6 +37,25 @@ async def test_async_step_bluetooth_valid_device(hass: HomeAssistant) -> None:
assert result2["result"].unique_id == "aa:bb:cc:dd:ee:ff"
async def test_async_step_bluetooth_device_without_name(hass: HomeAssistant) -> None:
"""Test discovery via bluetooth with a valid device that has no name."""
result = await hass.config_entries.flow.async_init(
DOMAIN,
context={"source": config_entries.SOURCE_BLUETOOTH},
data=VALID_DATA_SERVICE_INFO_WITH_NO_NAME,
)
assert result["type"] is FlowResultType.FORM
assert result["step_id"] == "bluetooth_confirm"
with patch("homeassistant.components.aranet.async_setup_entry", return_value=True):
result2 = await hass.config_entries.flow.async_configure(
result["flow_id"], user_input={}
)
assert result2["type"] is FlowResultType.CREATE_ENTRY
assert result2["title"] == "Aranet (EEFF)"
assert result2["data"] == {}
assert result2["result"].unique_id == "aa:bb:cc:dd:ee:ff"
async def test_async_step_bluetooth_not_aranet4(hass: HomeAssistant) -> None:
"""Test that we reject discovery via Bluetooth for an unrelated device."""
result = await hass.config_entries.flow.async_init(