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

Including a Unicode character in a JSON string object causes end }} to be removed #157

Closed
404invalid-user opened this issue Dec 29, 2024 · 2 comments

Comments

@404invalid-user
Copy link

example code:

OUTDOOR_TEMP_TOPIC = '/sensor/outdoor'
device = {
    "identifiers": "tempsensor1",
    "name": "TempSensor1",
    "manufacturer": "Raspberry Pi",
    "model": "Pi Pico W",
    "sw_version": "1.0"
}
outdoor_temp_payload = {
    "unique_id":"DHT11_outdoor_front_temperature",
    "name": "Outdoor Temperature",
    "state_topic": OUTDOOR_TEMP_TOPIC+'/state', 
    "unit_of_measurement": "\u2103", #remove this and it will be fine again
    "value_template": "{{ value | float }}",
    "device_class": "temperature",
    "device": device
}

async def descovery_pub(client):
    await client.publish(OUTDOOR_TEMP_TOPIC + "/config", ujson.dumps(outdoor_temp_payload), qos=1, retain=True)
    # confirm its not ujson.dumps
    print(ujson.dumps(outdoor_temp_payload))
    
    # simply adding +"}}" fixes it
    # await client.publish(OUTDOOR_TEMP_TOPIC + "/config", ujson.dumps(outdoor_temp_payload)+"}}", qos=1, retain=True)

look with mqtt explorer shows {"unit_of_measurement": "℃", "name": "Outdoor Temperature", "state_topic": "homeassistant/sensor/tempsensor1/outdoor_temperature/state", "device_class": "temperature", "unique_id": "DHT11_outdoor_front_temperature", "value_template": "{{ value | float }}", "device": {"identifiers": "tempsensor1", "manufacturer": "Raspberry Pi", "name": "TempSensor1", "model": "Pi Pico W", "sw_version": "1.0"

@peterhinch
Copy link
Owner

MQTT publications and subscriptions should be bytes objcts. This is easily fixed using string.encode and bytes.decode:

>>> s = "\u2103"
>>> p = ujson.dumps(s).encode()  # encode the entire json string
>>> p
b'"\xe2\x84\x83"'
>>> ujson.loads(p.decode('utf8'))  # Decode the bytes object from MQTT before running through JSON
'\u2103'

@404invalid-user
Copy link
Author

Ah i see this fixed my issue thank you

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

No branches or pull requests

2 participants