esphome / feature-requests

ESPHome Feature Request Tracker
https://esphome.io/
418 stars 26 forks source link

Enable online_image and/or http_request for platform: host #2918

Open kkilchrist opened 2 weeks ago

kkilchrist commented 2 weeks ago

Describe the problem you have/What new integration you would like
Add support for the http_request component on the host platform in ESPHome, or override / bypass the http_request check in the online_image component.

Please describe your use case for this integration and alternatives you've tried
I’m working on an on-device libwebp decoder for online_image. WebP images are much smaller than PNG (e.g., 2 KB vs 33 KB for a test image), which makes them ideal for low-bandwidth scenarios and fast transmission in IoT environments. Additionally, WebP supports both lossless and lossy compression.

Enabling the host platform (macOS) would make debugging much faster. The host platform already has networking support (e.g., for API access), so hopefully this is not a huge ask.

Additional context

kkilchrist commented 1 week ago

Made some pretty exciting progress on this today, after some trial and error (trying to use platformio packages, etc etc), turns out, a wrapper around the local lib curl works great on MacOS.

I also had to make some updates to the json component (which uses platform flags to allocate a read buffer).

Anyway, this allows HTTP requests to be on the host platform. I've only tested GET, but it works on my machine for both http and https endpoints.

For those interested, I’ve added the component to my GitHub fork.

Here's a sample YAML:

esphome:
  name: local-http-request
  platformio_options:
    build_flags: 
      - -lcurl

host: 

external_components:
  - source:
      type: git
      url: "https://github.com/kkilchrist/esphome"
      ref: "http_request_curl"
    components: [ "http_request", "json" ]

http_request:
  id: http_request_component
  timeout: 1s  

interval:
  - interval: 10s  
    then:
      - logger.log: "Making HTTP request"
      - http_request.get:
          url: "https://jsonplaceholder.typicode.com/todos/2"
          capture_response: true  # Capture the response for use in the Lambda function; when set to true, the response data will be captured and placed into the body variable as a std::string for use in lambdas
          on_response:
              - logger.log:
                  format: 'Response status: %d, Duration: %u ms'
                  args:
                    - response->status_code
                    - response->duration_ms
              - lambda: |-
                  ESP_LOGI("main", "Response body: %s", body.c_str());

logger:
  level: INFO  # Enable debug level logging to see detailed logs

This wasn't a small task as I had originally hoped, but it's progress!

By the way, since I have it handy, the syntax to manually set a few other things is here:

build_flags: 
      - -L/opt/homebrew/opt/curl/lib  # Path to libcurl library; these work on my macOS machine
      - -DCA_CERT_PATH=\"/usr/local/etc/openssl@1.1/cert.pem\"

The cert is a master CA cert; if you need to download one, see here

Quick edit: this is significantly a work in progress -- I've begun to try using it with online_image and something is working differently than I thought.