This forum uses cookies
This forum makes use of cookies to store your login information if you are registered, and your last visit if you are not. Cookies are small text documents stored on your computer; the cookies set by this forum can only be used on this website and pose no security risk. Cookies on this forum also track the specific topics you have read and when you last read them. Please confirm that you accept these cookies being set.

Python code to LUA
#1
Hi there,

I'm looking to convert this python code to LUA.
I have no clue how to start.
Can this be done?
Any help would be greatly appreciated.
The full code is attached to this post.

These are the Classes : 

Code:
# Copyright 2021, 2022 Milan Meulemans.
#
# This file is part of aioaseko.
#
# aioaseko is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# aioaseko is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with aioaseko.  If not, see <https://www.gnu.org/licenses/>.

"""aioAseko web API account."""
from __future__ import annotations

from dataclasses import dataclass

from aiohttp import ClientError, ClientResponse, ClientSession

from .exceptions import APIUnavailable, InvalidAuthCredentials
from .unit import Unit


class WebAccount:
    """Aseko web account."""

    def __init__(self, session: ClientSession, username: str, password: str) -> None:
        """Init Aseko account."""
        self._session = session
        self._username = username
        self._password = password

    async def _request(
        self, method: str, path: str, data: dict | None = None
    ) -> ClientResponse:
        """Make a request to the Aseko web API."""
        resp = await self._session.request(
            method, f"https://pool.aseko.com/api/{path}", data=data
        )
        if resp.status == 401:
            raise InvalidAuthCredentials
        try:
            resp.raise_for_status()
        except ClientError:
            raise APIUnavailable
        return resp

    async def login(self) -> AccountInfo:
        """Login to the Aseko web API."""
        resp = await self._request(
            "post",
            "login",
            {
                "username": self._username,
                "password": self._password,
                "agree": "on",
            },
        )
        data = await resp.json()
        return AccountInfo(data["email"], data["userId"], data.get("language"))

    async def get_units(self) -> list[Unit]:
        """Get units."""
        resp = await self._request("get", "units")
        data = await resp.json()
        return [
            Unit(
                self,
                int(item["serialNumber"]),
                item["type"],
                item.get("name"),
                item.get("notes"),
                item["timezone"],
                item["isOnline"],
                item["dateLastData"],
                item["hasError"],
            )
            for item in data["items"]
        ]


@dataclass(frozen=True)
class AccountInfo:
    """Aseko account info."""

    email: str
    user_id: str
    language: str | None

And this Is the code to run the classes :

Code:
from aiohttp import ClientSession
from asyncio import run

import aioaseko

async def main():
    async with ClientSession() as session:
        account = aioaseko.MobileAccount(session, "aioAseko@example.com", "passw0rd")
        try:
            await account.login()
        except aioaseko.InvalidAuthCredentials:
            print("The username or password you entered is wrong.")
            return
        units = await account.get_units()
        for unit in units:
            print(unit.name)
            await unit.get_state()
            print(f"Water flow: {unit.water_flow}")
            for variable in unit.variables:
                print(variable.name, variable.current_value, variable.unit)
        await account.logout()
run(main())

Attached Files
.gz   aioaseko-0.0.2.tar.gz (Size: 19.53 KB / Downloads: 1)
Reply


Messages In This Thread
Python code to LUA - by bednarekluc - 16.02.2023, 17:16
RE: Python code to LUA - by Batko - 16.02.2023, 19:01
RE: Python code to LUA - by bednarekluc - 16.02.2023, 19:05
RE: Python code to LUA - by admin - 17.02.2023, 08:26
RE: Python code to LUA - by bednarekluc - 17.02.2023, 08:57
RE: Python code to LUA - by admin - 17.02.2023, 09:14
RE: Python code to LUA - by bednarekluc - 17.02.2023, 09:30
RE: Python code to LUA - by admin - 17.02.2023, 09:38
RE: Python code to LUA - by bednarekluc - 17.02.2023, 10:09

Forum Jump: