Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions homeassistant/components/filesize/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
"""The filesize component."""
from __future__ import annotations

import logging
import pathlib

from homeassistant.config_entries import ConfigEntry
Expand All @@ -11,8 +10,6 @@

from .const import PLATFORMS

_LOGGER = logging.getLogger(__name__)


async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""Set up from a config entry."""
Expand Down
4 changes: 0 additions & 4 deletions homeassistant/components/filesize/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,6 @@ async def async_step_user(
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):
"""Path is not valid error."""
Expand Down
2 changes: 0 additions & 2 deletions homeassistant/components/filesize/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,3 @@

DOMAIN = "filesize"
PLATFORMS = [Platform.SENSOR]

CONF_FILE_PATHS = "file_paths"
37 changes: 2 additions & 35 deletions homeassistant/components/filesize/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,31 +6,26 @@
import os
import pathlib

import voluptuous as vol

from homeassistant.components.sensor import (
PLATFORM_SCHEMA as PARENT_PLATFORM_SCHEMA,
SensorDeviceClass,
SensorEntity,
SensorEntityDescription,
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.core import HomeAssistant
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.device_registry import DeviceEntryType
from homeassistant.helpers.entity import DeviceInfo, EntityCategory
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
from homeassistant.helpers.update_coordinator import (
CoordinatorEntity,
DataUpdateCoordinator,
UpdateFailed,
)
import homeassistant.util.dt as dt_util

from .const import CONF_FILE_PATHS, DOMAIN
from .const import DOMAIN

_LOGGER = logging.getLogger(__name__)

Expand Down Expand Up @@ -64,34 +59,6 @@
),
)

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(
hass: HomeAssistant,
Expand Down
23 changes: 2 additions & 21 deletions tests/components/filesize/test_config_flow.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
"""Tests for the Filesize config flow."""
from unittest.mock import patch

import pytest

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.core import HomeAssistant
from homeassistant.data_entry_flow import (
Expand Down Expand Up @@ -40,39 +38,22 @@ async def test_full_user_flow(hass: HomeAssistant) -> None:
assert result2.get("data") == {CONF_FILE_PATH: TEST_FILE}


@pytest.mark.parametrize("source", [SOURCE_USER, SOURCE_IMPORT])
async def test_unique_path(
hass: HomeAssistant,
mock_config_entry: MockConfigEntry,
source: str,
) -> None:
"""Test we abort if already setup."""
hass.config.allowlist_external_dirs = {TEST_DIR}
mock_config_entry.add_to_hass(hass)

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("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:
"""Test config flow errors."""
create_file(TEST_FILE)
Expand Down
48 changes: 2 additions & 46 deletions tests/components/filesize/test_init.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,10 @@
"""Tests for the Filesize integration."""
from unittest.mock import AsyncMock

from homeassistant.components.filesize.const import CONF_FILE_PATHS, DOMAIN
from homeassistant.components.sensor import DOMAIN as SENSOR_DOMAIN
from homeassistant.components.filesize.const import DOMAIN
from homeassistant.config_entries import ConfigEntryState
from homeassistant.const import CONF_FILE_PATH
from homeassistant.core import HomeAssistant
from homeassistant.setup import async_setup_component

from . import (
TEST_DIR,
TEST_FILE,
TEST_FILE2,
TEST_FILE_NAME,
TEST_FILE_NAME2,
create_file,
)
from . import create_file

from tests.common import MockConfigEntry

Expand Down Expand Up @@ -75,36 +64,3 @@ async def test_not_valid_path_to_file(
await hass.async_block_till_done()

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}
22 changes: 0 additions & 22 deletions tests/components/filesize/test_sensor.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
"""The tests for the filesize sensor."""
import os

from homeassistant.components.filesize.const import DOMAIN
from homeassistant.const import CONF_FILE_PATH, STATE_UNAVAILABLE
from homeassistant.core import HomeAssistant
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

Expand Down Expand Up @@ -71,23 +69,3 @@ async def test_state_unavailable(

state = hass.states.get("sensor.file_txt_size")
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