Sock.send with hex sequence - solved

Team,
sorry for the dummy question but I continue to get an error.
Starting from the TCP component I modified it to send a sequence to the destination.
It works fine till when I try to send a special sequence.
I get this error:

2022-12-08 18:37:56.390 ERROR (MainThread) [homeassistant.helpers.entity] Update for sensor.jvc_dila_sensor fails

Traceback (most recent call last):

File “/usr/src/homeassistant/homeassistant/helpers/entity.py”, line 527, in async_update_ha_state

await self.async_device_update()

File “/usr/src/homeassistant/homeassistant/helpers/entity.py”, line 722, in async_device_update

raise exc

File “/usr/local/lib/python3.10/concurrent/futures/thread.py”, line 58, in run

result = self.fn(*self.args, **self.kwargs)

File “/config/custom_components/jvc_dila/common.py”, line 153, in update

_LOGGER.warning(“6”,)

TypeError: a bytes-like object is required, not ‘str’

This is the latest code:

def update(self) -> None:

        """Get the latest value for this sensor."""

        _LOGGER.warning("0",)

        with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:

            sock.settimeout(self._config[CONF_TIMEOUT])

            try:

                sock.connect((self._config[CONF_HOST], self._config[CONF_PORT]))

            except OSError as err:

                _LOGGER.error(

                    "Unable to connect to %s on port %s: %s",

                    self._config[CONF_HOST],

                    self._config[CONF_PORT],

                    err,

                )

                sock.close()

                return

            _LOGGER.warning("1",)

            readable, _, _ = select.select([sock], [], [], self._config[CONF_TIMEOUT])

            if not readable:

                _LOGGER.warning(

                    "Timeout (%s second(s)) waiting for a response after "

                    "sending to %s on port %s",

                    self._config[CONF_TIMEOUT],

                    self._config[CONF_HOST],

                    self._config[CONF_PORT],

                )

                sock.close()

                return

            _LOGGER.warning("2",)

            value = sock.recv(1024).decode()

            """ debug log -> PJ_OK """

            _LOGGER.warning(

                "Input %s",

                value,

                )

            _LOGGER.warning("3",)

            try:

                sock.send("PJREQ".encode())

            except OSError as err:

                _LOGGER.error(

                    "Unable to send payload PJREQ on port %s: %s",

                    self._config[CONF_HOST],

                    self._config[CONF_PORT],

                    err,

                )

                sock.close()

                return

            _LOGGER.warning("4",)

            readable, _, _ = select.select([sock], [], [], self._config[CONF_TIMEOUT])

            if not readable:

                _LOGGER.warning(

                    "Timeout (%s second(s)) waiting for a response after PJREQ"

                    "sending to %s on port %s",

                    self._config[CONF_TIMEOUT],

                    self._config[CONF_HOST],

                    self._config[CONF_PORT],

                )

                sock.close()

                return

            _LOGGER.warning("5",)

            value = sock.recv(1024).decode()

            """ debug log -> PJACK """

            _LOGGER.warning(

                "Input %s",

                value,

                )

            _LOGGER.warning("6",)

            try:

                sock.send(self._config[CONF_PAYLOAD].encode())

            except OSError as err:

                _LOGGER.error(

                    "Unable to send payload PJREQ on port %s: %s",

                    self._config[CONF_HOST],

                    self._config[CONF_PORT],

                    err,

                )

                sock.close()

                return

            _LOGGER.warning("7",)

            value = sock.recv(1024).decode()

            """ debug log -> Answer"""

            _LOGGER.warning(

                "Input %s",

                value,

                )

               

            _LOGGER.warning("8",)

            sock.close()

            _LOGGER.warning("9",)

        value_template = self._config[CONF_VALUE_TEMPLATE]

        if value_template is not None:

            try:

                self._state = value_template.render(parse_result=False, value=value)

                return

            except TemplateError:

                _LOGGER.error(

                    "Unable to render template of %r with value: %r",

                    self._config[CONF_VALUE_TEMPLATE],

                    value,

                )

                return

        self._state = value

Teh payload is:

DEFAULT_PAYLOAD: Final = "\x3F\x89\x01\x50\x57\x0A"

I tried to replace

                sock.send(self._config[CONF_PAYLOAD].encode())

with

                sock.send("\x3F\x89\x01\x50\x57\x0A")

I know it should be trivial but I’m not able to understand :slight_smile:

Found myself the answer, here if anyone needs it:

            buffer[0] = 0x3F
            buffer[1] = 0x89
            buffer[2] = 0x01
            buffer[3] = 0x50
            buffer[4] = 0x57
            buffer[5] = 0x0A

            try:
                sock.send(buffer)
            except OSError as err:
                _LOGGER.error(
                    "Unable to send buffer on port %s: %s",
                    self._config[CONF_HOST],
                    self._config[CONF_PORT],
                    err,
                )

                sock.close()
                return