From b34ce3c792643dfce7a2d1750d9e99eb922326d5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A5le=20Stor=C3=B8=20Hauknes?= Date: Mon, 7 Aug 2023 19:15:51 +0200 Subject: [PATCH] Improve airthings ble (#97905) Co-authored-by: J. Nick Koston --- .../components/airthings_ble/config_flow.py | 10 ++++--- .../components/airthings_ble/manifest.json | 2 +- .../components/airthings_ble/sensor.py | 8 +++--- requirements_all.txt | 2 +- requirements_test_all.txt | 2 +- tests/components/airthings_ble/__init__.py | 3 +++ .../airthings_ble/test_config_flow.py | 26 ++++++++++++++----- 7 files changed, 38 insertions(+), 15 deletions(-) diff --git a/homeassistant/components/airthings_ble/config_flow.py b/homeassistant/components/airthings_ble/config_flow.py index 6d5df7ddd56..b562e837ff4 100644 --- a/homeassistant/components/airthings_ble/config_flow.py +++ b/homeassistant/components/airthings_ble/config_flow.py @@ -34,8 +34,12 @@ class Discovery: def get_name(device: AirthingsDevice) -> str: - """Generate name with identifier for device.""" - return f"{device.name} ({device.identifier})" + """Generate name with model and identifier for device.""" + + name = device.friendly_name() + if identifier := device.identifier: + name += f" ({identifier})" + return name class AirthingsDeviceUpdateError(Exception): @@ -156,7 +160,7 @@ class AirthingsConfigFlow(ConfigFlow, domain=DOMAIN): return self.async_abort(reason="no_devices_found") titles = { - address: get_name(discovery.device) + address: discovery.device.name for (address, discovery) in self._discovered_devices.items() } return self.async_show_form( diff --git a/homeassistant/components/airthings_ble/manifest.json b/homeassistant/components/airthings_ble/manifest.json index 8c78bbfb58d..ef9ad3a802e 100644 --- a/homeassistant/components/airthings_ble/manifest.json +++ b/homeassistant/components/airthings_ble/manifest.json @@ -24,5 +24,5 @@ "dependencies": ["bluetooth_adapters"], "documentation": "https://www.home-assistant.io/integrations/airthings_ble", "iot_class": "local_polling", - "requirements": ["airthings-ble==0.5.3"] + "requirements": ["airthings-ble==0.5.6-2"] } diff --git a/homeassistant/components/airthings_ble/sensor.py b/homeassistant/components/airthings_ble/sensor.py index 98190df6b8d..6bcd0337ed1 100644 --- a/homeassistant/components/airthings_ble/sensor.py +++ b/homeassistant/components/airthings_ble/sensor.py @@ -162,10 +162,11 @@ class AirthingsSensor( super().__init__(coordinator) self.entity_description = entity_description - name = f"{airthings_device.name} {airthings_device.identifier}" + name = airthings_device.name + if identifier := airthings_device.identifier: + name += f" ({identifier})" self._attr_unique_id = f"{name}_{entity_description.key}" - self._id = airthings_device.address self._attr_device_info = DeviceInfo( connections={ @@ -175,9 +176,10 @@ class AirthingsSensor( ) }, name=name, - manufacturer="Airthings", + manufacturer=airthings_device.manufacturer, hw_version=airthings_device.hw_version, sw_version=airthings_device.sw_version, + model=airthings_device.model, ) @property diff --git a/requirements_all.txt b/requirements_all.txt index b9933b28105..c683c57b702 100644 --- a/requirements_all.txt +++ b/requirements_all.txt @@ -378,7 +378,7 @@ aioymaps==1.2.2 airly==1.1.0 # homeassistant.components.airthings_ble -airthings-ble==0.5.3 +airthings-ble==0.5.6-2 # homeassistant.components.airthings airthings-cloud==0.1.0 diff --git a/requirements_test_all.txt b/requirements_test_all.txt index f8dece4fb7e..215ac1389b4 100644 --- a/requirements_test_all.txt +++ b/requirements_test_all.txt @@ -353,7 +353,7 @@ aioymaps==1.2.2 airly==1.1.0 # homeassistant.components.airthings_ble -airthings-ble==0.5.3 +airthings-ble==0.5.6-2 # homeassistant.components.airthings airthings-cloud==0.1.0 diff --git a/tests/components/airthings_ble/__init__.py b/tests/components/airthings_ble/__init__.py index 71875b9c4b1..0dd78718a30 100644 --- a/tests/components/airthings_ble/__init__.py +++ b/tests/components/airthings_ble/__init__.py @@ -77,8 +77,11 @@ UNKNOWN_SERVICE_INFO = BluetoothServiceInfoBleak( ) WAVE_DEVICE_INFO = AirthingsDevice( + manufacturer="Airthings AS", hw_version="REV A", sw_version="G-BLE-1.5.3-master+0", + model="Wave Plus", + model_raw="2930", name="Airthings Wave+", identifier="123456", sensors={ diff --git a/tests/components/airthings_ble/test_config_flow.py b/tests/components/airthings_ble/test_config_flow.py index 1702140864a..bc009f03027 100644 --- a/tests/components/airthings_ble/test_config_flow.py +++ b/tests/components/airthings_ble/test_config_flow.py @@ -25,7 +25,13 @@ from tests.common import MockConfigEntry async def test_bluetooth_discovery(hass: HomeAssistant) -> None: """Test discovery via bluetooth with a valid device.""" with patch_async_ble_device_from_address(WAVE_SERVICE_INFO), patch_airthings_ble( - AirthingsDevice(name="Airthings Wave+", identifier="123456") + AirthingsDevice( + manufacturer="Airthings AS", + model="Wave Plus", + model_raw="2930", + name="Airthings Wave Plus", + identifier="123456", + ) ): result = await hass.config_entries.flow.async_init( DOMAIN, @@ -35,7 +41,9 @@ async def test_bluetooth_discovery(hass: HomeAssistant) -> None: assert result["type"] == FlowResultType.FORM assert result["step_id"] == "bluetooth_confirm" - assert result["description_placeholders"] == {"name": "Airthings Wave+ (123456)"} + assert result["description_placeholders"] == { + "name": "Airthings Wave Plus (123456)" + } with patch_async_setup_entry(): result = await hass.config_entries.flow.async_configure( @@ -43,7 +51,7 @@ async def test_bluetooth_discovery(hass: HomeAssistant) -> None: ) await hass.async_block_till_done() assert result["type"] == FlowResultType.CREATE_ENTRY - assert result["title"] == "Airthings Wave+ (123456)" + assert result["title"] == "Airthings Wave Plus (123456)" assert result["result"].unique_id == "cc:cc:cc:cc:cc:cc" @@ -100,7 +108,13 @@ async def test_user_setup(hass: HomeAssistant) -> None: "homeassistant.components.airthings_ble.config_flow.async_discovered_service_info", return_value=[WAVE_SERVICE_INFO], ), patch_async_ble_device_from_address(WAVE_SERVICE_INFO), patch_airthings_ble( - AirthingsDevice(name="Airthings Wave+", identifier="123456") + AirthingsDevice( + manufacturer="Airthings AS", + model="Wave Plus", + model_raw="2930", + name="Airthings Wave Plus", + identifier="123456", + ) ): result = await hass.config_entries.flow.async_init( DOMAIN, context={"source": SOURCE_USER} @@ -112,7 +126,7 @@ async def test_user_setup(hass: HomeAssistant) -> None: schema = result["data_schema"].schema assert schema.get(CONF_ADDRESS).container == { - "cc:cc:cc:cc:cc:cc": "Airthings Wave+ (123456)" + "cc:cc:cc:cc:cc:cc": "Airthings Wave Plus" } with patch( @@ -125,7 +139,7 @@ async def test_user_setup(hass: HomeAssistant) -> None: await hass.async_block_till_done() assert result["type"] == FlowResultType.CREATE_ENTRY - assert result["title"] == "Airthings Wave+ (123456)" + assert result["title"] == "Airthings Wave Plus (123456)" assert result["result"].unique_id == "cc:cc:cc:cc:cc:cc"