diff --git a/homeassistant/components/ibeacon/coordinator.py b/homeassistant/components/ibeacon/coordinator.py index 3a18c77678a..0b813eca933 100644 --- a/homeassistant/components/ibeacon/coordinator.py +++ b/homeassistant/components/ibeacon/coordinator.py @@ -67,7 +67,7 @@ def async_name( base_name = service_info.name if unique_address: short_address = make_short_address(service_info.address) - if not base_name.endswith(short_address): + if not base_name.upper().endswith(short_address): return f"{base_name} {short_address}" return base_name @@ -233,6 +233,12 @@ class IBeaconCoordinator: address = service_info.address unique_id = f"{group_id}_{address}" new = unique_id not in self._last_rssi_by_unique_id + # Reject creating new trackers if the name is not set + if new and ( + service_info.device.name is None + or service_info.device.name.replace("-", ":") == service_info.device.address + ): + return self._last_rssi_by_unique_id[unique_id] = service_info.rssi self._async_track_ibeacon_with_unique_address(address, group_id, unique_id) if address not in self._unavailable_trackers: diff --git a/tests/components/ibeacon/__init__.py b/tests/components/ibeacon/__init__.py index f9b2c1576ad..f1b8928f67b 100644 --- a/tests/components/ibeacon/__init__.py +++ b/tests/components/ibeacon/__init__.py @@ -28,6 +28,15 @@ BLUECHARM_BEACON_SERVICE_INFO_2 = BluetoothServiceInfo( service_uuids=["0000feaa-0000-1000-8000-00805f9b34fb"], source="local", ) +BLUECHARM_BEACON_SERVICE_INFO_DBUS = BluetoothServiceInfo( + name="BlueCharm_177999", + address="AA:BB:CC:DD:EE:FF", + rssi=-63, + service_data={}, + manufacturer_data={76: b"\x02\x15BlueCharmBeacons\x0e\xfe\x13U\xc5"}, + service_uuids=[], + source="local", +) NO_NAME_BEACON_SERVICE_INFO = BluetoothServiceInfo( name="61DE521B-F0BF-9F44-64D4-75BBE1738105", address="61DE521B-F0BF-9F44-64D4-75BBE1738105", diff --git a/tests/components/ibeacon/test_coordinator.py b/tests/components/ibeacon/test_coordinator.py index a732b8ec2d3..cb7e0bdefc8 100644 --- a/tests/components/ibeacon/test_coordinator.py +++ b/tests/components/ibeacon/test_coordinator.py @@ -8,7 +8,7 @@ import pytest from homeassistant.components.ibeacon.const import DOMAIN from homeassistant.helpers.service_info.bluetooth import BluetoothServiceInfo -from . import BLUECHARM_BEACON_SERVICE_INFO +from . import BLUECHARM_BEACON_SERVICE_INFO, BLUECHARM_BEACON_SERVICE_INFO_DBUS from tests.common import MockConfigEntry from tests.components.bluetooth import inject_bluetooth_service_info @@ -73,3 +73,57 @@ async def test_ignore_not_ibeacons(hass): ) await hass.async_block_till_done() assert len(hass.states.async_entity_ids()) == before_entity_count + + +async def test_ignore_no_name_but_create_if_set_later(hass): + """Test we ignore devices with no name but create it if it set set later.""" + entry = MockConfigEntry( + domain=DOMAIN, + ) + entry.add_to_hass(hass) + + assert await hass.config_entries.async_setup(entry.entry_id) + await hass.async_block_till_done() + + before_entity_count = len(hass.states.async_entity_ids()) + inject_bluetooth_service_info( + hass, + replace(BLUECHARM_BEACON_SERVICE_INFO, name=None), + ) + await hass.async_block_till_done() + assert len(hass.states.async_entity_ids()) == before_entity_count + + inject_bluetooth_service_info( + hass, + replace( + BLUECHARM_BEACON_SERVICE_INFO, + service_data={ + "00002080-0000-1000-8000-00805f9b34fb": b"j\x0c\x0e\xfe\x13U", + "0000feaa-0000-1000-8000-00805f9b34fb": b" \x00\x0c\x00\x1c\x00\x00\x00\x06h\x00\x008\x10", + }, + ), + ) + await hass.async_block_till_done() + assert len(hass.states.async_entity_ids()) > before_entity_count + + +async def test_ignore_default_name(hass): + """Test we ignore devices with default name.""" + entry = MockConfigEntry( + domain=DOMAIN, + ) + entry.add_to_hass(hass) + + assert await hass.config_entries.async_setup(entry.entry_id) + await hass.async_block_till_done() + + before_entity_count = len(hass.states.async_entity_ids()) + inject_bluetooth_service_info( + hass, + replace( + BLUECHARM_BEACON_SERVICE_INFO_DBUS, + name=BLUECHARM_BEACON_SERVICE_INFO_DBUS.address, + ), + ) + await hass.async_block_till_done() + assert len(hass.states.async_entity_ids()) == before_entity_count