Skip to content

Commit

Permalink
test fire_last_message_received
Browse files Browse the repository at this point in the history
  • Loading branch information
grimmpp committed May 13, 2024
1 parent ede2856 commit c6abbe2
Showing 1 changed file with 14 additions and 24 deletions.
38 changes: 14 additions & 24 deletions custom_components/eltako/gateway.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,14 +88,15 @@ def __init__(self, general_settings:dict, hass: HomeAssistant,

def set_connection_state_changed_handler(self, handler):
self._connection_state_handler = handler
self._fire_connection_state_changed_event()
self._fire_connection_state_changed_event(self._bus.is_active())


def _fire_connection_state_changed_event(self):
LOGGER.info("Fire message connection state changed")
# event_id = config_helpers.get_bus_event_type(self.base_id, SIGNAL_GATEWAY_CONNECTION_STATUS)
# dispatcher_send(self.hass, event_id, self._bus.is_active())
self.process_connection_status_signal( self._bus.is_active() )
def _fire_connection_state_changed_event(self, status):
if self._connection_state_handler:
asyncio.ensure_future(
self._connection_state_handler(status),
loop= self._loop
)


def set_last_message_received_handler(self, handler):
Expand Down Expand Up @@ -123,20 +124,12 @@ def _fire_received_message_count_event(self):
)
LOGGER.info("RECEIVED MESSAGE COUNT EVENT")

def process_messages(self, data):
def process_messages(self, data=None):
"""Received message from bus in HA loop. (Actions needs to run outside bus thread!)"""
self._fire_received_message_count_event()
self._fire_last_message_received_event()

def process_connection_status_signal(self, status=None):
LOGGER.info(f"CHANGE CONNECTION STATUS: {status}")
if self._connection_state_handler:
asyncio.ensure_future(
self._connection_state_handler(status),
loop= self._loop
)



def _init_bus(self):
self._received_message_count = 0
self._fire_received_message_count_event()
Expand All @@ -146,7 +139,7 @@ def _init_bus(self):
else:
self._bus = ESP3SerialCommunicator(filename=self.serial_path, callback=self._callback_receive_message_from_serial_bus, esp2_translation_enabled=True)

self._bus.set_status_changed_handler(self.process_connection_status_signal)
self._bus.set_status_changed_handler(self._fire_connection_state_changed_event)


def _register_device(self) -> None:
Expand Down Expand Up @@ -224,11 +217,8 @@ async def async_setup(self):
self.hass, event_id, self._callback_send_message_to_serial_bus
)

event_id = config_helpers.get_bus_event_type(self.base_id, SIGNAL_RECEIVE_MESSAGE)
async_dispatcher_connect(self.hass, event_id, self.process_messages)

event_id = config_helpers.get_bus_event_type(self.base_id, SIGNAL_GATEWAY_CONNECTION_STATUS)
async_dispatcher_connect(self.hass, event_id, self.process_connection_status_signal)
# event_id = config_helpers.get_bus_event_type(self.base_id, SIGNAL_RECEIVE_MESSAGE)
# async_dispatcher_connect(self.hass, event_id, self.process_messages)

# Register home assistant service for sending arbitrary telegrams.
#
Expand Down Expand Up @@ -321,8 +311,8 @@ def _callback_receive_message_from_serial_bus(self, message):

if type(message) not in [EltakoPoll]:
LOGGER.debug("[Gateway] [Id: %d] Received message: %s", self.dev_id, message)
# self._fire_last_message_received_event()
# self._fire_received_message_count_event()
self.process_messages()

if isinstance(message, ESP2Message):
event_id = config_helpers.get_bus_event_type(self.base_id, SIGNAL_RECEIVE_MESSAGE)
dispatcher_send(self.hass, event_id, message)
Expand Down

0 comments on commit c6abbe2

Please sign in to comment.