Remove YAML configuration from filesize (#71692)

This commit is contained in:
Franck Nijhof 2022-05-12 10:31:42 +02:00 committed by GitHub
parent 3ce19cd6f8
commit 135326a4a6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 6 additions and 133 deletions

View File

@ -1,7 +1,6 @@
"""The filesize component.""" """The filesize component."""
from __future__ import annotations from __future__ import annotations
import logging
import pathlib import pathlib
from homeassistant.config_entries import ConfigEntry from homeassistant.config_entries import ConfigEntry
@ -11,8 +10,6 @@ from homeassistant.exceptions import ConfigEntryNotReady
from .const import PLATFORMS from .const import PLATFORMS
_LOGGER = logging.getLogger(__name__)
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""Set up from a config entry.""" """Set up from a config entry."""

View File

@ -67,10 +67,6 @@ class FilesizeConfigFlow(ConfigFlow, domain=DOMAIN):
step_id="user", data_schema=DATA_SCHEMA, errors=errors step_id="user", data_schema=DATA_SCHEMA, errors=errors
) )
async def async_step_import(self, user_input: dict[str, Any]) -> FlowResult:
"""Handle import from configuration.yaml."""
return await self.async_step_user(user_input)
class NotValidError(Exception): class NotValidError(Exception):
"""Path is not valid error.""" """Path is not valid error."""

View File

@ -4,5 +4,3 @@ from homeassistant.const import Platform
DOMAIN = "filesize" DOMAIN = "filesize"
PLATFORMS = [Platform.SENSOR] PLATFORMS = [Platform.SENSOR]
CONF_FILE_PATHS = "file_paths"

View File

@ -6,23 +6,18 @@ import logging
import os import os
import pathlib import pathlib
import voluptuous as vol
from homeassistant.components.sensor import ( from homeassistant.components.sensor import (
PLATFORM_SCHEMA as PARENT_PLATFORM_SCHEMA,
SensorDeviceClass, SensorDeviceClass,
SensorEntity, SensorEntity,
SensorEntityDescription, SensorEntityDescription,
SensorStateClass, SensorStateClass,
) )
from homeassistant.config_entries import SOURCE_IMPORT, ConfigEntry from homeassistant.config_entries import ConfigEntry
from homeassistant.const import CONF_FILE_PATH, DATA_BYTES, DATA_MEGABYTES from homeassistant.const import CONF_FILE_PATH, DATA_BYTES, DATA_MEGABYTES
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.device_registry import DeviceEntryType from homeassistant.helpers.device_registry import DeviceEntryType
from homeassistant.helpers.entity import DeviceInfo, EntityCategory from homeassistant.helpers.entity import DeviceInfo, EntityCategory
from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
from homeassistant.helpers.update_coordinator import ( from homeassistant.helpers.update_coordinator import (
CoordinatorEntity, CoordinatorEntity,
DataUpdateCoordinator, DataUpdateCoordinator,
@ -30,7 +25,7 @@ from homeassistant.helpers.update_coordinator import (
) )
import homeassistant.util.dt as dt_util import homeassistant.util.dt as dt_util
from .const import CONF_FILE_PATHS, DOMAIN from .const import DOMAIN
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
@ -64,34 +59,6 @@ SENSOR_TYPES = (
), ),
) )
PLATFORM_SCHEMA = PARENT_PLATFORM_SCHEMA.extend(
{vol.Required(CONF_FILE_PATHS): vol.All(cv.ensure_list, [cv.isfile])}
)
async def async_setup_platform(
hass: HomeAssistant,
config: ConfigType,
async_add_entities: AddEntitiesCallback,
discovery_info: DiscoveryInfoType | None = None,
) -> None:
"""Set up the file size sensor."""
_LOGGER.warning(
# Filesize config flow added in 2022.4 and should be removed in 2022.6
"Configuration of the Filesize sensor platform in YAML is deprecated and "
"will be removed in Home Assistant 2022.6; Your existing configuration "
"has been imported into the UI automatically and can be safely removed "
"from your configuration.yaml file"
)
for path in config[CONF_FILE_PATHS]:
hass.async_create_task(
hass.config_entries.flow.async_init(
DOMAIN,
context={"source": SOURCE_IMPORT},
data={CONF_FILE_PATH: path},
)
)
async def async_setup_entry( async def async_setup_entry(
hass: HomeAssistant, hass: HomeAssistant,

View File

@ -1,10 +1,8 @@
"""Tests for the Filesize config flow.""" """Tests for the Filesize config flow."""
from unittest.mock import patch from unittest.mock import patch
import pytest
from homeassistant.components.filesize.const import DOMAIN from homeassistant.components.filesize.const import DOMAIN
from homeassistant.config_entries import SOURCE_IMPORT, SOURCE_USER from homeassistant.config_entries import SOURCE_USER
from homeassistant.const import CONF_FILE_PATH from homeassistant.const import CONF_FILE_PATH
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.data_entry_flow import ( from homeassistant.data_entry_flow import (
@ -40,39 +38,22 @@ async def test_full_user_flow(hass: HomeAssistant) -> None:
assert result2.get("data") == {CONF_FILE_PATH: TEST_FILE} assert result2.get("data") == {CONF_FILE_PATH: TEST_FILE}
@pytest.mark.parametrize("source", [SOURCE_USER, SOURCE_IMPORT])
async def test_unique_path( async def test_unique_path(
hass: HomeAssistant, hass: HomeAssistant,
mock_config_entry: MockConfigEntry, mock_config_entry: MockConfigEntry,
source: str,
) -> None: ) -> None:
"""Test we abort if already setup.""" """Test we abort if already setup."""
hass.config.allowlist_external_dirs = {TEST_DIR} hass.config.allowlist_external_dirs = {TEST_DIR}
mock_config_entry.add_to_hass(hass) mock_config_entry.add_to_hass(hass)
result = await hass.config_entries.flow.async_init( result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": source}, data={CONF_FILE_PATH: TEST_FILE} DOMAIN, context={"source": SOURCE_USER}, data={CONF_FILE_PATH: TEST_FILE}
) )
assert result.get("type") == RESULT_TYPE_ABORT assert result.get("type") == RESULT_TYPE_ABORT
assert result.get("reason") == "already_configured" assert result.get("reason") == "already_configured"
async def test_import_flow(hass: HomeAssistant) -> None:
"""Test the import configuration flow."""
create_file(TEST_FILE)
hass.config.allowlist_external_dirs = {TEST_DIR}
result = await hass.config_entries.flow.async_init(
DOMAIN,
context={"source": SOURCE_IMPORT},
data={CONF_FILE_PATH: TEST_FILE},
)
assert result.get("type") == RESULT_TYPE_CREATE_ENTRY
assert result.get("title") == TEST_FILE_NAME
assert result.get("data") == {CONF_FILE_PATH: TEST_FILE}
async def test_flow_fails_on_validation(hass: HomeAssistant) -> None: async def test_flow_fails_on_validation(hass: HomeAssistant) -> None:
"""Test config flow errors.""" """Test config flow errors."""
create_file(TEST_FILE) create_file(TEST_FILE)

View File

@ -1,21 +1,10 @@
"""Tests for the Filesize integration.""" """Tests for the Filesize integration."""
from unittest.mock import AsyncMock from homeassistant.components.filesize.const import DOMAIN
from homeassistant.components.filesize.const import CONF_FILE_PATHS, DOMAIN
from homeassistant.components.sensor import DOMAIN as SENSOR_DOMAIN
from homeassistant.config_entries import ConfigEntryState from homeassistant.config_entries import ConfigEntryState
from homeassistant.const import CONF_FILE_PATH from homeassistant.const import CONF_FILE_PATH
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.setup import async_setup_component
from . import ( from . import create_file
TEST_DIR,
TEST_FILE,
TEST_FILE2,
TEST_FILE_NAME,
TEST_FILE_NAME2,
create_file,
)
from tests.common import MockConfigEntry from tests.common import MockConfigEntry
@ -75,36 +64,3 @@ async def test_not_valid_path_to_file(
await hass.async_block_till_done() await hass.async_block_till_done()
assert mock_config_entry.state is ConfigEntryState.SETUP_RETRY assert mock_config_entry.state is ConfigEntryState.SETUP_RETRY
async def test_import_config(
hass: HomeAssistant,
mock_setup_entry: AsyncMock,
) -> None:
"""Test Filesize being set up from config via import."""
create_file(TEST_FILE)
create_file(TEST_FILE2)
hass.config.allowlist_external_dirs = {TEST_DIR}
assert await async_setup_component(
hass,
SENSOR_DOMAIN,
{
SENSOR_DOMAIN: {
"platform": DOMAIN,
CONF_FILE_PATHS: [TEST_FILE, TEST_FILE2],
}
},
)
await hass.async_block_till_done()
config_entries = hass.config_entries.async_entries(DOMAIN)
assert len(config_entries) == 2
entry = config_entries[0]
assert entry.title == TEST_FILE_NAME
assert entry.unique_id == TEST_FILE
assert entry.data == {CONF_FILE_PATH: TEST_FILE}
entry2 = config_entries[1]
assert entry2.title == TEST_FILE_NAME2
assert entry2.unique_id == TEST_FILE2
assert entry2.data == {CONF_FILE_PATH: TEST_FILE2}

View File

@ -1,11 +1,9 @@
"""The tests for the filesize sensor.""" """The tests for the filesize sensor."""
import os import os
from homeassistant.components.filesize.const import DOMAIN
from homeassistant.const import CONF_FILE_PATH, STATE_UNAVAILABLE from homeassistant.const import CONF_FILE_PATH, STATE_UNAVAILABLE
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_component import async_update_entity from homeassistant.helpers.entity_component import async_update_entity
from homeassistant.setup import async_setup_component
from . import TEST_FILE, TEST_FILE_NAME, create_file from . import TEST_FILE, TEST_FILE_NAME, create_file
@ -71,23 +69,3 @@ async def test_state_unavailable(
state = hass.states.get("sensor.file_txt_size") state = hass.states.get("sensor.file_txt_size")
assert state.state == STATE_UNAVAILABLE assert state.state == STATE_UNAVAILABLE
async def test_import_query(hass: HomeAssistant, tmpdir: str) -> None:
"""Test import from yaml."""
testfile = f"{tmpdir}/file.txt"
create_file(testfile)
hass.config.allowlist_external_dirs = {tmpdir}
config = {
"sensor": {
"platform": "filesize",
"file_paths": [testfile],
}
}
assert await async_setup_component(hass, "sensor", config)
await hass.async_block_till_done()
assert hass.config_entries.async_entries(DOMAIN)
data = hass.config_entries.async_entries(DOMAIN)[0].data
assert data[CONF_FILE_PATH] == testfile