Sending Hex values via telnet

I’ve created a board for my watering controller that has 2x 2 channel relays and 2x 4 channel relays with rs232 (Sainsmart) interfaces on it. I also have a Digi Portserver 16 channel lan to serial unit to which i can connect these boards.

i can control the relays no worries, directly connected to the boards, and i have had some success via the Digi unit although it still requires some finishing touches. I have all the relevant control codes but the y are all require to be sent in hexadecimal.

I already have one of these boards in my loft controlling my xmas lights from a crestron program which works flawlessly (and has done for 2 years without a reboot)

Question now is, can i send Hex commands via the telnet component?, is there a syntax i need to adhere to so that it sends hex and not the ascii characters?

- platform: telnet
  switches:
    xmas1:
      name: "Xmas Lights Outlet 1"
      resource: "192.168.19.250"
      port: 2001
      command_on: "\x11"
      command_off: "\x21"
    xmas2:
      name: "Xmas Lights Outlet 2"
      resource: "192.168.19.250"
      port: 2001
      command_on: "\x12"
      command_off: "\x22"
    xmas3:
      name: "Xmas Lights Outlet 3"
      resource: "192.168.19.250"
      port: 2001
      command_on: "\x14"
      command_off: "\x24"
    xmas4:
      name: "Xmas Lights Outlet 4"
      resource: "192.168.19.250"
      port: 2001
      command_on: "\x18"
      command_off: "\x28"
    allxmas:
      name: "Xmas Lights Outlets"
      resource: "192.168.19.250"
      port: 2001
      command_on: "\x1f"
      command_off: "\x2f"

SOLUTION FOUND, SEE BOTTOM OF POST

I’m trying to use telnet to control a SONANCE DSP2-150 amplfier.
I am able to send and receive valid response responses using RealTerm but have been unable to get this working in Home Assistant.

The published commands from SONANCE are
TCP/IP PORT : 52000
Command / Query Format : TCP/IP Telnet

Operation Function Name Standard IP Codes Alternative IP Code Format
Command Power On FF 55 01 01 \xFF\x55\x01\x01
Command Power Off FF 55 01 02 \xFF\x55\x01\x02
Command Power Toggle FF 55 01 03 \xFF\x55\x01\x03
Query Power Status FF 55 01 70 \xFF\x55\x01\x70

I have found that using the Alternative IP Code Format works within the RealTerm “Send ASCII function”
My current configuration is

switch:
  - platform: telnet
    switches:
      kitchen_amp_dsp2_150:
        resource: 192.168.1.19
        port: 52000
        command_on: "\xFF\x55\x01\x01"
        command_off: "\xFF\x55\x01\x02"
        command_state: "\xFF\x55\x01\x70"
        value_template: '{{ value == "Power status :On" }}'
        timeout: 0.9

But trying to use the switch I see the following error in my logs. My guess is the above post from @Radebe2k may not be valid any more for sending hexadecimal using the telnet switch.

Logger: homeassistant.components.websocket_api.http.connection
Source: components/telnet/switch.py:116
Integration: Home Assistant WebSocket API (documentation, issues)
First occurred: 10:59:03 PM (7 occurrences)
Last logged: 11:01:28 PM

[140244985087312] 'ascii' codec can't encode character '\xff' in position 0: ordinal not in range(128)
[140244982959648] 'ascii' codec can't encode character '\xff' in position 0: ordinal not in range(128)
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/components/websocket_api/commands.py", line 143, in handle_call_service
    await hass.services.async_call(
  File "/usr/src/homeassistant/homeassistant/core.py", line 1480, in async_call
    task.result()
  File "/usr/src/homeassistant/homeassistant/core.py", line 1515, in _execute_service
    await handler.job.target(service_call)
  File "/usr/src/homeassistant/homeassistant/helpers/entity_component.py", line 206, in handle_service
    await self.hass.helpers.service.entity_service_call(
  File "/usr/src/homeassistant/homeassistant/helpers/service.py", line 649, in entity_service_call
    future.result()  # pop exception if have
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 692, in async_request_call
    await coro
  File "/usr/src/homeassistant/homeassistant/helpers/service.py", line 686, in _handle_entity_call
    await result
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 717, in async_turn_on
    await self.hass.async_add_executor_job(ft.partial(self.turn_on, **kwargs))
  File "/usr/local/lib/python3.8/concurrent/futures/thread.py", line 57, in run
    result = self.fn(*self.args, **self.kwargs)
  File "/usr/src/homeassistant/homeassistant/components/telnet/switch.py", line 157, in turn_on
    self._telnet_command(self._command_on)
  File "/usr/src/homeassistant/homeassistant/components/telnet/switch.py", line 116, in _telnet_command
    telnet.write(command.encode("ASCII") + b"\r")
UnicodeEncodeError: 'ascii' codec can't encode character '\xff' in position 0: ordinal not in range(128)

Any suggestions for next steps to properly send hex using the telnet switch? I tried removing the quotes from around the command but then I just get no response from the unit.

UPDATE: I found a solution using command_line switch. This may have been a strange application as I needed to wait before checking the state as the amp required a few seconds to turn on.

switch:
  - platform: command_line
    scan_interval: 15
    switches:
      kitchen_amp_dsp2_150:
        command_on: 'echo -n -e "\xFF\x55\x01\x01" | nc 192.168.1.19 52000 && sleep 9s'
        command_off: 'echo -n -e "\xFF\x55\x01\x02" | nc 192.168.1.19 52000  && sleep 2s'
        command_state: 'echo -n -e "\xFF\x55\x01\x70" | nc 192.168.1.19 52000 | grep -c "Power status :On"'
        value_template: '{{ value == "1" }}'
        command_timeout : 10.0