1 """Script to convert a device diagnostics file to a fixture."""
3 from __future__
import annotations
7 from pathlib
import Path
14 """Get parsed passed in arguments."""
15 parser = argparse.ArgumentParser(description=
"Z-Wave JS Fixture generator")
17 "diagnostics_file", type=Path, help=
"Device diagnostics file to convert"
23 "Dump fixture to file in fixtures folder. By default, the fixture will be "
24 "printed to standard output."
28 return parser.parse_args()
32 """Get path to fixtures directory."""
33 device_config = data[
"deviceConfig"]
35 f
"{device_config['manufacturer']}-{device_config['label']}_state"
37 path = Path(__file__).parents[1]
38 index = path.parts.index(
"homeassistant")
42 *path.parts[index + 1 :],
49 """Load file from path."""
50 return json.loads(path.read_text(
"utf8"))
54 """Extract fixture data from file."""
56 not isinstance(diagnostics_data, dict)
57 or "data" not in diagnostics_data
58 or "state" not in diagnostics_data[
"data"]
60 raise ValueError(
"Invalid diagnostics file format")
61 state: dict = diagnostics_data[
"data"][
"state"]
62 if not isinstance(state[
"values"], list):
63 values_dict: dict[str, dict] = state.pop(
"values")
64 state[
"values"] =
list(values_dict.values())
65 if not isinstance(state[
"endpoints"], list):
66 endpoints_dict: dict[str, dict] = state.pop(
"endpoints")
67 state[
"endpoints"] =
list(endpoints_dict.values())
73 """Create a file for the state dump in the fixtures directory."""
74 path.write_text(state_text,
"utf8")
78 """Run the main script."""
80 diagnostics_path: Path = args.diagnostics_file
83 fixture_text = json.dumps(fixture_data, indent=2)
91 if __name__ ==
"__main__":
Path get_fixtures_dir_path(dict data)
argparse.Namespace get_arguments()
dict extract_fixture_data(Any diagnostics_data)
None create_fixture_file(Path path, str state_text)