Add rainforest diagnostics (#64647)

This commit is contained in:
Paulus Schoutsen 2022-01-21 10:59:11 -08:00 committed by GitHub
parent e982df5199
commit 3899600771
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 174 additions and 108 deletions

View file

@ -0,0 +1,23 @@
"""Diagnostics support for Eagle."""
from __future__ import annotations
from homeassistant.components.diagnostics import async_redact_data
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
from .const import CONF_CLOUD_ID, CONF_INSTALL_CODE, DOMAIN
from .data import EagleDataCoordinator
TO_REDACT = {CONF_CLOUD_ID, CONF_INSTALL_CODE}
async def async_get_config_entry_diagnostics(
hass: HomeAssistant, config_entry: ConfigEntry
) -> dict:
"""Return diagnostics for a config entry."""
coordinator: EagleDataCoordinator = hass.data[DOMAIN][config_entry.entry_id]
return {
"config_entry": async_redact_data(config_entry.as_dict(), TO_REDACT),
"data": coordinator.data,
}

View file

@ -1 +1,36 @@
"""Tests for the Rainforest Eagle integration."""
MOCK_CLOUD_ID = "12345"
MOCK_200_RESPONSE_WITH_PRICE = {
"zigbee:InstantaneousDemand": {
"Name": "zigbee:InstantaneousDemand",
"Value": "1.152000",
},
"zigbee:CurrentSummationDelivered": {
"Name": "zigbee:CurrentSummationDelivered",
"Value": "45251.285000",
},
"zigbee:CurrentSummationReceived": {
"Name": "zigbee:CurrentSummationReceived",
"Value": "232.232000",
},
"zigbee:Price": {"Name": "zigbee:Price", "Value": "0.053990"},
"zigbee:PriceCurrency": {"Name": "zigbee:PriceCurrency", "Value": "USD"},
}
MOCK_200_RESPONSE_WITHOUT_PRICE = {
"zigbee:InstantaneousDemand": {
"Name": "zigbee:InstantaneousDemand",
"Value": "1.152000",
},
"zigbee:CurrentSummationDelivered": {
"Name": "zigbee:CurrentSummationDelivered",
"Value": "45251.285000",
},
"zigbee:CurrentSummationReceived": {
"Name": "zigbee:CurrentSummationReceived",
"Value": "232.232000",
},
"zigbee:Price": {"Name": "zigbee:Price", "Value": "invalid"},
"zigbee:PriceCurrency": {"Name": "zigbee:PriceCurrency", "Value": "USD"},
}

View file

@ -0,0 +1,85 @@
"""Conftest for rainforest_eagle."""
from unittest.mock import AsyncMock, Mock, patch
import pytest
from homeassistant.components.rainforest_eagle.const import (
CONF_CLOUD_ID,
CONF_HARDWARE_ADDRESS,
CONF_INSTALL_CODE,
DOMAIN,
TYPE_EAGLE_100,
TYPE_EAGLE_200,
)
from homeassistant.const import CONF_HOST, CONF_TYPE
from homeassistant.setup import async_setup_component
from . import MOCK_200_RESPONSE_WITHOUT_PRICE, MOCK_CLOUD_ID
from tests.common import MockConfigEntry
@pytest.fixture
def config_entry_200(hass):
"""Return a config entry."""
entry = MockConfigEntry(
domain="rainforest_eagle",
data={
CONF_CLOUD_ID: MOCK_CLOUD_ID,
CONF_HOST: "192.168.1.55",
CONF_INSTALL_CODE: "abcdefgh",
CONF_HARDWARE_ADDRESS: "mock-hw-address",
CONF_TYPE: TYPE_EAGLE_200,
},
)
entry.add_to_hass(hass)
return entry
@pytest.fixture
async def setup_rainforest_200(hass, config_entry_200):
"""Set up rainforest."""
with patch(
"aioeagle.ElectricMeter.create_instance",
return_value=Mock(
get_device_query=AsyncMock(return_value=MOCK_200_RESPONSE_WITHOUT_PRICE)
),
) as mock_update:
mock_update.return_value.is_connected = True
assert await async_setup_component(hass, DOMAIN, {})
await hass.async_block_till_done()
yield mock_update.return_value
@pytest.fixture
async def setup_rainforest_100(hass):
"""Set up rainforest."""
MockConfigEntry(
domain="rainforest_eagle",
data={
CONF_CLOUD_ID: MOCK_CLOUD_ID,
CONF_HOST: "192.168.1.55",
CONF_INSTALL_CODE: "abcdefgh",
CONF_HARDWARE_ADDRESS: None,
CONF_TYPE: TYPE_EAGLE_100,
},
).add_to_hass(hass)
with patch(
"homeassistant.components.rainforest_eagle.data.Eagle100Reader",
return_value=Mock(
get_instantaneous_demand=Mock(
return_value={"InstantaneousDemand": {"Demand": "1.152000"}}
),
get_current_summation=Mock(
return_value={
"CurrentSummation": {
"SummationDelivered": "45251.285000",
"SummationReceived": "232.232000",
}
}
),
),
) as mock_update:
assert await async_setup_component(hass, DOMAIN, {})
await hass.async_block_till_done()
yield mock_update

View file

@ -0,0 +1,29 @@
"""Test the Rainforest Eagle diagnostics."""
from homeassistant.components.diagnostics import REDACTED
from homeassistant.components.rainforest_eagle.const import (
CONF_CLOUD_ID,
CONF_INSTALL_CODE,
)
from . import MOCK_200_RESPONSE_WITHOUT_PRICE
from tests.components.diagnostics import get_diagnostics_for_config_entry
async def test_entry_diagnostics(
hass, hass_client, setup_rainforest_200, config_entry_200
):
"""Test config entry diagnostics."""
result = await get_diagnostics_for_config_entry(hass, hass_client, config_entry_200)
config_entry_dict = config_entry_200.as_dict()
config_entry_dict["data"][CONF_INSTALL_CODE] = REDACTED
config_entry_dict["data"][CONF_CLOUD_ID] = REDACTED
assert result == {
"config_entry": config_entry_dict,
"data": {
var["Name"]: var["Value"]
for var in MOCK_200_RESPONSE_WITHOUT_PRICE.values()
},
}

View file

@ -1,113 +1,7 @@
"""Tests for rainforest eagle sensors."""
from unittest.mock import AsyncMock, Mock, patch
from homeassistant.components.rainforest_eagle.const import DOMAIN
import pytest
from homeassistant.components.rainforest_eagle.const import (
CONF_CLOUD_ID,
CONF_HARDWARE_ADDRESS,
CONF_INSTALL_CODE,
DOMAIN,
TYPE_EAGLE_100,
TYPE_EAGLE_200,
)
from homeassistant.const import CONF_HOST, CONF_TYPE
from homeassistant.setup import async_setup_component
from tests.common import MockConfigEntry
MOCK_CLOUD_ID = "12345"
MOCK_200_RESPONSE_WITH_PRICE = {
"zigbee:InstantaneousDemand": {
"Name": "zigbee:InstantaneousDemand",
"Value": "1.152000",
},
"zigbee:CurrentSummationDelivered": {
"Name": "zigbee:CurrentSummationDelivered",
"Value": "45251.285000",
},
"zigbee:CurrentSummationReceived": {
"Name": "zigbee:CurrentSummationReceived",
"Value": "232.232000",
},
"zigbee:Price": {"Name": "zigbee:Price", "Value": "0.053990"},
"zigbee:PriceCurrency": {"Name": "zigbee:PriceCurrency", "Value": "USD"},
}
MOCK_200_RESPONSE_WITHOUT_PRICE = {
"zigbee:InstantaneousDemand": {
"Name": "zigbee:InstantaneousDemand",
"Value": "1.152000",
},
"zigbee:CurrentSummationDelivered": {
"Name": "zigbee:CurrentSummationDelivered",
"Value": "45251.285000",
},
"zigbee:CurrentSummationReceived": {
"Name": "zigbee:CurrentSummationReceived",
"Value": "232.232000",
},
"zigbee:Price": {"Name": "zigbee:Price", "Value": "invalid"},
"zigbee:PriceCurrency": {"Name": "zigbee:PriceCurrency", "Value": "USD"},
}
@pytest.fixture
async def setup_rainforest_200(hass):
"""Set up rainforest."""
MockConfigEntry(
domain="rainforest_eagle",
data={
CONF_CLOUD_ID: MOCK_CLOUD_ID,
CONF_HOST: "192.168.1.55",
CONF_INSTALL_CODE: "abcdefgh",
CONF_HARDWARE_ADDRESS: "mock-hw-address",
CONF_TYPE: TYPE_EAGLE_200,
},
).add_to_hass(hass)
with patch(
"aioeagle.ElectricMeter.create_instance",
return_value=Mock(
get_device_query=AsyncMock(return_value=MOCK_200_RESPONSE_WITHOUT_PRICE)
),
) as mock_update:
mock_update.return_value.is_connected = True
assert await async_setup_component(hass, DOMAIN, {})
await hass.async_block_till_done()
yield mock_update.return_value
@pytest.fixture
async def setup_rainforest_100(hass):
"""Set up rainforest."""
MockConfigEntry(
domain="rainforest_eagle",
data={
CONF_CLOUD_ID: MOCK_CLOUD_ID,
CONF_HOST: "192.168.1.55",
CONF_INSTALL_CODE: "abcdefgh",
CONF_HARDWARE_ADDRESS: None,
CONF_TYPE: TYPE_EAGLE_100,
},
).add_to_hass(hass)
with patch(
"homeassistant.components.rainforest_eagle.data.Eagle100Reader",
return_value=Mock(
get_instantaneous_demand=Mock(
return_value={"InstantaneousDemand": {"Demand": "1.152000"}}
),
get_current_summation=Mock(
return_value={
"CurrentSummation": {
"SummationDelivered": "45251.285000",
"SummationReceived": "232.232000",
}
}
),
),
) as mock_update:
assert await async_setup_component(hass, DOMAIN, {})
await hass.async_block_till_done()
yield mock_update
from . import MOCK_200_RESPONSE_WITH_PRICE
async def test_sensors_200(hass, setup_rainforest_200):