add polling option
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
"""Code shared between all platforms."""
|
"""Code shared between all platforms."""
|
||||||
import asyncio
|
import asyncio
|
||||||
import logging
|
import logging
|
||||||
|
from datetime import timedelta
|
||||||
|
|
||||||
from homeassistant.const import (
|
from homeassistant.const import (
|
||||||
CONF_DEVICE_ID,
|
CONF_DEVICE_ID,
|
||||||
@@ -9,8 +10,10 @@ from homeassistant.const import (
|
|||||||
CONF_HOST,
|
CONF_HOST,
|
||||||
CONF_ID,
|
CONF_ID,
|
||||||
CONF_PLATFORM,
|
CONF_PLATFORM,
|
||||||
|
CONF_SCAN_INTERVAL,
|
||||||
)
|
)
|
||||||
from homeassistant.core import callback
|
from homeassistant.core import callback
|
||||||
|
from homeassistant.helpers.event import async_track_time_interval
|
||||||
from homeassistant.helpers.dispatcher import (
|
from homeassistant.helpers.dispatcher import (
|
||||||
async_dispatcher_connect,
|
async_dispatcher_connect,
|
||||||
async_dispatcher_send,
|
async_dispatcher_send,
|
||||||
@@ -116,6 +119,7 @@ class TuyaDevice(pytuya.TuyaListener, pytuya.ContextualLogger):
|
|||||||
self.dps_to_request = {}
|
self.dps_to_request = {}
|
||||||
self._is_closing = False
|
self._is_closing = False
|
||||||
self._connect_task = None
|
self._connect_task = None
|
||||||
|
self._unsub_interval = None
|
||||||
self.set_logger(_LOGGER, config_entry[CONF_DEVICE_ID])
|
self.set_logger(_LOGGER, config_entry[CONF_DEVICE_ID])
|
||||||
|
|
||||||
# This has to be done in case the device type is type_0d
|
# This has to be done in case the device type is type_0d
|
||||||
@@ -151,6 +155,15 @@ class TuyaDevice(pytuya.TuyaListener, pytuya.ContextualLogger):
|
|||||||
raise Exception("Failed to retrieve status")
|
raise Exception("Failed to retrieve status")
|
||||||
|
|
||||||
self.status_updated(status)
|
self.status_updated(status)
|
||||||
|
if (
|
||||||
|
CONF_SCAN_INTERVAL in self._config_entry
|
||||||
|
and self._config_entry[CONF_SCAN_INTERVAL] > 0
|
||||||
|
):
|
||||||
|
self._unsub_interval = async_track_time_interval(
|
||||||
|
self._hass,
|
||||||
|
self._async_refresh,
|
||||||
|
timedelta(seconds=self._config_entry[CONF_SCAN_INTERVAL]),
|
||||||
|
)
|
||||||
except Exception: # pylint: disable=broad-except
|
except Exception: # pylint: disable=broad-except
|
||||||
self.exception(f"Connect to {self._config_entry[CONF_HOST]} failed")
|
self.exception(f"Connect to {self._config_entry[CONF_HOST]} failed")
|
||||||
if self._interface is not None:
|
if self._interface is not None:
|
||||||
@@ -158,6 +171,10 @@ class TuyaDevice(pytuya.TuyaListener, pytuya.ContextualLogger):
|
|||||||
self._interface = None
|
self._interface = None
|
||||||
self._connect_task = None
|
self._connect_task = None
|
||||||
|
|
||||||
|
async def _async_refresh(self, _now):
|
||||||
|
if self._interface is not None:
|
||||||
|
await self._interface.updatedps()
|
||||||
|
|
||||||
async def close(self):
|
async def close(self):
|
||||||
"""Close connection and stop re-connect loop."""
|
"""Close connection and stop re-connect loop."""
|
||||||
self._is_closing = True
|
self._is_closing = True
|
||||||
@@ -204,7 +221,9 @@ class TuyaDevice(pytuya.TuyaListener, pytuya.ContextualLogger):
|
|||||||
"""Device disconnected."""
|
"""Device disconnected."""
|
||||||
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)
|
||||||
|
if self._unsub_interval is not None:
|
||||||
|
self._unsub_interval()
|
||||||
|
self._unsub_interval = None
|
||||||
self._interface = None
|
self._interface = None
|
||||||
self.debug("Disconnected - waiting for discovery broadcast")
|
self.debug("Disconnected - waiting for discovery broadcast")
|
||||||
|
|
||||||
|
@@ -14,6 +14,7 @@ from homeassistant.const import (
|
|||||||
CONF_HOST,
|
CONF_HOST,
|
||||||
CONF_ID,
|
CONF_ID,
|
||||||
CONF_PLATFORM,
|
CONF_PLATFORM,
|
||||||
|
CONF_SCAN_INTERVAL,
|
||||||
)
|
)
|
||||||
from homeassistant.core import callback
|
from homeassistant.core import callback
|
||||||
|
|
||||||
@@ -44,6 +45,7 @@ BASIC_INFO_SCHEMA = vol.Schema(
|
|||||||
vol.Required(CONF_HOST): str,
|
vol.Required(CONF_HOST): str,
|
||||||
vol.Required(CONF_DEVICE_ID): str,
|
vol.Required(CONF_DEVICE_ID): str,
|
||||||
vol.Required(CONF_PROTOCOL_VERSION, default="3.3"): vol.In(["3.1", "3.3"]),
|
vol.Required(CONF_PROTOCOL_VERSION, default="3.3"): vol.In(["3.1", "3.3"]),
|
||||||
|
vol.Optional(CONF_SCAN_INTERVAL): int,
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -55,6 +57,7 @@ DEVICE_SCHEMA = vol.Schema(
|
|||||||
vol.Required(CONF_LOCAL_KEY): cv.string,
|
vol.Required(CONF_LOCAL_KEY): cv.string,
|
||||||
vol.Required(CONF_FRIENDLY_NAME): cv.string,
|
vol.Required(CONF_FRIENDLY_NAME): cv.string,
|
||||||
vol.Required(CONF_PROTOCOL_VERSION, default="3.3"): vol.In(["3.1", "3.3"]),
|
vol.Required(CONF_PROTOCOL_VERSION, default="3.3"): vol.In(["3.1", "3.3"]),
|
||||||
|
vol.Optional(CONF_SCAN_INTERVAL): int,
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -90,6 +93,7 @@ def options_schema(entities):
|
|||||||
vol.Required(CONF_HOST): str,
|
vol.Required(CONF_HOST): str,
|
||||||
vol.Required(CONF_LOCAL_KEY): str,
|
vol.Required(CONF_LOCAL_KEY): str,
|
||||||
vol.Required(CONF_PROTOCOL_VERSION, default="3.3"): vol.In(["3.1", "3.3"]),
|
vol.Required(CONF_PROTOCOL_VERSION, default="3.3"): vol.In(["3.1", "3.3"]),
|
||||||
|
vol.Optional(CONF_SCAN_INTERVAL): int,
|
||||||
vol.Required(
|
vol.Required(
|
||||||
CONF_ENTITIES, description={"suggested_value": entity_names}
|
CONF_ENTITIES, description={"suggested_value": entity_names}
|
||||||
): cv.multi_select(entity_names),
|
): cv.multi_select(entity_names),
|
||||||
|
@@ -382,7 +382,6 @@ class TuyaProtocol(asyncio.Protocol, ContextualLogger):
|
|||||||
while True:
|
while True:
|
||||||
try:
|
try:
|
||||||
await self.heartbeat()
|
await self.heartbeat()
|
||||||
await self.updatedps()
|
|
||||||
await asyncio.sleep(HEARTBEAT_INTERVAL)
|
await asyncio.sleep(HEARTBEAT_INTERVAL)
|
||||||
except asyncio.CancelledError:
|
except asyncio.CancelledError:
|
||||||
self.debug("Stopped heartbeat loop")
|
self.debug("Stopped heartbeat loop")
|
||||||
|
@@ -20,6 +20,7 @@
|
|||||||
"device_id": "Device ID",
|
"device_id": "Device ID",
|
||||||
"local_key": "Local key",
|
"local_key": "Local key",
|
||||||
"protocol_version": "Protocol Version",
|
"protocol_version": "Protocol Version",
|
||||||
|
"scan_interval": "Scan interval in seconds (fill only if the device is not updating automatically)",
|
||||||
"device_type": "Device type"
|
"device_type": "Device type"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
@@ -39,4 +40,4 @@
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
"title": "LocalTuya"
|
"title": "LocalTuya"
|
||||||
}
|
}
|
||||||
|
@@ -29,7 +29,8 @@
|
|||||||
"host": "Host",
|
"host": "Host",
|
||||||
"device_id": "Device ID",
|
"device_id": "Device ID",
|
||||||
"local_key": "Local key",
|
"local_key": "Local key",
|
||||||
"protocol_version": "Protocol Version"
|
"protocol_version": "Protocol Version",
|
||||||
|
"scan_interval": "Scan interval in seconds (fill only if the device is not updating automatically)"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"pick_entity_type": {
|
"pick_entity_type": {
|
||||||
@@ -89,6 +90,7 @@
|
|||||||
"host": "Host",
|
"host": "Host",
|
||||||
"local_key": "Local key",
|
"local_key": "Local key",
|
||||||
"protocol_version": "Protocol Version",
|
"protocol_version": "Protocol Version",
|
||||||
|
"scan_interval": "Scan interval in seconds (fill only if the device is not updating automatically)",
|
||||||
"entities": "Entities (uncheck an entity to remove it)"
|
"entities": "Entities (uncheck an entity to remove it)"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
Reference in New Issue
Block a user