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

Fix Address comparison / ValueReader #636

Merged
merged 4 commits into from
Mar 19, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
6 changes: 6 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## Unreleased changes

### Internals

- Comparing GroupAddress or IndividualAddress to other types don't raise TypeError anymore.

## 0.17.3 Passive addresses 2021-03-16

### Devices
Expand Down
13 changes: 7 additions & 6 deletions test/telegram_tests/address_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,10 @@ def test_equal(self):
self.assertEqual(IndividualAddress("1.0.0"), IndividualAddress(4096))
self.assertNotEqual(IndividualAddress("1.0.0"), IndividualAddress("1.1.1"))
self.assertNotEqual(IndividualAddress("1.0.0"), None)
with self.assertRaises(TypeError):
IndividualAddress(
"1.0.0"
) == "example" # pylint: disable=expression-not-assigned
self.assertNotEqual(IndividualAddress("1.0.0"), "example")
self.assertNotEqual(IndividualAddress("1.1.1"), GroupAddress("1/1/1"))
self.assertNotEqual(IndividualAddress(250), GroupAddress(250))
self.assertNotEqual(IndividualAddress(250), 250)

def test_representation(self):
"""Test string representation of address."""
Expand Down Expand Up @@ -217,8 +217,9 @@ def test_equal(self):
self.assertEqual(GroupAddress("1/0"), GroupAddress(2048))
self.assertNotEqual(GroupAddress("1/1"), GroupAddress("1/1/0"))
self.assertNotEqual(GroupAddress("1/0"), None)
with self.assertRaises(TypeError):
GroupAddress("1/0") == "example" # pylint: disable=expression-not-assigned
self.assertNotEqual(GroupAddress("1/0"), "example")
self.assertNotEqual(GroupAddress(1), IndividualAddress(1))
self.assertNotEqual(GroupAddress(1), 1)

def test_representation(self):
"""Test string representation of address."""
Expand Down
15 changes: 8 additions & 7 deletions xknx/core/telegram_queue.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,15 +119,16 @@ async def _telegram_consumer(self) -> None:
self.xknx.telegrams.task_done()
break

try:
if telegram.direction == TelegramDirection.INCOMING:
if telegram.direction == TelegramDirection.INCOMING:
try:
await self.process_telegram_incoming(telegram)
except XKNXException as ex:
logger.error("Error while processing incoming telegram %s", ex)
finally:
self.xknx.telegrams.task_done()
elif telegram.direction == TelegramDirection.OUTGOING:
self.outgoing_queue.put_nowait(telegram)
# self.xknx.telegrams.task_done() for outgoing is called in _outgoing_rate_limiter.
except XKNXException as ex:
logger.error("Error while processing telegram %s", ex)
elif telegram.direction == TelegramDirection.OUTGOING:
self.outgoing_queue.put_nowait(telegram)
# self.xknx.telegrams.task_done() for outgoing is called in _outgoing_rate_limiter.

async def _outgoing_rate_limiter(self) -> None:
"""Endless loop for processing outgoing telegrams."""
Expand Down
3 changes: 2 additions & 1 deletion xknx/core/value_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ def __init__(
async def read(self) -> Optional[Telegram]:
"""Send group read and wait for response."""
cb_obj = self.xknx.telegram_queue.register_telegram_received_cb(
self.telegram_received
self.telegram_received,
group_addresses=[self.group_address],
)
await self.send_group_read()

Expand Down
11 changes: 5 additions & 6 deletions xknx/telegram/address.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
* 2nd level: "1/2"
* Free format: "123"
"""
from abc import ABC
from enum import Enum
from re import compile as re_compile
from typing import Optional, Tuple, Union
Expand Down Expand Up @@ -41,7 +42,7 @@ def address_tuple_to_int(address: Tuple[int, int]) -> int:
return int(address[0] * 256 + address[1])


class BaseAddress: # pylint: disable=too-few-public-methods
class BaseAddress(ABC):
"""Base class for all knx address types."""

def __init__(self) -> None:
Expand All @@ -65,11 +66,9 @@ def __eq__(self, other: Optional[object]) -> bool:

Returns `False` if we check against `None`.
"""
if other is None:
return False
if type(self) is not type(other):
raise TypeError()
return self.__hash__() == other.__hash__()
if isinstance(self, type(other)):
return self.__hash__() == other.__hash__()
return False

def __hash__(self) -> int:
"""Hash Address so it can be used as dict key."""
Expand Down