mirror of
https://github.com/home-assistant/core.git
synced 2025-04-26 10:17:51 +00:00
Handle invalid dev ids for dev tracker + owntracks (#2174)
This commit is contained in:
parent
70d6ce5b79
commit
1798df7686
@ -17,7 +17,7 @@ from homeassistant.config import load_yaml_config_file
|
|||||||
from homeassistant.exceptions import HomeAssistantError
|
from homeassistant.exceptions import HomeAssistantError
|
||||||
from homeassistant.helpers import config_per_platform
|
from homeassistant.helpers import config_per_platform
|
||||||
from homeassistant.helpers.entity import Entity
|
from homeassistant.helpers.entity import Entity
|
||||||
from homeassistant.helpers.config_validation import PLATFORM_SCHEMA # noqa
|
import homeassistant.helpers.config_validation as cv
|
||||||
import homeassistant.util as util
|
import homeassistant.util as util
|
||||||
import homeassistant.util.dt as dt_util
|
import homeassistant.util.dt as dt_util
|
||||||
|
|
||||||
@ -26,6 +26,7 @@ from homeassistant.const import (
|
|||||||
ATTR_GPS_ACCURACY, ATTR_LATITUDE, ATTR_LONGITUDE,
|
ATTR_GPS_ACCURACY, ATTR_LATITUDE, ATTR_LONGITUDE,
|
||||||
DEVICE_DEFAULT_NAME, STATE_HOME, STATE_NOT_HOME)
|
DEVICE_DEFAULT_NAME, STATE_HOME, STATE_NOT_HOME)
|
||||||
|
|
||||||
|
PLATFORM_SCHEMA = cv.PLATFORM_SCHEMA
|
||||||
DOMAIN = "device_tracker"
|
DOMAIN = "device_tracker"
|
||||||
DEPENDENCIES = ['zone']
|
DEPENDENCIES = ['zone']
|
||||||
|
|
||||||
@ -193,7 +194,7 @@ class DeviceTracker(object):
|
|||||||
if not device:
|
if not device:
|
||||||
dev_id = util.slugify(host_name or '') or util.slugify(mac)
|
dev_id = util.slugify(host_name or '') or util.slugify(mac)
|
||||||
else:
|
else:
|
||||||
dev_id = str(dev_id).lower()
|
dev_id = cv.slug(str(dev_id).lower())
|
||||||
device = self.devices.get(dev_id)
|
device = self.devices.get(dev_id)
|
||||||
|
|
||||||
if device:
|
if device:
|
||||||
|
@ -186,7 +186,7 @@ def setup_scanner(hass, config, see):
|
|||||||
def _parse_see_args(topic, data):
|
def _parse_see_args(topic, data):
|
||||||
"""Parse the OwnTracks location parameters, into the format see expects."""
|
"""Parse the OwnTracks location parameters, into the format see expects."""
|
||||||
parts = topic.split('/')
|
parts = topic.split('/')
|
||||||
dev_id = '{}_{}'.format(parts[1], parts[2])
|
dev_id = slugify('{}_{}'.format(parts[1], parts[2]))
|
||||||
host_name = parts[1]
|
host_name = parts[1]
|
||||||
kwargs = {
|
kwargs = {
|
||||||
'dev_id': dev_id,
|
'dev_id': dev_id,
|
||||||
|
@ -210,3 +210,13 @@ class TestComponentsDeviceTracker(unittest.TestCase):
|
|||||||
config = device_tracker.load_config(self.yaml_devices, self.hass,
|
config = device_tracker.load_config(self.yaml_devices, self.hass,
|
||||||
timedelta(seconds=0), 0)
|
timedelta(seconds=0), 0)
|
||||||
assert len(config) == 2
|
assert len(config) == 2
|
||||||
|
|
||||||
|
def test_not_allow_invalid_dev_id(self):
|
||||||
|
"""Test that the device tracker will not allow invalid dev ids."""
|
||||||
|
self.assertTrue(device_tracker.setup(self.hass, {}))
|
||||||
|
|
||||||
|
device_tracker.see(self.hass, dev_id='hello-world')
|
||||||
|
|
||||||
|
config = device_tracker.load_config(self.yaml_devices, self.hass,
|
||||||
|
timedelta(seconds=0), 0)
|
||||||
|
assert len(config) == 0
|
||||||
|
@ -203,6 +203,13 @@ class TestDeviceTrackerOwnTracks(unittest.TestCase):
|
|||||||
state = self.hass.states.get(REGION_TRACKER_STATE)
|
state = self.hass.states.get(REGION_TRACKER_STATE)
|
||||||
self.assertEqual(state.attributes.get('gps_accuracy'), accuracy)
|
self.assertEqual(state.attributes.get('gps_accuracy'), accuracy)
|
||||||
|
|
||||||
|
def test_location_invalid_devid(self):
|
||||||
|
"""Test the update of a location."""
|
||||||
|
self.send_message('owntracks/paulus/nexus-5x', LOCATION_MESSAGE)
|
||||||
|
|
||||||
|
state = self.hass.states.get('device_tracker.paulus_nexus5x')
|
||||||
|
assert state.state == 'outer'
|
||||||
|
|
||||||
def test_location_update(self):
|
def test_location_update(self):
|
||||||
"""Test the update of a location."""
|
"""Test the update of a location."""
|
||||||
self.send_message(LOCATION_TOPIC, LOCATION_MESSAGE)
|
self.send_message(LOCATION_TOPIC, LOCATION_MESSAGE)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user