Skip to content
This repository has been archived by the owner on Sep 13, 2022. It is now read-only.

Commit

Permalink
Fix the issue with high version websocket([#1](#1))
Browse files Browse the repository at this point in the history
  • Loading branch information
georgezhao2010 authored Jun 28, 2021
1 parent 077aee0 commit c4c3650
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 21 deletions.
1 change: 0 additions & 1 deletion custom_components/climate_ewelink/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
from .statemanager import StateManager

_LOGGER = logging.getLogger(__name__)
_LOGGER.setLevel(logging.DEBUG)


async def async_setup(hass: HomeAssistant, hass_config: dict):
Expand Down
1 change: 0 additions & 1 deletion custom_components/climate_ewelink/ac_entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import logging

_LOGGER = logging.getLogger(__name__)
_LOGGER.setLevel(logging.DEBUG)

AC_ENTITIES = {
"climate": {
Expand Down
1 change: 0 additions & 1 deletion custom_components/climate_ewelink/climate.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@


_LOGGER = logging.getLogger(__name__)
_LOGGER.setLevel(logging.DEBUG)


async def async_setup_entry(hass, config_entry, async_add_entities):
Expand Down
1 change: 0 additions & 1 deletion custom_components/climate_ewelink/ewelinkcloud.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
from aiohttp import ClientSession

_LOGGER = logging.getLogger(__name__)
_LOGGER.setLevel(logging.DEBUG)


def update_payload(payload):
Expand Down
1 change: 0 additions & 1 deletion custom_components/climate_ewelink/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import logging

_LOGGER = logging.getLogger(__name__)
_LOGGER.setLevel(logging.DEBUG)


async def async_setup_entry(hass, config_entry, async_add_entities):
Expand Down
55 changes: 40 additions & 15 deletions custom_components/climate_ewelink/statemanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,28 @@
from .ewelinkcloud import EWeLinkCloud

_LOGGER = logging.getLogger(__name__)
_LOGGER.setLevel(logging.DEBUG)


class WebsocketNotOnlineException(Exception):
pass


_STATE_MANAGER = None


def on_open(ws):
_LOGGER.warning(f"WebSocket connection established, send userOnline")
_STATE_MANAGER.send_user_online()


def on_message(ws, message):
_STATE_MANAGER.process_message(message)


class StateManager(threading.Thread):
def __init__(self, ewelink_cloud: EWeLinkCloud):
super().__init__()
self._lock = threading.Lock()
self._ws = None
self._url = None
self._devices = {}
Expand All @@ -26,6 +38,16 @@ def __init__(self, ewelink_cloud: EWeLinkCloud):
self._apikey = None
self._last_ts = 0
self._device_updates = {}
global _STATE_MANAGER
_STATE_MANAGER = self

@property
def token(self):
return self._token

@property
def apikey(self):
return self._apikey

def update_device(self, deviceid, params):
if deviceid in self._device_updates:
Expand All @@ -34,7 +56,7 @@ def update_device(self, deviceid, params):
for update_state in updates:
update_state(params)

def on_open(self):
def send_user_online(self):
ts = time.time()
payload = {
'action': 'userOnline',
Expand All @@ -49,7 +71,8 @@ def on_open(self):
}
self.send_json(payload)

def on_message(self, message):
def process_message(self, message):
_LOGGER.debug(f"Received message {message}")
data = json.loads(message)
if data:
if "error" in data:
Expand All @@ -67,14 +90,16 @@ def on_message(self, message):
and "deviceid" in data and "params" in data:
self.update_device(data["deviceid"], data["params"])

def send_json(self, jsondata):
message = json.dumps(jsondata)
if self._ws:
self._ws.send(message)

def send_query(self, deviceid):
self.send_payload(deviceid, {"_query": 1})

def send_json(self, jsondata):
message = json.dumps(jsondata)
_LOGGER.debug(f"Send message {message}")
with self._lock:
if self._ws is not None:
self._ws.send(message)

def send_payload(self, deviceid, data):
while time.time() - self._last_ts < 0.1:
time.sleep(0.1)
Expand Down Expand Up @@ -112,17 +137,17 @@ def run(self):
self._token = self._ewelink_cloud.token
self._apikey = self._ewelink_cloud.apikey
else:
_LOGGER.debug("could not login to eWeLink cloud, retry after 30 seconds")
_LOGGER.warning("Could not login to eWeLink cloud, retry after 30 seconds")
time.sleep(30)

self._ws = websocket.WebSocketApp(self._url,
on_open=self.on_open, on_message=self.on_message)
with self._lock:
self._ws = websocket.WebSocketApp(self._url, on_open=on_open, on_message=on_message)
self._ws.keep_running = True
threading.Thread(target=self._ws.run_forever(ping_interval=145, ping_timeout=5))
_LOGGER.debug("websocket disconnected, retrying")
self._ws.close()
self._ws = None
_LOGGER.warning("WebSocket disconnected, retrying")
self._url = None
with self._lock:
self._ws.close()
self._ws = None

def start_keep_alive(self):
threading.Thread.start(self)
Expand Down
1 change: 0 additions & 1 deletion custom_components/climate_ewelink/switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import logging

_LOGGER = logging.getLogger(__name__)
_LOGGER.setLevel(logging.DEBUG)


async def async_setup_entry(hass, config_entry, async_add_entities):
Expand Down

0 comments on commit c4c3650

Please sign in to comment.