File: generate_metadata.py

package info (click to toggle)
python-aiohomekit 3.2.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, trixie
  • size: 1,620 kB
  • sloc: python: 16,560; sh: 14; makefile: 8
file content (155 lines) | stat: -rw-r--r-- 4,531 bytes parent folder | download
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#! env python

import json
import plistlib
import textwrap

from aiohomekit.uuid import normalize_uuid


plist = "/Applications/HomeKit Accessory Simulator.app/Contents/Frameworks/HAPAccessoryKit.framework/Versions/A/Resources/default.metadata.plist"
with open(plist, "rb") as fp:
    data  = plistlib.load(fp)


enrichment = {
    "00000120-0000-1000-8000-0026BB765291": {
        "struct": ".structs.StreamingStatus"
    },
    "00000117-0000-1000-8000-0026BB765291": {
        "struct": ".structs.SelectedRTPStreamConfiguration"
    },
    "00000116-0000-1000-8000-0026BB765291": {
        "struct": ".structs.SupportedRTPConfiguration",
        "array": True,
    },
    "00000115-0000-1000-8000-0026BB765291": {
        "struct": ".structs.SupportedAudioStreamConfiguration"
    },
    "00000114-0000-1000-8000-0026BB765291": {
        "struct": ".structs.SupportedVideoStreamConfiguration"
    },
}


characteristics = {}

for char in data.get('Characteristics', []):
    name = char['Name'].replace(".", "_").replace(" ", "_").upper()

    c = characteristics[char['UUID']] = {
        'name': name,
        'description': char['Name'],
    }

    if char.get('Properties'):
        c['perms'] = []
        for perm in char.get('Properties'):
            if perm == "read":
                c['perms'].append("pr")
            if perm == "write":
                c['perms'].append("pw")
            if perm == "cnotify":
                c['perms'].append("ev")

    if char.get('Format'):
        c['format'] = char['Format']

    if char.get('Unit'):
        c['unit'] = char['Unit']

    if 'Constraints' not in char:
        continue

    constraints = char['Constraints']

    if constraints.get('MaximumValue'):
        c['max_value'] = constraints['MaximumValue']
    if constraints.get('MaximumValue'):
        c['min_value'] = constraints['MinimumValue']
    if constraints.get('StepValue'):
        c['step_value'] = constraints['StepValue']


with open("aiohomekit/model/characteristics/data.py", "w") as fp:
    fp.write("# AUTOGENERATED, DO NOT EDIT\n\n")

    for char in enrichment.values():
        if "struct" in char:
            imp, frm = char["struct"].rsplit(".", 1)
            fp.write(f"from {imp} import {frm}\n")
    fp.write("\n\n")

    fp.write("characteristics = {\n")

    for char_uuid, char in characteristics.items():
        name = json.dumps(char["name"])
        description = json.dumps(char["description"])
        perms = json.dumps(char["perms"])
        format = json.dumps(char["format"])

        fp.write(f"    \"{char_uuid}\": {{\n")
        fp.write(f"        \"name\": {name},\n")
        fp.write(f"        \"description\": {description},\n")
        fp.write(f"        \"perms\": {perms},\n")
        fp.write(f"        \"format\": {format},\n")

        struct = enrichment.get(char_uuid, {}).get("struct")
        if struct:
            _, frm = struct.rsplit(".", 1)
            fp.write(f"        \"struct\": {frm},\n")

        array = enrichment.get(char_uuid, {}).get("array")
        if array:
            fp.write(f"        \"array\": {array},\n")

        if "unit" in char:
            unit = json.dumps(char["unit"])
            fp.write(f"        \"unit\": {unit},\n")

        if "max_value" in char:
            max_value = json.dumps(char["max_value"])
            fp.write(f"        \"max_value\": {max_value},\n")

        if "min_value" in char:
            min_value = json.dumps(char["min_value"])
            fp.write(f"        \"min_value\": {min_value},\n")

        if "step_value" in char:
            step_value = json.dumps(char["step_value"])
            fp.write(f"        \"min_step\": {step_value},\n")

        fp.write("    },\n")
        pass

    fp.write("}\n")

from aiohomekit.model.services import ServicesTypes

for serv in data.get('Services', []):
    name = serv['Name'].replace(" ", "_").upper()
    short = normalize_uuid(serv['UUID'])
    print(f'{name} = "{short}"')


services = {}

for serv in data.get('Services', []):
    name = serv['Name'].replace(" ", "_").upper()

    s = services[serv['UUID']] = {
        'name': name,
        'description': serv['Name'],
        'required': serv.get("RequiredCharacteristics", []),
        'optional': serv.get("OptionalCharacteristics", []),
    }


with open("aiohomekit/model/services/data.py", "w") as fp:
    fp.write(textwrap.dedent("""
    # AUTOGENERATED, DO NOT EDIT

    services = 
    """).strip())
    fp.write(" " + json.dumps(services, indent=4))
    fp.write("\n")