mqtt: use component_init for post initalization

Signed-off-by:  Eric Callahan <arksine.code@gmail.com>
This commit is contained in:
Eric Callahan 2021-11-02 12:47:45 -04:00 committed by Eric Callahan
parent fd59ace4f8
commit 799e763050
1 changed files with 14 additions and 17 deletions

View File

@ -229,31 +229,28 @@ class MQTTClient(APITransport, Subscribable):
f"Moonraker API topics - Request: {self.api_request_topic}, " f"Moonraker API topics - Request: {self.api_request_topic}, "
f"Response: {self.api_resp_topic}") f"Response: {self.api_resp_topic}")
self.event_loop.register_callback(self._initialize) async def component_init(self) -> None:
async def _initialize(self) -> None:
# We must wait for the IOLoop (asyncio event loop) to start # We must wait for the IOLoop (asyncio event loop) to start
# prior to retreiving it # prior to retreiving it
self.helper = AIOHelper(self.client) self.helper = AIOHelper(self.client)
if self.user_name is not None: if self.user_name is not None:
self.client.username_pw_set(self.user_name, self.password) self.client.username_pw_set(self.user_name, self.password)
retries = 15 retries = 5
while retries: for _ in range(retries):
try: try:
self.client.connect(self.address, self.port) self.client.connect(self.address, self.port)
except ConnectionRefusedError: except ConnectionRefusedError:
retries -= 1 logging.info("Unable to connect to MQTT broker, "
if retries: f"retries remaining: {retries}")
logging.info("Unable to connect to MQTT broker, " await asyncio.sleep(2.)
f"retries remaining: {retries}") else:
await asyncio.sleep(2.) break
continue else:
self.server.set_failed_component("mqtt") self.server.set_failed_component("mqtt")
self.server.add_warning( self.server.add_warning(
f"MQTT Broker Connection at ({self.address}, {self.port}) " f"MQTT Broker Connection at ({self.address}, {self.port}) "
"refused. Check your client and broker configuration.") "refused. Check your client and broker configuration.")
return return
break
self.client.socket().setsockopt( self.client.socket().setsockopt(
socket.SOL_SOCKET, socket.SO_SNDBUF, 2048) socket.SOL_SOCKET, socket.SO_SNDBUF, 2048)