Skip to content

Commit

Permalink
test thread sync
Browse files Browse the repository at this point in the history
  • Loading branch information
grimmpp committed May 14, 2024
1 parent 4328585 commit bf0882e
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 12 deletions.
19 changes: 7 additions & 12 deletions custom_components/eltako/gateway.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,8 @@ def set_connection_state_changed_handler(self, handler):

def _fire_connection_state_changed_event(self, status):
if self._connection_state_handler:
asyncio.run_coroutine_threadsafe(
self._connection_state_handler(status),
loop = self._loop
self.hass.create_task(
self._connection_state_handler(status)
)


Expand All @@ -105,9 +104,8 @@ def set_last_message_received_handler(self, handler):

def _fire_last_message_received_event(self):
if self._last_message_received_handler:
asyncio.run_coroutine_threadsafe(
self._last_message_received_handler( datetime.now(UTC).replace(tzinfo=pytz.UTC) ),
loop = self._loop
self.hass.create_task(
self._last_message_received_handler( datetime.now(UTC).replace(tzinfo=pytz.UTC) )
)


Expand All @@ -118,9 +116,8 @@ def set_received_message_count_handler(self, handler):
def _fire_received_message_count_event(self):
self._received_message_count += 1
if self._received_message_count_handler:
asyncio.run_coroutine_threadsafe(
self.hass.create_task(
self._received_message_count_handler( self._received_message_count ),
loop = self._loop
)

def process_messages(self, data=None):
Expand Down Expand Up @@ -293,10 +290,8 @@ def _callback_send_message_to_serial_bus(self, msg):
LOGGER.debug("[Gateway] [Id: %d] Send message: %s - Serialized: %s", self.dev_id, msg, msg.serialize().hex())

# put message on serial bus
# asyncio.ensure_future(self._bus.send(msg), loop=self._loop)
asyncio.run_coroutine_threadsafe(
self._bus.send(msg),
loop = self._loop
self.hass.create_task(
self._bus.send(msg)
)
else:
LOGGER.warn("[Gateway] [Id: %d] Serial port %s is not available!!! message (%s) was not sent.", self.dev_id, self.serial_path, msg)
Expand Down
2 changes: 2 additions & 0 deletions tests/test_binary_sensor_GatewayConnectionState.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ def test_GatewayConnectionState(self):
gateway=GatewayMock()
gateway._bus._is_active = False
gateway.hass.async_create_task = lambda a: []
gateway.hass.create_task = lambda a: []
bs = GatewayConnectionState(Platform.BINARY_SENSOR, gateway)

bs.value_changed(True)
Expand All @@ -32,6 +33,7 @@ def test_directly_online_GatewayConnectionState(self):
gateway=GatewayMock()
gateway._bus._is_active = True
gateway.hass.async_create_task = lambda a: []
gateway.hass.create_task = lambda a: []
bs = GatewayConnectionState(Platform.BINARY_SENSOR, gateway)

bs.value_changed(True)
Expand Down

0 comments on commit bf0882e

Please sign in to comment.