Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions core/connectors/homeassistant.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
# -*- coding: utf-8 -*-
import json
import time
from kivy.support import install_twisted_reactor

install_twisted_reactor()
from autobahn.twisted.websocket import WebSocketClientProtocol, \
WebSocketClientFactory
from twisted.internet.protocol import ReconnectingClientFactory
from twisted.internet import reactor


Expand Down Expand Up @@ -38,8 +40,7 @@ def onClose(self, was_clean, code, reason):
self.factory.handler.print_message("WebSocket connection closed: {0}".format(reason))
self.factory._proto = None


class HAClientFactory(WebSocketClientFactory):
class HAClientFactory(WebSocketClientFactory, ReconnectingClientFactory):
protocol = HAClientProtocol

def __init__(self, config, handler):
Expand All @@ -51,6 +52,15 @@ def __init__(self, config, handler):
self._proto = None
self.protocol.config = config

def clientConnectionFailed(self, connector, reason):
self.handler.print_message("WebSocket connection failed. Retrying in 5 seconds")
time.sleep(5)
self.retry(connector)

def clientConnectionLost(self, connector, reason):
self.handler.print_message("WebSocket connection lost. Reconnecting")
self.retry(connector)


class HomeAssistant:
def __init__(self, app, config):
Expand Down