Skip to content

Commit 1d5c584

Browse files
committed
add json output to zha_devices
1 parent 2d8be5d commit 1d5c584

File tree

6 files changed

+64
-3
lines changed

6 files changed

+64
-3
lines changed

README.md

+4
Original file line numberDiff line numberDiff line change
@@ -1851,6 +1851,10 @@ data:
18511851
# Optional, field the list is sorted by (example: sort by signal strength)
18521852
csvlabel: rssi
18531853
csvout: ../www/devices.csv
1854+
# Optional: JSON file to write devices data to - located in /config/json/...
1855+
json_out: zha_devices.json
1856+
# Optional: Add timestamp to Filename of JSON file. Defaults to False.
1857+
json_timestamp: True
18541858
event_done: zha_devices
18551859
```
18561860

custom_components/zha_toolkit/params.py

+4
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ class USER_PARAMS_consts: # pylint: disable=too-few-public-methods
3838
DOWNLOAD = "download"
3939
PATH = "path"
4040
USE_CACHE = "use_cache"
41+
JSON_OUT = "json_out"
42+
JSON_TIMESTAMP = "json_timestamp"
4143

4244

4345
class SERVICE_consts: # pylint: disable=too-few-public-methods
@@ -135,6 +137,8 @@ class INTERNAL_PARAMS_consts: # pylint: disable=too-few-public-methods
135137
WRITE_IF_EQUAL = "write_if_equal"
136138
CSV_FILE = "csvfile"
137139
CSV_LABEL = "csvlabel"
140+
JSON_OUT = "json_out"
141+
JSON_TIMESTAMP = "json_timestamp"
138142
DOWNLOAD = "download"
139143
PATH = "path"
140144
USE_CACHE = "use_cache"

custom_components/zha_toolkit/services.yaml

+17-1
Original file line numberDiff line numberDiff line change
@@ -2549,7 +2549,7 @@ znp_backup:
25492549
boolean:
25502550
zha_devices:
25512551
name: Get/Export Device Information
2552-
description: Export device information (Response, CSV File, Event)
2552+
description: Export device information (Response, CSV File, JSON File, Event)
25532553
fields:
25542554
ieee:
25552555
name: Device Reference
@@ -2599,6 +2599,22 @@ zha_devices:
25992599
- user_given_name
26002600
- device_reg_id
26012601
- area_id
2602+
json_out:
2603+
name: JSON Filename
2604+
description: >-
2605+
Filename of JSON to write read data to. Written to '/config/json' directory
2606+
required: false
2607+
example: zha_devices.json
2608+
selector:
2609+
text:
2610+
json_timestamp:
2611+
name: JSON Timestamp
2612+
description: >-
2613+
Add timestamp to Filename of JSON file. Defaults to False.
2614+
required: false
2615+
example: True
2616+
selector:
2617+
boolean:
26022618
event_success:
26032619
name: Success Event Name
26042620
description: Event name in case of success

custom_components/zha_toolkit/translations/en.json

+9-1
Original file line numberDiff line numberDiff line change
@@ -1474,7 +1474,7 @@
14741474
},
14751475
"zha_devices": {
14761476
"name": "Get/Export Device Information",
1477-
"description": "Export device information (Response, CSV File, Event)",
1477+
"description": "Export device information (Response, CSV File, JSON File, Event)",
14781478
"fields": {
14791479
"ieee": {
14801480
"name": "Device Reference",
@@ -1492,6 +1492,14 @@
14921492
"name": "CSV Sort Column",
14931493
"description": "Column to sort table by"
14941494
},
1495+
"json_out": {
1496+
"name": "JSON Filename",
1497+
"description": "Filename of JSON to write read data to. Written to '/config/json' directory"
1498+
},
1499+
"json_timestamp": {
1500+
"name": "JSON Timestamp",
1501+
"description": "Add timestamp to Filename of JSON file. Defaults to False."
1502+
},
14951503
"event_success": {
14961504
"name": "Success Event Name",
14971505
"description": "Event name in case of success"

custom_components/zha_toolkit/utils.py

+15-1
Original file line numberDiff line numberDiff line change
@@ -568,7 +568,7 @@ def dict_to_jsonable(src_dict):
568568

569569

570570
def write_json_to_file(
571-
data, subdir, fname, desc, listener=None, normalize_name=False
571+
data, subdir, fname, desc, listener=None, normalize_name=False, ts=None
572572
):
573573
if listener is None or subdir == "local":
574574
base_dir = os.path.dirname(__file__)
@@ -579,6 +579,12 @@ def write_json_to_file(
579579
if not os.path.isdir(out_dir):
580580
os.mkdir(out_dir)
581581

582+
if ts is not None:
583+
if '.' in fname:
584+
base, ext = fname.rsplit('.', 1)
585+
fname = base + '_' + ts + '.' + ext
586+
else:
587+
fname = fname + '_' + ts
582588
if normalize_name:
583589
file_name = os.path.join(out_dir, normalize_filename(fname))
584590
else:
@@ -903,6 +909,8 @@ def extractParams( # noqa: C901
903909
p.DOWNLOAD: None,
904910
p.PATH: None,
905911
p.USE_CACHE: False,
912+
p.JSON_OUT: None,
913+
p.JSON_TIMESTAMP: False,
906914
}
907915

908916
# Endpoint to send command to
@@ -1055,6 +1063,12 @@ def extractParams( # noqa: C901
10551063
if P.CSVLABEL in rawParams:
10561064
params[p.CSV_LABEL] = rawParams[P.CSVLABEL]
10571065

1066+
if P.JSON_OUT in rawParams:
1067+
params[p.JSON_OUT] = rawParams[P.JSON_OUT]
1068+
1069+
if P.JSON_TIMESTAMP in rawParams:
1070+
params[p.JSON_TIMESTAMP] = rawParams[P.JSON_TIMESTAMP]
1071+
10581072
return params
10591073

10601074

custom_components/zha_toolkit/zha.py

+15
Original file line numberDiff line numberDiff line change
@@ -121,3 +121,18 @@ async def zha_devices(
121121
)
122122
if selectDeviceFields:
123123
event_data["devices"] = slimmedDevices
124+
125+
if params[p.JSON_OUT] is not None:
126+
timeStamp = None
127+
if params[p.JSON_TIMESTAMP] is not None:
128+
timeStamp = event_data['start_time'].split('.', 1)[0]
129+
u.write_json_to_file(
130+
event_data,
131+
subdir="json",
132+
fname=params[p.JSON_OUT],
133+
desc="zha_devices",
134+
listener=listener,
135+
normalize_name=False,
136+
ts=timeStamp
137+
)
138+

0 commit comments

Comments
 (0)