mirror of
https://github.com/home-assistant/core.git
synced 2025-07-17 02:07:09 +00:00
Better handling if invalid known_devices.csv encountered.
This commit is contained in:
parent
59fa58beaf
commit
4ce550b432
@ -89,8 +89,12 @@ class DeviceTracker(object):
|
|||||||
|
|
||||||
self.lock = threading.Lock()
|
self.lock = threading.Lock()
|
||||||
|
|
||||||
|
# Dictionary to keep track of known devices and devices we track
|
||||||
self.known_devices = {}
|
self.known_devices = {}
|
||||||
|
|
||||||
|
# Did we encounter a valid known devices file
|
||||||
|
self.invalid_known_devices_file = False
|
||||||
|
|
||||||
# Read known devices if file exists
|
# Read known devices if file exists
|
||||||
if os.path.isfile(KNOWN_DEVICES_FILE):
|
if os.path.isfile(KNOWN_DEVICES_FILE):
|
||||||
with open(KNOWN_DEVICES_FILE) as inp:
|
with open(KNOWN_DEVICES_FILE) as inp:
|
||||||
@ -100,6 +104,7 @@ class DeviceTracker(object):
|
|||||||
# so we can ensure we have unique categories.
|
# so we can ensure we have unique categories.
|
||||||
used_categories = []
|
used_categories = []
|
||||||
|
|
||||||
|
try:
|
||||||
for row in csv.DictReader(inp):
|
for row in csv.DictReader(inp):
|
||||||
device = row['device']
|
device = row['device']
|
||||||
|
|
||||||
@ -127,6 +132,9 @@ class DeviceTracker(object):
|
|||||||
self.known_devices[device]['category'] = category
|
self.known_devices[device]['category'] = category
|
||||||
used_categories.append(category)
|
used_categories.append(category)
|
||||||
|
|
||||||
|
except KeyError:
|
||||||
|
self.invalid_known_devices_file = False
|
||||||
|
self.logger.warning("Invalid {} found. We won't update it with new found devices.".format(KNOWN_DEVICES_FILE))
|
||||||
|
|
||||||
if len(self.device_state_categories()) == 0:
|
if len(self.device_state_categories()) == 0:
|
||||||
self.logger.warning("No devices to track. Please update {}.".format(KNOWN_DEVICES_FILE))
|
self.logger.warning("No devices to track. Please update {}.".format(KNOWN_DEVICES_FILE))
|
||||||
@ -169,6 +177,8 @@ class DeviceTracker(object):
|
|||||||
self.statemachine.set_state(STATE_CATEGORY_ALL_DEVICES, all_devices_state)
|
self.statemachine.set_state(STATE_CATEGORY_ALL_DEVICES, all_devices_state)
|
||||||
|
|
||||||
# If we come along any unknown devices we will write them to the known devices file
|
# If we come along any unknown devices we will write them to the known devices file
|
||||||
|
# but only if we did not encounter an invalid known devices file
|
||||||
|
if not self.invalid_known_devices_file:
|
||||||
unknown_devices = [device for device in found_devices if device not in self.known_devices]
|
unknown_devices = [device for device in found_devices if device not in self.known_devices]
|
||||||
|
|
||||||
if len(unknown_devices) > 0:
|
if len(unknown_devices) > 0:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user