delfick / photons-homeassistant

Home Assistant Addon repository
3 stars 2 forks source link

500 Internal Server Error When Calling with RESTful Command #10

Open RohanHinton opened 1 month ago

RohanHinton commented 1 month ago

Hi all,

Just installed this addon and added in the default RESTful command from the doc's page and received the below error.

Steps:

  1. Installed addon & started it up
  2. Added below to configuration.yaml
    rest_command:
    lights_red:
    url: 'http://X.X.X.X:6100/v1/lifx/command'
    method: "put"
    payload: |
      {
        "command": "transform",
        "args": {
          "transform": {
            "color": "red saturation:0.5 brightness:0.5"
          }
        }
      }
  3. Confirmed that visiting http://X.X.X.X:6100/v1/lifx/command loads to ensure correct address
  4. Called service "RESTful Command: lights_red"
  5. Addon sends 500 error with below traceback in logs:
    2024-07-20 12:55:47,996 ERROR   interactor.commander.commands.legacy:LegacyCommands:legacy_put TypeError: LegacyBody.__init__() missing 1 required positional argument: 'command'   request_identifier=01J37128RC7PE7CDA2F6YPW9YG
    Traceback (most recent call last):
    File "/usr/local/lib/python3.12/site-packages/photons_web_server/commander/store.py", line 306, in route
    route_args = self.store.determine_http_args_and_kwargs(
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File "/usr/local/lib/python3.12/site-packages/photons_web_server/commander/store.py", line 661, in determine_http_args_and_kwargs
    use.extend(self._determine_keyword_args_and_kwargs(values, meta, request, kwargs))
    File "/usr/local/lib/python3.12/site-packages/photons_web_server/commander/store.py", line 702, in _determine_keyword_args_and_kwargs
    yield self.strcs_register.create(nxt.annotation, body_raw, meta=meta)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File "/usr/local/lib/python3.12/site-packages/strcs/register.py", line 211, in create
    return CreateStructureHook.structure(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File "/usr/local/lib/python3.12/site-packages/strcs/hooks.py", line 99, in structure
    ret = hooks.convert(value, typ)
          ^^^^^^^^^^^^^^^^^^^^^^^^^
    File "/usr/local/lib/python3.12/site-packages/strcs/hooks.py", line 184, in convert
    return instantiate(want, value, self.converter)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File "/usr/local/lib/python3.12/site-packages/strcs/disassemble/_creation.py", line 76, in instantiate
    return instantiator(**conv_obj)
           ^^^^^^^^^^^^^^^^^^^^^^^^
    TypeError: LegacyBody.__init__() missing 1 required positional argument: 'command'
    2024-07-20 12:55:48,012 ERROR   photons_web_server.server Response  method=PUT  remote_addr=    request_identifier=01J37128RC7PE7CDA2F6YPW9YG   status=500  took_seconds=0.02   uri=/v1/lifx/command

Not sure how to resolve and where I've miss stepped on this one. Seems to suggest a missing argument in the rest call?

delfick commented 1 month ago

Hi,

I imagine what's missing is making sure the rest command sends a content_type of application/json

On Sat, 20 July 2024, 13:04 RoRo, @.***> wrote:

Hi all,

Just installed this addon and added in the default RESTful command from the doc's page and received the below error.

Steps:

  1. Installed addon & started it up
  2. Added below to configuration.yaml

rest_command: lights_red: url: 'http://X.X.X.X:6100/v1/lifx/command' method: "put" payload: | { "command": "transform", "args": { "transform": { "color": "red saturation:0.5 brightness:0.5" } } }

  1. Confirmed that visiting http://X.X.X.X:6100/v1/lifx/command loads to ensure correct address
  2. Called service "RESTful Command: lights_red"
  3. Addon sends 500 error with below traceback in logs:

2024-07-20 12:55:47,996 ERROR interactor.commander.commands.legacy:LegacyCommands:legacy_put TypeError: LegacyBody.init() missing 1 required positional argument: 'command' request_identifier=01J37128RC7PE7CDA2F6YPW9YG Traceback (most recent call last): File "/usr/local/lib/python3.12/site-packages/photons_web_server/commander/store.py", line 306, in route route_args = self.store.determine_http_args_and_kwargs( ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/local/lib/python3.12/site-packages/photons_web_server/commander/store.py", line 661, in determine_http_args_and_kwargs use.extend(self._determine_keyword_args_and_kwargs(values, meta, request, kwargs)) File "/usr/local/lib/python3.12/site-packages/photons_web_server/commander/store.py", line 702, in _determine_keyword_args_and_kwargs yield self.strcs_register.create(nxt.annotation, body_raw, meta=meta) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/local/lib/python3.12/site-packages/strcs/register.py", line 211, in create return CreateStructureHook.structure( ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/local/lib/python3.12/site-packages/strcs/hooks.py", line 99, in structure ret = hooks.convert(value, typ) ^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/local/lib/python3.12/site-packages/strcs/hooks.py", line 184, in convert return instantiate(want, value, self.converter) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/local/lib/python3.12/site-packages/strcs/disassemble/_creation.py", line 76, in instantiate return instantiator(**conv_obj) ^^^^^^^^^^^^^^^^^^^^^^^^ TypeError: LegacyBody.init() missing 1 required positional argument: 'command' 2024-07-20 12:55:48,012 ERROR photons_web_server.server Response method=PUT remote_addr= request_identifier=01J37128RC7PE7CDA2F6YPW9YG status=500 took_seconds=0.02 uri=/v1/lifx/command

Not sure how to resolve and where I've miss stepped on this one. Seems to suggest a missing argument in the rest call?

— Reply to this email directly, view it on GitHub https://github.com/delfick/photons-homeassistant/issues/10, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAA2V5JVLPZDWDKDIQIFEO3ZNHHVTAVCNFSM6AAAAABLFRLQ4OVHI2DSMVQWIX3LMV43ASLTON2WKOZSGQZDANJSGE4TKOI . You are receiving this because you are subscribed to this thread.Message ID: @.***>

RohanHinton commented 1 month ago

Hey @delfick, that's perfect. Thanks! Added content_type: "application/json" and it fixed it right up.

RohanHinton commented 1 month ago

I have now listed out the devices in the lifx.yml with each mac & IP address:

---

discovery_options:
  hardcoded_discovery:
    d073d5XXXXXX: "X.X.X.X"
    d073d5XXXXXX: "X.X.X.X"

One of my devices works great (a strip led on 2.9 firmware), but all others either timeout or give an empty 200 response, instead of the "ok" from the serial. eg:

2024-07-21 01:25:12,790 ERROR photons_control.device_finder Failed to determine if device matched filter error={"errors": [{"message": "Timed out. Waiting for reply to a packet", "sent_pkt_type": 101, "sequence": 251, "serial": "d073d5XXXXXX", "source": 1725501813}, {"message": "Timed out. Waiting for reply to a packet", "sent_pkt_type": 32, "sequence": 250, "serial": "d073d5XXXXXX", "source": 1725501813}], "message": "Bad Run"} exc_typ=RunErrors

I have triple checked the IP and Mac addresses for each, but still only one works. Most other lights are on version 3.9. Any chance this is causing the issue or could it be something else? The lights work through the Lifx integration in HA and Hubkit, so I know they're accessible, but can't work out why Photons works with 1 strip but not the others.

delfick commented 1 month ago

Yay that the content type worked!

As for the time outs. Sounds like you set the wrong ip addresses.

How did you figure out what ip addresses to use?

On Sun, 21 July 2024, 01:32 RoRo, @.***> wrote:

I have now listed out the devices in the lifx.yml with each mac & IP address:


discovery_options: hardcoded_discovery: d073d5XXXXXX: "X.X.X.X" d073d5XXXXXX: "X.X.X.X"

One of my devices works great (a strip led on 2.9 firmware), but all others either timeout or give an empty 200 response, instead of the "ok" from the serial. eg:

2024-07-21 01:25:12,790 ERROR photons_control.device_finder Failed to determine if device matched filter error={"errors": [{"message": "Timed out. Waiting for reply to a packet", "sent_pkt_type": 101, "sequence": 251, "serial": "d073d5XXXXXX", "source": 1725501813}, {"message": "Timed out. Waiting for reply to a packet", "sent_pkt_type": 32, "sequence": 250, "serial": "d073d5XXXXXX", "source": 1725501813}], "message": "Bad Run"} exc_typ=RunErrors

I have triple checked the IP and Mac addresses for each, but still only one works. Most other lights are on version 3.9. Any chance this is causing the issue or could it be something else? The lights work through the Lifx integration in HA and Hubkit, so I know they're accessible, but can't work out why Photons works with 1 strip but not the others.

— Reply to this email directly, view it on GitHub https://github.com/delfick/photons-homeassistant/issues/10#issuecomment-2241185495, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAA2V5IOKJCUFISQE46JTI3ZNJ7I3AVCNFSM6AAAAABLFRLQ4OVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDENBRGE4DKNBZGU . You are receiving this because you were mentioned.Message ID: @.***>

RohanHinton commented 1 month ago

Hmm, I have checked those quite a few times. I used both Unifi portal & pfSense ARP tables to get/cross check the IP & mac addresses.

Eg: Unifi for one that isn't working:

IP Address: 192.168.4.39
MAC Address: d0:73:d5:6e:36:25

pfSense then says:

192.168.4.39 | d0:73:d5:6e:36:25 (Lifi Labs Management)

And in the config it's written as:

d073d56e3625: "192.168.4.39"

When I go into the device in HA it shows two mac addresses for some. For this one it shows:

MAC: D0:73:D5:6E:36:24
MAC: D0:73:D5:6E:36:25 

I tried using both of these in the hardcoded discovery. Sometimes the calls don't throw an error, but doesn't return an ok, and the light doesn't turn on:

content:
  results: {}
status: 200

Other times I get the below traceback:

2024-07-21 08:35:20,002 ERROR   photons_web_server.server Response  method=PUT  remote_addr=    request_identifier=01J394HQNTA2FFMNXENMAMBQZR   status=503  took_seconds=10.62  uri=/v1/lifx/command
Traceback (most recent call last):
  File "/usr/local/lib/python3.12/site-packages/photons_web_server/commander/store.py", line 312, in route
    await hp.wait_for_first_future(
  File "/usr/local/lib/python3.12/site-packages/photons_app/helpers.py", line 1277, in wait_for_first_future
    await waiter
asyncio.exceptions.CancelledError

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "handle_request", line 102, in handle_request
    if TYPE_CHECKING:

  File "/usr/local/lib/python3.12/site-packages/photons_web_server/commander/store.py", line 332, in route
    raise sanic.exceptions.ServiceUnavailable("Cancelled")
sanic.exceptions.ServiceUnavailable: Cancelled
RohanHinton commented 1 month ago

Just adding on to the above, whine the network tools all say 36:25 is the mac address for the strip, the SN on the back of the device says 36:24 is the serial. Not sure if this is causing the confusion?

delfick commented 1 month ago

Do you get different values if you go onto a computer on your network and do the following?

python3 -m venv photons_venv
./photons_venv/bin/python -m pip install lifx-photons-core
./photons_venv/bin/lifx lan:find_ips -- '{"settings_output": true}'
RohanHinton commented 1 month ago

Thanks so much for your continued help @delfick, I'm really appreciating it.

I had to run it without args as I got this when I ran it with your line:

Something went wrong! -- BadOption
        "Bad Option. The options after -- wasn't valid json"    error=Expecting value: line 1 column 1 (char 0)

But when I just ran it with lan:find_ips I got:

...
10:01:52 INFO    delfick_project.addons Found lifx.photons.canvas addon
10:01:52 INFO    delfick_project.addons Found lifx.photons.control addon
10:01:52 INFO    delfick_project.addons Found lifx.photons.core addon
10:01:52 INFO    delfick_project.addons Found lifx.photons.pytest addon
10:01:52 INFO    delfick_project.addons Found lifx.photons.transport addon
10:01:52 INFO    delfick_project.addons Found lifx.photons.web_server addon
10:01:52 INFO    delfick_project.option_merge.collector Converting photons_app
10:01:52 INFO    delfick_project.option_merge.collector Converting target_register
10:01:52 INFO    delfick_project.option_merge.collector Converting reference_resolver_register
10:01:52 INFO    delfick_project.option_merge.collector Converting targets
10:01:52 INFO    delfick_project.option_merge.collector Converting discovery_options
10:01:52 INFO    delfick_project.option_merge.collector Converting protocol_register
10:01:52 INFO    photons_transport.transports.udp Creating datagram endpoint    address=('255.255.255.255', 56700)     serial=None
...
d073d544439c: 192.168.4.24
...
...
...
d073d56e3624: 192.168.4.39

The first one listed is the led strip that has been working well. I tried using a transform via the cli but got the same invalid json error when using the lan:transform -- etc. example on the installation documentation page. I updated the lifx.yml file with the values from this list, and the result for all but the 439c strip come back as:

content:
  results: {}
status: 200

I also did a full reset on one strip to see if there was an issue with it's setup (5 unplugs and plugins, then re added to network), but same result.

delfick commented 1 month ago

If you're doing it from a windows shell then the single quotes won't be enough and you need to escape the quotes a bit differently https://photons.delfick.com/commandline/#running-cli-commands-on-windows

On Sun, 21 July 2024, 10:30 RoRo, @.***> wrote:

I had to run it without args as I got this when I ran it with your line:

Something went wrong! -- BadOption "Bad Option. The options after -- wasn't valid json" error=Expecting value: line 1 column 1 (char 0)

But when I just ran it with lan:find_ips I got:

... 10:01:52 INFO delfick_project.addons Found lifx.photons.canvas addon 10:01:52 INFO delfick_project.addons Found lifx.photons.control addon 10:01:52 INFO delfick_project.addons Found lifx.photons.core addon 10:01:52 INFO delfick_project.addons Found lifx.photons.pytest addon 10:01:52 INFO delfick_project.addons Found lifx.photons.transport addon 10:01:52 INFO delfick_project.addons Found lifx.photons.web_server addon 10:01:52 INFO delfick_project.option_merge.collector Converting photons_app 10:01:52 INFO delfick_project.option_merge.collector Converting target_register 10:01:52 INFO delfick_project.option_merge.collector Converting reference_resolver_register 10:01:52 INFO delfick_project.option_merge.collector Converting targets 10:01:52 INFO delfick_project.option_merge.collector Converting discovery_options 10:01:52 INFO delfick_project.option_merge.collector Converting protocol_register 10:01:52 INFO photons_transport.transports.udp Creating datagram endpoint address=('255.255.255.255', 56700) serial=None ... d073d544439c: 192.168.4.24 ... ... ... d073d56e3624: 192.168.4.39

The first one listed is the led strip that has been working well. I tried using a transform via the cli but got the same invalid json error when using the lan:transform -- etc. example on the installation documentation page. I updated the lifx.yml file with the values from this list, and the result for all but the 439c strip come back as:

content: results: {} status: 200

I also did a full reset on one strip to see if there was an issue with it's setup (5 unplugs and plugins, then re added to network), but same result.

— Reply to this email directly, view it on GitHub https://github.com/delfick/photons-homeassistant/issues/10#issuecomment-2241351208, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAA2V5P5VS32FRR6CI36KBLZNL6KDAVCNFSM6AAAAABLFRLQ4OVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDENBRGM2TCMRQHA . You are receiving this because you were mentioned.Message ID: @.***>

RohanHinton commented 1 month ago

Using the proper escapes like lifx lan:transform -- "{\"power\": \"on\"}" via the CLI on the laptop does correctly turn on all strips. This method works great so long as the laptop is on the same vLan as the strips. Woo! Getting somewhere.

RohanHinton commented 1 month ago

And by creating the lifx.yml file with the hardcoded values, it works fine across vLans too. So strange the HA addon struggles but this standalone works so great. Love how fast the response time of the scripts are too! Huge improvement over the cloud based response time.

delfick commented 1 month ago

And by creating the lifx.yml file with the hardcoded values, it works fine across vLans too.

nice

So strange the HA addon struggles

Sounds like something about what you run your Home assistant on has some kind of network restriction blocking it from sending packets to where your lights are?

but this standalone works so great. Love how fast the response time of the scripts are too!

Wooh!