Device tracker: remove support for old config
This commit is contained in:
parent
1159360281
commit
fb46eff5f8
3 changed files with 13 additions and 87 deletions
|
@ -8,7 +8,6 @@ https://home-assistant.io/components/device_tracker/
|
||||||
"""
|
"""
|
||||||
# pylint: disable=too-many-instance-attributes, too-many-arguments
|
# pylint: disable=too-many-instance-attributes, too-many-arguments
|
||||||
# pylint: disable=too-many-locals
|
# pylint: disable=too-many-locals
|
||||||
import csv
|
|
||||||
from datetime import timedelta
|
from datetime import timedelta
|
||||||
import logging
|
import logging
|
||||||
import os
|
import os
|
||||||
|
@ -36,7 +35,6 @@ ENTITY_ID_ALL_DEVICES = group.ENTITY_ID_FORMAT.format('all_devices')
|
||||||
|
|
||||||
ENTITY_ID_FORMAT = DOMAIN + '.{}'
|
ENTITY_ID_FORMAT = DOMAIN + '.{}'
|
||||||
|
|
||||||
CSV_DEVICES = "known_devices.csv"
|
|
||||||
YAML_DEVICES = 'known_devices.yaml'
|
YAML_DEVICES = 'known_devices.yaml'
|
||||||
|
|
||||||
CONF_TRACK_NEW = "track_new_devices"
|
CONF_TRACK_NEW = "track_new_devices"
|
||||||
|
@ -93,10 +91,6 @@ def see(hass, mac=None, dev_id=None, host_name=None, location_name=None,
|
||||||
def setup(hass, config):
|
def setup(hass, config):
|
||||||
""" Setup device tracker """
|
""" Setup device tracker """
|
||||||
yaml_path = hass.config.path(YAML_DEVICES)
|
yaml_path = hass.config.path(YAML_DEVICES)
|
||||||
csv_path = hass.config.path(CSV_DEVICES)
|
|
||||||
if os.path.isfile(csv_path) and not os.path.isfile(yaml_path) and \
|
|
||||||
convert_csv_config(csv_path, yaml_path):
|
|
||||||
os.remove(csv_path)
|
|
||||||
|
|
||||||
conf = config.get(DOMAIN, {})
|
conf = config.get(DOMAIN, {})
|
||||||
if isinstance(conf, list):
|
if isinstance(conf, list):
|
||||||
|
@ -370,21 +364,6 @@ class Device(Entity):
|
||||||
self.last_update_home = True
|
self.last_update_home = True
|
||||||
|
|
||||||
|
|
||||||
def convert_csv_config(csv_path, yaml_path):
|
|
||||||
""" Convert CSV config file format to YAML. """
|
|
||||||
used_ids = set()
|
|
||||||
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).lower(),
|
|
||||||
used_ids)
|
|
||||||
used_ids.add(dev_id)
|
|
||||||
device = Device(None, None, None, row['track'] == '1', dev_id,
|
|
||||||
row['device'], row['name'], row['picture'])
|
|
||||||
update_config(yaml_path, dev_id, device)
|
|
||||||
return True
|
|
||||||
|
|
||||||
|
|
||||||
def load_config(path, hass, consider_home, home_range):
|
def load_config(path, hass, consider_home, home_range):
|
||||||
""" Load devices from YAML config file. """
|
""" Load devices from YAML config file. """
|
||||||
if not os.path.isfile(path):
|
if not os.path.isfile(path):
|
||||||
|
|
|
@ -51,55 +51,6 @@ class TestComponentsDeviceTracker(unittest.TestCase):
|
||||||
|
|
||||||
self.assertFalse(device_tracker.is_on(self.hass, entity_id))
|
self.assertFalse(device_tracker.is_on(self.hass, entity_id))
|
||||||
|
|
||||||
def test_migrating_config(self):
|
|
||||||
csv_devices = self.hass.config.path(device_tracker.CSV_DEVICES)
|
|
||||||
|
|
||||||
self.assertFalse(os.path.isfile(csv_devices))
|
|
||||||
self.assertFalse(os.path.isfile(self.yaml_devices))
|
|
||||||
|
|
||||||
person1 = {
|
|
||||||
'mac': 'AB:CD:EF:GH:IJ:KL',
|
|
||||||
'name': 'Paulus',
|
|
||||||
'track': True,
|
|
||||||
'picture': 'http://placehold.it/200x200',
|
|
||||||
}
|
|
||||||
person2 = {
|
|
||||||
'mac': 'MN:OP:QR:ST:UV:WX:YZ',
|
|
||||||
'name': '',
|
|
||||||
'track': False,
|
|
||||||
'picture': None,
|
|
||||||
}
|
|
||||||
|
|
||||||
try:
|
|
||||||
with open(csv_devices, 'w') as fil:
|
|
||||||
fil.write('device,name,track,picture\n')
|
|
||||||
for pers in (person1, person2):
|
|
||||||
fil.write('{},{},{},{}\n'.format(
|
|
||||||
pers['mac'], pers['name'],
|
|
||||||
'1' if pers['track'] else '0', pers['picture'] or ''))
|
|
||||||
|
|
||||||
self.assertTrue(device_tracker.setup(self.hass, {}))
|
|
||||||
self.assertFalse(os.path.isfile(csv_devices))
|
|
||||||
self.assertTrue(os.path.isfile(self.yaml_devices))
|
|
||||||
|
|
||||||
yaml_config = load_yaml_config_file(self.yaml_devices)
|
|
||||||
|
|
||||||
self.assertEqual(2, len(yaml_config))
|
|
||||||
|
|
||||||
for pers, yaml_pers in zip(
|
|
||||||
(person1, person2), sorted(yaml_config.values(),
|
|
||||||
key=lambda pers: pers['mac'])):
|
|
||||||
for key, value in pers.items():
|
|
||||||
if key == 'name' and value == '':
|
|
||||||
value = DEVICE_DEFAULT_NAME
|
|
||||||
self.assertEqual(value, yaml_pers.get(key))
|
|
||||||
|
|
||||||
finally:
|
|
||||||
try:
|
|
||||||
os.remove(csv_devices)
|
|
||||||
except FileNotFoundError:
|
|
||||||
pass
|
|
||||||
|
|
||||||
def test_reading_yaml_config(self):
|
def test_reading_yaml_config(self):
|
||||||
dev_id = 'test'
|
dev_id = 'test'
|
||||||
device = device_tracker.Device(
|
device = device_tracker.Device(
|
||||||
|
|
|
@ -1,9 +1,4 @@
|
||||||
"""
|
"""Tests device sun light trigger component."""
|
||||||
tests.test_component_device_sun_light_trigger
|
|
||||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
||||||
|
|
||||||
Tests device sun light trigger component.
|
|
||||||
"""
|
|
||||||
# pylint: disable=too-many-public-methods,protected-access
|
# pylint: disable=too-many-public-methods,protected-access
|
||||||
import os
|
import os
|
||||||
import unittest
|
import unittest
|
||||||
|
@ -19,25 +14,26 @@ from tests.common import (
|
||||||
ensure_sun_set)
|
ensure_sun_set)
|
||||||
|
|
||||||
|
|
||||||
KNOWN_DEV_CSV_PATH = os.path.join(get_test_config_dir(),
|
|
||||||
device_tracker.CSV_DEVICES)
|
|
||||||
KNOWN_DEV_YAML_PATH = os.path.join(get_test_config_dir(),
|
KNOWN_DEV_YAML_PATH = os.path.join(get_test_config_dir(),
|
||||||
device_tracker.YAML_DEVICES)
|
device_tracker.YAML_DEVICES)
|
||||||
|
|
||||||
|
|
||||||
def setUpModule(): # pylint: disable=invalid-name
|
def setUpModule(): # pylint: disable=invalid-name
|
||||||
""" Initalizes a Home Assistant server. """
|
"""Write a device tracker known devices file to be used."""
|
||||||
with open(KNOWN_DEV_CSV_PATH, 'w') as fil:
|
device_tracker.update_config(
|
||||||
fil.write('device,name,track,picture\n')
|
KNOWN_DEV_YAML_PATH, 'device_1', device_tracker.Device(
|
||||||
fil.write('DEV1,device 1,1,http://example.com/dev1.jpg\n')
|
None, None, None, True, 'device_1', 'DEV1',
|
||||||
fil.write('DEV2,device 2,1,http://example.com/dev2.jpg\n')
|
picture='http://example.com/dev1.jpg'))
|
||||||
|
|
||||||
|
device_tracker.update_config(
|
||||||
|
KNOWN_DEV_YAML_PATH, 'device_2', device_tracker.Device(
|
||||||
|
None, None, None, True, 'device_2', 'DEV2',
|
||||||
|
picture='http://example.com/dev2.jpg'))
|
||||||
|
|
||||||
|
|
||||||
def tearDownModule(): # pylint: disable=invalid-name
|
def tearDownModule(): # pylint: disable=invalid-name
|
||||||
""" Stops the Home Assistant server. """
|
"""Remove device tracker known devices file."""
|
||||||
for fil in (KNOWN_DEV_CSV_PATH, KNOWN_DEV_YAML_PATH):
|
os.remove(KNOWN_DEV_YAML_PATH)
|
||||||
if os.path.isfile(fil):
|
|
||||||
os.remove(fil)
|
|
||||||
|
|
||||||
|
|
||||||
class TestDeviceSunLightTrigger(unittest.TestCase):
|
class TestDeviceSunLightTrigger(unittest.TestCase):
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue