Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/ClusterM/skykettle-ha.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlexey 'Cluster' Avdyukhin <clusterrr@clusterrr.com>2022-02-08 23:29:57 +0300
committerAlexey 'Cluster' Avdyukhin <clusterrr@clusterrr.com>2022-02-08 23:29:57 +0300
commit8bd3268c507a115f398d4aa541bc32678fa6b022 (patch)
tree4a1f8ea4dd051fbc99758ab18101d1632fe69c03
parent584a157473339a7cc99a5f0572390fa2e99e322c (diff)
Fixed issue #5 (blocking call inside the event loop)
-rw-r--r--custom_components/skykettle/__init__.py7
-rw-r--r--custom_components/skykettle/config_flow.py6
-rw-r--r--custom_components/skykettle/kettle_connection.py23
3 files changed, 25 insertions, 11 deletions
diff --git a/custom_components/skykettle/__init__.py b/custom_components/skykettle/__init__.py
index f442c92..f229c56 100644
--- a/custom_components/skykettle/__init__.py
+++ b/custom_components/skykettle/__init__.py
@@ -29,9 +29,10 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry):
if entry.entry_id not in hass.data: hass.data[DOMAIN][entry.entry_id] = {}
kettle = KettleConnection(
- entry.data[CONF_MAC],
- entry.data[CONF_PASSWORD],
- entry.data[CONF_PERSISTENT_CONNECTION]
+ mac=entry.data[CONF_MAC],
+ key=entry.data[CONF_PASSWORD],
+ persistent=entry.data[CONF_PERSISTENT_CONNECTION],
+ hass=hass
)
hass.data[DOMAIN][entry.entry_id][DATA_CONNECTION] = kettle
diff --git a/custom_components/skykettle/config_flow.py b/custom_components/skykettle/config_flow.py
index fd16857..d594837 100644
--- a/custom_components/skykettle/config_flow.py
+++ b/custom_components/skykettle/config_flow.py
@@ -126,9 +126,9 @@ class SkyKettleConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
"""Handle the connect step."""
errors = {}
kettle = KettleConnection(
- self.config[CONF_MAC],
- self.config[CONF_PASSWORD],
- persistent=True
+ mac=self.config[CONF_MAC],
+ key=self.config[CONF_PASSWORD],
+ persistent=True
)
tries = 3
while tries > 0 and not kettle._last_connect_ok:
diff --git a/custom_components/skykettle/kettle_connection.py b/custom_components/skykettle/kettle_connection.py
index 399c231..9b04afa 100644
--- a/custom_components/skykettle/kettle_connection.py
+++ b/custom_components/skykettle/kettle_connection.py
@@ -22,11 +22,12 @@ class KettleConnection(SkyKettle):
STATS_INTERVAL = 15
TARGET_TTL = 30
- def __init__(self, mac, key, persistent=True):
+ def __init__(self, mac, key, persistent=True, hass=None):
super().__init__()
self._child = None
self._mac = mac
self._key = key
+ self.hass = hass
self.persistent = persistent
self._connected = False
self._auth_ok = False
@@ -48,6 +49,18 @@ class KettleConnection(SkyKettle):
self._colors = {}
self._disposed = False
+ async def _sendline(self, data):
+ if self.hass == None:
+ self._child.sendline(data)
+ else:
+ await self.hass.async_add_job(self._child.sendline, data)
+
+ async def _sendcontrol(self, data):
+ if self.hass == None:
+ self._child.sendcontrol(data)
+ else:
+ await self.hass.async_add_job(self._child.sendcontrol, data)
+
async def command(self, command, params=[]):
if self._disposed:
raise DisposedError()
@@ -86,11 +99,11 @@ class KettleConnection(SkyKettle):
self._child = pexpect.spawn("gatttool", ['-I', '-t', 'random', '-b', self._mac], timeout=KettleConnection.BLE_TIMEOUT)
await self._child.expect(r"\[LE\]> ", async_=True)
_LOGGER.debug("Started gatttool")
- self._child.sendline(f"connect")
+ await self._sendline(f"connect")
await self._child.expect(r"Attempting to connect.*?\[LE\]> ", async_=True)
_LOGGER.debug("Attempting to connect...")
await self._child.expect(r"Connection successful.*?\[LE\]> ", async_=True)
- self._child.sendline("char-write-cmd 0x000c 0100")
+ await self._sendline("char-write-cmd 0x000c 0100")
await self._child.expect(r"\[LE\]> ", async_=True)
_LOGGER.debug("Connected to the Kettle")
@@ -99,7 +112,7 @@ class KettleConnection(SkyKettle):
async def _disconnect(self):
try:
if self._child and self._child.isalive():
- self._child.sendline(f"disconnect")
+ await self._sendline(f"disconnect")
await self._child.expect(r"\[LE\]> ", async_=True)
if self._connected:
_LOGGER.debug("Disconnected")
@@ -111,7 +124,7 @@ class KettleConnection(SkyKettle):
try:
if self._child and self._child.isalive():
try:
- self._child.sendcontrol('d')
+ await self._sendcontrol('d')
timeout = 1
while self._child.isalive():
await asyncio.sleep(0.025)