Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to store cookies in a HA sensor #407

Open
Joao-Sousa-71 opened this issue Aug 27, 2024 · 4 comments
Open

How to store cookies in a HA sensor #407

Joao-Sousa-71 opened this issue Aug 27, 2024 · 4 comments

Comments

@Joao-Sousa-71
Copy link

Joao-Sousa-71 commented Aug 27, 2024

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.

@danieldotnl
Copy link
Owner

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

@Joao-Sousa-71
Copy link
Author

@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.

@danieldotnl
Copy link
Owner

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?

@Joao-Sousa-71
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants