package setup work

This commit is contained in:
ari committed 2022-08-27 13:58:41 -04:00
1 parent ddbd780560
commit 30a6f2ca6e
28 files changed
+85 -150

No files matched your search

+10
View File
@@ -36,6 +36,16 @@ def extract_automation_paths(
yield "", "automations.yaml"
def extract_package_automation_paths(hass_config: HassConfig):
if homeassistant_config := hass_config.configurations.get("homeassistant", None):
if isinstance(homeassistant_config, IncludedYamlDir): # type: ignore
homeassistant_config_d = homeassistant_config.to_normalized_json()
elif isinstance(homeassistant_config, dict):
homeassistant_config_d = homeassistant_config
else:
raise AssertionError("configurations.homeassistant must be a dictionary!")
def get_base_automation_key(hass_config: HassConfig):
found_any = False
for key, value in extract_automation_keys(hass_config):
+24 -1
View File
@@ -2,7 +2,7 @@ from pathlib import Path
from typing import Optional
from src.automations.tags import TagManager
from src.yaml_serializer import IncludedYaml, load_yaml
from src.yaml_serializer import IncludedYaml, IncludedYamlDir, load_yaml
class HassConfig:
@@ -14,6 +14,29 @@ class HassConfig:
with self.get_configuration_path().open("r") as f:
return dict(load_yaml(f, root_path=self.root_path)) # type: ignore
@property
def homeassistant(self) -> dict:
if homeassistant_config := self.configurations.get("homeassistant", None):
if isinstance(homeassistant_config, IncludedYamlDir): # type: ignore
homeassistant_config = homeassistant_config.to_normalized_json()
if isinstance(homeassistant_config, dict):
return homeassistant_config
else:
raise AssertionError("configurations.homeassistant must be a dictionary!")
return {}
@property
def pacakges(self) -> dict:
if packages_config := self.homeassistant.get("packages", None):
if isinstance(packages_config, IncludedYamlDir): # type: ignore
packages_config = packages_config.to_normalized_json()
if isinstance(packages_config, dict):
return packages_config
else:
raise AssertionError("configurations.homeassistant.packages must be a dictionary!")
return {}
@property
def automation_tags(self) -> TagManager:
tag_path = self.get_automation_tags_path()
+2 -2
View File
@@ -81,7 +81,7 @@ class IncludedYamlDirMergedNamed(NamedTuple):
def to_normalized_json(self):
out = {}
for path, data in load_dir_yaml(self.path):
overlap = set(data.key()).intersection(set(out.keys()))
overlap = set(data.keys()).intersection(set(out.keys()))
if len(overlap) > 0:
raise AssertionError(
f"The keys {overlap} were found in more than 1 file, cannot handle overlapping keys due to undefined behavior expectation. See {path}"
@@ -114,7 +114,7 @@ class IncludedYamlDirNamed(NamedTuple):
def to_normalized_json(self):
out = {}
for path, data in load_dir_yaml(self.path):
if out.get(path.name, None):
if out.get(path.name, None) is None:
out[path.name] = data
else:
raise AssertionError(
@@ -13,6 +13,4 @@ script: !include scripts.yaml
scene: !include scenes.yaml
homeassistant:
packages:
ha:
automation: !include automations/ui.yaml
packages: !include_dir_merge_named packages
@@ -0,0 +1,2 @@
ha:
automation: !include ../automations/ui.yaml
@@ -0,0 +1,15 @@
package2:
automation:
- id: "package-ha"
alias: package ha
description: Example package auto
mode: single
trigger:
- platform: homeassistant
event: start
condition: []
action:
- service: counter.increment
data: {}
target:
entity_id: counter.up_times
@@ -1,23 +0,0 @@
- id: "base1"
alias: BASE
description: Example
mode: single
trigger:
- platform: homeassistant
event: start
condition: []
action:
- service: counter.increment
data: {}
target:
entity_id: counter.up_times
- id: "base2"
alias: base 2
description: ""
trigger:
- platform: sun
event: sunrise
offset: 0
condition: []
action: []
mode: single
@@ -1,14 +0,0 @@
id: "1659114822642"
alias: On Home Down
description: Example
mode: single
trigger:
- platform: homeassistant
event: shutdown
condition: []
action:
- service: counter.increment
target:
entity_id:
- counter.up_times
data: {}
@@ -1,13 +0,0 @@
id: "1659114647066"
alias: HA Up
description: Example
mode: single
trigger:
- platform: homeassistant
event: start
condition: []
action:
- service: counter.increment
data: {}
target:
entity_id: counter.up_times
@@ -1,13 +0,0 @@
id: "1659114647067"
alias: Notify Washer
description: Example
mode: single
trigger:
- platform: homeassistant
event: start
condition: []
action:
- service: counter.increment
data: {}
target:
entity_id: counter.up_times
@@ -1,7 +0,0 @@
- id: "mlist1"
alias: Merged List 1
description: ""
trigger: []
condition: []
action: []
mode: single
@@ -1,14 +0,0 @@
- id: "mlist2"
alias: Merged List 2
description: ""
trigger: []
condition: []
action: []
mode: single
- id: "mlist3"
alias: Merged List 3
description: ""
trigger: []
condition: []
action: []
mode: single
@@ -1,14 +0,0 @@
- id: "sublist3-mlist2"
alias: Sublist 3 - Merged List 2
description: ""
trigger: []
condition: []
action: []
mode: single
- id: "sublist3-mlist3"
alias: Sublist 3 - Merged List 3
description: ""
trigger: []
condition: []
action: []
mode: single
@@ -1,23 +0,0 @@
- id: "ui1"
alias: UI 1
description: Example
mode: single
trigger:
- platform: homeassistant
event: start
condition: []
action:
- service: counter.increment
data: {}
target:
entity_id: counter.up_times
- id: "ui2"
alias: UI 2
description: ""
trigger:
- platform: sun
event: sunrise
offset: 0
condition: []
action: []
mode: single
@@ -1,18 +0,0 @@
# Loads default set of integrations. Do not remove.
default_config:
# Text to speech
tts:
- platform: google_translate
# automation manual: !include_dir_list ./automations/include_dir_list
# automation ui: !include automations/ui.yaml
# automation: !include automations/base.yaml
# automation cools: !include_dir_merge_list automations/include_dir_merge_list
script: !include scripts.yaml
scene: !include scenes.yaml
homeassistant:
packages:
ha:
automation: !include automations/ui.yaml
@@ -13,7 +13,4 @@ script: !include scripts.yaml
scene: !include scenes.yaml
homeassistant:
packages: !include_dir_merge_named packages
packages base:
ha:
automation: !include automations/ui.yaml
packages: !include_dir_named packages
@@ -0,0 +1 @@
automation: !include ../automations/ui.yaml
@@ -0,0 +1,14 @@
automation:
- id: "package-ha"
alias: package ha
description: Example package auto
mode: single
trigger:
- platform: homeassistant
event: start
condition: []
action:
- service: counter.increment
data: {}
target:
entity_id: counter.up_times
@@ -1,2 +1,4 @@
# empty configuration file
cloud:
homeassistant:
+13 -1
View File
@@ -2,7 +2,7 @@ from unittest import TestCase
from src.automations.types import BaseAutomation
from src.hass_config.loader import HassConfig
from tests.utils import create_dummy_config_folder
from tests.utils import HA_CONFIG8_EXAMPLE, SAMPLES_FOLDER, create_dummy_config_folder
class loader_tests(TestCase):
@@ -41,3 +41,15 @@ class loader_tests(TestCase):
(folder / "automations.yaml").unlink()
hass_config = HassConfig(folder)
self.assertIsInstance(hass_config.configurations, dict)
def test_load_homeassitant(self):
for folder in SAMPLES_FOLDER.iterdir():
with self.subTest(name=folder.name):
hass_config = HassConfig(folder)
assert isinstance(hass_config.homeassistant, dict)
def test_load_packages(self):
for folder in SAMPLES_FOLDER.iterdir():
with self.subTest(name=folder.name):
hass_config = HassConfig(folder)
assert isinstance(hass_config.pacakges, dict)