Remove yaml config from modem_callerid (#59526)

pull/59848/head
Robert Hillis 2021-11-13 08:14:49 -05:00 committed by GitHub
parent 27b2aa04c9
commit 68e80f1431
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 10 additions and 102 deletions

View File

@ -1,10 +1,9 @@
"""Config flow for Modem Caller ID integration."""
from __future__ import annotations
import logging
from typing import Any
from phone_modem import DEFAULT_PORT, PhoneModem
from phone_modem import PhoneModem
import serial.tools.list_ports
from serial.tools.list_ports_common import ListPortInfo
import voluptuous as vol
@ -16,8 +15,6 @@ from homeassistant.data_entry_flow import FlowResult
from .const import DEFAULT_NAME, DOMAIN, EXCEPTIONS
_LOGGER = logging.getLogger(__name__)
DATA_SCHEMA = vol.Schema({"name": str, "device": str})
@ -102,30 +99,6 @@ class PhoneModemFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
schema = vol.Schema({vol.Required(CONF_DEVICE): vol.In(unused_ports)})
return self.async_show_form(step_id="user", data_schema=schema, errors=errors)
async def async_step_import(self, config: dict[str, Any]) -> FlowResult:
"""Import a config entry from configuration.yaml."""
if self._async_current_entries():
_LOGGER.warning(
"Loading Modem_callerid via platform setup is deprecated; Please remove it from your configuration"
)
if CONF_DEVICE not in config:
config[CONF_DEVICE] = DEFAULT_PORT
ports = await self.hass.async_add_executor_job(serial.tools.list_ports.comports)
for port in ports:
if port.device == config[CONF_DEVICE]:
if (
await self.validate_device_errors(
dev_path=port.device,
unique_id=_generate_unique_id(port),
)
is None
):
return self.async_create_entry(
title=config.get(CONF_NAME, DEFAULT_NAME),
data={CONF_DEVICE: port.device},
)
return self.async_abort(reason="cannot_connect")
async def validate_device_errors(
self, dev_path: str, unique_id: str
) -> dict[str, str] | None:

View File

@ -5,7 +5,6 @@ from phone_modem import exceptions
from serial import SerialException
DATA_KEY_API = "api"
DATA_KEY_COORDINATOR = "coordinator"
DEFAULT_NAME = "Phone Modem"
DOMAIN = "modem_callerid"
ICON = "mdi:phone-classic"

View File

@ -1,48 +1,15 @@
"""A sensor for incoming calls using a USB modem that supports caller ID."""
from __future__ import annotations
from phone_modem import DEFAULT_PORT, PhoneModem
import voluptuous as vol
from phone_modem import PhoneModem
from homeassistant.components.sensor import PLATFORM_SCHEMA, SensorEntity
from homeassistant.config_entries import SOURCE_IMPORT, ConfigEntry
from homeassistant.const import (
CONF_DEVICE,
CONF_NAME,
EVENT_HOMEASSISTANT_STOP,
STATE_IDLE,
)
from homeassistant.components.sensor import SensorEntity
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import CONF_DEVICE, EVENT_HOMEASSISTANT_STOP, STATE_IDLE
from homeassistant.core import Event, HomeAssistant, callback
from homeassistant.helpers import config_validation as cv, entity_platform
from homeassistant.helpers.typing import DiscoveryInfoType
from homeassistant.helpers import entity_platform
from .const import CID, DATA_KEY_API, DEFAULT_NAME, DOMAIN, ICON, SERVICE_REJECT_CALL
# Deprecated in Home Assistant 2021.10
PLATFORM_SCHEMA = cv.deprecated(
vol.All(
PLATFORM_SCHEMA.extend(
{
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
vol.Optional(CONF_DEVICE, default=DEFAULT_PORT): cv.string,
}
)
)
)
async def async_setup_platform(
hass: HomeAssistant,
config: ConfigEntry,
async_add_entities: entity_platform.AddEntitiesCallback,
discovery_info: DiscoveryInfoType | None = None,
) -> None:
"""Set up the Modem Caller ID component."""
hass.async_create_task(
hass.config_entries.flow.async_init(
DOMAIN, context={"source": SOURCE_IMPORT}, data=config
)
)
from .const import CID, DATA_KEY_API, DOMAIN, ICON, SERVICE_REJECT_CALL
async def async_setup_entry(

View File

@ -5,8 +5,8 @@ import phone_modem
import serial.tools.list_ports
from homeassistant.components import usb
from homeassistant.components.modem_callerid.const import DEFAULT_NAME, DOMAIN
from homeassistant.config_entries import SOURCE_IMPORT, SOURCE_USB, SOURCE_USER
from homeassistant.components.modem_callerid.const import DOMAIN
from homeassistant.config_entries import SOURCE_USB, SOURCE_USER
from homeassistant.const import CONF_DEVICE, CONF_SOURCE
from homeassistant.core import HomeAssistant
from homeassistant.data_entry_flow import (
@ -15,7 +15,7 @@ from homeassistant.data_entry_flow import (
RESULT_TYPE_FORM,
)
from . import CONF_DATA, IMPORT_DATA, _patch_config_flow_modem
from . import _patch_config_flow_modem
DISCOVERY_INFO = {
"device": phone_modem.DEFAULT_PORT,
@ -171,34 +171,3 @@ async def test_abort_user_with_existing_flow(hass: HomeAssistant):
assert result2["type"] == RESULT_TYPE_ABORT
assert result2["reason"] == "already_in_progress"
@patch("serial.tools.list_ports.comports", MagicMock(return_value=[com_port()]))
async def test_flow_import(hass: HomeAssistant):
"""Test import step."""
with _patch_config_flow_modem(AsyncMock()):
result = await hass.config_entries.flow.async_init(
DOMAIN, context={CONF_SOURCE: SOURCE_IMPORT}, data=IMPORT_DATA
)
assert result["type"] == RESULT_TYPE_CREATE_ENTRY
assert result["title"] == DEFAULT_NAME
assert result["data"] == CONF_DATA
result = await hass.config_entries.flow.async_init(
DOMAIN, context={CONF_SOURCE: SOURCE_IMPORT}, data=IMPORT_DATA
)
assert result["type"] == RESULT_TYPE_ABORT
assert result["reason"] == "already_configured"
async def test_flow_import_cannot_connect(hass: HomeAssistant):
"""Test import connection error."""
with _patch_config_flow_modem(AsyncMock()) as modemmock:
modemmock.side_effect = phone_modem.exceptions.SerialError
result = await hass.config_entries.flow.async_init(
DOMAIN, context={CONF_SOURCE: SOURCE_IMPORT}, data=IMPORT_DATA
)
assert result["type"] == RESULT_TYPE_ABORT
assert result["reason"] == "cannot_connect"