Device tracker improvements

This commit is contained in:
Paulus Schoutsen 2015-09-15 23:35:28 -07:00
parent 5af1643297
commit 86aea83f64
4 changed files with 30 additions and 21 deletions

View File

@ -176,10 +176,8 @@ class DeviceTracker(object):
self.track_new = track_new
self.lock = threading.Lock()
entity_ids = []
for device in devices:
if device.track:
entity_ids.append(device.entity_id)
device.update_ha_state()
self.group = None
@ -194,9 +192,9 @@ class DeviceTracker(object):
mac = mac.upper()
device = self.mac_to_dev.get(mac)
if not device:
dev_id = util.slugify(host_name or mac)
dev_id = util.slugify(host_name or '') or util.slugify(mac)
else:
dev_id = str(dev_id)
dev_id = str(dev_id).lower()
device = self.devices.get(dev_id)
if device:
@ -234,7 +232,8 @@ class DeviceTracker(object):
""" Update stale devices. """
with self.lock:
for device in self.devices.values():
if device.last_update_home and device.stale(now):
if (device.track and device.last_update_home and
device.stale(now)):
device.update_ha_state(True)
@ -336,7 +335,8 @@ def convert_csv_config(csv_path, yaml_path):
with open(csv_path) as inp:
for row in csv.DictReader(inp):
dev_id = util.ensure_unique_string(
util.slugify(row['name']) or DEVICE_DEFAULT_NAME, used_ids)
(util.slugify(row['name']) or DEVICE_DEFAULT_NAME).lower(),
used_ids)
used_ids.add(dev_id)
device = Device(None, None, row['track'] == '1', dev_id,
row['device'], row['name'], row['picture'])
@ -350,8 +350,9 @@ def load_config(path, hass, consider_home):
return []
return [
Device(hass, consider_home, device.get('track', False),
str(dev_id), device.get('mac'), device.get('name'),
device.get('picture'), device.get(CONF_AWAY_HIDE, False))
str(dev_id).lower(), str(device.get('mac')).upper(),
device.get('name'), device.get('picture'),
device.get(CONF_AWAY_HIDE, False))
for dev_id, device in load_yaml_config_file(path).items()]

View File

@ -21,7 +21,7 @@ from .dt import datetime_to_local_str, utcnow
RE_SANITIZE_FILENAME = re.compile(r'(~|\.\.|/|\\)')
RE_SANITIZE_PATH = re.compile(r'(~|\.(\.)+)')
RE_SLUGIFY = re.compile(r'[^A-Za-z0-9_]+')
RE_SLUGIFY = re.compile(r'[^a-z0-9_]+')
def sanitize_filename(filename):
@ -36,7 +36,7 @@ def sanitize_path(path):
def slugify(text):
""" Slugifies a given text. """
text = text.replace(" ", "_")
text = text.lower().replace(" ", "_")
return RE_SLUGIFY.sub("", text)

View File

@ -7,7 +7,7 @@ Tests the device tracker compoments.
# pylint: disable=protected-access,too-many-public-methods
import unittest
from unittest.mock import patch
from datetime import timedelta
from datetime import datetime, timedelta
import os
from homeassistant.config import load_yaml_config_file
@ -127,7 +127,7 @@ class TestComponentsDeviceTracker(unittest.TestCase):
device_tracker.DOMAIN: {CONF_PLATFORM: 'test'}}))
config = device_tracker.load_config(self.yaml_devices, self.hass,
timedelta(seconds=0))[0]
self.assertEqual('DEV1', config.dev_id)
self.assertEqual('dev1', config.dev_id)
self.assertEqual(True, config.track)
def test_discovery(self):
@ -145,17 +145,25 @@ class TestComponentsDeviceTracker(unittest.TestCase):
scanner.reset()
scanner.come_home('DEV1')
self.assertTrue(device_tracker.setup(self.hass, {
device_tracker.DOMAIN: {CONF_PLATFORM: 'test'}}))
register_time = datetime(2015, 9, 15, 23, tzinfo=dt_util.UTC)
scan_time = datetime(2015, 9, 15, 23, 1, tzinfo=dt_util.UTC)
with patch('homeassistant.components.device_tracker.dt_util.utcnow',
return_value=register_time):
self.assertTrue(device_tracker.setup(self.hass, {
'device_tracker': {
'platform': 'test',
'consider_home': 59,
}}))
self.assertEqual(STATE_HOME,
self.hass.states.get('device_tracker.dev1').state)
scanner.leave_home('DEV1')
now = dt_util.utcnow().replace(second=0) + timedelta(hours=1)
with patch('homeassistant.util.dt.utcnow', return_value=now):
fire_time_changed(self.hass, now)
with patch('homeassistant.components.device_tracker.dt_util.utcnow',
return_value=scan_time):
fire_time_changed(self.hass, scan_time)
self.hass.pool.block_till_done()
self.assertEqual(STATE_NOT_HOME,

View File

@ -32,9 +32,9 @@ class TestUtil(unittest.TestCase):
def test_slugify(self):
""" Test slugify. """
self.assertEqual("Test", util.slugify("T-!@#$!#@$!$est"))
self.assertEqual("Test_More", util.slugify("Test More"))
self.assertEqual("Test_More", util.slugify("Test_(More)"))
self.assertEqual("test", util.slugify("T-!@#$!#@$!$est"))
self.assertEqual("test_more", util.slugify("Test More"))
self.assertEqual("test_more", util.slugify("Test_(More)"))
def test_split_entity_id(self):
""" Test split_entity_id. """