Home / Resolving Home Assistant upgrade errors in custom Python Components
Home Assistant Integrations

Resolving Home Assistant upgrade errors in custom Python Components

Upgrading to Home Assistant v0.69.0 broke some of my custom components that use the MQTT Python module. The reason for this is a small change in the module API. This post explains how to fix your custom components to work with the latest version of Home Assistant!

The error I received is shown below for searchability:

2018-07-17T09:47:48.877623459Z 2018-07-17 09:47:48 ERROR (MainThread) [homeassistant.setup] Error during setup of component mqtt_interpreter
2018-07-17T09:47:48.878074501Z Traceback (most recent call last):
2018-07-17T09:47:48.878298824Z   File "/usr/lib/python3.6/site-packages/homeassistant/setup.py", line 146, in _async_setup_component
2018-07-17T09:47:48.878375699Z     component.setup, hass, processed_config)  # type: ignore
2018-07-17T09:47:48.878439396Z   File "/usr/lib/python3.6/concurrent/futures/thread.py", line 56, in run
2018-07-17T09:47:48.878566792Z     result = self.fn(*self.args, **self.kwargs)
2018-07-17T09:47:48.879028980Z   File "/config/custom_components/mqtt_interpreter.py", line 20, in setup
2018-07-17T09:47:48.879485750Z     mqtt = loader.get_component('mqtt')
2018-07-17T09:47:48.879743979Z TypeError: get_component() missing 1 required positional argument: 'comp_or_platform'

It indicates there is a problem with the mqtt import statement.

mqtt = loader.get_component('mqtt')

Change that line to:

mqtt = hass.components.mqtt

This requires an additinal change. We no longer have to pass hass as the first parameter into our calls to mqtt.subscribe and mqtt.publish.

mqtt.subscribe(topic_string, callback_function)
mqtt.publish(topic_string, payload_string)

If you don't update your method calls you might get a homeassistant.exceptions.HomeAssistantError: topic needs to be a string! error as shown below:

2018-07-17T09:52:03.297486025Z 2018-07-17 09:52:03 ERROR (MainThread) [homeassistant.setup] Error during setup of component mqtt_interpreter
2018-07-17T09:52:03.297672535Z Traceback (most recent call last):
2018-07-17T09:52:03.297740295Z   File "/usr/lib/python3.6/site-packages/homeassistant/setup.py", line 146, in _async_setup_component
2018-07-17T09:52:03.297805035Z     component.setup, hass, processed_config)  # type: ignore
2018-07-17T09:52:03.297866181Z   File "/usr/lib/python3.6/concurrent/futures/thread.py", line 56, in run
2018-07-17T09:52:03.298022222Z     result = self.fn(*self.args, **self.kwargs)
2018-07-17T09:52:03.298085972Z   File "/config/custom_components/mqtt_interpreter.py", line 65, in setup
2018-07-17T09:52:03.298148629Z     mqtt.subscribe(hass, topic, message_received)
2018-07-17T09:52:03.298208576Z   File "/usr/lib/python3.6/site-packages/homeassistant/components/mqtt/__init__.py", line 283, in subscribe
2018-07-17T09:52:03.298271285Z     async_subscribe(hass, topic, msg_callback, qos, encoding), hass.loop
2018-07-17T09:52:03.298331285Z   File "/usr/lib/python3.6/concurrent/futures/_base.py", line 432, in result
2018-07-17T09:52:03.298392587Z     return self.__get_result()
2018-07-17T09:52:03.298451285Z   File "/usr/lib/python3.6/concurrent/futures/_base.py", line 384, in __get_result
2018-07-17T09:52:03.298512170Z     raise self._exception
2018-07-17T09:52:03.298570868Z   File "/usr/lib/python3.6/site-packages/homeassistant/components/mqtt/__init__.py", line 273, in async_subscribe
2018-07-17T09:52:03.298633264Z     topic, msg_callback, qos, encoding)
2018-07-17T09:52:03.298692378Z   File "/usr/lib/python3.6/site-packages/homeassistant/components/mqtt/__init__.py", line 586, in async_subscribe
2018-07-17T09:52:03.298754826Z     raise HomeAssistantError("topic needs to be a string!")
2018-07-17T09:52:03.298814826Z homeassistant.exceptions.HomeAssistantError: topic needs to be a string!

Continue your adventure here

Leave a Comment

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More

DON’T MISS OUT!
Subscribe To Newsletter
Be the first to get latest updates and exclusive content straight to your email inbox.
Stay Updated
Give it a try, you can unsubscribe anytime.
close-link