Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor Telegram.payload to support other APCI payloads #519

Merged
merged 22 commits into from
Dec 9, 2020
Merged
Show file tree
Hide file tree
Changes from 21 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

- renamed "PhysicalAddress" to "IndividualAddress"
- Telegram: `group_address` renamed to `destination_address`, to prepare support for other APCI services and add `source_address`
- Telegram: remove `Telegram.telegramtype` and replace with payload object derived from `xknx.telegram.apci.APCI`.
- CEMIFrame: remove `CEMIFrame.cmd`, which can be derived from `CEMIFrame.payload`.
- Farewell Travis CI; Welcome Github Actions!

## 0.15.6 Bugfix for StateUpater 2020-11-26
Expand Down
11 changes: 9 additions & 2 deletions examples/example_tunnel.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from xknx.dpt import DPTBinary
from xknx.io import GatewayScanner, Tunnel
from xknx.telegram import GroupAddress, IndividualAddress, Telegram
from xknx.telegram.apci import GroupValueWrite


async def main():
Expand Down Expand Up @@ -38,11 +39,17 @@ async def main():
await tunnel.connect()

await tunnel.send_telegram(
Telegram(destination_address=GroupAddress("1/0/15"), payload=DPTBinary(1))
Telegram(
destination_address=GroupAddress("1/0/15"),
payload=GroupValueWrite(DPTBinary(1)),
)
)
await asyncio.sleep(2)
await tunnel.send_telegram(
Telegram(destination_address=GroupAddress("1/0/15"), payload=DPTBinary(0))
Telegram(
destination_address=GroupAddress("1/0/15"),
payload=GroupValueWrite(DPTBinary(0)),
)
)
await asyncio.sleep(2)

Expand Down
11 changes: 9 additions & 2 deletions home-assistant-plugin/custom_components/xknx/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
ConnectionType,
)
from xknx.telegram import AddressFilter, GroupAddress, Telegram
from xknx.telegram.apci import GroupValueResponse, GroupValueWrite

from homeassistant.const import (
CONF_ENTITY_ID,
Expand Down Expand Up @@ -322,14 +323,20 @@ def async_create_exposures(self):

async def telegram_received_cb(self, telegram):
"""Call invoked after a KNX telegram was received."""
data = None

# Not all telegrams have serializable data.
if isinstance(telegram, (GroupValueWrite, GroupValueResponse)):
data = telegram.dpt.value

self.hass.bus.async_fire(
"knx_event",
{
"data": telegram.payload.value,
"data": data,
"destination": str(telegram.destination_address),
"direction": telegram.direction.value,
"source": str(telegram.source_address),
"telegramtype": telegram.telegramtype.value,
"telegramtype": telegram.payload.__class__.__name__,
},
)

Expand Down
31 changes: 15 additions & 16 deletions test/core_tests/telegram_queue_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from xknx.dpt import DPTBinary
from xknx.exceptions import CouldNotParseTelegram
from xknx.telegram import AddressFilter, GroupAddress, Telegram, TelegramDirection
from xknx.telegram.apci import GroupValueWrite


class AsyncMock(MagicMock):
Expand Down Expand Up @@ -40,7 +41,7 @@ def test_start(self):
telegram_in = Telegram(
destination_address=GroupAddress("1/2/3"),
direction=TelegramDirection.INCOMING,
payload=DPTBinary(1),
payload=GroupValueWrite(DPTBinary(1)),
)

self.loop.run_until_complete(xknx.telegram_queue.start())
Expand Down Expand Up @@ -75,14 +76,12 @@ async def async_none():

telegram_in = Telegram(
direction=TelegramDirection.INCOMING,
payload=DPTBinary(1),
destination_address=GroupAddress("1/2/3"),
payload=GroupValueWrite(DPTBinary(1)),
)

telegram_out = Telegram(
direction=TelegramDirection.OUTGOING,
payload=DPTBinary(1),
destination_address=GroupAddress("1/2/3"),
payload=GroupValueWrite(DPTBinary(1)),
)

self.loop.run_until_complete(xknx.telegram_queue.start())
Expand Down Expand Up @@ -120,7 +119,7 @@ async def async_telegram_received_cb(device):
telegram = Telegram(
destination_address=GroupAddress("1/2/3"),
direction=TelegramDirection.INCOMING,
payload=DPTBinary(1),
payload=GroupValueWrite(DPTBinary(1)),
)
self.loop.run_until_complete(
xknx.telegram_queue.process_telegram_incoming(telegram)
Expand All @@ -144,7 +143,7 @@ def test_unregister(self):
telegram = Telegram(
destination_address=GroupAddress("1/2/3"),
direction=TelegramDirection.INCOMING,
payload=DPTBinary(1),
payload=GroupValueWrite(DPTBinary(1)),
)
self.loop.run_until_complete(
xknx.telegram_queue.process_telegram_incoming(telegram)
Expand All @@ -170,7 +169,7 @@ def test_process_to_device(self, devices_by_ga_mock):
telegram = Telegram(
destination_address=GroupAddress("1/2/3"),
direction=TelegramDirection.INCOMING,
payload=DPTBinary(1),
payload=GroupValueWrite(DPTBinary(1)),
)
self.loop.run_until_complete(
xknx.telegram_queue.process_telegram_incoming(telegram)
Expand All @@ -193,7 +192,7 @@ def test_process_to_callback(self, devices_process):
telegram = Telegram(
destination_address=GroupAddress("1/2/3"),
direction=TelegramDirection.INCOMING,
payload=DPTBinary(1),
payload=GroupValueWrite(DPTBinary(1)),
)
self.loop.run_until_complete(
xknx.telegram_queue.process_telegram_incoming(telegram)
Expand All @@ -215,7 +214,7 @@ def test_outgoing(self, logger_warning_mock, if_mock):
telegram = Telegram(
destination_address=GroupAddress("1/2/3"),
direction=TelegramDirection.OUTGOING,
payload=DPTBinary(1),
payload=GroupValueWrite(DPTBinary(1)),
)

# log a warning if there is no KNXIP interface instanciated
Expand Down Expand Up @@ -249,7 +248,7 @@ async def process_exception():
telegram = Telegram(
destination_address=GroupAddress("1/2/3"),
direction=TelegramDirection.INCOMING,
payload=DPTBinary(1),
payload=GroupValueWrite(DPTBinary(1)),
)

xknx.telegrams.put_nowait(telegram)
Expand Down Expand Up @@ -279,12 +278,12 @@ def test_process_all_telegrams(
telegram_in = Telegram(
destination_address=GroupAddress("1/2/3"),
direction=TelegramDirection.INCOMING,
payload=DPTBinary(1),
payload=GroupValueWrite(DPTBinary(1)),
)
telegram_out = Telegram(
destination_address=GroupAddress("1/2/3"),
direction=TelegramDirection.OUTGOING,
payload=DPTBinary(1),
payload=GroupValueWrite(DPTBinary(1)),
)

xknx.telegrams.put_nowait(telegram_in)
Expand Down Expand Up @@ -314,7 +313,7 @@ async def async_telegram_received_cb(device):
telegram = Telegram(
destination_address=GroupAddress("1/2/3"),
direction=TelegramDirection.INCOMING,
payload=DPTBinary(1),
payload=GroupValueWrite(DPTBinary(1)),
)
xknx.telegrams.put_nowait(telegram)
self.loop.run_until_complete(xknx.telegram_queue._process_all_telegrams())
Expand Down Expand Up @@ -343,7 +342,7 @@ async def async_telegram_received_cb(device):
telegram = Telegram(
destination_address=GroupAddress("1/2/3"),
direction=TelegramDirection.INCOMING,
payload=DPTBinary(1),
payload=GroupValueWrite(DPTBinary(1)),
)
xknx.telegrams.put_nowait(telegram)
self.loop.run_until_complete(xknx.telegram_queue._process_all_telegrams())
Expand Down Expand Up @@ -372,7 +371,7 @@ async def async_telegram_received_cb(device):
telegram = Telegram(
destination_address=GroupAddress("1/2/3"),
direction=TelegramDirection.INCOMING,
payload=DPTBinary(1),
payload=GroupValueWrite(DPTBinary(1)),
)
xknx.telegrams.put_nowait(telegram)
self.loop.run_until_complete(xknx.telegram_queue._process_all_telegrams())
Expand Down
21 changes: 8 additions & 13 deletions test/core_tests/value_reader_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
from xknx import XKNX
from xknx.core import ValueReader
from xknx.dpt import DPTBinary
from xknx.telegram import GroupAddress, Telegram, TelegramDirection, TelegramType
from xknx.telegram import GroupAddress, Telegram, TelegramDirection
from xknx.telegram.apci import GroupValueRead, GroupValueResponse, GroupValueWrite


class TestValueReader(unittest.TestCase):
Expand All @@ -28,9 +29,8 @@ def test_value_reader_read_success(self, timeout_mock):
test_group_address = GroupAddress("0/0/0")
response_telegram = Telegram(
destination_address=test_group_address,
telegramtype=TelegramType.GROUP_RESPONSE,
direction=TelegramDirection.INCOMING,
payload=DPTBinary(1),
payload=GroupValueResponse(DPTBinary(1)),
)

value_reader = ValueReader(xknx, test_group_address)
Expand Down Expand Up @@ -94,8 +94,7 @@ def test_value_reader_send_group_read(self):
self.assertEqual(
telegram,
Telegram(
destination_address=GroupAddress("0/0/0"),
telegramtype=TelegramType.GROUP_READ,
destination_address=GroupAddress("0/0/0"), payload=GroupValueRead()
),
)

Expand All @@ -105,27 +104,23 @@ def test_value_reader_telegram_received(self):
test_group_address = GroupAddress("0/0/0")
expected_telegram_1 = Telegram(
destination_address=test_group_address,
telegramtype=TelegramType.GROUP_RESPONSE,
direction=TelegramDirection.INCOMING,
payload=DPTBinary(1),
payload=GroupValueResponse(DPTBinary(1)),
)
expected_telegram_2 = Telegram(
destination_address=test_group_address,
telegramtype=TelegramType.GROUP_WRITE,
direction=TelegramDirection.INCOMING,
payload=DPTBinary(1),
payload=GroupValueWrite(DPTBinary(1)),
)
telegram_wrong_address = Telegram(
destination_address=GroupAddress("0/0/1"),
telegramtype=TelegramType.GROUP_RESPONSE,
direction=TelegramDirection.INCOMING,
payload=DPTBinary(1),
payload=GroupValueResponse(DPTBinary(1)),
)
telegram_wrong_type = Telegram(
destination_address=test_group_address,
telegramtype=TelegramType.GROUP_READ,
direction=TelegramDirection.INCOMING,
payload=DPTBinary(1),
payload=GroupValueRead(),
)

value_reader = ValueReader(xknx, test_group_address)
Expand Down
Loading