Skip to content

Commit

Permalink
Added Service for sending arbritrary ESP2 messages
Browse files Browse the repository at this point in the history
  • Loading branch information
grimmpp committed Mar 21, 2024
1 parent 8ffd92d commit 67d5ca7
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 0 deletions.
3 changes: 3 additions & 0 deletions changes.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Changes and Feature List

## Version 1.4.1 Support for sending arbritrary messages
* Added Service for sending arbritrary ESP2 messages

## Version 1.4.0 ESP3 Support (USB300)
* Docs about gateway usage added.
* Added EEPs F6-02-01 and F6-02-02 as sender EEP for lights so that regular switch commands can be sent from Home Assistant.
Expand Down
67 changes: 67 additions & 0 deletions custom_components/eltako/gateway.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from eltakobus.message import ESP2Message, RPSMessage, Regular1BSMessage, Regular4BSMessage, EltakoPoll, prettify

from eltakobus.util import AddressExpression
from eltakobus.eep import EEP

from enocean.communicators import SerialCommunicator
from enocean.protocol.packet import RadioPacket, RORG, Packet
Expand Down Expand Up @@ -205,6 +206,72 @@ async def async_setup(self):
self.hass, event_id, self._callback_send_message_to_serial_bus
)

# Register home assistant service for sending arbitrary telegrams.
#
# 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._attr_dev_id}_send_message"
self.hass.services.async_register(DOMAIN, service_name, self.async_service_send_message)


# Command Section
async def async_service_send_message(self, event) -> None:
"""Send an arbitrary message with the provided eep."""
try:
sender_id_str = event.data.get("id", None)
sender_id:AddressExpression = AddressExpression.parse(sender_id_str)
except:
LOGGER.error(f"[Service: Send Message] No valid sender id defined. (Given sender id: {sender_id_str})")
return

try:
sender_eep_str = event.data.get("eep", None)
sender_eep:EEP = EEP.find(sender_eep_str)
except:
LOGGER.error(f"[Service: Send Message] No valid sender id defined. (Given sender id: {sender_id_str})")
return

eep:EEP = sender_eep()
for k in eep.__dict__.keys():
if k in event.data.keys():
setattr(eep, k, event.data.get(k[1:])) # key k starts always with '_' because it is a private attribute
else:
LOGGER.warn(f"[Service: Send Message] Argument {k} is not provided for sending {eep.eep_string} message. Set default value: {k}=0")
setattr(eep, k, 0)

try:
message = eep.encode_message(sender_id)
self.send_message(message)
except:
LOGGER.error(f"[Service: Send Message] Cannot send message.")


# if sender_id and sender_eep is not None:
# sender_address = AddressExpression((0xFF, 0x82, 0x3E, 0x04))
# message = None

# if (A5_38_08.eep_string == sender_eep):
# command = event.data.get("command")
# if command is not None:
# switching = CentralCommandSwitching(0, 1, 0, 0, command)
# message = A5_38_08(command=0x01, switching=switching).encode_message(sender_address)

# if (A5_10_06.eep_string == sender_eep):
# target_temperature = event.data.get("target_temperature")
# current_temperature = event.data.get("current_temperature")
# if target_temperature and current_temperature is not None:
# message = A5_10_06(A5_10_06.Heater_Mode.NORMAL, target_temperature, current_temperature, False).encode_message(sender_address)

# if message is not None:
# self.send_message(message)


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.base_id, SIGNAL_SEND_MESSAGE)
dispatcher_send(self.hass, event_id, msg)


def unload(self):
"""Disconnect callbacks established at init time."""
Expand Down

0 comments on commit 67d5ca7

Please sign in to comment.