mirror of
https://github.com/home-assistant/core.git
synced 2025-07-09 22:37:11 +00:00
Better handle file not found when loading YAML (#23908)
* Better handle file not found * Lint
This commit is contained in:
parent
f991ec15f2
commit
e356d0bcda
@ -1,6 +1,5 @@
|
|||||||
"""APNS Notification platform."""
|
"""APNS Notification platform."""
|
||||||
import logging
|
import logging
|
||||||
import os
|
|
||||||
|
|
||||||
import voluptuous as vol
|
import voluptuous as vol
|
||||||
|
|
||||||
@ -149,7 +148,8 @@ class ApnsNotificationService(BaseNotificationService):
|
|||||||
self.devices = {}
|
self.devices = {}
|
||||||
self.device_states = {}
|
self.device_states = {}
|
||||||
self.topic = topic
|
self.topic = topic
|
||||||
if os.path.isfile(self.yaml_path):
|
|
||||||
|
try:
|
||||||
self.devices = {
|
self.devices = {
|
||||||
str(key): ApnsDevice(
|
str(key): ApnsDevice(
|
||||||
str(key),
|
str(key),
|
||||||
@ -160,6 +160,8 @@ class ApnsNotificationService(BaseNotificationService):
|
|||||||
for (key, value) in
|
for (key, value) in
|
||||||
load_yaml_config_file(self.yaml_path).items()
|
load_yaml_config_file(self.yaml_path).items()
|
||||||
}
|
}
|
||||||
|
except FileNotFoundError:
|
||||||
|
pass
|
||||||
|
|
||||||
tracking_ids = [
|
tracking_ids = [
|
||||||
device.full_tracking_device_id
|
device.full_tracking_device_id
|
||||||
|
@ -3,7 +3,6 @@ from collections import defaultdict
|
|||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
from ipaddress import ip_address
|
from ipaddress import ip_address
|
||||||
import logging
|
import logging
|
||||||
import os
|
|
||||||
|
|
||||||
from aiohttp.web import middleware
|
from aiohttp.web import middleware
|
||||||
from aiohttp.web_exceptions import HTTPForbidden, HTTPUnauthorized
|
from aiohttp.web_exceptions import HTTPForbidden, HTTPUnauthorized
|
||||||
@ -155,11 +154,10 @@ async def async_load_ip_bans_config(hass: HomeAssistant, path: str):
|
|||||||
"""Load list of banned IPs from config file."""
|
"""Load list of banned IPs from config file."""
|
||||||
ip_list = []
|
ip_list = []
|
||||||
|
|
||||||
if not os.path.isfile(path):
|
|
||||||
return ip_list
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
list_ = await hass.async_add_executor_job(load_yaml_config_file, path)
|
list_ = await hass.async_add_executor_job(load_yaml_config_file, path)
|
||||||
|
except FileNotFoundError:
|
||||||
|
return ip_list
|
||||||
except HomeAssistantError as err:
|
except HomeAssistantError as err:
|
||||||
_LOGGER.error('Unable to load %s: %s', path, str(err))
|
_LOGGER.error('Unable to load %s: %s', path, str(err))
|
||||||
return ip_list
|
return ip_list
|
||||||
|
@ -363,13 +363,11 @@ def find_config_file(config_dir: Optional[str]) -> Optional[str]:
|
|||||||
def load_yaml_config_file(config_path: str) -> Dict[Any, Any]:
|
def load_yaml_config_file(config_path: str) -> Dict[Any, Any]:
|
||||||
"""Parse a YAML configuration file.
|
"""Parse a YAML configuration file.
|
||||||
|
|
||||||
|
Raises FileNotFoundError or HomeAssistantError.
|
||||||
|
|
||||||
This method needs to run in an executor.
|
This method needs to run in an executor.
|
||||||
"""
|
"""
|
||||||
try:
|
conf_dict = load_yaml(config_path)
|
||||||
conf_dict = load_yaml(config_path)
|
|
||||||
except FileNotFoundError as err:
|
|
||||||
raise HomeAssistantError("Config file not found: {}".format(
|
|
||||||
getattr(err, 'filename', err)))
|
|
||||||
|
|
||||||
if not isinstance(conf_dict, dict):
|
if not isinstance(conf_dict, dict):
|
||||||
msg = "The configuration file {} does not contain a dictionary".format(
|
msg = "The configuration file {} does not contain a dictionary".format(
|
||||||
|
@ -312,6 +312,8 @@ async def check_ha_config_file(hass):
|
|||||||
return result.add_error("File configuration.yaml not found.")
|
return result.add_error("File configuration.yaml not found.")
|
||||||
config = await hass.async_add_executor_job(
|
config = await hass.async_add_executor_job(
|
||||||
load_yaml_config_file, config_path)
|
load_yaml_config_file, config_path)
|
||||||
|
except FileNotFoundError:
|
||||||
|
return result.add_error("File not found: {}".format(config_path))
|
||||||
except HomeAssistantError as err:
|
except HomeAssistantError as err:
|
||||||
return result.add_error(
|
return result.add_error(
|
||||||
"Error loading {}: {}".format(config_path, err))
|
"Error loading {}: {}".format(config_path, err))
|
||||||
|
Loading…
x
Reference in New Issue
Block a user