HA (Home Assistant) and umqtt.simple #19751
shariltumin
started this conversation in
General
Replies: 2 comments
|
Before 1.29.0 the code would have crashed, but now Unicode is available I agree that the modules will need to take this into account. Your proposal seem reasonable The doc change to only use bytes then can be updated to refer only to older versions |
0 replies
|
One workaround is to provide already encoded bytes. Instead of encoding a from umqtt.simple import MQTTClient
client = MQTTClient("Test-1", "127.0.0.1")
client.connect()
TOPIC = b'\xc3\x9cbererde:'
DATA_FMT = b"%.2f \xc2\xb0C"
# old style c formatting
# In CPython the format method does not exist for str
# Mandela effect?
# opening client connection etc...
# then publish
client.publish(TOPIC, DATA_FMT % 12.42)Output: Too cold. |
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
I'm just sharing my experience.
Consider this test script
test_1.pyRunning it on an ESP32
Notice the difference in the
lenvalues.This gave me problems when trying to register an "MQTT device" with my HA (Home Assistant) server using HA discovery by publishing its configuration in a
json.dumps(config)string. The config has{ "unit_of_measurement": "°C", }among other attributes.The
publishmethod in theMQTTClientof theumqtt.simplelibrary has these intial lines of code:If the
msgis a string as exemplified above the calculatedszwill be incorrect.I know, the
example_pub.pyinmicropython/lib/micropython-lib/micropython/umqtt.simplshows (to those who are willing to read it) that one should use bytes values:for both topic and msg, but it is so easy to overlook.
I suggest we bring the conversion in the method like so:
with the above fix in place, I can confidently do:
It took me some time to identify the problem. I have now managed to register my sensor with my HA server.
All reactions