diff --git a/custom_components/eltako/const.py b/custom_components/eltako/const.py index c3157eb6..c15c429c 100644 --- a/custom_components/eltako/const.py +++ b/custom_components/eltako/const.py @@ -20,6 +20,7 @@ SIGNAL_RECEIVE_MESSAGE: Final = "receive_message" SIGNAL_SEND_MESSAGE: Final = "send_message" +SIGNAL_GATEWAY_CONNECTION_STATUS: Final = "gateway_connection_status" EVENT_BUTTON_PRESSED: Final = "btn_pressed" EVENT_CONTACT_CLOSED: Final = "contact_closed" diff --git a/custom_components/eltako/gateway.py b/custom_components/eltako/gateway.py index 7a5bea43..7c9649a6 100644 --- a/custom_components/eltako/gateway.py +++ b/custom_components/eltako/gateway.py @@ -88,14 +88,11 @@ 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._bus and self._bus.is_active()) def _fire_connection_state_changed_event(self, connected:bool): - if self._connection_state_handler: - self.hass.async_create_task( - self._connection_state_handler( connected ) - ) + event_id = config_helpers.get_bus_event_type(self.base_id, SIGNAL_GATEWAY_CONNECTION_STATUS) + dispatcher_send(self.hass, event_id, None) def set_last_message_received_handler(self, handler): @@ -121,7 +118,15 @@ def _fire_received_message_count_event(self): ) def process_messages(self, data): - LOGGER.info("GATEWAY: HANDLE MESSAGES") + """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, data): + if self._connection_state_handler: + self.hass.async_create_task( + self._connection_state_handler( self._bus.is_active() ) + ) def _init_bus(self): @@ -132,7 +137,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._fire_connection_state_changed_event) + self._bus.set_status_changed_handler(self._fire_connection_state_changed_event) def _register_device(self) -> None: @@ -213,6 +218,9 @@ async def async_setup(self): 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) + # Register home assistant service for sending arbitrary telegrams. # # The service will be registered for each gateway, as the user