Skip to content

Commit

Permalink
silencing invalid errors
Browse files Browse the repository at this point in the history
  • Loading branch information
asosnovsky committed Sep 7, 2022
1 parent c5cf8ed commit 41dc77f
Show file tree
Hide file tree
Showing 9 changed files with 85 additions and 43 deletions.
4 changes: 4 additions & 0 deletions api/src/automations/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ class FailedDeletion(AutomationLoaderException):
pass


class InvalidPackage(AutomationLoaderException):
pass


class DBError(Exception):
pass

Expand Down
96 changes: 53 additions & 43 deletions api/src/automations/loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
)

from ..hass_config.loader import HassConfig
from .errors import InvalidAutomationFile
from .errors import InvalidAutomationFile, InvalidPackage
from .tags import TagManager
from .types import (
AutomationExtractedIter,
Expand Down Expand Up @@ -46,7 +46,9 @@ def load_and_iter_automations(
elif isinstance(ref, InlineAutomation):
yield from load_automation_inline_ref(hass_config.root_path, tag_manager, ref)
else:
raise AssertionError(f"ref has an invalid type of {type(ref)} = {ref}")
raise logger.error(
AssertionError(f"ref has an invalid type of {type(ref)} = {ref}")
)
except InvalidAutomationFile as e:
logger.error(e)

Expand All @@ -56,12 +58,17 @@ def extract_automation_refs(
) -> AutomationExtractedIter:
found = False

for ref in chain(
extract_automation_root_refs(hass_config), extract_automation_package_refs(hass_config)
):
for ref in extract_automation_root_refs(hass_config):
found = True
yield ref

try:
for ref in extract_automation_package_refs(hass_config):
found = True
yield ref
except InvalidPackage as err:
logger.fatal(err)

if not found:
yield IncludedAutoamtion([""], IncludedYaml(hass_config.root_path / "automations.yaml"))

Expand All @@ -80,7 +87,7 @@ def extract_automation_root_refs(
source_file=hass_config.get_configuration_path(),
)
else:
logger.warning(f"found an invalid type in {key}!")
logger.error(f"found an invalid type in {key}!")


def extract_automation_package_refs(hass_config: HassConfig) -> AutomationExtractedIter:
Expand All @@ -93,7 +100,7 @@ def extract_automation_package_refs(hass_config: HassConfig) -> AutomationExtrac
packages,
)
elif isinstance(packages_config, IncludedYamlDirList):
raise AssertionError("cannot configure packages with 'included_dir_list' directive")
raise InvalidPackage("cannot configure packages with 'included_dir_list' directive")
elif isinstance(packages_config, IncludedYamlDirNamed):
packages = packages_config.to_normalized_json(use_path_as_keys=True)
for package_path, package in packages.items():
Expand All @@ -109,7 +116,7 @@ def extract_automation_package_refs(hass_config: HassConfig) -> AutomationExtrac
package_set,
) in iter(packages_config):
if not isinstance(package_set, dict):
raise AssertionError(
raise InvalidPackage(
f"expected {pacakge_set_path} to be a dictionary but got {type(package_set)}"
)
yield from extract_automation_inline_package_refs(
Expand All @@ -124,7 +131,7 @@ def extract_automation_package_refs(hass_config: HassConfig) -> AutomationExtrac
packages_config,
)
else:
raise AssertionError("configurations.homeassistant.packages must be a dictionary!")
raise InvalidPackage("configurations.homeassistant.packages must be a dictionary!")


def extract_automation_inline_package_refs(
Expand All @@ -134,44 +141,47 @@ def extract_automation_inline_package_refs(
ignore_package_name=False,
) -> AutomationExtractedIter:
for package_name, package_data in packages_config.items():
if isinstance(package_data, IncludedYaml):
package_mapping = package_data.to_normalized_json()
yield from extract_automation_from_package(
package_data=package_mapping,
package_name=package_name,
source_file=source_file,
prefix_config_keys=prefix_config_keys,
ignore_package_name=ignore_package_name,
)
elif isinstance(package_data, IncludedYamlDirList):
raise NotImplementedError(package_data)
elif isinstance(package_data, IncludedYamlDirNamed):
for package_key_path in package_data.to_normalized_json(True).keys():
if package_key_path.stem == "automation":
yield IncludedAutoamtion(
configuration_key=[package_name], ref=IncludedYaml(package_key_path)
try:
if isinstance(package_data, IncludedYaml):
package_mapping = package_data.to_normalized_json()
yield from extract_automation_from_package(
package_data=package_mapping,
package_name=package_name,
source_file=source_file,
prefix_config_keys=prefix_config_keys,
ignore_package_name=ignore_package_name,
)
elif isinstance(package_data, IncludedYamlDirList):
raise NotImplementedError(package_data)
elif isinstance(package_data, IncludedYamlDirNamed):
for package_key_path in package_data.to_normalized_json(True).keys():
if package_key_path.stem == "automation":
yield IncludedAutoamtion(
configuration_key=[package_name], ref=IncludedYaml(package_key_path)
)
elif isinstance(package_data, IncludedYamlDirMergedNamed):
for data_path, data_set in iter(package_data):
yield from extract_automation_from_package(
package_name=package_name,
package_data=data_set,
source_file=data_path,
prefix_config_keys=[],
ignore_package_name=True,
)
elif isinstance(package_data, IncludedYamlDirMergedNamed):
for data_path, data_set in iter(package_data):
elif isinstance(package_data, dict):
yield from extract_automation_from_package(
package_data=package_data,
package_name=package_name,
package_data=data_set,
source_file=data_path,
prefix_config_keys=[],
ignore_package_name=True,
source_file=source_file,
prefix_config_keys=prefix_config_keys,
ignore_package_name=ignore_package_name,
)
elif isinstance(package_data, dict):
yield from extract_automation_from_package(
package_data=package_data,
package_name=package_name,
source_file=source_file,
prefix_config_keys=prefix_config_keys,
ignore_package_name=ignore_package_name,
)
else:
raise AssertionError(
f"configurations.homeassistant.packages[{package_name}] must be a dictionary!"
)
else:
raise InvalidPackage(
f"configurations.homeassistant.packages[{package_name}] must be a dictionary!"
)
except Exception as err:
logger.error(err)


def extract_automation_from_package(
Expand Down
21 changes: 21 additions & 0 deletions api/tests/samples/config-14/automations.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
- alias: UI 1
description: Example
mode: single
trigger:
- platform: homeassistant
event: start
condition: []
action:
- service: counter.increment
data: {}
target:
entity_id: counter.up_times
- alias: UI 2
description: ""
trigger:
- platform: sun
event: sunrise
offset: 0
condition: []
action: []
mode: single
2 changes: 2 additions & 0 deletions api/tests/samples/config-14/configuration.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
homeassistant:
packages: !include_dir_named integrations
Empty file.
1 change: 1 addition & 0 deletions api/tests/samples/config-14/integrations/automation.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
automation: !include ../automations.yaml
1 change: 1 addition & 0 deletions api/tests/samples/config-14/integrations/media_player.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
media_player: !include_dir_list ../entities/media_players
2 changes: 2 additions & 0 deletions api/tests/unittests/automations/test_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from src.automations.types import ExtenededAutomation
from src.hass_config.loader import HassConfig
from tests.utils import (
HA_CONFIG14_EXAMPLE,
HA_CONFIG2_EXAMPLE,
HA_CONFIG3_EXAMPLE,
HA_CONFIG4_EXAMPLE,
Expand Down Expand Up @@ -162,6 +163,7 @@ def test_extract_all_automation_files(self):
(HA_CONFIG11_EXAMPLE, 3),
(HA_CONFIG12_EXAMPLE, 4),
(HA_CONFIG13_EXAMPLE, 4),
(HA_CONFIG14_EXAMPLE, 1),
]:
with self.subTest(config_name=ha_path.name, expected=expected):
hass_config = HassConfig(ha_path)
Expand Down
1 change: 1 addition & 0 deletions api/tests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
HA_CONFIG11_EXAMPLE = SAMPLES_FOLDER / "config-11"
HA_CONFIG12_EXAMPLE = SAMPLES_FOLDER / "config-12"
HA_CONFIG13_EXAMPLE = SAMPLES_FOLDER / "config-13"
HA_CONFIG14_EXAMPLE = SAMPLES_FOLDER / "config-14"


def get_example_automation_loader(
Expand Down

0 comments on commit 41dc77f

Please sign in to comment.