Add diagnostics to WattTime (#64887)
This commit is contained in:
parent
f92e8ab931
commit
e9a71231c2
5 changed files with 99 additions and 6 deletions
42
homeassistant/components/watttime/diagnostics.py
Normal file
42
homeassistant/components/watttime/diagnostics.py
Normal file
|
@ -0,0 +1,42 @@
|
|||
"""Diagnostics support for WattTime."""
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Any
|
||||
|
||||
from homeassistant.components.diagnostics import async_redact_data
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.const import (
|
||||
CONF_LATITUDE,
|
||||
CONF_LONGITUDE,
|
||||
CONF_PASSWORD,
|
||||
CONF_USERNAME,
|
||||
)
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator
|
||||
|
||||
from .const import DOMAIN
|
||||
|
||||
TO_REDACT = {
|
||||
CONF_LATITUDE,
|
||||
CONF_LONGITUDE,
|
||||
CONF_PASSWORD,
|
||||
CONF_USERNAME,
|
||||
}
|
||||
|
||||
|
||||
async def async_get_config_entry_diagnostics(
|
||||
hass: HomeAssistant, entry: ConfigEntry
|
||||
) -> dict[str, Any]:
|
||||
"""Return diagnostics for a config entry."""
|
||||
coordinator: DataUpdateCoordinator = hass.data[DOMAIN][entry.entry_id]
|
||||
|
||||
return async_redact_data(
|
||||
{
|
||||
"entry": {
|
||||
"data": dict(entry.data),
|
||||
"options": dict(entry.options),
|
||||
},
|
||||
"data": coordinator.data,
|
||||
},
|
||||
TO_REDACT,
|
||||
)
|
|
@ -8,7 +8,11 @@ from homeassistant.components.watttime.config_flow import (
|
|||
CONF_LOCATION_TYPE,
|
||||
LOCATION_TYPE_COORDINATES,
|
||||
)
|
||||
from homeassistant.components.watttime.const import DOMAIN
|
||||
from homeassistant.components.watttime.const import (
|
||||
CONF_BALANCING_AUTHORITY,
|
||||
CONF_BALANCING_AUTHORITY_ABBREV,
|
||||
DOMAIN,
|
||||
)
|
||||
from homeassistant.const import (
|
||||
CONF_LATITUDE,
|
||||
CONF_LONGITUDE,
|
||||
|
@ -21,11 +25,13 @@ from tests.common import MockConfigEntry, load_fixture
|
|||
|
||||
|
||||
@pytest.fixture(name="client")
|
||||
def client_fixture(get_grid_region):
|
||||
def client_fixture(get_grid_region, data_realtime_emissions):
|
||||
"""Define an aiowatttime client."""
|
||||
client = Mock()
|
||||
client.emissions.async_get_grid_region = get_grid_region
|
||||
client.emissions.async_get_realtime_emissions = AsyncMock()
|
||||
client.emissions.async_get_realtime_emissions = AsyncMock(
|
||||
return_value=data_realtime_emissions
|
||||
)
|
||||
return client
|
||||
|
||||
|
||||
|
@ -61,7 +67,12 @@ def config_entry_fixture(hass, config_auth, config_coordinates, unique_id):
|
|||
entry = MockConfigEntry(
|
||||
domain=DOMAIN,
|
||||
unique_id=unique_id,
|
||||
data={**config_auth, **config_coordinates},
|
||||
data={
|
||||
**config_auth,
|
||||
**config_coordinates,
|
||||
CONF_BALANCING_AUTHORITY: "PJM New Jersey",
|
||||
CONF_BALANCING_AUTHORITY_ABBREV: "PJM_NJ",
|
||||
},
|
||||
)
|
||||
entry.add_to_hass(hass)
|
||||
return entry
|
||||
|
@ -73,6 +84,12 @@ def data_grid_region_fixture():
|
|||
return json.loads(load_fixture("grid_region_data.json", "watttime"))
|
||||
|
||||
|
||||
@pytest.fixture(name="data_realtime_emissions", scope="session")
|
||||
def data_realtime_emissions_fixture():
|
||||
"""Define realtime emissions data."""
|
||||
return json.loads(load_fixture("realtime_emissions_data.json", "watttime"))
|
||||
|
||||
|
||||
@pytest.fixture(name="get_grid_region")
|
||||
def get_grid_region_fixture(data_grid_region):
|
||||
"""Define an aiowatttime method to get grid region data."""
|
||||
|
|
|
@ -0,0 +1,8 @@
|
|||
{
|
||||
"freq": "300",
|
||||
"ba": "CAISO_NORTH",
|
||||
"percent": "53",
|
||||
"moer": "850.743982",
|
||||
"point_time": "2019-01-29T14:55:00.00Z"
|
||||
}
|
||||
|
|
@ -159,8 +159,6 @@ async def test_step_reauth(
|
|||
data={
|
||||
**config_auth,
|
||||
**config_coordinates,
|
||||
CONF_BALANCING_AUTHORITY: "Authority 1",
|
||||
CONF_BALANCING_AUTHORITY_ABBREV: "AUTH_1",
|
||||
},
|
||||
)
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
|
|
28
tests/components/watttime/test_diagnostics.py
Normal file
28
tests/components/watttime/test_diagnostics.py
Normal file
|
@ -0,0 +1,28 @@
|
|||
"""Test WattTime diagnostics."""
|
||||
from homeassistant.components.diagnostics import REDACTED
|
||||
|
||||
from tests.components.diagnostics import get_diagnostics_for_config_entry
|
||||
|
||||
|
||||
async def test_entry_diagnostics(hass, config_entry, hass_client, setup_watttime):
|
||||
"""Test config entry diagnostics."""
|
||||
assert await get_diagnostics_for_config_entry(hass, hass_client, config_entry) == {
|
||||
"entry": {
|
||||
"data": {
|
||||
"username": REDACTED,
|
||||
"password": REDACTED,
|
||||
"latitude": REDACTED,
|
||||
"longitude": REDACTED,
|
||||
"balancing_authority": "PJM New Jersey",
|
||||
"balancing_authority_abbreviation": "PJM_NJ",
|
||||
},
|
||||
"options": {},
|
||||
},
|
||||
"data": {
|
||||
"freq": "300",
|
||||
"ba": "CAISO_NORTH",
|
||||
"percent": "53",
|
||||
"moer": "850.743982",
|
||||
"point_time": "2019-01-29T14:55:00.00Z",
|
||||
},
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue