Make bluetooth use naming from the entity description (#97401)

* Make bluetooth use the translation from the entity description

* Remove links to other platforms

* Remove links to other platforms

* Remove links to other platforms

* Add test

* Use is

* Fix test

* Update homeassistant/components/bluetooth/passive_update_processor.py

Co-authored-by: J. Nick Koston <nick@koston.org>

---------

Co-authored-by: J. Nick Koston <nick@koston.org>
This commit is contained in:
Joost Lekkerkerker 2024-02-06 17:12:15 +01:00 committed by GitHub
parent 6295f91a1f
commit 6519b24319
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 84 additions and 9 deletions

View File

@ -649,7 +649,8 @@ class PassiveBluetoothProcessorEntity(Entity, Generic[_PassiveBluetoothDataProce
self._attr_device_info[ATTR_NAME] = self.processor.coordinator.name self._attr_device_info[ATTR_NAME] = self.processor.coordinator.name
if device_id is None: if device_id is None:
self._attr_device_info[ATTR_CONNECTIONS] = {(CONNECTION_BLUETOOTH, address)} self._attr_device_info[ATTR_CONNECTIONS] = {(CONNECTION_BLUETOOTH, address)}
self._attr_name = processor.entity_names.get(entity_key) if (name := processor.entity_names.get(entity_key)) is not None:
self._attr_name = name
@property @property
def available(self) -> bool: def available(self) -> bool:

View File

@ -41,6 +41,7 @@ from homeassistant.config_entries import current_entry
from homeassistant.const import UnitOfTemperature from homeassistant.const import UnitOfTemperature
from homeassistant.core import CoreState, HomeAssistant, callback from homeassistant.core import CoreState, HomeAssistant, callback
from homeassistant.helpers.device_registry import DeviceInfo from homeassistant.helpers.device_registry import DeviceInfo
from homeassistant.helpers.typing import UNDEFINED
from homeassistant.setup import async_setup_component from homeassistant.setup import async_setup_component
from homeassistant.util import dt as dt_util from homeassistant.util import dt as dt_util
@ -1263,14 +1264,8 @@ async def test_passive_bluetooth_entity_with_entity_platform(
await hass.async_block_till_done() await hass.async_block_till_done()
inject_bluetooth_service_info(hass, NO_DEVICES_BLUETOOTH_SERVICE_INFO_2) inject_bluetooth_service_info(hass, NO_DEVICES_BLUETOOTH_SERVICE_INFO_2)
await hass.async_block_till_done() await hass.async_block_till_done()
assert ( assert hass.states.get("test_domain.temperature") is not None
hass.states.get("test_domain.test_platform_aa_bb_cc_dd_ee_ff_temperature") assert hass.states.get("test_domain.pressure") is not None
is not None
)
assert (
hass.states.get("test_domain.test_platform_aa_bb_cc_dd_ee_ff_pressure")
is not None
)
cancel_coordinator() cancel_coordinator()
@ -1765,3 +1760,82 @@ async def test_integration_multiple_entity_platforms_with_reload_and_restart(
unregister_binary_sensor_processor() unregister_binary_sensor_processor()
unregister_sensor_processor() unregister_sensor_processor()
await hass.async_stop() await hass.async_stop()
NAMING_PASSIVE_BLUETOOTH_DATA_UPDATE = PassiveBluetoothDataUpdate(
devices={
None: DeviceInfo(
name="Test Device", model="Test Model", manufacturer="Test Manufacturer"
),
},
entity_data={
PassiveBluetoothEntityKey("temperature", None): 14.5,
},
entity_names={
PassiveBluetoothEntityKey("temperature", None): None,
},
entity_descriptions={
PassiveBluetoothEntityKey("temperature", None): SensorEntityDescription(
key="temperature",
native_unit_of_measurement=UnitOfTemperature.CELSIUS,
device_class=SensorDeviceClass.TEMPERATURE,
),
},
)
async def test_naming(
hass: HomeAssistant,
mock_bleak_scanner_start: MagicMock,
mock_bluetooth_adapters: None,
) -> None:
"""Test basic usage of the PassiveBluetoothProcessorCoordinator."""
await async_setup_component(hass, DOMAIN, {DOMAIN: {}})
@callback
def _mock_update_method(
service_info: BluetoothServiceInfo,
) -> dict[str, str]:
return {"test": "data"}
coordinator = PassiveBluetoothProcessorCoordinator(
hass,
_LOGGER,
"aa:bb:cc:dd:ee:ff",
BluetoothScanningMode.ACTIVE,
_mock_update_method,
)
assert coordinator.available is False # no data yet
sensor_processor = PassiveBluetoothDataProcessor(
lambda service_info: NAMING_PASSIVE_BLUETOOTH_DATA_UPDATE
)
coordinator.async_register_processor(sensor_processor)
cancel_coordinator = coordinator.async_start()
sensor_processor.async_add_listener(MagicMock())
mock_add_sensor_entities = MagicMock()
sensor_processor.async_add_entities_listener(
PassiveBluetoothProcessorEntity,
mock_add_sensor_entities,
)
inject_bluetooth_service_info(hass, GENERIC_BLUETOOTH_SERVICE_INFO)
# First call with just the remote sensor entities results in them being added
assert len(mock_add_sensor_entities.mock_calls) == 1
sensor_entities = [
*mock_add_sensor_entities.mock_calls[0][1][0],
]
sensor_entity: PassiveBluetoothProcessorEntity = sensor_entities[0]
sensor_entity.hass = hass
assert sensor_entity.available is True
assert sensor_entity.name is UNDEFINED
assert sensor_entity.device_class is SensorDeviceClass.TEMPERATURE
assert sensor_entity.translation_key is None
cancel_coordinator()