core/homeassistant/components/rainforest_raven/diagnostics.py

42 lines
1.2 KiB
Python
Raw Permalink Normal View History

Add Rainforest RAVEn integration (#80061) * Add Rainforest RAVEn integration * Add Rainforest Automation brand * Add diagnostics to Rainforest RAVEn integration * Drop a test assertion for an undefined behavior * Add DEVICE_NAME test constant * Catch up with reality * Use Platform.SENSOR Co-authored-by: Robert Resch <robert@resch.dev> * Make rainforest_raven translatable * Stop setting device_class on unsupported scenarios * Rename rainforest_raven.data -> rainforest_raven.coordinator * Make _generate_unique_id more reusable * Move device synchronization into third party library * Switch from asyncio_timeout to asyncio.timeout * Ignore non-electric meters Co-authored-by: Robert Resch <robert@resch.dev> * Drop direct dependency on iso4217, bump aioraven * Use RAVEn-specific exceptions * Add timeouts to data updates * Move DeviceInfo generation from Sensor to Coordinator * Store meter macs as strings * Convert to using SelectSelector * Drop test_flow_user_invalid_mac This test isn't necessary now that SelectSelector is used. * Implement PR feedback - Split some long format lines - Simplify meter mac_id extraction in diagnostics - Expose unique_id using an attribute instead of a property - Add a comment about the meters dictionary shallow copy Co-authored-by: Erik Montnemery <erik@montnemery.com> * Simplify mac address redaction Co-authored-by: Joakim Sørensen <ludeeus@ludeeus.dev> * Freeze RAVEnSensorEntityDescription dataclass Co-authored-by: Erik Montnemery <erik@montnemery.com> --------- Co-authored-by: Robert Resch <robert@resch.dev> Co-authored-by: Erik Montnemery <erik@montnemery.com> Co-authored-by: Joakim Sørensen <ludeeus@ludeeus.dev>
2024-01-05 13:00:54 +00:00
"""Diagnostics support for a Rainforest RAVEn device."""
Add Rainforest RAVEn integration (#80061) * Add Rainforest RAVEn integration * Add Rainforest Automation brand * Add diagnostics to Rainforest RAVEn integration * Drop a test assertion for an undefined behavior * Add DEVICE_NAME test constant * Catch up with reality * Use Platform.SENSOR Co-authored-by: Robert Resch <robert@resch.dev> * Make rainforest_raven translatable * Stop setting device_class on unsupported scenarios * Rename rainforest_raven.data -> rainforest_raven.coordinator * Make _generate_unique_id more reusable * Move device synchronization into third party library * Switch from asyncio_timeout to asyncio.timeout * Ignore non-electric meters Co-authored-by: Robert Resch <robert@resch.dev> * Drop direct dependency on iso4217, bump aioraven * Use RAVEn-specific exceptions * Add timeouts to data updates * Move DeviceInfo generation from Sensor to Coordinator * Store meter macs as strings * Convert to using SelectSelector * Drop test_flow_user_invalid_mac This test isn't necessary now that SelectSelector is used. * Implement PR feedback - Split some long format lines - Simplify meter mac_id extraction in diagnostics - Expose unique_id using an attribute instead of a property - Add a comment about the meters dictionary shallow copy Co-authored-by: Erik Montnemery <erik@montnemery.com> * Simplify mac address redaction Co-authored-by: Joakim Sørensen <ludeeus@ludeeus.dev> * Freeze RAVEnSensorEntityDescription dataclass Co-authored-by: Erik Montnemery <erik@montnemery.com> --------- Co-authored-by: Robert Resch <robert@resch.dev> Co-authored-by: Erik Montnemery <erik@montnemery.com> Co-authored-by: Joakim Sørensen <ludeeus@ludeeus.dev>
2024-01-05 13:00:54 +00:00
from __future__ import annotations
from collections.abc import Mapping
from typing import Any
from homeassistant.components.diagnostics import async_redact_data
from homeassistant.const import CONF_MAC
from homeassistant.core import HomeAssistant, callback
from .coordinator import RAVEnConfigEntry
Add Rainforest RAVEn integration (#80061) * Add Rainforest RAVEn integration * Add Rainforest Automation brand * Add diagnostics to Rainforest RAVEn integration * Drop a test assertion for an undefined behavior * Add DEVICE_NAME test constant * Catch up with reality * Use Platform.SENSOR Co-authored-by: Robert Resch <robert@resch.dev> * Make rainforest_raven translatable * Stop setting device_class on unsupported scenarios * Rename rainforest_raven.data -> rainforest_raven.coordinator * Make _generate_unique_id more reusable * Move device synchronization into third party library * Switch from asyncio_timeout to asyncio.timeout * Ignore non-electric meters Co-authored-by: Robert Resch <robert@resch.dev> * Drop direct dependency on iso4217, bump aioraven * Use RAVEn-specific exceptions * Add timeouts to data updates * Move DeviceInfo generation from Sensor to Coordinator * Store meter macs as strings * Convert to using SelectSelector * Drop test_flow_user_invalid_mac This test isn't necessary now that SelectSelector is used. * Implement PR feedback - Split some long format lines - Simplify meter mac_id extraction in diagnostics - Expose unique_id using an attribute instead of a property - Add a comment about the meters dictionary shallow copy Co-authored-by: Erik Montnemery <erik@montnemery.com> * Simplify mac address redaction Co-authored-by: Joakim Sørensen <ludeeus@ludeeus.dev> * Freeze RAVEnSensorEntityDescription dataclass Co-authored-by: Erik Montnemery <erik@montnemery.com> --------- Co-authored-by: Robert Resch <robert@resch.dev> Co-authored-by: Erik Montnemery <erik@montnemery.com> Co-authored-by: Joakim Sørensen <ludeeus@ludeeus.dev>
2024-01-05 13:00:54 +00:00
TO_REDACT_CONFIG = {CONF_MAC}
TO_REDACT_DATA = {"device_mac_id", "meter_mac_id"}
@callback
def async_redact_meter_macs(data: dict) -> dict:
"""Redact meter MAC addresses from mapping keys."""
if not data.get("Meters"):
return data
redacted = {**data, "Meters": {}}
for idx, mac_id in enumerate(data["Meters"]):
redacted["Meters"][f"**REDACTED{idx}**"] = data["Meters"][mac_id]
return redacted
async def async_get_config_entry_diagnostics(
hass: HomeAssistant, config_entry: RAVEnConfigEntry
Add Rainforest RAVEn integration (#80061) * Add Rainforest RAVEn integration * Add Rainforest Automation brand * Add diagnostics to Rainforest RAVEn integration * Drop a test assertion for an undefined behavior * Add DEVICE_NAME test constant * Catch up with reality * Use Platform.SENSOR Co-authored-by: Robert Resch <robert@resch.dev> * Make rainforest_raven translatable * Stop setting device_class on unsupported scenarios * Rename rainforest_raven.data -> rainforest_raven.coordinator * Make _generate_unique_id more reusable * Move device synchronization into third party library * Switch from asyncio_timeout to asyncio.timeout * Ignore non-electric meters Co-authored-by: Robert Resch <robert@resch.dev> * Drop direct dependency on iso4217, bump aioraven * Use RAVEn-specific exceptions * Add timeouts to data updates * Move DeviceInfo generation from Sensor to Coordinator * Store meter macs as strings * Convert to using SelectSelector * Drop test_flow_user_invalid_mac This test isn't necessary now that SelectSelector is used. * Implement PR feedback - Split some long format lines - Simplify meter mac_id extraction in diagnostics - Expose unique_id using an attribute instead of a property - Add a comment about the meters dictionary shallow copy Co-authored-by: Erik Montnemery <erik@montnemery.com> * Simplify mac address redaction Co-authored-by: Joakim Sørensen <ludeeus@ludeeus.dev> * Freeze RAVEnSensorEntityDescription dataclass Co-authored-by: Erik Montnemery <erik@montnemery.com> --------- Co-authored-by: Robert Resch <robert@resch.dev> Co-authored-by: Erik Montnemery <erik@montnemery.com> Co-authored-by: Joakim Sørensen <ludeeus@ludeeus.dev>
2024-01-05 13:00:54 +00:00
) -> Mapping[str, Any]:
"""Return diagnostics for a config entry."""
return {
"config_entry": async_redact_data(config_entry.as_dict(), TO_REDACT_CONFIG),
"data": async_redact_meter_macs(
async_redact_data(config_entry.runtime_data.data, TO_REDACT_DATA)
Add Rainforest RAVEn integration (#80061) * Add Rainforest RAVEn integration * Add Rainforest Automation brand * Add diagnostics to Rainforest RAVEn integration * Drop a test assertion for an undefined behavior * Add DEVICE_NAME test constant * Catch up with reality * Use Platform.SENSOR Co-authored-by: Robert Resch <robert@resch.dev> * Make rainforest_raven translatable * Stop setting device_class on unsupported scenarios * Rename rainforest_raven.data -> rainforest_raven.coordinator * Make _generate_unique_id more reusable * Move device synchronization into third party library * Switch from asyncio_timeout to asyncio.timeout * Ignore non-electric meters Co-authored-by: Robert Resch <robert@resch.dev> * Drop direct dependency on iso4217, bump aioraven * Use RAVEn-specific exceptions * Add timeouts to data updates * Move DeviceInfo generation from Sensor to Coordinator * Store meter macs as strings * Convert to using SelectSelector * Drop test_flow_user_invalid_mac This test isn't necessary now that SelectSelector is used. * Implement PR feedback - Split some long format lines - Simplify meter mac_id extraction in diagnostics - Expose unique_id using an attribute instead of a property - Add a comment about the meters dictionary shallow copy Co-authored-by: Erik Montnemery <erik@montnemery.com> * Simplify mac address redaction Co-authored-by: Joakim Sørensen <ludeeus@ludeeus.dev> * Freeze RAVEnSensorEntityDescription dataclass Co-authored-by: Erik Montnemery <erik@montnemery.com> --------- Co-authored-by: Robert Resch <robert@resch.dev> Co-authored-by: Erik Montnemery <erik@montnemery.com> Co-authored-by: Joakim Sørensen <ludeeus@ludeeus.dev>
2024-01-05 13:00:54 +00:00
),
}