mirror of
https://github.com/home-assistant/core.git
synced 2025-07-17 10:17:09 +00:00
Preserve new BLE tracker item name if seen before adding discovery (#30318)
* Preserve new BLE device name if one seen before the last scan but not on it * Save an indented else block * Use async_fire_time_changed and mock_device_tracker_conf in tests
This commit is contained in:
parent
fffc5a5fbb
commit
f400b77837
@ -84,7 +84,6 @@ omit =
|
|||||||
homeassistant/components/blockchain/sensor.py
|
homeassistant/components/blockchain/sensor.py
|
||||||
homeassistant/components/bloomsky/*
|
homeassistant/components/bloomsky/*
|
||||||
homeassistant/components/bluesound/*
|
homeassistant/components/bluesound/*
|
||||||
homeassistant/components/bluetooth_le_tracker/device_tracker.py
|
|
||||||
homeassistant/components/bluetooth_tracker/*
|
homeassistant/components/bluetooth_tracker/*
|
||||||
homeassistant/components/bme280/sensor.py
|
homeassistant/components/bme280/sensor.py
|
||||||
homeassistant/components/bme680/sensor.py
|
homeassistant/components/bme680/sensor.py
|
||||||
|
@ -44,23 +44,26 @@ def setup_scanner(hass, config, see, discovery_info=None):
|
|||||||
|
|
||||||
def see_device(address, name, new_device=False):
|
def see_device(address, name, new_device=False):
|
||||||
"""Mark a device as seen."""
|
"""Mark a device as seen."""
|
||||||
|
if name is not None:
|
||||||
|
name = name.strip("\x00")
|
||||||
|
|
||||||
if new_device:
|
if new_device:
|
||||||
if address in new_devices:
|
if address in new_devices:
|
||||||
new_devices[address] += 1
|
new_devices[address]["seen"] += 1
|
||||||
_LOGGER.debug("Seen %s %s times", address, new_devices[address])
|
if name:
|
||||||
if new_devices[address] >= MIN_SEEN_NEW:
|
new_devices[address]["name"] = name
|
||||||
|
else:
|
||||||
|
name = new_devices[address]["name"]
|
||||||
|
_LOGGER.debug("Seen %s %s times", address, new_devices[address]["seen"])
|
||||||
|
if new_devices[address]["seen"] < MIN_SEEN_NEW:
|
||||||
|
return
|
||||||
_LOGGER.debug("Adding %s to tracked devices", address)
|
_LOGGER.debug("Adding %s to tracked devices", address)
|
||||||
devs_to_track.append(address)
|
devs_to_track.append(address)
|
||||||
else:
|
|
||||||
return
|
|
||||||
else:
|
else:
|
||||||
_LOGGER.debug("Seen %s for the first time", address)
|
_LOGGER.debug("Seen %s for the first time", address)
|
||||||
new_devices[address] = 1
|
new_devices[address] = {"seen": 1, "name": name}
|
||||||
return
|
return
|
||||||
|
|
||||||
if name is not None:
|
|
||||||
name = name.strip("\x00")
|
|
||||||
|
|
||||||
see(
|
see(
|
||||||
mac=BLE_PREFIX + address,
|
mac=BLE_PREFIX + address,
|
||||||
host_name=name,
|
host_name=name,
|
||||||
|
@ -428,6 +428,10 @@ pyfritzhome==0.4.0
|
|||||||
# homeassistant.components.ifttt
|
# homeassistant.components.ifttt
|
||||||
pyfttt==0.3
|
pyfttt==0.3
|
||||||
|
|
||||||
|
# homeassistant.components.bluetooth_le_tracker
|
||||||
|
# homeassistant.components.skybeacon
|
||||||
|
pygatt[GATTTOOL]==4.0.5
|
||||||
|
|
||||||
# homeassistant.components.version
|
# homeassistant.components.version
|
||||||
pyhaversion==3.1.0
|
pyhaversion==3.1.0
|
||||||
|
|
||||||
|
1
tests/components/bluetooth_le_tracker/__init__.py
Normal file
1
tests/components/bluetooth_le_tracker/__init__.py
Normal file
@ -0,0 +1 @@
|
|||||||
|
"""Tests for the Bluetooth LE tracker component."""
|
56
tests/components/bluetooth_le_tracker/test_device_tracker.py
Normal file
56
tests/components/bluetooth_le_tracker/test_device_tracker.py
Normal file
@ -0,0 +1,56 @@
|
|||||||
|
"""Test Bluetooth LE device tracker."""
|
||||||
|
|
||||||
|
from datetime import timedelta
|
||||||
|
from unittest.mock import patch
|
||||||
|
|
||||||
|
from homeassistant.components.bluetooth_le_tracker import device_tracker
|
||||||
|
from homeassistant.components.device_tracker.const import (
|
||||||
|
CONF_SCAN_INTERVAL,
|
||||||
|
CONF_TRACK_NEW,
|
||||||
|
DOMAIN,
|
||||||
|
)
|
||||||
|
from homeassistant.const import CONF_PLATFORM
|
||||||
|
from homeassistant.setup import async_setup_component
|
||||||
|
from homeassistant.util import dt as dt_util, slugify
|
||||||
|
|
||||||
|
from tests.common import async_fire_time_changed
|
||||||
|
|
||||||
|
|
||||||
|
async def test_preserve_new_tracked_device_name(hass, mock_device_tracker_conf):
|
||||||
|
"""Test preserving tracked device name across new seens."""
|
||||||
|
|
||||||
|
address = "DE:AD:BE:EF:13:37"
|
||||||
|
name = "Mock device name"
|
||||||
|
entity_id = f"{DOMAIN}.{slugify(name)}"
|
||||||
|
|
||||||
|
with patch(
|
||||||
|
"homeassistant.components."
|
||||||
|
"bluetooth_le_tracker.device_tracker.pygatt.GATTToolBackend"
|
||||||
|
) as mock_backend, patch.object(device_tracker, "MIN_SEEN_NEW", 3):
|
||||||
|
|
||||||
|
# Return with name when seen first time
|
||||||
|
device = {"address": address, "name": name}
|
||||||
|
mock_backend.return_value.scan.return_value = [device]
|
||||||
|
|
||||||
|
config = {
|
||||||
|
CONF_PLATFORM: "bluetooth_le_tracker",
|
||||||
|
CONF_SCAN_INTERVAL: timedelta(minutes=1),
|
||||||
|
CONF_TRACK_NEW: True,
|
||||||
|
}
|
||||||
|
result = await async_setup_component(hass, DOMAIN, {DOMAIN: config})
|
||||||
|
assert result
|
||||||
|
|
||||||
|
# Seen once here; return without name when seen subsequent times
|
||||||
|
device["name"] = None
|
||||||
|
|
||||||
|
# Tick until device seen enough times for to be registered for tracking
|
||||||
|
for _ in range(device_tracker.MIN_SEEN_NEW - 1):
|
||||||
|
async_fire_time_changed(
|
||||||
|
hass,
|
||||||
|
dt_util.utcnow() + config[CONF_SCAN_INTERVAL] + timedelta(seconds=1),
|
||||||
|
)
|
||||||
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
|
state = hass.states.get(entity_id)
|
||||||
|
assert state
|
||||||
|
assert state.name == name
|
Loading…
x
Reference in New Issue
Block a user