Remove YAML import from System monitor (#119782)
This commit is contained in:
parent
b1c5845d35
commit
75b0acf6b6
4 changed files with 2 additions and 266 deletions
|
@ -8,11 +8,9 @@ from typing import Any
|
|||
import voluptuous as vol
|
||||
|
||||
from homeassistant.components.binary_sensor import DOMAIN as BINARY_SENSOR_DOMAIN
|
||||
from homeassistant.components.homeassistant import DOMAIN as HOMEASSISTANT_DOMAIN
|
||||
from homeassistant.config_entries import ConfigFlowResult
|
||||
from homeassistant.core import callback
|
||||
from homeassistant.helpers import entity_registry as er
|
||||
from homeassistant.helpers.issue_registry import IssueSeverity, async_create_issue
|
||||
from homeassistant.helpers.schema_config_entry_flow import (
|
||||
SchemaCommonFlowHandler,
|
||||
SchemaConfigFlowHandler,
|
||||
|
@ -53,37 +51,6 @@ async def validate_sensor_setup(
|
|||
return {}
|
||||
|
||||
|
||||
async def validate_import_sensor_setup(
|
||||
handler: SchemaCommonFlowHandler, user_input: dict[str, Any]
|
||||
) -> dict[str, Any]:
|
||||
"""Validate sensor input."""
|
||||
# Standard behavior is to merge the result with the options.
|
||||
# In this case, we want to add a sub-item so we update the options directly.
|
||||
sensors: dict[str, list] = handler.options.setdefault(BINARY_SENSOR_DOMAIN, {})
|
||||
import_processes: list[str] = user_input["processes"]
|
||||
processes = sensors.setdefault(CONF_PROCESS, [])
|
||||
processes.extend(import_processes)
|
||||
legacy_resources: list[str] = handler.options.setdefault("resources", [])
|
||||
legacy_resources.extend(user_input["legacy_resources"])
|
||||
|
||||
async_create_issue(
|
||||
handler.parent_handler.hass,
|
||||
HOMEASSISTANT_DOMAIN,
|
||||
f"deprecated_yaml_{DOMAIN}",
|
||||
breaks_in_ha_version="2024.7.0",
|
||||
is_fixable=False,
|
||||
is_persistent=False,
|
||||
issue_domain=DOMAIN,
|
||||
severity=IssueSeverity.WARNING,
|
||||
translation_key="deprecated_yaml",
|
||||
translation_placeholders={
|
||||
"domain": DOMAIN,
|
||||
"integration_title": "System Monitor",
|
||||
},
|
||||
)
|
||||
return {}
|
||||
|
||||
|
||||
async def get_sensor_setup_schema(handler: SchemaCommonFlowHandler) -> vol.Schema:
|
||||
"""Return process sensor setup schema."""
|
||||
hass = handler.parent_handler.hass
|
||||
|
@ -112,10 +79,6 @@ async def get_suggested_value(handler: SchemaCommonFlowHandler) -> dict[str, Any
|
|||
|
||||
CONFIG_FLOW = {
|
||||
"user": SchemaFlowFormStep(schema=vol.Schema({})),
|
||||
"import": SchemaFlowFormStep(
|
||||
schema=vol.Schema({}),
|
||||
validate_user_input=validate_import_sensor_setup,
|
||||
),
|
||||
}
|
||||
OPTIONS_FLOW = {
|
||||
"init": SchemaFlowFormStep(
|
||||
|
|
|
@ -15,20 +15,15 @@ import time
|
|||
from typing import Any, Literal
|
||||
|
||||
from psutil import NoSuchProcess
|
||||
import voluptuous as vol
|
||||
|
||||
from homeassistant.components.sensor import (
|
||||
DOMAIN as SENSOR_DOMAIN,
|
||||
PLATFORM_SCHEMA,
|
||||
SensorDeviceClass,
|
||||
SensorEntity,
|
||||
SensorEntityDescription,
|
||||
SensorStateClass,
|
||||
)
|
||||
from homeassistant.config_entries import SOURCE_IMPORT
|
||||
from homeassistant.const import (
|
||||
CONF_RESOURCES,
|
||||
CONF_TYPE,
|
||||
PERCENTAGE,
|
||||
STATE_OFF,
|
||||
STATE_ON,
|
||||
|
@ -39,11 +34,10 @@ from homeassistant.const import (
|
|||
)
|
||||
from homeassistant.core import HomeAssistant, callback
|
||||
from homeassistant.helpers import entity_registry as er
|
||||
import homeassistant.helpers.config_validation as cv
|
||||
from homeassistant.helpers.device_registry import DeviceEntryType, DeviceInfo
|
||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||
from homeassistant.helpers.issue_registry import IssueSeverity, async_create_issue
|
||||
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType, StateType
|
||||
from homeassistant.helpers.typing import StateType
|
||||
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
||||
from homeassistant.util import slugify
|
||||
|
||||
|
@ -410,20 +404,6 @@ SENSOR_TYPES: dict[str, SysMonitorSensorEntityDescription] = {
|
|||
}
|
||||
|
||||
|
||||
def check_required_arg(value: Any) -> Any:
|
||||
"""Validate that the required "arg" for the sensor types that need it are set."""
|
||||
for sensor in value:
|
||||
sensor_type = sensor[CONF_TYPE]
|
||||
sensor_arg = sensor.get(CONF_ARG)
|
||||
|
||||
if sensor_arg is None and SENSOR_TYPES[sensor_type].mandatory_arg:
|
||||
raise vol.RequiredFieldInvalid(
|
||||
f"Mandatory 'arg' is missing for sensor type '{sensor_type}'."
|
||||
)
|
||||
|
||||
return value
|
||||
|
||||
|
||||
def check_legacy_resource(resource: str, resources: set[str]) -> bool:
|
||||
"""Return True if legacy resource was configured."""
|
||||
# This function to check legacy resources can be removed
|
||||
|
@ -435,23 +415,6 @@ def check_legacy_resource(resource: str, resources: set[str]) -> bool:
|
|||
return False
|
||||
|
||||
|
||||
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
||||
{
|
||||
vol.Optional(CONF_RESOURCES, default={CONF_TYPE: "disk_use"}): vol.All(
|
||||
cv.ensure_list,
|
||||
[
|
||||
vol.Schema(
|
||||
{
|
||||
vol.Required(CONF_TYPE): vol.In(SENSOR_TYPES),
|
||||
vol.Optional(CONF_ARG): cv.string,
|
||||
}
|
||||
)
|
||||
],
|
||||
check_required_arg,
|
||||
)
|
||||
}
|
||||
)
|
||||
|
||||
IO_COUNTER = {
|
||||
"network_out": 0,
|
||||
"network_in": 1,
|
||||
|
@ -463,44 +426,6 @@ IO_COUNTER = {
|
|||
IF_ADDRS_FAMILY = {"ipv4_address": socket.AF_INET, "ipv6_address": socket.AF_INET6}
|
||||
|
||||
|
||||
async def async_setup_platform(
|
||||
hass: HomeAssistant,
|
||||
config: ConfigType,
|
||||
async_add_entities: AddEntitiesCallback,
|
||||
discovery_info: DiscoveryInfoType | None = None,
|
||||
) -> None:
|
||||
"""Set up the system monitor sensors."""
|
||||
processes = [
|
||||
resource[CONF_ARG]
|
||||
for resource in config[CONF_RESOURCES]
|
||||
if resource[CONF_TYPE] == "process"
|
||||
]
|
||||
legacy_config: list[dict[str, str]] = config[CONF_RESOURCES]
|
||||
resources = []
|
||||
for resource_conf in legacy_config:
|
||||
if (_type := resource_conf[CONF_TYPE]).startswith("disk_"):
|
||||
if (arg := resource_conf.get(CONF_ARG)) is None:
|
||||
resources.append(f"{_type}_/")
|
||||
continue
|
||||
resources.append(f"{_type}_{arg}")
|
||||
continue
|
||||
resources.append(f"{_type}_{resource_conf.get(CONF_ARG, '')}")
|
||||
_LOGGER.debug(
|
||||
"Importing config with processes: %s, resources: %s", processes, resources
|
||||
)
|
||||
|
||||
# With removal of the import also cleanup legacy_resources logic in setup_entry
|
||||
# Also cleanup entry.options["resources"] which is only imported for legacy reasons
|
||||
|
||||
hass.async_create_task(
|
||||
hass.config_entries.flow.async_init(
|
||||
DOMAIN,
|
||||
context={"source": SOURCE_IMPORT},
|
||||
data={"processes": processes, "legacy_resources": resources},
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
async def async_setup_entry(
|
||||
hass: HomeAssistant,
|
||||
entry: SystemMonitorConfigEntry,
|
||||
|
|
|
@ -5,15 +5,10 @@ from __future__ import annotations
|
|||
from unittest.mock import AsyncMock
|
||||
|
||||
from homeassistant import config_entries
|
||||
from homeassistant.components.homeassistant import DOMAIN as HOMEASSISTANT_DOMAIN
|
||||
from homeassistant.components.systemmonitor.const import CONF_PROCESS, DOMAIN
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.data_entry_flow import FlowResultType
|
||||
from homeassistant.helpers import (
|
||||
device_registry as dr,
|
||||
entity_registry as er,
|
||||
issue_registry as ir,
|
||||
)
|
||||
from homeassistant.helpers import device_registry as dr, entity_registry as er
|
||||
|
||||
from tests.common import MockConfigEntry
|
||||
|
||||
|
@ -39,51 +34,6 @@ async def test_form(hass: HomeAssistant, mock_setup_entry: AsyncMock) -> None:
|
|||
assert len(mock_setup_entry.mock_calls) == 1
|
||||
|
||||
|
||||
async def test_import(
|
||||
hass: HomeAssistant, mock_setup_entry: AsyncMock, issue_registry: ir.IssueRegistry
|
||||
) -> None:
|
||||
"""Test import."""
|
||||
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN,
|
||||
context={"source": config_entries.SOURCE_IMPORT},
|
||||
data={
|
||||
"processes": ["systemd", "octave-cli"],
|
||||
"legacy_resources": [
|
||||
"disk_use_percent_/",
|
||||
"memory_free_",
|
||||
"network_out_eth0",
|
||||
"process_systemd",
|
||||
"process_octave-cli",
|
||||
],
|
||||
},
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert result["type"] is FlowResultType.CREATE_ENTRY
|
||||
assert result["options"] == {
|
||||
"binary_sensor": {"process": ["systemd", "octave-cli"]},
|
||||
"resources": [
|
||||
"disk_use_percent_/",
|
||||
"memory_free_",
|
||||
"network_out_eth0",
|
||||
"process_systemd",
|
||||
"process_octave-cli",
|
||||
],
|
||||
}
|
||||
|
||||
assert len(mock_setup_entry.mock_calls) == 1
|
||||
|
||||
issue = issue_registry.async_get_issue(
|
||||
HOMEASSISTANT_DOMAIN, f"deprecated_yaml_{DOMAIN}"
|
||||
)
|
||||
assert issue.issue_domain == DOMAIN
|
||||
assert issue.translation_placeholders == {
|
||||
"domain": DOMAIN,
|
||||
"integration_title": "System Monitor",
|
||||
}
|
||||
|
||||
|
||||
async def test_form_already_configured(
|
||||
hass: HomeAssistant, mock_setup_entry: AsyncMock
|
||||
) -> None:
|
||||
|
@ -111,55 +61,6 @@ async def test_form_already_configured(
|
|||
assert result["reason"] == "already_configured"
|
||||
|
||||
|
||||
async def test_import_already_configured(
|
||||
hass: HomeAssistant, mock_setup_entry: AsyncMock, issue_registry: ir.IssueRegistry
|
||||
) -> None:
|
||||
"""Test abort when already configured for import."""
|
||||
config_entry = MockConfigEntry(
|
||||
domain=DOMAIN,
|
||||
source=config_entries.SOURCE_USER,
|
||||
options={
|
||||
"binary_sensor": [{CONF_PROCESS: "systemd"}],
|
||||
"resources": [
|
||||
"disk_use_percent_/",
|
||||
"memory_free_",
|
||||
"network_out_eth0",
|
||||
"process_systemd",
|
||||
"process_octave-cli",
|
||||
],
|
||||
},
|
||||
)
|
||||
config_entry.add_to_hass(hass)
|
||||
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN,
|
||||
context={"source": config_entries.SOURCE_IMPORT},
|
||||
data={
|
||||
"processes": ["systemd", "octave-cli"],
|
||||
"legacy_resources": [
|
||||
"disk_use_percent_/",
|
||||
"memory_free_",
|
||||
"network_out_eth0",
|
||||
"process_systemd",
|
||||
"process_octave-cli",
|
||||
],
|
||||
},
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert result["type"] is FlowResultType.ABORT
|
||||
assert result["reason"] == "already_configured"
|
||||
|
||||
issue = issue_registry.async_get_issue(
|
||||
HOMEASSISTANT_DOMAIN, f"deprecated_yaml_{DOMAIN}"
|
||||
)
|
||||
assert issue.issue_domain == DOMAIN
|
||||
assert issue.translation_placeholders == {
|
||||
"domain": DOMAIN,
|
||||
"integration_title": "System Monitor",
|
||||
}
|
||||
|
||||
|
||||
async def test_add_and_remove_processes(
|
||||
hass: HomeAssistant,
|
||||
device_registry: dr.DeviceRegistry,
|
||||
|
|
|
@ -17,7 +17,6 @@ from homeassistant.config_entries import ConfigEntry
|
|||
from homeassistant.const import STATE_OFF, STATE_ON, STATE_UNAVAILABLE, STATE_UNKNOWN
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers import entity_registry as er
|
||||
from homeassistant.setup import async_setup_component
|
||||
|
||||
from .conftest import MockProcess
|
||||
|
||||
|
@ -142,58 +141,6 @@ async def test_sensor_icon(
|
|||
assert get_cpu_icon() == "mdi:cpu-64-bit"
|
||||
|
||||
|
||||
@pytest.mark.usefixtures("entity_registry_enabled_by_default")
|
||||
async def test_sensor_yaml(
|
||||
hass: HomeAssistant,
|
||||
mock_psutil: Mock,
|
||||
mock_os: Mock,
|
||||
) -> None:
|
||||
"""Test the sensor imported from YAML."""
|
||||
config = {
|
||||
"sensor": {
|
||||
"platform": "systemmonitor",
|
||||
"resources": [
|
||||
{"type": "disk_use_percent"},
|
||||
{"type": "disk_use_percent", "arg": "/media/share"},
|
||||
{"type": "memory_free", "arg": "/"},
|
||||
{"type": "network_out", "arg": "eth0"},
|
||||
{"type": "process", "arg": "python3"},
|
||||
],
|
||||
}
|
||||
}
|
||||
assert await async_setup_component(hass, "sensor", config)
|
||||
await hass.async_block_till_done()
|
||||
memory_sensor = hass.states.get("sensor.system_monitor_memory_free")
|
||||
assert memory_sensor is not None
|
||||
assert memory_sensor.state == "40.0"
|
||||
|
||||
process_sensor = hass.states.get("binary_sensor.system_monitor_process_python3")
|
||||
assert process_sensor is not None
|
||||
assert process_sensor.state == STATE_ON
|
||||
|
||||
|
||||
@pytest.mark.usefixtures("entity_registry_enabled_by_default")
|
||||
async def test_sensor_yaml_fails_missing_argument(
|
||||
caplog: pytest.LogCaptureFixture,
|
||||
hass: HomeAssistant,
|
||||
mock_psutil: Mock,
|
||||
mock_os: Mock,
|
||||
) -> None:
|
||||
"""Test the sensor imported from YAML fails on missing mandatory argument."""
|
||||
config = {
|
||||
"sensor": {
|
||||
"platform": "systemmonitor",
|
||||
"resources": [
|
||||
{"type": "network_in"},
|
||||
],
|
||||
}
|
||||
}
|
||||
assert await async_setup_component(hass, "sensor", config)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert "Mandatory 'arg' is missing for sensor type 'network_in'" in caplog.text
|
||||
|
||||
|
||||
async def test_sensor_updating(
|
||||
hass: HomeAssistant,
|
||||
mock_psutil: Mock,
|
||||
|
|
Loading…
Add table
Reference in a new issue