Fix device tracker see for entity registry entities (#24633)

* Add a test for see service gaurd

* Guard from seeing devices part of entity registry

* Await registry task early

* Lint

* Correct comment

* Clean up wait for registry

* Fix spelling

Co-Authored-By: Paulus Schoutsen <paulus@home-assistant.io>

* Fix spelling

Co-Authored-By: Paulus Schoutsen <paulus@home-assistant.io>
This commit is contained in:
Martin Hjelmare 2019-06-20 03:22:33 +02:00 committed by GitHub
parent 76537a7f41
commit 7881081207
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 36 additions and 6 deletions

View File

@ -14,6 +14,7 @@ from homeassistant.components.zone import async_active_zone
from homeassistant.config import load_yaml_config_file, async_log_exception from homeassistant.config import load_yaml_config_file, async_log_exception
from homeassistant.exceptions import HomeAssistantError from homeassistant.exceptions import HomeAssistantError
import homeassistant.helpers.config_validation as cv import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.entity_registry import async_get_registry
from homeassistant.helpers.restore_state import RestoreEntity from homeassistant.helpers.restore_state import RestoreEntity
from homeassistant.helpers.typing import GPSType, HomeAssistantType from homeassistant.helpers.typing import GPSType, HomeAssistantType
from homeassistant import util from homeassistant import util
@ -115,6 +116,7 @@ class DeviceTracker:
This method is a coroutine. This method is a coroutine.
""" """
registry = await async_get_registry(self.hass)
if mac is None and dev_id is None: if mac is None and dev_id is None:
raise HomeAssistantError('Neither mac or device id passed in') raise HomeAssistantError('Neither mac or device id passed in')
if mac is not None: if mac is not None:
@ -134,6 +136,14 @@ class DeviceTracker:
await device.async_update_ha_state() await device.async_update_ha_state()
return return
# Guard from calling see on entity registry entities.
entity_id = ENTITY_ID_FORMAT.format(dev_id)
if registry.async_is_registered(entity_id):
LOGGER.error(
"The see service is not supported for this entity %s",
entity_id)
return
# If no device can be found, create it # If no device can be found, create it
dev_id = util.ensure_unique_string(dev_id, self.devices.keys()) dev_id = util.ensure_unique_string(dev_id, self.devices.keys())
device = Device( device = Device(

View File

@ -3,7 +3,7 @@ from datetime import datetime, timedelta
import json import json
import logging import logging
import os import os
from unittest.mock import call from unittest.mock import Mock, call
from asynctest import patch from asynctest import patch
import pytest import pytest
@ -12,9 +12,9 @@ from homeassistant.components import zone
import homeassistant.components.device_tracker as device_tracker import homeassistant.components.device_tracker as device_tracker
from homeassistant.components.device_tracker import const, legacy from homeassistant.components.device_tracker import const, legacy
from homeassistant.const import ( from homeassistant.const import (
ATTR_ENTITY_ID, ATTR_ENTITY_PICTURE, ATTR_FRIENDLY_NAME, ATTR_HIDDEN, ATTR_ENTITY_ID, ATTR_ENTITY_PICTURE, ATTR_FRIENDLY_NAME, ATTR_GPS_ACCURACY,
ATTR_ICON, CONF_PLATFORM, STATE_HOME, STATE_NOT_HOME, ATTR_HIDDEN, ATTR_ICON, ATTR_LATITUDE, ATTR_LONGITUDE, CONF_PLATFORM,
ATTR_LATITUDE, ATTR_LONGITUDE, ATTR_GPS_ACCURACY) STATE_HOME, STATE_NOT_HOME)
from homeassistant.core import State, callback from homeassistant.core import State, callback
from homeassistant.exceptions import HomeAssistantError from homeassistant.exceptions import HomeAssistantError
from homeassistant.helpers import discovery from homeassistant.helpers import discovery
@ -23,8 +23,8 @@ from homeassistant.setup import async_setup_component
import homeassistant.util.dt as dt_util import homeassistant.util.dt as dt_util
from tests.common import ( from tests.common import (
assert_setup_component, async_fire_time_changed, mock_restore_cache, assert_setup_component, async_fire_time_changed, mock_registry,
patch_yaml_files) mock_restore_cache, patch_yaml_files)
from tests.components.device_tracker import common from tests.components.device_tracker import common
TEST_PLATFORM = {device_tracker.DOMAIN: {CONF_PLATFORM: 'test'}} TEST_PLATFORM = {device_tracker.DOMAIN: {CONF_PLATFORM: 'test'}}
@ -321,6 +321,26 @@ async def test_see_service(mock_see, hass):
assert mock_see.call_args == call(**params) assert mock_see.call_args == call(**params)
async def test_see_service_guard_config_entry(hass, mock_device_tracker_conf):
"""Test the guard if the device is registered in the entity registry."""
mock_entry = Mock()
dev_id = 'test'
entity_id = const.ENTITY_ID_FORMAT.format(dev_id)
mock_registry(hass, {entity_id: mock_entry})
devices = mock_device_tracker_conf
assert await async_setup_component(
hass, device_tracker.DOMAIN, TEST_PLATFORM)
params = {
'dev_id': dev_id,
'gps': [.3, .8],
}
common.async_see(hass, **params)
await hass.async_block_till_done()
assert not devices
async def test_new_device_event_fired(hass, mock_device_tracker_conf): async def test_new_device_event_fired(hass, mock_device_tracker_conf):
"""Test that the device tracker will fire an event.""" """Test that the device tracker will fire an event."""
with assert_setup_component(1, device_tracker.DOMAIN): with assert_setup_component(1, device_tracker.DOMAIN):