From e3900f0c0aedca3c3ee55f99d625efc51b8187c6 Mon Sep 17 00:00:00 2001 From: Chris Talkington Date: Sun, 23 Jan 2022 16:38:07 -0600 Subject: [PATCH] Add diagnostics support to roku (#64729) --- homeassistant/components/roku/diagnostics.py | 27 ++++++ .../roku/fixtures/roku3-diagnostics-data.json | 85 +++++++++++++++++++ tests/components/roku/test_diagnostics.py | 38 +++++++++ 3 files changed, 150 insertions(+) create mode 100644 homeassistant/components/roku/diagnostics.py create mode 100644 tests/components/roku/fixtures/roku3-diagnostics-data.json create mode 100644 tests/components/roku/test_diagnostics.py diff --git a/homeassistant/components/roku/diagnostics.py b/homeassistant/components/roku/diagnostics.py new file mode 100644 index 00000000000..bdd32f251ea --- /dev/null +++ b/homeassistant/components/roku/diagnostics.py @@ -0,0 +1,27 @@ +"""Diagnostics support for Roku.""" +from __future__ import annotations + +from typing import Any + +from homeassistant.config_entries import ConfigEntry +from homeassistant.core import HomeAssistant + +from .const import DOMAIN +from .coordinator import RokuDataUpdateCoordinator + + +async def async_get_config_entry_diagnostics( + hass: HomeAssistant, config_entry: ConfigEntry +) -> dict[str, Any]: + """Return diagnostics for a config entry.""" + coordinator: RokuDataUpdateCoordinator = hass.data[DOMAIN][config_entry.entry_id] + + return { + "entry": { + "data": { + **config_entry.data, + }, + "unique_id": config_entry.unique_id, + }, + "data": coordinator.data.as_dict(), + } diff --git a/tests/components/roku/fixtures/roku3-diagnostics-data.json b/tests/components/roku/fixtures/roku3-diagnostics-data.json new file mode 100644 index 00000000000..a63664fa96e --- /dev/null +++ b/tests/components/roku/fixtures/roku3-diagnostics-data.json @@ -0,0 +1,85 @@ +{ + "app": { + "app_id": null, + "name": "Roku", + "screensaver": false, + "version": null + }, + "apps": [ + { + "app_id": "11", + "name": "Roku Channel Store", + "screensaver": false, + "version": null + }, + { + "app_id": "12", + "name": "Netflix", + "screensaver": false, + "version": null + }, + { + "app_id": "13", + "name": "Amazon Video on Demand", + "screensaver": false, + "version": null + }, + { + "app_id": "14", + "name": "MLB.TVĀ®", + "screensaver": false, + "version": null + }, + { + "app_id": "26", + "name": "Free FrameChannel Service", + "screensaver": false, + "version": null + }, + { + "app_id": "27", + "name": "Mediafly", + "screensaver": false, + "version": null + }, + { + "app_id": "28", + "name": "Pandora", + "screensaver": false, + "version": null + }, + { + "app_id": "74519", + "name": "Pluto TV - It's Free TV", + "screensaver": false, + "version": "5.2.0" + } + ], + "channel": null, + "channels": [], + "info": { + "brand": "Roku", + "device_location": null, + "device_type": "box", + "ethernet_mac": "b0:a7:37:96:4d:fa", + "ethernet_support": true, + "headphones_connected": false, + "model_name": "Roku 3", + "model_number": "4200X", + "name": "My Roku 3", + "network_name": null, + "network_type": "ethernet", + "serial_number": "1GU48T017973", + "supports_airplay": false, + "supports_find_remote": false, + "supports_private_listening": false, + "version": "7.5.0", + "wifi_mac": "b0:a7:37:96:4d:fb" + }, + "media": null, + "state": { + "at": "2022-01-23T21:05:03.154737", + "available": true, + "standby": false + } +} diff --git a/tests/components/roku/test_diagnostics.py b/tests/components/roku/test_diagnostics.py new file mode 100644 index 00000000000..d4e36e2a9a9 --- /dev/null +++ b/tests/components/roku/test_diagnostics.py @@ -0,0 +1,38 @@ +"""Tests for the diagnostics data provided by the Roku integration.""" +import json + +from aiohttp import ClientSession + +from homeassistant.core import HomeAssistant + +from tests.common import MockConfigEntry, load_fixture +from tests.components.diagnostics import get_diagnostics_for_config_entry + + +async def test_diagnostics( + hass: HomeAssistant, + hass_client: ClientSession, + init_integration: MockConfigEntry, +): + """Test diagnostics for config entry.""" + diagnostics_data = json.loads(load_fixture("roku/roku3-diagnostics-data.json")) + + result = await get_diagnostics_for_config_entry(hass, hass_client, init_integration) + + assert isinstance(result, dict) + assert isinstance(result["entry"], dict) + assert result["entry"]["data"] == {"host": "192.168.1.160"} + assert result["entry"]["unique_id"] == "1GU48T017973" + + assert isinstance(result["data"], dict) + assert result["data"]["app"] == diagnostics_data["app"] + assert result["data"]["apps"] == diagnostics_data["apps"] + assert result["data"]["channel"] == diagnostics_data["channel"] + assert result["data"]["channels"] == diagnostics_data["channels"] + assert result["data"]["info"] == diagnostics_data["info"] + assert result["data"]["media"] == diagnostics_data["media"] + + data_state = result["data"]["state"] + assert isinstance(data_state, dict) + assert data_state["available"] == diagnostics_data["state"]["available"] + assert data_state["standby"] == diagnostics_data["state"]["standby"]