-
-
Notifications
You must be signed in to change notification settings - Fork 37.1k
Add diagnostics support to Tuya #64374
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,156 @@ | ||
| """Diagnostics support for Tuya.""" | ||
| from __future__ import annotations | ||
|
|
||
| from contextlib import suppress | ||
| import json | ||
| from typing import Any, cast | ||
|
|
||
| from tuya_iot import TuyaDevice | ||
|
|
||
| from homeassistant.config_entries import ConfigEntry | ||
| from homeassistant.core import HomeAssistant, callback | ||
| from homeassistant.helpers import device_registry as dr, entity_registry as er | ||
| from homeassistant.util import dt as dt_util | ||
|
|
||
| from . import HomeAssistantTuyaData | ||
| from .const import ( | ||
| CONF_APP_TYPE, | ||
| CONF_AUTH_TYPE, | ||
| CONF_COUNTRY_CODE, | ||
| CONF_ENDPOINT, | ||
| DOMAIN, | ||
| DPCode, | ||
| ) | ||
|
|
||
|
|
||
| async def async_get_config_entry_diagnostics( | ||
| hass: HomeAssistant, entry: ConfigEntry | ||
| ) -> dict[str, Any]: | ||
| """Return diagnostics for a config entry.""" | ||
| hass_data: HomeAssistantTuyaData = hass.data[DOMAIN][entry.entry_id] | ||
|
|
||
| mqtt_connected = None | ||
| if hass_data.home_manager.mq.client: | ||
| mqtt_connected = hass_data.home_manager.mq.client.is_connected() | ||
|
|
||
| return { | ||
| "endpoint": entry.data[CONF_ENDPOINT], | ||
| "auth_type": entry.data[CONF_AUTH_TYPE], | ||
| "country_code": entry.data[CONF_COUNTRY_CODE], | ||
| "app_type": entry.data[CONF_APP_TYPE], | ||
| "mqtt_connected": mqtt_connected, | ||
| "disabled_by": entry.disabled_by, | ||
| "disabled_polling": entry.pref_disable_polling, | ||
| "devices": [ | ||
| _async_device_as_dict(hass, device) | ||
| for device in hass_data.device_manager.device_map.values() | ||
| ], | ||
| } | ||
|
|
||
|
|
||
| @callback | ||
| def _async_device_as_dict(hass: HomeAssistant, device: TuyaDevice) -> dict[str, Any]: | ||
| """Represent a Tuya device as a dictionary.""" | ||
|
|
||
| # Base device information, without sensitive information. | ||
| data = { | ||
| "name": device.model, | ||
| "model": device.model, | ||
| "category": device.category, | ||
| "product_id": device.product_id, | ||
| "product_name": device.product_name, | ||
| "online": device.online, | ||
| "sub": device.sub, | ||
| "time_zone": device.time_zone, | ||
| "active_time": dt_util.utc_from_timestamp(device.active_time).isoformat(), | ||
| "create_time": dt_util.utc_from_timestamp(device.create_time).isoformat(), | ||
| "update_time": dt_util.utc_from_timestamp(device.update_time).isoformat(), | ||
| "function": {}, | ||
| "status_range": {}, | ||
| "status": {}, | ||
| "home_assistant": {}, | ||
| } | ||
|
|
||
| # Gather Tuya states | ||
| for dpcode, value in device.status.items(): | ||
| # These statuses may contain sensitive information, redact these.. | ||
| if dpcode in {DPCode.ALARM_MESSAGE, DPCode.MOVEMENT_DETECT_PIC}: | ||
| data["status"][dpcode] = "**REDACTED**" | ||
| continue | ||
|
|
||
| with suppress(ValueError, TypeError): | ||
| value = json.loads(value) | ||
| data["status"][dpcode] = value | ||
|
|
||
| # Gather Tuya functions | ||
| for function in device.function.values(): | ||
| value = function.values | ||
| with suppress(ValueError, TypeError, AttributeError): | ||
| value = json.loads(cast(str, function.values)) | ||
|
|
||
| data["function"][function.code] = { | ||
| "type": function.type, | ||
| "value": value, | ||
| } | ||
|
|
||
| # Gather Tuya status ranges | ||
| for status_range in device.status_range.values(): | ||
| value = status_range.values | ||
| with suppress(ValueError, TypeError, AttributeError): | ||
| value = json.loads(status_range.values) | ||
|
|
||
| data["status_range"][status_range.code] = { | ||
| "type": status_range.type, | ||
| "value": value, | ||
| } | ||
|
|
||
| # Gather information how this Tuya device is represented in Home Assistant | ||
| device_registry = dr.async_get(hass) | ||
| entity_registry = er.async_get(hass) | ||
| hass_device = device_registry.async_get_device(identifiers={(DOMAIN, device.id)}) | ||
| if hass_device: | ||
| data["home_assistant"] = { | ||
| "name": hass_device.name, | ||
| "name_by_user": hass_device.name_by_user, | ||
| "disabled": hass_device.disabled, | ||
| "disabled_by": hass_device.disabled_by, | ||
| "entities": [], | ||
| } | ||
|
|
||
| hass_entities = er.async_entries_for_device( | ||
| entity_registry, | ||
| device_id=hass_device.id, | ||
| include_disabled_entities=True, | ||
| ) | ||
|
|
||
| for entity_entry in hass_entities: | ||
| state = hass.states.get(entity_entry.entity_id) | ||
| state_dict = None | ||
| if state: | ||
| state_dict = state.as_dict() | ||
|
|
||
| # Redact the `entity_picture` attribute as it contains a token. | ||
| if "entity_picture" in state_dict["attributes"]: | ||
| state_dict["attributes"] = { | ||
| **state_dict["attributes"], | ||
| "entity_picture": "**REDACTED**", | ||
| } | ||
|
|
||
| # The context doesn't provide useful information in this case. | ||
| state_dict.pop("context", None) | ||
|
|
||
| data["home_assistant"]["entities"].append( | ||
| { | ||
| "disabled": entity_entry.disabled, | ||
| "disabled_by": entity_entry.disabled_by, | ||
| "entity_category": entity_entry.entity_category, | ||
| "device_class": entity_entry.device_class, | ||
| "original_device_class": entity_entry.original_device_class, | ||
| "icon": entity_entry.icon, | ||
| "original_icon": entity_entry.original_icon, | ||
| "unit_of_measurement": entity_entry.unit_of_measurement, | ||
| "state": state_dict, | ||
| } | ||
| ) | ||
|
|
||
| return data | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You're mutating the source dictionary. Make a copy instead.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oh nevermind, that's the output.