Skip to content

Commit

Permalink
Really fixed issue ClusterM#5 for config flow (blocking call inside t…
Browse files Browse the repository at this point in the history
…he event loop)
  • Loading branch information
ClusterM committed Feb 10, 2022
1 parent e4e96b5 commit 482095e
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
8 changes: 5 additions & 3 deletions custom_components/skykettle/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,12 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry):

async def poll(now, **kwargs) -> None:
await kettle.update()
async_dispatcher_send(hass, DISPATCHER_UPDATE)
await hass.async_add_executor_job(async_dispatcher_send, hass, DISPATCHER_UPDATE)
if hass.data[DOMAIN][DATA_WORKING]:
hass.data[DOMAIN][DATA_CANCEL] = ev.async_call_later(
hass, timedelta(seconds=entry.data[CONF_SCAN_INTERVAL]), poll)
await hass.async_add_executor_job(schedule_poll)

def schedule_poll():
hass.data[DOMAIN][DATA_CANCEL] = ev.async_call_later(hass, timedelta(seconds=entry.data[CONF_SCAN_INTERVAL]), poll)

hass.data[DOMAIN][DATA_WORKING] = True
hass.data[DOMAIN][DATA_CANCEL] = ev.async_call_later(
Expand Down
11 changes: 7 additions & 4 deletions custom_components/skykettle/kettle_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,13 @@ async def _sendline(self, data):
if self.hass == None:
self._child.sendline(data)
else:
await self.hass.async_add_job(self._child.sendline, data)
await self.hass.async_add_executor_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)
await self.hass.async_add_executor_job(self._child.sendcontrol, data)

async def command(self, command, params=[]):
if self._disposed:
Expand All @@ -70,7 +70,7 @@ async def command(self, command, params=[]):
_LOGGER.debug(f"Writing command {command:02x}, data: [{' '.join([f'{c:02x}' for c in params])}]")
data = f"char-write-req 0x000e 55{self._iter:02x}{''.join([f'{c:02x}' for c in [command] + list(params)])}aa"
#_LOGGER.debug(f"Writing {data}")
self._child.sendline(data)
await self._sendline(data)
while True:
r = await self._child.expect([
r"value:([ 0-9a-f]*)\r\n.*?\[LE\]> ",
Expand Down Expand Up @@ -339,7 +339,10 @@ def stop(self):
_LOGGER.info("Disposed.")

def __del__(self):
self.stop()
if self.hass == None:
self.stop()
else:
self.hass.async_add_executor_job(self.stop)

@property
def available(self):
Expand Down

0 comments on commit 482095e

Please sign in to comment.