Files
2022-09-01 15:29:16 -04:00

163 lines
4.9 KiB
Python

import io
from pathlib import Path
from unittest import TestCase
from src.yaml_serializer import (
IncludedYaml,
IncludedYamlDirList,
IncludedYamlMergeListDir,
SecretValue,
dump_yaml,
load_yaml,
)
class dumping_yamls_tests(TestCase):
def test_dumping_includes(self):
yaml = dump_yaml(
{
"automations": IncludedYaml(
Path.cwd() / "automations.yaml",
)
},
Path.cwd(),
)
self.assertEqual(yaml, "automations: !include 'automations.yaml'\n")
def test_dumping_secrets(self):
yaml = dump_yaml({"google_password": SecretValue("gpass", "supersec")}, Path.cwd())
self.assertEqual(yaml, "google_password: !secret 'gpass'\n")
def test_dumping_include_dir_list(self):
yaml = dump_yaml({"sensor": IncludedYamlDirList(Path.cwd() / "sensors")}, Path.cwd())
self.assertEqual(yaml, "sensor: !include_dir_list 'sensors'\n")
def test_dumping_include_dir_merge_list(self):
yaml = dump_yaml(
{"sensor": IncludedYamlMergeListDir(Path.cwd() / "sensors")},
Path.cwd(),
)
self.assertEqual(yaml, "sensor: !include_dir_merge_list 'sensors'\n")
def test_load_complex_automation_setup(self):
yaml_obj = load_yaml(
io.StringIO(
"""
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
"""
),
Path.cwd(),
)
self.assertDictEqual(
dict(yaml_obj),
{
"automation manual": IncludedYamlDirList(
Path.cwd() / "automations/include_dir_list"
),
"automation ui": IncludedYaml(Path.cwd() / "automations/ui.yaml"),
"automation cools": IncludedYamlMergeListDir(
Path.cwd() / "automations/include_dir_merge_list"
),
"automation": IncludedYaml(Path.cwd() / "automations/base.yaml"),
},
)
def test_load_template_in_yaml(self):
yaml_obj = load_yaml(
io.StringIO(
"""
- id: '1652069225859'
alias: 'Lower the volume'
description: ''
mode: single
trigger:
- platform: state
entity_id: input_number.preferred_temperature
condition: []
action:
- service: media_player.volume_set
data_template:
entity_id: media_player.denon_avr_x2500h
volume_level: '{{ ((states(''input_number.volume_video'')|int)+80)/100 }}'
"""
),
Path.cwd(),
)
self.assertIsInstance(yaml_obj, list)
self.assertEqual(len(yaml_obj), 1)
self.assertDictEqual(
dict(yaml_obj[0]),
{
"id": "1652069225859",
"alias": "Lower the volume",
"description": "",
"mode": "single",
"trigger": [
{"platform": "state", "entity_id": "input_number.preferred_temperature"}
],
"condition": [],
"action": [
{
"service": "media_player.volume_set",
"data_template": {
"entity_id": "media_player.denon_avr_x2500h",
"volume_level": "{{ ((states('input_number.volume_video')|int)+80)/100 }}",
},
}
],
},
)
def test_load_stub(self):
yaml_obj = load_yaml(
io.StringIO(
"""
name: cool
date: "10:00"
bools:
- "yes"
- "no"
- true
- false
bobIsNull:
bobIsNull2: null
google_password: !secret gpass
sensor: !include_dir_list sensors
automations: !include automations.yaml
"""
),
Path.cwd(),
)
self.assertDictEqual(
dict(yaml_obj),
{
"name": "cool",
"date": "10:00",
"bools": ["yes", "no", True, False],
"bobIsNull": None,
"bobIsNull2": None,
"google_password": SecretValue("gpass", "n/a"),
"sensor": IncludedYamlDirList(Path.cwd() / "sensors"),
"automations": IncludedYaml(Path.cwd() / "automations.yaml"),
},
)
self.assertEqual(
dump_yaml(yaml_obj, Path.cwd()),
"""name: cool
date: '10:00'
bools:
- 'yes'
- 'no'
- true
- false
bobIsNull: null
bobIsNull2: null
google_password: !secret 'gpass'
sensor: !include_dir_list 'sensors'
automations: !include 'automations.yaml'
""",
)