Skip to content

Commit

Permalink
Merge pull request #1227 from rospogrigio/device_debugging_option
Browse files Browse the repository at this point in the history
Device debugging option
  • Loading branch information
rospogrigio committed Jan 10, 2023
2 parents 20ab401 + 1e3b047 commit 1f85551
Show file tree
Hide file tree
Showing 9 changed files with 42 additions and 31 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -164,8 +164,11 @@ logger:
default: warning
logs:
custom_components.localtuya: debug
custom_components.localtuya.pytuya: debug
```
Then, edit the device that is showing problems and check the "Enable debugging for this device" button.
# Notes:
* Do not declare anything as "tuya", such as by initiating a "switch.tuya". Using "tuya" launches Home Assistant's built-in, cloud-based Tuya integration in lieu of localtuya.
Expand Down
2 changes: 2 additions & 0 deletions custom_components/localtuya/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
ATTR_STATE,
ATTR_UPDATED_AT,
CONF_DEFAULT_VALUE,
CONF_ENABLE_DEBUG,
CONF_LOCAL_KEY,
CONF_MODEL,
CONF_PASSIVE_ENTITY,
Expand Down Expand Up @@ -188,6 +189,7 @@ async def _make_connection(self):
self._dev_config_entry[CONF_DEVICE_ID],
self._local_key,
float(self._dev_config_entry[CONF_PROTOCOL_VERSION]),
self._dev_config_entry.get(CONF_ENABLE_DEBUG, False),
self,
)
self._interface.add_dps_to_request(self.dps_to_request)
Expand Down
34 changes: 12 additions & 22 deletions custom_components/localtuya/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
CONF_ADD_DEVICE,
CONF_DPS_STRINGS,
CONF_EDIT_DEVICE,
CONF_ENABLE_DEBUG,
CONF_LOCAL_KEY,
CONF_MANUAL_DPS,
CONF_MODEL,
Expand Down Expand Up @@ -82,30 +83,17 @@
}
)

CONFIGURE_DEVICE_SCHEMA = vol.Schema(
{
vol.Required(CONF_FRIENDLY_NAME): str,
vol.Required(CONF_LOCAL_KEY): str,
vol.Required(CONF_HOST): str,
vol.Required(CONF_DEVICE_ID): str,
vol.Required(CONF_PROTOCOL_VERSION, default="3.3"): vol.In(
["3.1", "3.2", "3.3", "3.4"]
),
vol.Optional(CONF_SCAN_INTERVAL): int,
vol.Optional(CONF_MANUAL_DPS): str,
vol.Optional(CONF_RESET_DPIDS): str,
}
)

DEVICE_SCHEMA = vol.Schema(
{
vol.Required(CONF_FRIENDLY_NAME): cv.string,
vol.Required(CONF_HOST): cv.string,
vol.Required(CONF_DEVICE_ID): cv.string,
vol.Required(CONF_LOCAL_KEY): cv.string,
vol.Required(CONF_FRIENDLY_NAME): cv.string,
vol.Required(CONF_PROTOCOL_VERSION, default="3.3"): vol.In(
["3.1", "3.2", "3.3", "3.4"]
),
vol.Required(CONF_ENABLE_DEBUG, default=False): bool,
vol.Optional(CONF_SCAN_INTERVAL): int,
vol.Optional(CONF_MANUAL_DPS): cv.string,
vol.Optional(CONF_RESET_DPIDS): str,
Expand Down Expand Up @@ -145,15 +133,16 @@ def options_schema(entities):
]
return vol.Schema(
{
vol.Required(CONF_FRIENDLY_NAME): str,
vol.Required(CONF_HOST): str,
vol.Required(CONF_LOCAL_KEY): str,
vol.Required(CONF_FRIENDLY_NAME): cv.string,
vol.Required(CONF_HOST): cv.string,
vol.Required(CONF_LOCAL_KEY): cv.string,
vol.Required(CONF_PROTOCOL_VERSION, default="3.3"): vol.In(
["3.1", "3.2", "3.3", "3.4"]
),
vol.Optional(CONF_SCAN_INTERVAL): int,
vol.Optional(CONF_MANUAL_DPS): str,
vol.Optional(CONF_RESET_DPIDS): str,
vol.Required(CONF_ENABLE_DEBUG, default=False): bool,
vol.Optional(CONF_SCAN_INTERVAL): cv.string,
vol.Optional(CONF_MANUAL_DPS): cv.string,
vol.Optional(CONF_RESET_DPIDS): cv.string,
vol.Required(
CONF_ENTITIES, description={"suggested_value": entity_names}
): cv.multi_select(entity_names),
Expand Down Expand Up @@ -253,6 +242,7 @@ async def validate_input(hass: core.HomeAssistant, data):
data[CONF_DEVICE_ID],
data[CONF_LOCAL_KEY],
float(data[CONF_PROTOCOL_VERSION]),
data[CONF_ENABLE_DEBUG],
)
if CONF_RESET_DPIDS in data:
reset_ids_str = data[CONF_RESET_DPIDS].split(",")
Expand Down Expand Up @@ -622,7 +612,7 @@ async def async_step_configure_device(self, user_input=None):
if dev_id in cloud_devs:
defaults[CONF_LOCAL_KEY] = cloud_devs[dev_id].get(CONF_LOCAL_KEY)
defaults[CONF_FRIENDLY_NAME] = cloud_devs[dev_id].get(CONF_NAME)
schema = schema_defaults(CONFIGURE_DEVICE_SCHEMA, **defaults)
schema = schema_defaults(DEVICE_SCHEMA, **defaults)

placeholders = {"for_device": ""}

Expand Down
1 change: 1 addition & 0 deletions custom_components/localtuya/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

# config flow
CONF_LOCAL_KEY = "local_key"
CONF_ENABLE_DEBUG = "enable_debug"
CONF_PROTOCOL_VERSION = "protocol_version"
CONF_DPS_STRINGS = "dps_strings"
CONF_MODEL = "model"
Expand Down
27 changes: 18 additions & 9 deletions custom_components/localtuya/pytuya/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,13 +233,17 @@ class ContextualLogger:
def __init__(self):
"""Initialize a new ContextualLogger."""
self._logger = None
self._enable_debug = False

def set_logger(self, logger, device_id):
def set_logger(self, logger, device_id, enable_debug=False):
"""Set base logger to use."""
self._enable_debug = enable_debug
self._logger = TuyaLoggingAdapter(logger, {"device_id": device_id})

def debug(self, msg, *args):
"""Debug level log."""
if not self._enable_debug:
return
return self._logger.log(logging.DEBUG, msg, *args)

def info(self, msg, *args):
Expand Down Expand Up @@ -415,15 +419,15 @@ class MessageDispatcher(ContextualLogger):
RESET_SEQNO = -101
SESS_KEY_SEQNO = -102

def __init__(self, dev_id, listener, protocol_version, local_key):
def __init__(self, dev_id, listener, protocol_version, local_key, enable_debug):
"""Initialize a new MessageBuffer."""
super().__init__()
self.buffer = b""
self.listeners = {}
self.listener = listener
self.version = protocol_version
self.local_key = local_key
self.set_logger(_LOGGER, dev_id)
self.set_logger(_LOGGER, dev_id, enable_debug)

def abort(self):
"""Abort all waiting clients."""
Expand Down Expand Up @@ -540,7 +544,9 @@ def disconnected(self):
class TuyaProtocol(asyncio.Protocol, ContextualLogger):
"""Implementation of the Tuya protocol."""

def __init__(self, dev_id, local_key, protocol_version, on_connected, listener):
def __init__(
self, dev_id, local_key, protocol_version, enable_debug, on_connected, listener
):
"""
Initialize a new TuyaInterface.
Expand All @@ -554,7 +560,7 @@ def __init__(self, dev_id, local_key, protocol_version, on_connected, listener):
"""
super().__init__()
self.loop = asyncio.get_running_loop()
self.set_logger(_LOGGER, dev_id)
self.set_logger(_LOGGER, dev_id, enable_debug)
self.id = dev_id
self.local_key = local_key.encode("latin1")
self.real_local_key = self.local_key
Expand All @@ -572,7 +578,7 @@ def __init__(self, dev_id, local_key, protocol_version, on_connected, listener):
self.seqno = 1
self.transport = None
self.listener = weakref.ref(listener)
self.dispatcher = self._setup_dispatcher()
self.dispatcher = self._setup_dispatcher(enable_debug)
self.on_connected = on_connected
self.heartbeater = None
self.dps_cache = {}
Expand Down Expand Up @@ -603,7 +609,7 @@ def error_json(self, number=None, payload=None):

return json.loads('{ "Error":"%s", "Err":"%s", "Payload":%s }' % vals)

def _setup_dispatcher(self):
def _setup_dispatcher(self, enable_debug):
def _status_update(msg):
if msg.seqno > 0:
self.seqno = msg.seqno + 1
Expand All @@ -615,7 +621,9 @@ def _status_update(msg):
if listener is not None:
listener.status_updated(self.dps_cache)

return MessageDispatcher(self.id, _status_update, self.version, self.local_key)
return MessageDispatcher(
self.id, _status_update, self.version, self.local_key, enable_debug
)

def connection_made(self, transport):
"""Did connect to the device."""
Expand Down Expand Up @@ -988,7 +996,6 @@ async def _negotiate_session_key(self):
)

# self.debug("session local nonce: %r remote nonce: %r", self.local_nonce, self.remote_nonce)

rkey_hmac = hmac.new(self.local_key, self.remote_nonce, sha256).digest()
await self.exchange_quick(MessagePayload(SESS_KEY_NEG_FINISH, rkey_hmac), None)

Expand Down Expand Up @@ -1145,6 +1152,7 @@ async def connect(
device_id,
local_key,
protocol_version,
enable_debug,
listener=None,
port=6668,
timeout=5,
Expand All @@ -1157,6 +1165,7 @@ async def connect(
device_id,
local_key,
protocol_version,
enable_debug,
on_connected,
listener or EmptyListener(),
),
Expand Down
1 change: 1 addition & 0 deletions custom_components/localtuya/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@
"device_id": "Device ID",
"local_key": "Local key",
"protocol_version": "Protocol Version",
"enable_debug": "Enable debugging for this device (debug must be enabled also in configuration.yaml)",
"scan_interval": "Scan interval (seconds, only when not updating automatically)",
"entities": "Entities (uncheck an entity to remove it)",
"manual_dps_strings": "Manual DPS to add (separated by commas ',') - used when detection is not working (optional)",
Expand Down
1 change: 1 addition & 0 deletions custom_components/localtuya/translations/it.json
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@
"device_id": "ID del dispositivo",
"local_key": "Chiave locale",
"protocol_version": "Versione del protocollo",
"enable_debug": "Abilita il debugging per questo device (il debug va abilitato anche in configuration.yaml)",
"scan_interval": "Intervallo di scansione (secondi, solo quando non si aggiorna automaticamente)",
"entities": "Entities (deseleziona un'entity per rimuoverla)"
}
Expand Down
1 change: 1 addition & 0 deletions custom_components/localtuya/translations/pt-BR.json
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@
"device_id": "ID do dispositivo",
"local_key": "Local key",
"protocol_version": "Versão do protocolo",
"enable_debug": "Ative a depuração para este dispositivo (a depuração também deve ser ativada em configuration.yaml)",
"scan_interval": "Intervalo de escaneamento (segundos, somente quando não estiver atualizando automaticamente)",
"entities": "Entidades (desmarque uma entidade para removê-la)"
}
Expand Down
3 changes: 3 additions & 0 deletions info.md
Original file line number Diff line number Diff line change
Expand Up @@ -164,8 +164,11 @@ logger:
default: warning
logs:
custom_components.localtuya: debug
custom_components.localtuya.pytuya: debug
```
Then, edit the device that is showing problems and check the "Enable debugging for this device" button.
# Notes:
* Do not declare anything as "tuya", such as by initiating a "switch.tuya". Using "tuya" launches Home Assistant's built-in, cloud-based Tuya integration in lieu of localtuya.
Expand Down

0 comments on commit 1f85551

Please sign in to comment.