# Installation

This library can be easily installed via the Pypi packet manager.

    pip3 install elmax-api --user

or, to install it globally:

    pip3 install elmax-api

# Quick Start

The following code snippet illustrates how to login to the Elmax Cloud API, fetch the current zone status and toggle some actuator

>     import asyncio
>     import os

>     from elmax_api.http import Elmax
>     from elmax_api.model.command import SwitchCommand

>     MY_USERNAME = os.getenv('ELMAX_USERNAME') or 'TYPE_HERE_YOUR_USERNAME'
>     MY_PASSWORD = os.getenv('ELMAX_PASSWORD') or 'TYPE_HERE_YOUR_PASSWORD'

>     async def main():
>         # Instantiate the Elmax API client
>         client = Elmax(username=MY_USERNAME, password=MY_PASSWORD)

>         # List panels for your user
>         panels = await client.list_control_panels()
>         print(f"Found {len(panels)} panels for user {client.get_authenticated_username()}")

>         # Get online panels only
>         online_panels = []
>         for p in panels:
>             status = 'ONLINE' if p.online else 'OFFLINE'
>             print(f"+ {p.hash}: {status}")
>             if p.online:
>                 online_panels.append(p)

>         if len(online_panels) == 0:
>             print("Sorry, no panel to work with. Exiting.")
>             exit(0)

>         # Fetch status of first panel
>         p = online_panels[0]
>         panel_status = await client.get_panel_status(control_panel_id=p.hash)

>         # Print some zone status
>         for z in panel_status.zones:
>             print(f"Zone '{z.name}' open: {z.opened}")

>         # Toggle some actuator
>         actuator = panel_status.actuators[0]
>         old_status = actuator.opened
>         print(f"Actuator {actuator.name} was {'ON' if old_status else 'OFF'}")
>         print(f"Switching {'OFF' if old_status else 'ON'} actuator {actuator.name}")
>         await client.execute_command(endpoint_id=actuator.endpoint_id, command=SwitchCommand.TURN_ON if not old_status else SwitchCommand.TURN_OFF)

>         print("Waiting a bit...")
>         await asyncio.sleep(5)

>         print("Reverting back original actuator status")
>         await client.execute_command(endpoint_id=actuator.endpoint_id,
>                                      command=SwitchCommand.TURN_ON if old_status else SwitchCommand.TURN_OFF)
>         print("Done!")

>     if __name__ == '__main__':
>         asyncio.run(main())

# Listen for push notifications

In order to listen for push notification events, it is just necessary to register a callback coroutine using a PushNotificationHandler helper object.

The following example shows how to to so.

>     import asyncio
>     import ssl

>     from elmax_api.http import ElmaxLocal
>     from elmax_api.model.panel import PanelStatus
>     from elmax_api.push.push import PushNotificationHandler

>     PANEL_ENDPOINT="https://192.168.7.249/api/v2"
>     PUSH_ENDPOINT="wss://192.168.7.249/api/v2/push"

>     async def main():
>         context = ssl.create_default_context()
>         context.check_hostname = False
>         context.verify_mode = ssl.VerifyMode(ssl.CERT_NONE)
>         client = ElmaxLocal(panel_api_url=PANEL_ENDPOINT, panel_code="000000", ssl_context=context)
>         handler = PushNotificationHandler(PUSH_ENDPOINT, client, context)

>         async def _panel_updated(status: PanelStatus):
>             print("An event has occurred!")
>             print(status)

>         handler.register_push_notification_handler(_panel_updated)
>         handler.start(asyncio.get_event_loop())

>         for i in range(60):
>             await asyncio.sleep(1)
>             print("Slept!")

>         handler.unregister_push_notification_handler(_panel_updated)
>         handler.stop()

>     if __name__ == '__main__':
>         asyncio.run(main())