Fix bug when unloading an entry
This commit is contained in:
committed by
rospogrigio
parent
5cd76f87ea
commit
59dfc35759
@@ -56,12 +56,11 @@ import logging
|
|||||||
from homeassistant.config_entries import SOURCE_IMPORT, ConfigEntry
|
from homeassistant.config_entries import SOURCE_IMPORT, ConfigEntry
|
||||||
from homeassistant.core import HomeAssistant, callback
|
from homeassistant.core import HomeAssistant, callback
|
||||||
from homeassistant.const import (
|
from homeassistant.const import (
|
||||||
|
CONF_DEVICE_ID,
|
||||||
CONF_PLATFORM,
|
CONF_PLATFORM,
|
||||||
CONF_ENTITIES,
|
CONF_ENTITIES,
|
||||||
SERVICE_RELOAD,
|
SERVICE_RELOAD,
|
||||||
)
|
)
|
||||||
from homeassistant.helpers.event import async_track_time_interval
|
|
||||||
from homeassistant.helpers.dispatcher import async_dispatcher_send
|
|
||||||
from homeassistant.helpers.reload import async_integration_yaml_config
|
from homeassistant.helpers.reload import async_integration_yaml_config
|
||||||
|
|
||||||
from .const import DOMAIN, TUYA_DEVICE
|
from .const import DOMAIN, TUYA_DEVICE
|
||||||
|
@@ -101,6 +101,7 @@ class TuyaDevice(pytuya.TuyaListener):
|
|||||||
self._interface = None
|
self._interface = None
|
||||||
self._status = {}
|
self._status = {}
|
||||||
self._dps_to_request = {}
|
self._dps_to_request = {}
|
||||||
|
self._is_closing = False
|
||||||
self._connect_task = None
|
self._connect_task = None
|
||||||
self._connection_attempts = 0
|
self._connection_attempts = 0
|
||||||
|
|
||||||
@@ -110,7 +111,7 @@ class TuyaDevice(pytuya.TuyaListener):
|
|||||||
|
|
||||||
def connect(self):
|
def connect(self):
|
||||||
"""Connet to device if not already connected."""
|
"""Connet to device if not already connected."""
|
||||||
if self._connect_task is None or self._interface:
|
if not self._is_closing and self._connect_task is None and not self._interface:
|
||||||
self._connect_task = asyncio.ensure_future(self._make_connection())
|
self._connect_task = asyncio.ensure_future(self._make_connection())
|
||||||
|
|
||||||
async def _make_connection(self):
|
async def _make_connection(self):
|
||||||
@@ -148,8 +149,16 @@ class TuyaDevice(pytuya.TuyaListener):
|
|||||||
self._hass.loop.call_soon(self.connect)
|
self._hass.loop.call_soon(self.connect)
|
||||||
self._connect_task = None
|
self._connect_task = None
|
||||||
|
|
||||||
|
def close(self):
|
||||||
|
"""Close connection and stop re-connect loop."""
|
||||||
|
self._is_closing = True
|
||||||
|
if self._connect_task:
|
||||||
|
self._connect_task.cancel()
|
||||||
|
if self._interface:
|
||||||
|
self._interface.close()
|
||||||
|
|
||||||
async def set_dps(self, state, dps_index):
|
async def set_dps(self, state, dps_index):
|
||||||
"""Change value of a DP of the Tuya device and update the cached status."""
|
"""Change value of a DP of the Tuya device."""
|
||||||
if self._interface is not None:
|
if self._interface is not None:
|
||||||
try:
|
try:
|
||||||
await self._interface.set_dps(state, dps_index)
|
await self._interface.set_dps(state, dps_index)
|
||||||
@@ -172,6 +181,9 @@ class TuyaDevice(pytuya.TuyaListener):
|
|||||||
@callback
|
@callback
|
||||||
def disconnected(self, exc):
|
def disconnected(self, exc):
|
||||||
"""Device disconnected."""
|
"""Device disconnected."""
|
||||||
|
if exc is not None:
|
||||||
|
_LOGGER.error("Disconnected from %: %s", exc)
|
||||||
|
|
||||||
signal = f"localtuya_{self._config_entry[CONF_DEVICE_ID]}"
|
signal = f"localtuya_{self._config_entry[CONF_DEVICE_ID]}"
|
||||||
async_dispatcher_send(self._hass, signal, None)
|
async_dispatcher_send(self._hass, signal, None)
|
||||||
|
|
||||||
|
@@ -359,7 +359,11 @@ class TuyaProtocol(asyncio.Protocol):
|
|||||||
dev_type = self.dev_type
|
dev_type = self.dev_type
|
||||||
|
|
||||||
# Wait for special sequence number if heartbeat
|
# Wait for special sequence number if heartbeat
|
||||||
seqno = MessageDispatcher.HEARTBEAT_SEQNO if command == HEARTBEAT else (self.seqno - 1)
|
seqno = (
|
||||||
|
MessageDispatcher.HEARTBEAT_SEQNO
|
||||||
|
if command == HEARTBEAT
|
||||||
|
else (self.seqno - 1)
|
||||||
|
)
|
||||||
|
|
||||||
self.transport.write(payload)
|
self.transport.write(payload)
|
||||||
msg = await self.dispatcher.wait_for(seqno)
|
msg = await self.dispatcher.wait_for(seqno)
|
||||||
@@ -387,8 +391,6 @@ class TuyaProtocol(asyncio.Protocol):
|
|||||||
|
|
||||||
async def heartbeat(self):
|
async def heartbeat(self):
|
||||||
"""Send a heartbeat message."""
|
"""Send a heartbeat message."""
|
||||||
# We don't expect a response to this, just send blindly
|
|
||||||
#self.transport.write(self._generate_payload(HEARTBEAT))
|
|
||||||
return await self.exchange(HEARTBEAT)
|
return await self.exchange(HEARTBEAT)
|
||||||
|
|
||||||
async def set_dps(self, value, dps_index):
|
async def set_dps(self, value, dps_index):
|
||||||
|
Reference in New Issue
Block a user