Giter Club home page Giter Club logo

aioremootio's Introduction

aioremootio - An asynchronous API client library for Remootio

aioremootio is an asynchronous API client library for Remootio written in Python 3 and based on asyncio and aiohttp.

Supported functionalities of the device

With this client library is currently possible to listen to state changes of a Remootio device, to listen to some events triggered by it, furthermore to operate the gate or garage door connected to it.

This client library supports currently the listening to following kind of events triggered by the device:

  • STATE_CHANGE which is triggered by the device when its state changes
  • RELAY_TRIGGER which is triggered by the device when its control output has been triggered to operate the connected gate or garage door
  • LEFT_OPEN which is triggered by the device when the connected gate or garage door has been left open
  • RESTART which is triggered by the device when it was restarted

Using the library

The following example demonstrates how you can use this library.

from typing import NoReturn
import logging
import asyncio
import aiohttp
import aioremootio


class ExampleStateListener(aioremootio.Listener[aioremootio.StateChange]):
    __logger: logging.Logger

    def __init__(self, logger: logging.Logger):
        self.__logger = logger

    async def execute(self, client: aioremootio.RemootioClient, subject: aioremootio.StateChange) -> NoReturn:
        self.__logger.info("State of the device has been changed. Host [%s] OldState [%s] NewState [%s]" %
                           (client.host, subject.old_state, subject.new_state))


async def main() -> NoReturn:
    logger = logging.getLogger(__name__)
    logger.setLevel(logging.INFO)

    handler: logging.Handler = logging.StreamHandler()
    handler.setFormatter(logging.Formatter(fmt="%(asctime)s [%(levelname)s] %(message)s"))
    logger.addHandler(handler)

    connection_options: aioremootio.ConnectionOptions = \
        aioremootio.ConnectionOptions("192.168.0.1", "API_SECRET_KEY", "API_AUTH_KEY")

    state_change_listener: aioremootio.Listener[aioremootio.StateChange] = ExampleStateListener(logger)

    remootio_client: aioremootio.RemootioClient
    
    async with aiohttp.ClientSession() as client_session:
        try:
            remootio_client = await aioremootio.RemootioClient(
                connection_options,
                client_session,
                aioremootio.LoggerConfiguration(logger=logger),
                [state_change_listener]
            )
        except aioremootio.RemootioClientConnectionEstablishmentError:
            logger.exception("The client has failed to establish connection to the Remootio device.")
        except aioremootio.RemootioClientAuthenticationError:
            logger.exception("The client has failed to authenticate with the Remootio device.")
        except aioremootio.RemootioError:
            logger.exception("Failed to create client because of an error.")
        else:
            logger.info("State of the device: %s", remootio_client.state)
    
            if remootio_client.state == aioremootio.State.NO_SENSOR_INSTALLED:
                await remootio_client.trigger()
            else:
                await remootio_client.trigger_open()
                await remootio_client.trigger_close()
    
        while True:
            await asyncio.sleep(0.1)

if __name__ == "__main__":
    try:
        asyncio.run(main())
    except KeyboardInterrupt:
        pass

To get the API Secret Key and API Auth Key of your Remootio device you must enable the API on it according to the Remootio Websocket API documentation.

Running the bundled examples

The project source does also contain two examples.

The example example.py demonstrates how you can use the client as a Python object.

The example example_mc.py demonstrates how you can use the client as a Python object where it does not establish a connection to the Remootio device automatically during its initialization.

The example example_acm.py demonstrates how you can use the client as an asynchronous context manager.

To run the bundled examples you must

  1. also enable the API on your Remootio device to get the API Secret Key and API Auth Key of it, and
  2. add the source folder /src of the repository to your PYTHONPATH.

After the two steps described above you can run the bundled examples with the argument --help to show the usage information. E.g.:

python example.py --help

Running the bundled tests

To run the bundled tests you must create the .\remootio_device.configuration.json file with a content according to the following template.

{
    "host": "IP-ADDRESS-OR-HOST-NAME-OF-YOUR-DEVICE",
    "api_secret_key": "API-SECRET-KEY-OF-YOUR-DEVICE",
    "api_auth_key": "API-AUTH-KEY-OF-YOUR-DEVICE",
    "api_version": API-VERSION-OF-YOUR-DEVICE
}

Copyright © 2021 Gergö Gabor Ilyes-Veisz. Licensed under the Apache License, Version 2.0

Buy Me A Coffee

aioremootio's People

Contributors

ivgg-me avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

aioremootio's Issues

home assistant 2024.8 dependencies

Upgrade to 2024.8 breaks remootio integrqation.

Logger: homeassistant.util.package
Source: util/package.py:123
First occurred: 8:52:20 AM (3 occurrences)
Last logged: 8:52:47 AM

Unable to install package aioremootio @ git+https://github.com/peaceduck/aioremootio: ERROR: Cannot install aioremootio==1.0.0 because these package versions have conflicting dependencies. ERROR: ResolutionImpossible: for help visit https://pip.pypa.io/en/latest/topics/dependency-resolution/#dealing-with-dependency-conflicts

Bump required libraries up

Hi,

Would it be possible to bump up and test the following libraries please as the older ones specified are breaking integrations with home assistant:

aiohttp==3.8.5
pycryptodome==3.17

Thanks,
Sean

ModuleNotFoundError: No module named 'asyncio.exceptions'

Running on ubuntu18.0 with python3.6.9 virtual environment.

I have installed all the packages using your requirements.txt, and I have added the /src into my PYTHONPATH.

My python3 -m pip list within the virtual environment shows:

Package Version


aiohttp 3.8.1
aiosignal 1.2.0
async-class 0.5.0
async-timeout 4.0.2
asynctest 0.13.0
attrs 21.4.0
charset-normalizer 2.0.12
frozenlist 1.2.0
idna 3.3
idna-ssl 1.1.0
multidict 5.2.0
pip 21.3.1
pycryptodome 3.14.1
setuptools 59.6.0
typing_extensions 4.1.1
voluptuous 0.12.2
wheel 0.37.1
yarl 1.7.2

It seems that the version of asyncio is not correct on my system. Which version are you guys using and how can I resolve this issue? Thank you.

authentication timeout causes loop that cannot recover

Home assistant seems to get stuck in a loop when it fails to connect to the device. This loop goes on indefinitely and only can be recovered by restarting home-assistant.

Last night Home Assistant was restarted at 12, first authentication timeout happened at 5:26 AM. This works fine in the homebridge installation so perhaps a bug that is unable to recover when the first timeout happens.

2022-05-18 08:40:43 ERROR (MainThread) [custom_components.remootio.aioremootio.remootioclient] An error has been occurred on the device. Type [ErrorType.AUTHENTICATION_TIMEOUT]
2022-05-18 08:41:19 ERROR (MainThread) [custom_components.remootio.aioremootio.remootioclient] An error has been occurred on the device. Type [ErrorType.AUTHENTICATION_TIMEOUT]
2022-05-18 08:41:58 ERROR (MainThread) [custom_components.remootio.aioremootio.remootioclient] An error has been occurred on the device. Type [ErrorType.AUTHENTICATION_TIMEOUT]
2022-05-18 08:42:35 ERROR (MainThread) [custom_components.remootio.aioremootio.remootioclient] An error has been occurred on the device. Type [ErrorType.AUTHENTICATION_TIMEOUT]
2022-05-18 08:43:35 ERROR (MainThread) [custom_components.remootio.aioremootio.remootioclient] An error has been occurred on the device. Type [ErrorType.AUTHENTICATION_TIMEOUT]
2022-05-18 08:44:12 ERROR (MainThread) [custom_components.remootio.aioremootio.remootioclient] An error has been occurred on the device. Type [ErrorType.AUTHENTICATION_TIMEOUT]
2022-05-18 08:44:49 ERROR (MainThread) [custom_components.remootio.aioremootio.remootioclient] An error has been occurred on the device. Type [ErrorType.AUTHENTICATION_TIMEOUT]

Randomly throughout the night this problem popped up

Traceback (most recent call last):
File "/usr/local/lib/python3.9/site-packages/aioremootio/remootioclient.py", line 328, in __open_connection
self.__ws = await self.__client_session.ws_connect(
File "/usr/local/lib/python3.9/site-packages/aiohttp/client.py", line 776, in _ws_connect
resp = await self.request(
File "/usr/local/lib/python3.9/site-packages/aiohttp/client.py", line 559, in _request
await resp.start(conn)
File "/usr/local/lib/python3.9/site-packages/aiohttp/client_reqrep.py", line 898, in start
message, payload = await protocol.read() # type: ignore[union-attr]
File "/usr/local/lib/python3.9/site-packages/aiohttp/streams.py", line 616, in read
await self._waiter
aiohttp.client_exceptions.ClientOSError: [Errno 104] Connection reset by peer

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.