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

Object of type MugFirmwareInfo is not JSON serializable #47

Closed
RedEarth1 opened this issue Feb 3, 2024 · 4 comments
Closed

Object of type MugFirmwareInfo is not JSON serializable #47

RedEarth1 opened this issue Feb 3, 2024 · 4 comments

Comments

@RedEarth1
Copy link

  • Home Assistant Version: 2024.1.5
  • Ember Mug Component Version: 0.9.1
  • Which device(s) are you using for Bluetooth (e.g. Bluetooth Dongle / ESPHome Proxy): Bluetooth Dongle

Description

I have MQTT Statestream enabled in Home Assistant, and the Ember Mug Integration is causing errors each time the mug state changes because MugFirmwareInfo is not JSON serializable. I have had to temporarily exclude the mug state in the MQTT statestream config using an entity glob, this prevents the errors.

Logs / Errors

2024-02-03 18:11:12.415 INFO (MainThread) [ember_mug.mug] Subscribe to Push Events
2024-02-03 18:11:19.763 ERROR (MainThread) [homeassistant] Error doing job: Task exception was never retrieved
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/components/mqtt_statestream/__init__.py", line 86, in _state_publisher
    encoded_val = json.dumps(val, cls=JSONEncoder)
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/json/__init__.py", line 238, in dumps
    **kw).encode(obj)
          ^^^^^^^^^^^
  File "/usr/local/lib/python3.11/json/encoder.py", line 200, in encode
    chunks = self.iterencode(o, _one_shot=True)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/json/encoder.py", line 258, in iterencode
    return _iterencode(o, 0)
           ^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/helpers/json.py", line 40, in default
    return json.JSONEncoder.default(self, o)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/json/encoder.py", line 180, in default
    raise TypeError(f'Object of type {o.__class__.__name__} '
TypeError: Object of type MugFirmwareInfo is not JSON serializable
@sopelj
Copy link
Owner

sopelj commented Feb 4, 2024

Hey. That does make sense as the object is not JSON serializable. It's surprising that is just blindly tries to dump the state into a JSON encoder, it could easily be serialized by using todict. Many integrations use dataclasses for passing data, so I'm surprised there aren't problems more often. I'll have a look to see what I can do, but this requires a few changes to both this integration and the base library just to be compatible with this other integration.

@sopelj
Copy link
Owner

sopelj commented Feb 11, 2024

Oh ok, it turns out this isn't too major as the encoder in Home Assistant has some support for custom methods. I think it should work now as of 1.0.0 beta-2.

@RedEarth1
Copy link
Author

Hi, I've tried 1.0.0 beta-2 and I think your fix has solved the problem. The error has stopped appearing in the HA logs and I can now see the mug firmware being reported on the MQTT statestream in MQTT explorer. Thanks for looking into it

@sopelj
Copy link
Owner

sopelj commented Feb 11, 2024

Awesome! Thanks for pointing it out. I'll close this then, but don't hesitate if you have other issues.

@sopelj sopelj closed this as completed Feb 11, 2024
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