File size: 2,431 Bytes
f005306
 
 
 
114ea19
f005306
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
114ea19
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
from __future__ import annotations

import json

from server import _document_uses_moving, _merge_class_names, _merge_documents
from tiny_trigger.automation import AutomationDocument, load_automation_text


def test_merge_documents_replaces_same_named_rule() -> None:
    existing = load_automation_text(
        json.dumps(
            {
                "rules": [
                    {
                        "name": "monitor-lights",
                        "when": {"all": [{"present": {"label": "person"}}]},
                        "then": [{"type": "simulate", "name": "old action"}],
                    }
                ]
            }
        )
    )
    compiled = load_automation_text(
        json.dumps(
            {
                "rules": [
                    {
                        "name": "monitor-lights",
                        "when": {"all": [{"present": {"label": "monitor"}}]},
                        "then": [{"type": "simulate", "name": "new action"}],
                    }
                ]
            }
        )
    )

    merged = _merge_documents(existing, compiled)

    assert isinstance(merged, AutomationDocument)
    assert [rule.name for rule in merged.rules] == ["monitor-lights"]
    assert merged.rules[0].then[0].name == "new action"


def test_merge_class_names_adds_rule_labels_without_duplicates() -> None:
    assert _merge_class_names(["person", "Monitor"], ["monitor", "guitar"]) == [
        "person",
        "Monitor",
        "guitar",
    ]


def test_document_uses_moving_only_for_motion_rules() -> None:
    presence = load_automation_text(
        json.dumps(
            {
                "rules": [
                    {
                        "name": "person-visible",
                        "when": {"all": [{"present": {"label": "person"}}]},
                        "then": [{"type": "simulate", "name": "notify"}],
                    }
                ]
            }
        )
    )
    moving = load_automation_text(
        json.dumps(
            {
                "rules": [
                    {
                        "name": "car-moving",
                        "when": {"all": [{"moving": {"label": "car"}}]},
                        "then": [{"type": "simulate", "name": "notify"}],
                    }
                ]
            }
        )
    )

    assert not _document_uses_moving(presence)
    assert _document_uses_moving(moving)