Skip to content

Commit

Permalink
Add examples with APCI payloads
Browse files Browse the repository at this point in the history
- example_restart.py will restart actor 1.2.3
- example_info.py will print actor information
  • Loading branch information
basilfx committed Nov 25, 2020
1 parent cd0e582 commit 505d237
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 1 deletion.
40 changes: 40 additions & 0 deletions examples/example_info.py
Original file line number Diff line number Diff line change
@@ -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()
46 changes: 46 additions & 0 deletions examples/example_restart.py
Original file line number Diff line number Diff line change
@@ -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()
9 changes: 8 additions & 1 deletion examples/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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|
|[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|

0 comments on commit 505d237

Please sign in to comment.