Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MQTT random binary or other formats data #116437

Closed
sca075 opened this issue Apr 30, 2024 · 3 comments
Closed

MQTT random binary or other formats data #116437

sca075 opened this issue Apr 30, 2024 · 3 comments

Comments

@sca075
Copy link

sca075 commented Apr 30, 2024

The problem

The subscribed topics format changed somehow, normally the strings are set as b"mystring" for this reason is necessary to convert the topic with my_variable.decode("utf-8"). I'm developing an "integration" and I had to, as work around, create a function to check the payload format. The $state of the entity sampled is a b"string" usually.. but sometimes it was output as str not binary value from MQTT (there was no change either on the way to subscribe the topic or in the vacuum data as there was no update on both).

What version of Home Assistant Core has the issue?

2024.5.b02

What was the last working version of Home Assistant Core?

2024.4.4

What type of installation are you running?

Home Assistant OS

Integration causing the issue

MQTT

Link to integration documentation on our website

https://www.home-assistant.io/integrations/mqtt/

Diagnostics information

Sorry no diagnostic available.

Example YAML snippet

No response

Anything in the logs that might be useful for us?

2024-04-29 16:38:51.694 INFO (MainThread) [custom_components.valetudo_vacuum_camera.valetudo.MQTT.connector] silenttepidstinkbug not ready, not connected to MQTT.
2024-04-29 16:38:54.167 ERROR (MainThread) [homeassistant.util.logging] Exception in async_message_received when handling msg on 'valetudo/SilentTepidStinkbug/$state': 'init'
Traceback (most recent call last):
  File "/config/custom_components/valetudo_vacuum_camera/valetudo/MQTT/connector.py", line 326, in async_message_received
    await self.hypfer_handle_connect_state(msg)
  File "/config/custom_components/valetudo_vacuum_camera/valetudo/MQTT/connector.py", line 179, in hypfer_handle_connect_state
    self._payload = msg.payload.decode("utf-8")
                    ^^^^^^^^^^^^^^^^^^
AttributeError: 'str' object has no attribute 'decode'. Did you mean: 'encode'?

2024-04-29 16:38:54.218 ERROR (MainThread) [homeassistant.util.logging] Exception in async_message_received when handling msg on 'valetudo/SilentTepidStinkbug/$state': 'ready'
Traceback (most recent call last):
  File "/config/custom_components/valetudo_vacuum_camera/valetudo/MQTT/connector.py", line 326, in async_message_received
    await self.hypfer_handle_connect_state(msg)
  File "/config/custom_components/valetudo_vacuum_camera/valetudo/MQTT/connector.py", line 179, in hypfer_handle_connect_state
    self._payload = msg.payload.decode("utf-8")
                    ^^^^^^^^^^^^^^^^^^
AttributeError: 'str' object has no attribute 'decode'. Did you mean: 'encode'?

2024-04-29 16:38:54.255 ERROR (MainThread) [homeassistant.util.logging] Exception in async_message_received when handling msg on 'valetudo/SilentTepidStinkbug/$state': 'init'
Traceback (most recent call last):
  File "/config/custom_components/valetudo_vacuum_camera/valetudo/MQTT/connector.py", line 326, in async_message_received
    await self.hypfer_handle_connect_state(msg)
  File "/config/custom_components/valetudo_vacuum_camera/valetudo/MQTT/connector.py", line 179, in hypfer_handle_connect_state
    self._payload = msg.payload.decode("utf-8")
                    ^^^^^^^^^^^^^^^^^^
AttributeError: 'str' object has no attribute 'decode'. Did you mean: 'encode'?

2024-04-29 16:38:54.258 INFO (MainThread) [custom_components.valetudo_vacuum_camera.valetudo.MQTT.connector] Received silenttepidstinkbug image data from MQTT
2024-04-29 16:38:54.402 ERROR (MainThread) [homeassistant.util.logging] Exception in async_message_received when handling msg on 'valetudo/SilentTepidStinkbug/$state': 'ready'
Traceback (most recent call last):
  File "/config/custom_components/valetudo_vacuum_camera/valetudo/MQTT/connector.py", line 326, in async_message_received
    await self.hypfer_handle_connect_state(msg)
  File "/config/custom_components/valetudo_vacuum_camera/valetudo/MQTT/connector.py", line 179, in hypfer_handle_connect_state
    self._payload = msg.payload.decode("utf-8")
                    ^^^^^^^^^^^^^^^^^^
AttributeError: 'str' object has no attribute 'decode'. Did you mean: 'encode'?

2024-04-29 16:38:54.426 ERROR (MainThread) [homeassistant.util.logging] Exception in async_message_received when handling msg on 'valetudo/SilentTepidStinkbug/$state': 'init'
Traceback (most recent call last):
  File "/config/custom_components/valetudo_vacuum_camera/valetudo/MQTT/connector.py", line 326, in async_message_received
    await self.hypfer_handle_connect_state(msg)
  File "/config/custom_components/valetudo_vacuum_camera/valetudo/MQTT/connector.py", line 179, in hypfer_handle_connect_state
    self._payload = msg.payload.decode("utf-8")
                    ^^^^^^^^^^^^^^^^^^
AttributeError: 'str' object has no attribute 'decode'. Did you mean: 'encode'?

2024-04-29 16:38:54.612 ERROR (MainThread) [homeassistant.util.logging] Exception in async_message_received when handling msg on 'valetudo/SilentTepidStinkbug/$state': 'ready'
Traceback (most recent call last):
  File "/config/custom_components/valetudo_vacuum_camera/valetudo/MQTT/connector.py", line 326, in async_message_received
    await self.hypfer_handle_connect_state(msg)
  File "/config/custom_components/valetudo_vacuum_camera/valetudo/MQTT/connector.py", line 179, in hypfer_handle_connect_state
    self._payload = msg.payload.decode("utf-8")
                    ^^^^^^^^^^^^^^^^^^
AttributeError: 'str' object has no attribute 'decode'. Did you mean: 'encode'?

2024-04-29 16:38:54.696 INFO (MainThread) [custom_components.valetudo_vacuum_camera.valetudo.MQTT.connector] silenttepidstinkbug not ready, not connected to MQTT.

Additional information

https://github.com/sca075/valetudo_vacuum_camera/blob/2024.05/custom_components/valetudo_vacuum_camera/valetudo/MQTT/connector.py

    @staticmethod
    async def async_decode_mqtt_payload(msg):
        """Check if we need to decode or not the payload"""
        if isinstance(msg.payload, bytes):
            # If it's binary data, decode it
            payload_str = msg.payload.decode("utf-8")
            return payload_str
        elif isinstance(msg.payload, (int, float)):
            # If it's a number, return it as is
            return msg.payload
        else:
            # If it's already a string or another type, return it as is
            return msg.payload
@home-assistant
Copy link

Hey there @emontnemery, @jbouwh, @bdraco, mind taking a look at this issue as it has been labeled with an integration (mqtt) you are listed as a code owner for? Thanks!

Code owner commands

Code owners of mqtt can trigger bot actions by commenting:

  • @home-assistant close Closes the issue.
  • @home-assistant rename Awesome new title Renames the issue.
  • @home-assistant reopen Reopen the issue.
  • @home-assistant unassign mqtt Removes the current integration label and assignees on the issue, add the integration domain after the command.
  • @home-assistant add-label needs-more-information Add a label (needs-more-information, problem in dependency, problem in custom component) to the issue.
  • @home-assistant remove-label needs-more-information Remove a label (needs-more-information, problem in dependency, problem in custom component) on the issue.

(message by CodeOwnersMention)


mqtt documentation
mqtt source
(message by IssueLinks)

@jbouwh
Copy link
Contributor

jbouwh commented Apr 30, 2024

Nothing changed between the tagged versions of Home Assistant related to encoding.
It seems you are using/wiriting a custom component which we do not support, but I assume you know that.

By default we try to decode incoming payloads as utf-8 on the supported entity platforms. If you do not want that you can either set a custom encoding or set it to None if you want to handle the decoding yourself.

This this signature of API method for async_subscribe:

@bind_hass
async def async_subscribe(
    hass: HomeAssistant,
    topic: str,
    msg_callback: AsyncMessageCallbackType | MessageCallbackType,
    qos: int = DEFAULT_QOS,
    encoding: str | None = DEFAULT_ENCODING,
) -> CALLBACK_TYPE:
    """Subscribe to an MQTT topic.
   ....

@jbouwh jbouwh closed this as not planned Won't fix, can't repro, duplicate, stale Apr 30, 2024
@sca075
Copy link
Author

sca075 commented Apr 30, 2024

Nothing changed between the tagged versions of Home Assistant related to encoding. It seems you are using/wiriting a custom component which we do not support, but I assume you know that.

By default we try to decode incoming payloads as utf-8 on the supported entity platforms. If you do not want that you can either set a custom encoding or set it to None if you want to handle the decoding yourself.

This this signature of API method for async_subscribe:

@bind_hass
async def async_subscribe(
    hass: HomeAssistant,
    topic: str,
    msg_callback: AsyncMessageCallbackType | MessageCallbackType,
    qos: int = DEFAULT_QOS,
    encoding: str | None = DEFAULT_ENCODING,
) -> CALLBACK_TYPE:
    """Subscribe to an MQTT topic.
   ....

Yes it is a custom component @jbouwh, the problem was that I had to implement that function as per sometimes the value is output as binary and sometime as string.. anyhow so far the work around works fine and of course if you do not plan to correct it (and thanks for the clarification) it is fine for me too.

  • Device connected to HA $state=b'Ready'
  • Disconnected ..
  • Reconnect to HA, $state="Ready"
    I think this can create problems also to you guys.

Anyhow NodeRed and other "custom components" later on.. did become add-on or integrations, and as soon I can clean the code as it should be I hope to present it to you guys (so far I test it in the real world and it is not creating problems to the core) as per I try to keep up with your development as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants