Remove YAML support from cert_expiry (#132350)
* Deprecate yaml import in cert_expiry * Simplify * Do full cleanup * Cleanup morepull/132768/head
parent
c6bcd5a036
commit
7ba5038509
|
@ -94,10 +94,3 @@ class CertexpiryConfigFlow(ConfigFlow, domain=DOMAIN):
|
||||||
),
|
),
|
||||||
errors=self._errors,
|
errors=self._errors,
|
||||||
)
|
)
|
||||||
|
|
||||||
async def async_step_import(self, import_data: dict[str, Any]) -> ConfigFlowResult:
|
|
||||||
"""Import a config entry.
|
|
||||||
|
|
||||||
Only host was required in the yaml file all other fields are optional
|
|
||||||
"""
|
|
||||||
return await self.async_step_user(import_data)
|
|
||||||
|
|
|
@ -2,63 +2,18 @@
|
||||||
|
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
from datetime import datetime, timedelta
|
from datetime import datetime
|
||||||
|
|
||||||
import voluptuous as vol
|
from homeassistant.components.sensor import SensorDeviceClass, SensorEntity
|
||||||
|
from homeassistant.core import HomeAssistant
|
||||||
from homeassistant.components.sensor import (
|
|
||||||
PLATFORM_SCHEMA as SENSOR_PLATFORM_SCHEMA,
|
|
||||||
SensorDeviceClass,
|
|
||||||
SensorEntity,
|
|
||||||
)
|
|
||||||
from homeassistant.config_entries import SOURCE_IMPORT
|
|
||||||
from homeassistant.const import CONF_HOST, CONF_PORT, EVENT_HOMEASSISTANT_START
|
|
||||||
from homeassistant.core import Event, HomeAssistant, callback
|
|
||||||
import homeassistant.helpers.config_validation as cv
|
|
||||||
from homeassistant.helpers.device_registry import DeviceEntryType, DeviceInfo
|
from homeassistant.helpers.device_registry import DeviceEntryType, DeviceInfo
|
||||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||||
from homeassistant.helpers.event import async_call_later
|
|
||||||
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
|
|
||||||
|
|
||||||
from . import CertExpiryConfigEntry
|
from . import CertExpiryConfigEntry
|
||||||
from .const import DEFAULT_PORT, DOMAIN
|
from .const import DOMAIN
|
||||||
from .coordinator import CertExpiryDataUpdateCoordinator
|
from .coordinator import CertExpiryDataUpdateCoordinator
|
||||||
from .entity import CertExpiryEntity
|
from .entity import CertExpiryEntity
|
||||||
|
|
||||||
SCAN_INTERVAL = timedelta(hours=12)
|
|
||||||
|
|
||||||
PLATFORM_SCHEMA = SENSOR_PLATFORM_SCHEMA.extend(
|
|
||||||
{
|
|
||||||
vol.Required(CONF_HOST): cv.string,
|
|
||||||
vol.Optional(CONF_PORT, default=DEFAULT_PORT): cv.port,
|
|
||||||
}
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
async def async_setup_platform(
|
|
||||||
hass: HomeAssistant,
|
|
||||||
config: ConfigType,
|
|
||||||
async_add_entities: AddEntitiesCallback,
|
|
||||||
discovery_info: DiscoveryInfoType | None = None,
|
|
||||||
) -> None:
|
|
||||||
"""Set up certificate expiry sensor."""
|
|
||||||
|
|
||||||
@callback
|
|
||||||
def schedule_import(_: Event) -> None:
|
|
||||||
"""Schedule delayed import after HA is fully started."""
|
|
||||||
async_call_later(hass, 10, do_import)
|
|
||||||
|
|
||||||
@callback
|
|
||||||
def do_import(_: datetime) -> None:
|
|
||||||
"""Process YAML import."""
|
|
||||||
hass.async_create_task(
|
|
||||||
hass.config_entries.flow.async_init(
|
|
||||||
DOMAIN, context={"source": SOURCE_IMPORT}, data=dict(config)
|
|
||||||
)
|
|
||||||
)
|
|
||||||
|
|
||||||
hass.bus.async_listen_once(EVENT_HOMEASSISTANT_START, schedule_import)
|
|
||||||
|
|
||||||
|
|
||||||
async def async_setup_entry(
|
async def async_setup_entry(
|
||||||
hass: HomeAssistant,
|
hass: HomeAssistant,
|
||||||
|
|
|
@ -7,13 +7,12 @@ from unittest.mock import patch
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
from homeassistant import config_entries
|
from homeassistant import config_entries
|
||||||
from homeassistant.components.cert_expiry.const import DEFAULT_PORT, DOMAIN
|
from homeassistant.components.cert_expiry.const import DOMAIN
|
||||||
from homeassistant.const import CONF_HOST, CONF_NAME, CONF_PORT
|
from homeassistant.const import CONF_HOST, CONF_PORT
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
from homeassistant.data_entry_flow import FlowResultType
|
from homeassistant.data_entry_flow import FlowResultType
|
||||||
|
|
||||||
from .const import HOST, PORT
|
from .const import HOST, PORT
|
||||||
from .helpers import future_timestamp
|
|
||||||
|
|
||||||
from tests.common import MockConfigEntry
|
from tests.common import MockConfigEntry
|
||||||
|
|
||||||
|
@ -64,122 +63,6 @@ async def test_user_with_bad_cert(hass: HomeAssistant) -> None:
|
||||||
assert result["result"].unique_id == f"{HOST}:{PORT}"
|
assert result["result"].unique_id == f"{HOST}:{PORT}"
|
||||||
|
|
||||||
|
|
||||||
async def test_import_host_only(hass: HomeAssistant) -> None:
|
|
||||||
"""Test import with host only."""
|
|
||||||
with (
|
|
||||||
patch(
|
|
||||||
"homeassistant.components.cert_expiry.config_flow.get_cert_expiry_timestamp"
|
|
||||||
),
|
|
||||||
patch(
|
|
||||||
"homeassistant.components.cert_expiry.coordinator.get_cert_expiry_timestamp",
|
|
||||||
return_value=future_timestamp(1),
|
|
||||||
),
|
|
||||||
):
|
|
||||||
result = await hass.config_entries.flow.async_init(
|
|
||||||
DOMAIN,
|
|
||||||
context={"source": config_entries.SOURCE_IMPORT},
|
|
||||||
data={CONF_HOST: HOST},
|
|
||||||
)
|
|
||||||
await hass.async_block_till_done()
|
|
||||||
|
|
||||||
assert result["type"] is FlowResultType.CREATE_ENTRY
|
|
||||||
assert result["title"] == HOST
|
|
||||||
assert result["data"][CONF_HOST] == HOST
|
|
||||||
assert result["data"][CONF_PORT] == DEFAULT_PORT
|
|
||||||
assert result["result"].unique_id == f"{HOST}:{DEFAULT_PORT}"
|
|
||||||
|
|
||||||
|
|
||||||
async def test_import_host_and_port(hass: HomeAssistant) -> None:
|
|
||||||
"""Test import with host and port."""
|
|
||||||
with (
|
|
||||||
patch(
|
|
||||||
"homeassistant.components.cert_expiry.config_flow.get_cert_expiry_timestamp"
|
|
||||||
),
|
|
||||||
patch(
|
|
||||||
"homeassistant.components.cert_expiry.coordinator.get_cert_expiry_timestamp",
|
|
||||||
return_value=future_timestamp(1),
|
|
||||||
),
|
|
||||||
):
|
|
||||||
result = await hass.config_entries.flow.async_init(
|
|
||||||
DOMAIN,
|
|
||||||
context={"source": config_entries.SOURCE_IMPORT},
|
|
||||||
data={CONF_HOST: HOST, CONF_PORT: PORT},
|
|
||||||
)
|
|
||||||
await hass.async_block_till_done()
|
|
||||||
|
|
||||||
assert result["type"] is FlowResultType.CREATE_ENTRY
|
|
||||||
assert result["title"] == HOST
|
|
||||||
assert result["data"][CONF_HOST] == HOST
|
|
||||||
assert result["data"][CONF_PORT] == PORT
|
|
||||||
assert result["result"].unique_id == f"{HOST}:{PORT}"
|
|
||||||
|
|
||||||
|
|
||||||
async def test_import_non_default_port(hass: HomeAssistant) -> None:
|
|
||||||
"""Test import with host and non-default port."""
|
|
||||||
with (
|
|
||||||
patch(
|
|
||||||
"homeassistant.components.cert_expiry.config_flow.get_cert_expiry_timestamp"
|
|
||||||
),
|
|
||||||
patch(
|
|
||||||
"homeassistant.components.cert_expiry.coordinator.get_cert_expiry_timestamp",
|
|
||||||
return_value=future_timestamp(1),
|
|
||||||
),
|
|
||||||
):
|
|
||||||
result = await hass.config_entries.flow.async_init(
|
|
||||||
DOMAIN,
|
|
||||||
context={"source": config_entries.SOURCE_IMPORT},
|
|
||||||
data={CONF_HOST: HOST, CONF_PORT: 888},
|
|
||||||
)
|
|
||||||
await hass.async_block_till_done()
|
|
||||||
|
|
||||||
assert result["type"] is FlowResultType.CREATE_ENTRY
|
|
||||||
assert result["title"] == f"{HOST}:888"
|
|
||||||
assert result["data"][CONF_HOST] == HOST
|
|
||||||
assert result["data"][CONF_PORT] == 888
|
|
||||||
assert result["result"].unique_id == f"{HOST}:888"
|
|
||||||
|
|
||||||
|
|
||||||
async def test_import_with_name(hass: HomeAssistant) -> None:
|
|
||||||
"""Test import with name (deprecated)."""
|
|
||||||
with (
|
|
||||||
patch(
|
|
||||||
"homeassistant.components.cert_expiry.config_flow.get_cert_expiry_timestamp"
|
|
||||||
),
|
|
||||||
patch(
|
|
||||||
"homeassistant.components.cert_expiry.coordinator.get_cert_expiry_timestamp",
|
|
||||||
return_value=future_timestamp(1),
|
|
||||||
),
|
|
||||||
):
|
|
||||||
result = await hass.config_entries.flow.async_init(
|
|
||||||
DOMAIN,
|
|
||||||
context={"source": config_entries.SOURCE_IMPORT},
|
|
||||||
data={CONF_NAME: "legacy", CONF_HOST: HOST, CONF_PORT: PORT},
|
|
||||||
)
|
|
||||||
await hass.async_block_till_done()
|
|
||||||
|
|
||||||
assert result["type"] is FlowResultType.CREATE_ENTRY
|
|
||||||
assert result["title"] == HOST
|
|
||||||
assert result["data"][CONF_HOST] == HOST
|
|
||||||
assert result["data"][CONF_PORT] == PORT
|
|
||||||
assert result["result"].unique_id == f"{HOST}:{PORT}"
|
|
||||||
|
|
||||||
|
|
||||||
async def test_bad_import(hass: HomeAssistant) -> None:
|
|
||||||
"""Test import step."""
|
|
||||||
with patch(
|
|
||||||
"homeassistant.components.cert_expiry.helper.async_get_cert",
|
|
||||||
side_effect=ConnectionRefusedError(),
|
|
||||||
):
|
|
||||||
result = await hass.config_entries.flow.async_init(
|
|
||||||
DOMAIN,
|
|
||||||
context={"source": config_entries.SOURCE_IMPORT},
|
|
||||||
data={CONF_HOST: HOST},
|
|
||||||
)
|
|
||||||
|
|
||||||
assert result["type"] is FlowResultType.ABORT
|
|
||||||
assert result["reason"] == "import_failed"
|
|
||||||
|
|
||||||
|
|
||||||
async def test_abort_if_already_setup(hass: HomeAssistant) -> None:
|
async def test_abort_if_already_setup(hass: HomeAssistant) -> None:
|
||||||
"""Test we abort if the cert is already setup."""
|
"""Test we abort if the cert is already setup."""
|
||||||
MockConfigEntry(
|
MockConfigEntry(
|
||||||
|
@ -188,14 +71,6 @@ async def test_abort_if_already_setup(hass: HomeAssistant) -> None:
|
||||||
unique_id=f"{HOST}:{PORT}",
|
unique_id=f"{HOST}:{PORT}",
|
||||||
).add_to_hass(hass)
|
).add_to_hass(hass)
|
||||||
|
|
||||||
result = await hass.config_entries.flow.async_init(
|
|
||||||
DOMAIN,
|
|
||||||
context={"source": config_entries.SOURCE_IMPORT},
|
|
||||||
data={CONF_HOST: HOST, CONF_PORT: PORT},
|
|
||||||
)
|
|
||||||
assert result["type"] is FlowResultType.ABORT
|
|
||||||
assert result["reason"] == "already_configured"
|
|
||||||
|
|
||||||
result = await hass.config_entries.flow.async_init(
|
result = await hass.config_entries.flow.async_init(
|
||||||
DOMAIN,
|
DOMAIN,
|
||||||
context={"source": config_entries.SOURCE_USER},
|
context={"source": config_entries.SOURCE_USER},
|
||||||
|
|
|
@ -1,59 +1,24 @@
|
||||||
"""Tests for Cert Expiry setup."""
|
"""Tests for Cert Expiry setup."""
|
||||||
|
|
||||||
from datetime import timedelta
|
|
||||||
from unittest.mock import patch
|
from unittest.mock import patch
|
||||||
|
|
||||||
from freezegun import freeze_time
|
from freezegun import freeze_time
|
||||||
|
|
||||||
from homeassistant.components.cert_expiry.const import DOMAIN
|
from homeassistant.components.cert_expiry.const import 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 (
|
from homeassistant.const import (
|
||||||
CONF_HOST,
|
CONF_HOST,
|
||||||
CONF_PORT,
|
CONF_PORT,
|
||||||
EVENT_HOMEASSISTANT_START,
|
|
||||||
EVENT_HOMEASSISTANT_STARTED,
|
EVENT_HOMEASSISTANT_STARTED,
|
||||||
STATE_UNAVAILABLE,
|
STATE_UNAVAILABLE,
|
||||||
)
|
)
|
||||||
from homeassistant.core import CoreState, HomeAssistant
|
from homeassistant.core import CoreState, HomeAssistant
|
||||||
from homeassistant.setup import async_setup_component
|
from homeassistant.setup import async_setup_component
|
||||||
import homeassistant.util.dt as dt_util
|
|
||||||
|
|
||||||
from .const import HOST, PORT
|
from .const import HOST, PORT
|
||||||
from .helpers import future_timestamp, static_datetime
|
from .helpers import future_timestamp, static_datetime
|
||||||
|
|
||||||
from tests.common import MockConfigEntry, async_fire_time_changed
|
from tests.common import MockConfigEntry
|
||||||
|
|
||||||
|
|
||||||
async def test_setup_with_config(hass: HomeAssistant) -> None:
|
|
||||||
"""Test setup component with config."""
|
|
||||||
assert hass.state is CoreState.running
|
|
||||||
|
|
||||||
config = {
|
|
||||||
SENSOR_DOMAIN: [
|
|
||||||
{"platform": DOMAIN, CONF_HOST: HOST, CONF_PORT: PORT},
|
|
||||||
{"platform": DOMAIN, CONF_HOST: HOST, CONF_PORT: 888},
|
|
||||||
],
|
|
||||||
}
|
|
||||||
|
|
||||||
with (
|
|
||||||
patch(
|
|
||||||
"homeassistant.components.cert_expiry.config_flow.get_cert_expiry_timestamp"
|
|
||||||
),
|
|
||||||
patch(
|
|
||||||
"homeassistant.components.cert_expiry.coordinator.get_cert_expiry_timestamp",
|
|
||||||
return_value=future_timestamp(1),
|
|
||||||
),
|
|
||||||
):
|
|
||||||
assert await async_setup_component(hass, SENSOR_DOMAIN, config) is True
|
|
||||||
await hass.async_block_till_done()
|
|
||||||
hass.bus.async_fire(EVENT_HOMEASSISTANT_START)
|
|
||||||
await hass.async_block_till_done()
|
|
||||||
next_update = dt_util.utcnow() + timedelta(seconds=20)
|
|
||||||
async_fire_time_changed(hass, next_update)
|
|
||||||
await hass.async_block_till_done(wait_background_tasks=True)
|
|
||||||
|
|
||||||
assert len(hass.config_entries.async_entries(DOMAIN)) == 2
|
|
||||||
|
|
||||||
|
|
||||||
async def test_update_unique_id(hass: HomeAssistant) -> None:
|
async def test_update_unique_id(hass: HomeAssistant) -> None:
|
||||||
|
|
Loading…
Reference in New Issue