Trying to use restful interface on old lighting gateway

I’ve still got what must be one of the first ‘smart home’ lighting systems controlling around 20 lamps around my house. It’s a TCP brand product that they abandoned maybe a decade ago. It has a restful API which is quite well documented and I can turn on/off lights etc with a handful of shell commands i.e.

  set_brightness: >-
    curl -k --ciphers 'DEFAULT:!DH' 'https://192.168.1.2/gwr/gop.php?cmd=RoomSendCommand&data=<gip><version>1</version><token>5mcopjq4x2j5rv429pbbkp7tbvq0ubtpov4j5zs8</token><rid>{{ rid }}</rid><value>{{ brightness }}</value><type>level</type></gip>&fmt=xml'

as it has only got TLS1.2 I had to figure out how to get around using https (it doesn’t respond to plain http) but that’s not my issue.

What I’m struggling with is getting the current status so I can track changes to lights coming from other sources. I have a working ‘get_carousel’ shell command that returns a long xml list for this but I’m finding it difficult making a regular request and parsing it out to sensor states.

As a first attempt, I tried to get the whole response back like this:

  get_carousel: >-
    curl -k --ciphers 'DEFAULT:!DH' 'https://192.168.1.2/gwr/gop.php?cmd=GWRBatch&data=<gwrcmds><gwrcmd><gcmd>RoomGetCarousel</gcmd><gdata><gip><version>1</version><token>5mcopjq4x2j5rv429pbbkp7tbvq0ubtpov4j5zs8</token><fields>status</fields></gip></gdata></gwrcmd></gwrcmds>&fmt=xml'

sensor:
  - platform: command_line
    name: Carousel Data
    unique_id: carousel_data_sensor
    command: "curl -k --ciphers 'DEFAULT:!DH' 'https://192.168.1.2/gwr/gop.php?cmd=GWRBatch&data=<gwrcmds><gwrcmd><gcmd>RoomGetCarousel</gcmd><gdata><gip><version>1</version><token>5mcopjq4x2j5rv429pbbkp7tbvq0ubtpov4j5zs8</token><fields>status</fields></gip></gdata></gwrcmd></gwrcmds>&fmt=xml'"
    value_template: "{{ value }}"
    scan_interval: 300

No sensor of that name is to be found though. Obviously I’ve not understood things right. Studying various examples I found other approaches apparently including the use of rest like so:

rest:
  - resource: "http://date.jsontest.com/"
    scan_interval: 120
    sensor:
      - unique_id: rest_test1
        name: test1
        value_template: >
          {{ value_json.date }}

But that doesn’t create a sensor either. Confusingly, when I look for HA documentation it seems I should be using the RESTful Command but I can’t figure out how to get my return data into a sensor(s) from which to update the current state of things. (I realise this is several steps on - for now I just want to see the XML payload in its entirety)

I may well be going about this in the wrong way. AppDaemon may be a better approach but I can’t seem to get round the TLS1.2 issue with that.

There are REST integrations;

Here are two examples for my REST:

rest:
  - resource: http://192.168.1.154/getxml?location=/status/video/input/source
    scan_interval: 10
    method: GET
    headers:
      Content-Type: application/xml
    sensor:
      - name: "RefreshRate"
        json_attributes_path: "$.Video.Input.Source[1].Resolution"
        value_template: "{{ value_json['Status']['Video']['Input']['Source'][1]['Resolution']['RefreshRate']['#text'] }}"
# Send Rest command
rest_command:
  system_off:
    url: "http://192.168.1.154/putxml"
    method: "POST"
    headers:
      Content-Type: "text/xml"
    payload: |
      <Command>
        <Standby>
          <Activate command = "True">
          </Activate>
        </Standby>
      </Command>

but without knowing how your xml actually looks like, it is hard to give you any advice.

One thing that helped me for sure was to turn on REST logging :wink:

logger:
  default: warning
  logs:
    homeassistant.components.rest: debug

Keep in mind states are limited to 255 chars, and I assume your XML is larger than that…

Cheers for that. However…

Your assumption is correct. Thanks for the warning. Looks like I’m forced into using python but my first attempt in AppDaemon failed at the TLS1.2 hurdle.

Command line is now a top-level integration.

Command line looks useful although I seem to be going round in circles! TBH I haven’t yet figured out what I’m going to do with the carousel data. Unfortunately the restful api provided by the gateway requires it to be actively polled to keep track of lamp states.

Really I need a way to make the light: configurations dynamic - meaning they get discovered and added to HA as they are re-arranged, renamed and have their states change etc. Is this even feasible?