Fllorent0D / ha-custom-component-sodexo-be

Sodexo Be - Custom Component for Home Assistant
MIT License
4 stars 2 forks source link

Explanation how this works #2

Closed giloser closed 1 year ago

giloser commented 1 year ago

HI,

Could you explain me how you made this works? with the token?

I tried this on 2 site with similar token (monizze.be and mabibli.be) but I failed to made it works.

thanks a lot

Fllorent0D commented 1 year ago

The api.py imports the necessary dependencies, including aiohttp for asynchronous HTTP requests, logging for logging messages, json for working with JSON data, and BeautifulSoup for HTML parsing.

It defines constants like LOGIN_URL, LUNCH_PASS_SELECTOR, ECO_PASS_SELECTOR, and GIFT_PASS_SELECTOR, which represent specific URLs and HTML selectors used for extracting data from the Sodexo website.

The module provides a SodexoAPI class, which serves as the interface to the Sodexo website. It has an initializer that takes a websession object as a parameter, representing the HTTP session used for making requests.

The SodexoAPI class includes a login method, responsible for logging in to the Sodexo website. It sends a GET request to the login URL and retrieves the login page HTML. Then, it extracts the CSRF token from the HTML using BeautifulSoup and constructs a payload with the username, password, and CSRF token. Finally, it sends a POST request with the payload to perform the login. If the login is successful (status code 200), it returns the string 'ok'.

The SodexoAPI class also provides a getAccountDetails method for retrieving the account details from the Sodexo website. It sends a GET request to the login URL and retrieves the account details page HTML. Using BeautifulSoup, it parses the HTML and extracts the balance amounts for lunch pass, eco pass, and gift pass by selecting the corresponding elements using the provided selectors. The extracted values are converted to floats, and an AccountDetails object is created with the balance amounts and the current date and time.