diff --git a/custom_components/eltako/const.py b/custom_components/eltako/const.py index 4f6136e..67a9155 100644 --- a/custom_components/eltako/const.py +++ b/custom_components/eltako/const.py @@ -20,9 +20,10 @@ SIGNAL_RECEIVE_MESSAGE: Final = "receive_message" SIGNAL_SEND_MESSAGE: Final = "send_message" +SIGNAL_SEND_MESSAGE_SERVICE: Final = "send_message_service" EVENT_BUTTON_PRESSED: Final = "btn_pressed" EVENT_CONTACT_CLOSED: Final = "contact_closed" -GOBAL_EVENT_BUS_ID: Final = "eltako_global_event_bus" +GLOBAL_EVENT_BUS_ID: Final = "eltako_global_event_bus" LOGGER: Final = logging.getLogger(DOMAIN) diff --git a/custom_components/eltako/gateway.py b/custom_components/eltako/gateway.py index 31f49fb..7319d8e 100644 --- a/custom_components/eltako/gateway.py +++ b/custom_components/eltako/gateway.py @@ -283,6 +283,7 @@ async def async_setup(self): # receive messages from HA event bus event_id = config_helpers.get_bus_event_type(self.dev_id, SIGNAL_SEND_MESSAGE) + LOGGER.debug("[Gateway] [Id: %d] Register gateway bus for message event_id %s", event_id) self.dispatcher_disconnect_handle = async_dispatcher_connect( self.hass, event_id, self._callback_send_message_to_serial_bus ) @@ -292,7 +293,8 @@ async def async_setup(self): # The service will be registered for each gateway, as the user # might have different gateways that cause the eltako relays # only to react on them. - service_name = f"gateway_{self.dev_id}_send_message" + service_name = config_helpers.get_bus_event_type(self.dev_id, SIGNAL_SEND_MESSAGE_SERVICE) + LOGGER.debug("[Gateway] [Id: %d] Register send message service event_id %s", event_id) self.hass.services.async_register(DOMAIN, service_name, self.async_service_send_message) @@ -345,7 +347,7 @@ def send_message(self, msg: ESP2Message): """Put message on RS485 bus. First the message is put onto HA event bus so that other automations can react on messages.""" event_id = config_helpers.get_bus_event_type(self.dev_id, SIGNAL_SEND_MESSAGE) dispatcher_send(self.hass, event_id, msg) - dispatcher_send(self.hass, GOBAL_EVENT_BUS_ID, {'gateway':self, 'esp2_msg': msg}) + dispatcher_send(self.hass, GLOBAL_EVENT_BUS_ID, {'gateway':self, 'esp2_msg': msg}) def unload(self): @@ -368,7 +370,7 @@ def _callback_send_message_to_serial_bus(self, msg): self.hass.create_task( self._bus.send(msg) ) - dispatcher_send(self.hass, GOBAL_EVENT_BUS_ID, {'gateway':self, 'esp2_msg': msg}) + dispatcher_send(self.hass, GLOBAL_EVENT_BUS_ID, {'gateway':self, 'esp2_msg': msg}) else: LOGGER.warning("[Gateway] [Id: %d] Serial port %s is not available!!! message (%s) was not sent.", self.dev_id, self.serial_path, msg) @@ -389,10 +391,17 @@ def _callback_receive_message_from_serial_bus(self, message:ESP2Message): self._attr_base_id = AddressExpression( (message.body[2:6], None) ) self._fire_base_id_change_handlers(self.base_id) + if self.base_id != b'\x00\x00\x00\x00' and isinstance(message, ESP2Message): event_id = config_helpers.get_bus_event_type(self.dev_id, SIGNAL_RECEIVE_MESSAGE) dispatcher_send(self.hass, event_id, message) - dispatcher_send(self.hass, GOBAL_EVENT_BUS_ID, {'gateway':self, 'esp2_msg': message}) + + global_msg = ESP2Message(message.body) + address = message.body[6:10] + if address[0] == b'\x00' and address[1] == b'\x00': + global_msg.address = bytes((a + b) & 0xFF for a, b in zip(self.base_id[0], address)) + + dispatcher_send(self.hass, GLOBAL_EVENT_BUS_ID, {'gateway':self, 'esp2_msg': message}) @property diff --git a/custom_components/eltako/virtual_network_gateway.py b/custom_components/eltako/virtual_network_gateway.py index ff6aefa..8afd779 100644 --- a/custom_components/eltako/virtual_network_gateway.py +++ b/custom_components/eltako/virtual_network_gateway.py @@ -17,7 +17,7 @@ from .const import * from . import config_helpers -from .gateway import EnOceanGateway, GOBAL_EVENT_BUS_ID +from .gateway import EnOceanGateway, GLOBAL_EVENT_BUS_ID VIRT_GW_PORT = 12345 VIRT_GW_DEVICE_NAME = "ESP2 Netowrk Reverse Bridge" @@ -239,7 +239,7 @@ async def async_setup(self): # register for all incoming and outgoing messages from all gateways self.dispatcher_disconnect_handle = async_dispatcher_connect( - self.hass, GOBAL_EVENT_BUS_ID, self._forward_message + self.hass, GLOBAL_EVENT_BUS_ID, self._forward_message ) self.zeroconf:Zeroconf = await zeroconf.async_get_instance(self.hass)