Skip to content

Commit

Permalink
docs: add a minimal hello world example
Browse files Browse the repository at this point in the history
  • Loading branch information
zehnm committed Oct 30, 2023
1 parent 6b764e9 commit 655bc4d
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 0 deletions.
18 changes: 18 additions & 0 deletions examples/hello_integration.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"driver_id": "hello_integration",
"version": "0.0.1",
"min_core_api": "0.20.0",
"name": { "en": "Hello Python integration" },
"icon": "uc:integration",
"description": {
"en": "Minimal Python integration driver example."
},
"port": 9080,
"developer": {
"name": "Unfolded Circle ApS",
"email": "[email protected]",
"url": "https://www.unfoldedcircle.com"
},
"home_page": "https://www.unfoldedcircle.com",
"release_date": "2023-10-30"
}
43 changes: 43 additions & 0 deletions examples/hello_integration.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#!/usr/bin/env python3
"""Hello world integration example. Bare minimum of an integration driver."""
import asyncio
import logging
from typing import Any

import ucapi

loop = asyncio.get_event_loop()
api = ucapi.IntegrationAPI(loop)


async def cmd_handler(entity: ucapi.Button, cmd_id: str, _params: dict[str, Any] | None) -> ucapi.StatusCodes:
"""
Push button command handler.
Called by the integration-API if a command is sent to a configured button-entity.
:param entity: button entity
:param cmd_id: command
:param _params: optional command parameters
:return: status of the command
"""
print(f"Got {entity.id} command request: {cmd_id}")

return ucapi.StatusCodes.OK


if __name__ == "__main__":
logging.basicConfig()

button = ucapi.Button(
"button1",
"Push the button",
cmd_handler=cmd_handler,
)
api.available_entities.add(button)

# We are ready all the time! Otherwise, use @api.listens_to(ucapi.Events.CONNECT) & DISCONNECT
api.set_device_state(ucapi.DeviceStates.CONNECTED)

loop.run_until_complete(api.init("hello_integration.json"))
loop.run_forever()

0 comments on commit 655bc4d

Please sign in to comment.