danieldotnl / ha-multiscrape

Home Assistant custom component for scraping (html, xml or json) multiple values (from a single HTTP request) with a separate sensor/attribute for each value. Support for (login) form-submit functionality.
MIT License
247 stars 14 forks source link

Form variables #374

Open jeremicmilan opened 1 month ago

jeremicmilan commented 1 month ago

Form variables are a feature to configure the variables you want to be forwarded from scraping the form-submit page to scraping the main page for sensor data. A common use case is to populate the X-Login-Token header which is the result of the login.

Example:

multiscrape:
  - resource: "https://website-api.airvisual.com/v1/users/65a28a0cec1ff309a74ba414/devices/avo_65a2bd77c2f7aeabcd715393?units.system=metric&AQI=US&language=en"
    form_submit:
      submit_once: True
      resource: "https://website-api.airvisual.com/v1/auth/signin/by/email"
      input:
        email: "<email>"
        password: "<password>"
      variables:
        - name: token
          value_template: "{{ (value | from_json).loginToken }}"
    headers:
      X-Login-Token: "{{ token }}"
    sensor:
      - name: AirVisual Outdoor AQI
        value_template: "{{ (value | from_json).current.aqi.value }}"
        unit_of_measurement: "AQI US"

Log into https://dashboard.iqair.com/personal/devices, select the device to get the <device_id> in the URL. After that analyze network traffic and find the name starting with <device_id>. That will contain the entire path in the example including <user_id> (there's probably an easier way to get <user_id>, but this works),

jeremicmilan commented 1 month ago

This is continuation of #327. Please refer to it for history.

@danieldotnl, please review.

jeremicmilan commented 1 month ago

@danieldotnl, debugging the extension does not work for me. It worked when I initially worked on the first PR (header mappings). Does it work for you?

My setup is WSL, I enable debugpy and attach locally. The debugger attaches, but it refuses to hit breakpoints (where before it did). Very weird. Do you know what's going on? BTW, I implemented the form variables without the debugger, it was a little bit annoying. :D

jeremicmilan commented 3 weeks ago

@danieldotnl, it took a while to get some time for this, but I finally addressed all the feedback. Please review again.

jeremicmilan commented 2 weeks ago

@danieldotnl, fun trivia, the token that I started using months ago did not yet expire. It usually should expire in 90 days. So, I did not need to update manually the token on my production home assistant installation. 😄

danieldotnl commented 2 weeks ago

@danieldotnl, fun trivia, the token that I started using months ago did not yet expire. It usually should expire in 90 days. So, I did not need to update manually the token on my production home assistant installation. 😄

Good for you as this took way longer than 90 days 😅

danieldotnl commented 2 weeks ago

Let's try to finish this soon now, as people are waiting for the cookies PR to be merged, but I don't want to bother you with merging that one as well in this PR. So I would like to merge you PR first.

jeremicmilan commented 1 week ago

@danieldotnl , I'll have a lot more time in July. Probably won't make it before that.