ha-multiscrape icon indicating copy to clipboard operation
ha-multiscrape copied to clipboard

How to store cookies in a HA sensor

Open Joao-Sousa-71 opened this issue 1 year ago • 4 comments

Description:

This is not a bug or issue but within the documentation I was not able to find a way to store the cookies received after the authentication and store them in HA sensor to be used in some rest commands. I'm using this component to managem my pellets stove and after the authentication two cookies are received: "XSRF-TOKEN" and "myceza_session".

This is the content of the page_response_cookies.txt file

<Cookies[<Cookie XSRF-TOKEN=eyJpdiI6IlNjYXVwZFJOR1Zlam13UDlZYW5wSnc9PSIsInZhbHVlIjoiSFdzN0V6QWdUbkxaU2txaXh5WWEreWo4c1YzczFTa0xneU9ZdFdzbjREUWYrK0tLNDl4bVp3clhsVkg5UE1YayIsIm1hYyI6ImEwYWY5ODJkMTgwNzE5ODI1NTIyYzFlNjZmOTBlMDM1MGFkYzAxMGUyYzJkODEzYzA1YjBjOWU0YmU0NTgzNGQifQ%3D%3D for myceza.it />, <Cookie myceza_session=eyJpdiI6Ild5bVlDS21qaFhkUVwvK3AxQ2h1RmtBPT0iLCJ2YWx1ZSI6IjZNSXlWckJrcFBXSVlueWZOZEx1ZTZpdVpieW8xd2RIVEVJR1djVVhvcDk5NEJ4TVRqenQrYjgzVUVMTmNkTE8iLCJtYWMiOiI5ZjA4MmM4MjdjZThlMGFkMDI1MGQ0MzViZDAxNmE0Njc1MjhiODc1OTUwNDYxY2FkM2RkYzNhYmQ4Mzg3NWVjIn0%3D for myceza.it />]>

What I need is to have this two cookies available in two HA sensora to be possible to manage the stove by using rest commands. With the latest version can this be done? And if not, I can create a feature request.

Version of the custom_component

7.1.2 (latest)

Multiscrape configuration

multiscrape:
  - name: solzaima ha integration  
    resource: 'https://myceza.it/en'
    scan_interval: 86400
    log_response: True 
    form_submit:
      submit_once: False
      resubmit_on_error: True
      resource: 'https://myceza.it/en/login'
      select: '#main > div > div.panel-body > form'
      input:
        username: [email protected]
        password: 'mypassword'
    sensor:
      - unique_id: mycezadatatoken
        select: 'div#app-meta'
        name: Myceza Data Token
        attribute: 'data-token'
        value_template: '{{ value }}'
        on_error:
          log: error
      - unique_id: mycezacsrftoken
        select: 'head > meta:nth-child(3)'
        name: Myceza CSRF Token
        attribute: 'content'
        value_template: '{{ value }}'        
        on_error:
          log: error

Thank you.

Joao-Sousa-71 avatar Aug 27 '24 21:08 Joao-Sousa-71

No that's not possible I'm afraid. But why don't you do the rest requests in multiscrape?

danieldotnl avatar Aug 31 '24 10:08 danieldotnl

@danieldotnl thank you for your answer. The rest requests that are called by scripst and/or automations are used to turn on/off change temperature, fan speed etc. Here is an example:

rest_command:
  myceza_stove_set_temperature:
    url: https://myceza.it/api/stove/command/en/xx:xx:xx:xx:xx:xx
    method: POST
    headers:
      Host: myceza.it
      Connection: keep-alive
      X-XSRF-TOKEN: >
        {{ states('sensor.mycezaxsrftoken') | replace("%3D","=") }}
      Authorization: >
        Bearer {{ states('sensor.mycezadatatoken_actual_ks') }}
      Content-Type: application/json;charset=UTF-8
      Accept: application/json, */*
      Origin: https://myceza.it
      Sec-Fetch-Site: same-origin
      Sec-Fetch-Mode: cors
      Sec-Fetch-Dest: empty
      Referer: https://myceza.it/en/stove/xx:xx:xx:xx:xx:xx
      Accept-Encoding: gzip, deflate, br
      Accept-Language: en-US,en;q=0.9
      Cookie: >
        XSRF-TOKEN={{ states('sensor.mycezaxsrftoken')}}; myceza_session={{ states('sensor.mycezasession') }}
    payload: >
      {"value":"{{states.input_number.stove_temp_sel.state | int}}","code":"E001"}

The cookies are the XSFR-TOKEN and myceza_session which right now I've workaround by opening the great debug files with the responses and extract them but pursuiting soething more clean and straight. I'm a bit surprised that the component allow to put any data from the web page in HA sensor but not a cookie that is also handled in the code or maybe I'm missing something.

Joao-Sousa-71 avatar Sep 01 '24 12:09 Joao-Sousa-71

I released v8.0.2 yesterday. While that doesn't make it possible to add cookie data to a sensor, I do believe that it can replace your rest_command. Or am I missing something?

danieldotnl avatar Oct 04 '24 08:10 danieldotnl

Thanks for the reply. I will install it in the next days and try it. Keep you posted.

Joao-Sousa-71 avatar Oct 04 '24 22:10 Joao-Sousa-71