8000 Add machine-readable output · Issue #209 · python-kasa/python-kasa · GitHub
[go: up one dir, main page]

Skip to content

Add machine-readable output #209

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
huntsman95 opened this issue Sep 23, 2021 · 5 comments · Fixed by #430
Closed

Add machine-readable output #209

huntsman95 opened this issue Sep 23, 2021 · 5 comments · Fixed by #430

Comments

@huntsman95
Copy link

CLI Module run with "sysinfo" flag or datatypes returned in JSON format are invalid as variable names and string values are encapsulated with single-quotes. The JSON standard requires double-quotes. This is making it difficult to pipe data from the script to any service/application that expects proper JSON data without running it through a wrapper that does a find/replace on the single quotes.

@rytilahti
Copy link
Member

None of the commands of the cli tool output json, but a PR to add support for defining the output format would be welcome!

@rytilahti rytilahti added the enhancement New feature or request label Nov 25, 2021
@rytilahti rytilahti changed the title CLI module outputs invalid JSON with single-quotes Add machine-readable output Nov 25, 2021
@tuxthepenguin84
Copy link
tuxthepenguin84 commented Nov 26, 2022

I'm sure there's a cleaner way but this works as a temp solution:

kasa --type plug --host your_host_here sysinfo | sed "s/'/\"/g" | sed 1d | jq

And if you want to just grab the data from a key:

kasa --type plug --host your_host_here sysinfo | sed "s/'/\"/g" | sed 1d | jq '.relay_state'

@lazynooblet
Copy link
lazynooblet commented Jan 6, 2023

I hacked something togethor to make a working JSON output.

(kasa --debug --host IP) 2>&1 1>/dev/null | sed -n '/DEBUG:kasa.protocol:[^ ]* <</,/DEBUG/p;' | sed 's/DEBUG:kasa.protocol:[^ ]* << \({.*\)$/BEGIN\n\1/; s/^DEBUG.*$/END/; s/'"'"'/"/g' | awk '/^BEGIN/ {n++;} !/BEGIN|END/ {if (n==3) print;}'

The debug output has the JSON we need.

It seems that the kasa code repeats the same commands so we get the same output twice (this is due to not specifying type).

Using SED and AWK. I managed to split the JSON from other debug text, split each output into sections and then only print one output, which has all the information we need. We can then parse using JQ.

I've tested this using a KP115 (smartplug) and KP303 (smartstrip).

I also have an HS110 which outputs non-sensical multi-line JSON so JQ can't parse it.

  	"dev_name": "Smart Wi-Fi Plug With Energy "
  	"Monitoring",

This gives: parse error: Expected separator between values at line 84, column 52

For my working KP115, as an example, I can fetch the emeter information using jq

image

Regarding

None of the commands of the cli tool output json, but a PR to add support for defining the output format would be welcome!

Simply the output from the device is all we'd want. Any calculations or interpretation aren't needed.

@rytilahti
Copy link
Member
rytilahti commented Feb 18, 2023

@lazynooblet (and everyone else), feel free to test the linked PR, it should cover ~all commands if I didn't miss something. The simplest way to use the raw data is just access the output from state which provides the complete internal state of the device:

❯ kasa --json --host 192.168.xx.xx --type plug state|jq 'map_values(keys)'
{
  "system": [
    "get_sysinfo"
  ],
  "schedule": [
    "get_daystat",
    "get_monthstat",
    "get_next_action",
    "get_realtime",
    "get_rules"
  ],
  "anti_theft": [
    "get_next_action",
    "get_rules"
  ],
  "time": [
    "get_time",
    "get_timezone"
  ],
  "cnCloud": [
    "get_info"
  ],
  "emeter": [
    "get_daystat",
    "get_monthstat",
    "get_realtime"
  ]
}

@rytilahti rytilahti removed the enhancement New feature or request label Feb 18, 2023
@lazynooblet
Copy link

@rytilahti this works great thank you!

Its also much faster. Whilst there was a pause of half a second as the commands went back and forth, now the response is instant.
I now just get the data I need, nothing else, which I then graph in Zabbix.

I can finally remove that monstrosity of sed/awk/jq 😏

Thanks again! 🥇

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
388D
Development

Successfully merging a pull request may close this issue.

4 participants
0