From 505d2373dff55ce0f6c861052b8187a1f41cc0aa Mon Sep 17 00:00:00 2001 From: Bas Stottelaar Date: Mon, 9 Dec 2019 23:21:16 +0100 Subject: [PATCH] Add examples with APCI payloads - example_restart.py will restart actor 1.2.3 - example_info.py will print actor information --- examples/example_info.py | 40 ++++++++++++++++++++++++++++++++ examples/example_restart.py | 46 +++++++++++++++++++++++++++++++++++++ examples/readme.md | 9 +++++++- 3 files changed, 94 insertions(+), 1 deletion(-) create mode 100644 examples/example_info.py create mode 100644 examples/example_restart.py diff --git a/examples/example_info.py b/examples/example_info.py new file mode 100644 index 0000000000..7d7e9c3e09 --- /dev/null +++ b/examples/example_info.py @@ -0,0 +1,40 @@ +"""Example on how to read mask version and properties from a KNX actor.""" +import asyncio + +from xknx import XKNX +from xknx.core import PayloadReader +from xknx.telegram import PhysicalAddress, \ + PropertyRead, PropertyResponse, MaskVersionRead, MaskVersionResponse + + +async def main(): + """Connect and read properties from KNX bus.""" + xknx = XKNX() + await xknx.start() + + reader = PayloadReader(xknx, PhysicalAddress("1.1.1")) + + # Read the mask version of the device (descriptor 0). + payload = await reader.send(MaskVersionRead(descriptor=0), response_class=MaskVersionResponse) + if payload is not None: + print("Mask version: %04x" % payload.value) + + # Read the serial number of the device (object 0, property 11). + payload = await reader.send(PropertyRead(object_index=0, property_id=11, count=1, start_index=1), response_class=PropertyResponse) + if payload is not None: + print("Serial number: %02x%02x:%02x%02x%02x%02x" % ( + payload.data[0], payload.data[1], payload.data[2], + payload.data[3], payload.data[4], payload.data[5])) + + # Check if the device is in programming mode (object 0, property 54). + payload = await reader.send(PropertyRead(object_index=0, property_id=54, count=1, start_index=1), response_class=PropertyResponse) + if payload is not None: + print("Programming mode: %s" % ("ON" if payload.data[0] else "OFF")) + + await xknx.stop() + + +# pylint: disable=invalid-name +loop = asyncio.get_event_loop() +loop.run_until_complete(main()) +loop.close() diff --git a/examples/example_restart.py b/examples/example_restart.py new file mode 100644 index 0000000000..ad4826f7b7 --- /dev/null +++ b/examples/example_restart.py @@ -0,0 +1,46 @@ +"""Example on how to connecto to a KNX/IP tunneling device.""" +import asyncio + +from xknx import XKNX +from xknx.io import GatewayScanner, Tunnel +from xknx.telegram import PhysicalAddress, Telegram, Restart + + +async def main(): + """Connect to a tunnel, send 2 telegrams and disconnect.""" + xknx = XKNX() + gatewayscanner = GatewayScanner(xknx) + gateways = await gatewayscanner.scan() + + if not gateways: + print("No Gateways found") + return + + gateway = gateways[0] + src_address = PhysicalAddress("15.15.249") + + print("Connecting to {}:{} from {}".format( + gateway.ip_addr, + gateway.port, + gateway.local_ip)) + + tunnel = Tunnel( + xknx, + src_address, + local_ip=gateway.local_ip, + gateway_ip=gateway.ip_addr, + gateway_port=gateway.port) + + await tunnel.connect_udp() + await tunnel.connect() + + await tunnel.send_telegram(Telegram(PhysicalAddress('1.2.3'), payload=Restart())) + await asyncio.sleep(2) + + await tunnel.connectionstate() + await tunnel.disconnect() + +# pylint: disable=invalid-name +loop = asyncio.get_event_loop() +loop.run_until_complete(main()) +loop.close() diff --git a/examples/readme.md b/examples/readme.md index 30ce8d0eb0..e65370e530 100644 --- a/examples/readme.md +++ b/examples/readme.md @@ -26,4 +26,11 @@ |[Sensor](./example_sensor.py)|Example for Sensor device| |[Switch](./example_switch.py)|Example for Switch device| |[Scene](./example_scene.py)|Example for switching a light on and off| -|[Weather](./example_weather.py)|Example for Weather device and reading sensor data| \ No newline at end of file +|[Weather](./example_weather.py)|Example for Weather device and reading sensor data| + +## Low-level + +|Example|Description| +|-|-| +|[Info](./example_info.py)|Example on how to read device information such as serial and programming mode (depends on actor if supported)| +|[Restart](./example_restart.py)|Example on how to restart an actor| \ No newline at end of file