FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Add machine-readable output · Issue #209 · python-kasa/python-kasa · GitHub

Repository navigation

Add machine-readable output #209

Closed

Description

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.

Activity

  1. rytilahti commented on Oct 4, 2021

    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!

  2. changed the title [-]CLI module outputs invalid JSON with single-quotes[/-] [+]Add machine-readable output[/+] on Nov 25, 2021
  3. tuxthepenguin84 commented on 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'
    
  4. lazynooblet commented on 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

    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.

  5. rytilahti commented on Feb 18, 2023

    Member

    @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"
      ]
    }
    
    
  6. lazynooblet commented on Mar 7, 2023

    @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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions


      Back | FazBrowse Home | New Git URL