Remove deprecated yaml from foscam (#61761)
This commit is contained in:
parent
8cda315cd1
commit
dfcadd600c
4 changed files with 4 additions and 254 deletions
|
@ -23,8 +23,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||||
"""Set up foscam from a config entry."""
|
"""Set up foscam from a config entry."""
|
||||||
hass.config_entries.async_setup_platforms(entry, PLATFORMS)
|
hass.config_entries.async_setup_platforms(entry, PLATFORMS)
|
||||||
|
|
||||||
hass.data.setdefault(DOMAIN, {})
|
hass.data.setdefault(DOMAIN, {})[entry.entry_id] = entry.data
|
||||||
hass.data[DOMAIN][entry.entry_id] = entry.data
|
|
||||||
|
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
|
@ -6,36 +6,11 @@ import asyncio
|
||||||
from libpyfoscam import FoscamCamera
|
from libpyfoscam import FoscamCamera
|
||||||
import voluptuous as vol
|
import voluptuous as vol
|
||||||
|
|
||||||
from homeassistant.components.camera import PLATFORM_SCHEMA, SUPPORT_STREAM, Camera
|
from homeassistant.components.camera import SUPPORT_STREAM, Camera
|
||||||
from homeassistant.config_entries import SOURCE_IMPORT
|
from homeassistant.const import CONF_HOST, CONF_PASSWORD, CONF_PORT, CONF_USERNAME
|
||||||
from homeassistant.const import (
|
|
||||||
CONF_HOST,
|
|
||||||
CONF_NAME,
|
|
||||||
CONF_PASSWORD,
|
|
||||||
CONF_PORT,
|
|
||||||
CONF_USERNAME,
|
|
||||||
)
|
|
||||||
from homeassistant.helpers import config_validation as cv, entity_platform
|
from homeassistant.helpers import config_validation as cv, entity_platform
|
||||||
|
|
||||||
from .const import (
|
from .const import CONF_RTSP_PORT, CONF_STREAM, LOGGER, SERVICE_PTZ, SERVICE_PTZ_PRESET
|
||||||
CONF_RTSP_PORT,
|
|
||||||
CONF_STREAM,
|
|
||||||
DOMAIN,
|
|
||||||
LOGGER,
|
|
||||||
SERVICE_PTZ,
|
|
||||||
SERVICE_PTZ_PRESET,
|
|
||||||
)
|
|
||||||
|
|
||||||
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
|
||||||
{
|
|
||||||
vol.Required("ip"): cv.string,
|
|
||||||
vol.Required(CONF_PASSWORD): cv.string,
|
|
||||||
vol.Required(CONF_USERNAME): cv.string,
|
|
||||||
vol.Optional(CONF_NAME, default="Foscam Camera"): cv.string,
|
|
||||||
vol.Optional(CONF_PORT, default=88): cv.port,
|
|
||||||
vol.Optional(CONF_RTSP_PORT): cv.port,
|
|
||||||
}
|
|
||||||
)
|
|
||||||
|
|
||||||
DIR_UP = "up"
|
DIR_UP = "up"
|
||||||
DIR_DOWN = "down"
|
DIR_DOWN = "down"
|
||||||
|
@ -67,29 +42,6 @@ ATTR_PRESET_NAME = "preset_name"
|
||||||
PTZ_GOTO_PRESET_COMMAND = "ptz_goto_preset"
|
PTZ_GOTO_PRESET_COMMAND = "ptz_goto_preset"
|
||||||
|
|
||||||
|
|
||||||
async def async_setup_platform(hass, config, async_add_entities, discovery_info=None):
|
|
||||||
"""Set up a Foscam IP Camera."""
|
|
||||||
LOGGER.warning(
|
|
||||||
"Loading foscam via platform config is deprecated, it will be automatically imported; Please remove it afterwards"
|
|
||||||
)
|
|
||||||
|
|
||||||
config_new = {
|
|
||||||
CONF_NAME: config[CONF_NAME],
|
|
||||||
CONF_HOST: config["ip"],
|
|
||||||
CONF_PORT: config[CONF_PORT],
|
|
||||||
CONF_USERNAME: config[CONF_USERNAME],
|
|
||||||
CONF_PASSWORD: config[CONF_PASSWORD],
|
|
||||||
CONF_STREAM: "Main",
|
|
||||||
CONF_RTSP_PORT: config.get(CONF_RTSP_PORT, 554),
|
|
||||||
}
|
|
||||||
|
|
||||||
hass.async_create_task(
|
|
||||||
hass.config_entries.flow.async_init(
|
|
||||||
DOMAIN, context={"source": SOURCE_IMPORT}, data=config_new
|
|
||||||
)
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
async def async_setup_entry(hass, config_entry, async_add_entities):
|
async def async_setup_entry(hass, config_entry, async_add_entities):
|
||||||
"""Add a Foscam IP camera from a config entry."""
|
"""Add a Foscam IP camera from a config entry."""
|
||||||
platform = entity_platform.async_get_current_platform()
|
platform = entity_platform.async_get_current_platform()
|
||||||
|
|
|
@ -116,34 +116,6 @@ class ConfigFlow(config_entries.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, import_config):
|
|
||||||
"""Handle config import from yaml."""
|
|
||||||
try:
|
|
||||||
return await self._validate_and_create(import_config)
|
|
||||||
|
|
||||||
except CannotConnect:
|
|
||||||
LOGGER.error("Error importing foscam platform config: cannot connect")
|
|
||||||
return self.async_abort(reason="cannot_connect")
|
|
||||||
|
|
||||||
except InvalidAuth:
|
|
||||||
LOGGER.error("Error importing foscam platform config: invalid auth")
|
|
||||||
return self.async_abort(reason="invalid_auth")
|
|
||||||
|
|
||||||
except InvalidResponse:
|
|
||||||
LOGGER.exception(
|
|
||||||
"Error importing foscam platform config: invalid response from camera"
|
|
||||||
)
|
|
||||||
return self.async_abort(reason="invalid_response")
|
|
||||||
|
|
||||||
except AbortFlow:
|
|
||||||
raise
|
|
||||||
|
|
||||||
except Exception: # pylint: disable=broad-except
|
|
||||||
LOGGER.exception(
|
|
||||||
"Error importing foscam platform config: unexpected exception"
|
|
||||||
)
|
|
||||||
return self.async_abort(reason="unknown")
|
|
||||||
|
|
||||||
|
|
||||||
class CannotConnect(exceptions.HomeAssistantError):
|
class CannotConnect(exceptions.HomeAssistantError):
|
||||||
"""Error to indicate we cannot connect."""
|
"""Error to indicate we cannot connect."""
|
||||||
|
|
|
@ -245,176 +245,3 @@ async def test_user_unknown_exception(hass):
|
||||||
|
|
||||||
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
|
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
|
||||||
assert result["errors"] == {"base": "unknown"}
|
assert result["errors"] == {"base": "unknown"}
|
||||||
|
|
||||||
|
|
||||||
async def test_import_user_valid(hass):
|
|
||||||
"""Test valid config from import."""
|
|
||||||
|
|
||||||
with patch(
|
|
||||||
"homeassistant.components.foscam.config_flow.FoscamCamera",
|
|
||||||
) as mock_foscam_camera, patch(
|
|
||||||
"homeassistant.components.foscam.async_setup_entry",
|
|
||||||
return_value=True,
|
|
||||||
) as mock_setup_entry:
|
|
||||||
setup_mock_foscam_camera(mock_foscam_camera)
|
|
||||||
|
|
||||||
result = await hass.config_entries.flow.async_init(
|
|
||||||
config_flow.DOMAIN,
|
|
||||||
context={"source": config_entries.SOURCE_IMPORT},
|
|
||||||
data=VALID_CONFIG,
|
|
||||||
)
|
|
||||||
|
|
||||||
await hass.async_block_till_done()
|
|
||||||
|
|
||||||
assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
|
|
||||||
assert result["title"] == CAMERA_NAME
|
|
||||||
assert result["data"] == VALID_CONFIG
|
|
||||||
|
|
||||||
assert len(mock_setup_entry.mock_calls) == 1
|
|
||||||
|
|
||||||
|
|
||||||
async def test_import_user_valid_with_name(hass):
|
|
||||||
"""Test valid config with extra name from import."""
|
|
||||||
|
|
||||||
with patch(
|
|
||||||
"homeassistant.components.foscam.config_flow.FoscamCamera",
|
|
||||||
) as mock_foscam_camera, patch(
|
|
||||||
"homeassistant.components.foscam.async_setup_entry",
|
|
||||||
return_value=True,
|
|
||||||
) as mock_setup_entry:
|
|
||||||
setup_mock_foscam_camera(mock_foscam_camera)
|
|
||||||
|
|
||||||
name = CAMERA_NAME + " 1234"
|
|
||||||
with_name = VALID_CONFIG.copy()
|
|
||||||
with_name[config_flow.CONF_NAME] = name
|
|
||||||
|
|
||||||
result = await hass.config_entries.flow.async_init(
|
|
||||||
config_flow.DOMAIN,
|
|
||||||
context={"source": config_entries.SOURCE_IMPORT},
|
|
||||||
data=with_name,
|
|
||||||
)
|
|
||||||
|
|
||||||
await hass.async_block_till_done()
|
|
||||||
|
|
||||||
assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
|
|
||||||
assert result["title"] == name
|
|
||||||
assert result["data"] == VALID_CONFIG
|
|
||||||
|
|
||||||
assert len(mock_setup_entry.mock_calls) == 1
|
|
||||||
|
|
||||||
|
|
||||||
async def test_import_invalid_auth(hass):
|
|
||||||
"""Test we handle invalid auth from import."""
|
|
||||||
|
|
||||||
with patch(
|
|
||||||
"homeassistant.components.foscam.config_flow.FoscamCamera",
|
|
||||||
) as mock_foscam_camera:
|
|
||||||
setup_mock_foscam_camera(mock_foscam_camera)
|
|
||||||
|
|
||||||
invalid_user = VALID_CONFIG.copy()
|
|
||||||
invalid_user[config_flow.CONF_USERNAME] = "invalid"
|
|
||||||
|
|
||||||
result = await hass.config_entries.flow.async_init(
|
|
||||||
config_flow.DOMAIN,
|
|
||||||
context={"source": config_entries.SOURCE_IMPORT},
|
|
||||||
data=invalid_user,
|
|
||||||
)
|
|
||||||
|
|
||||||
await hass.async_block_till_done()
|
|
||||||
|
|
||||||
assert result["type"] == data_entry_flow.RESULT_TYPE_ABORT
|
|
||||||
assert result["reason"] == "invalid_auth"
|
|
||||||
|
|
||||||
|
|
||||||
async def test_import_cannot_connect(hass):
|
|
||||||
"""Test we handle cannot connect error from import."""
|
|
||||||
|
|
||||||
with patch(
|
|
||||||
"homeassistant.components.foscam.config_flow.FoscamCamera",
|
|
||||||
) as mock_foscam_camera:
|
|
||||||
setup_mock_foscam_camera(mock_foscam_camera)
|
|
||||||
|
|
||||||
invalid_host = VALID_CONFIG.copy()
|
|
||||||
invalid_host[config_flow.CONF_HOST] = "127.0.0.1"
|
|
||||||
|
|
||||||
result = await hass.config_entries.flow.async_init(
|
|
||||||
config_flow.DOMAIN,
|
|
||||||
context={"source": config_entries.SOURCE_IMPORT},
|
|
||||||
data=invalid_host,
|
|
||||||
)
|
|
||||||
|
|
||||||
await hass.async_block_till_done()
|
|
||||||
|
|
||||||
assert result["type"] == data_entry_flow.RESULT_TYPE_ABORT
|
|
||||||
assert result["reason"] == "cannot_connect"
|
|
||||||
|
|
||||||
|
|
||||||
async def test_import_invalid_response(hass):
|
|
||||||
"""Test we handle invalid response error from import."""
|
|
||||||
|
|
||||||
with patch(
|
|
||||||
"homeassistant.components.foscam.config_flow.FoscamCamera",
|
|
||||||
) as mock_foscam_camera:
|
|
||||||
setup_mock_foscam_camera(mock_foscam_camera)
|
|
||||||
|
|
||||||
invalid_response = VALID_CONFIG.copy()
|
|
||||||
invalid_response[config_flow.CONF_USERNAME] = INVALID_RESPONSE_CONFIG[
|
|
||||||
config_flow.CONF_USERNAME
|
|
||||||
]
|
|
||||||
|
|
||||||
result = await hass.config_entries.flow.async_init(
|
|
||||||
config_flow.DOMAIN,
|
|
||||||
context={"source": config_entries.SOURCE_IMPORT},
|
|
||||||
data=invalid_response,
|
|
||||||
)
|
|
||||||
|
|
||||||
await hass.async_block_till_done()
|
|
||||||
|
|
||||||
assert result["type"] == data_entry_flow.RESULT_TYPE_ABORT
|
|
||||||
assert result["reason"] == "invalid_response"
|
|
||||||
|
|
||||||
|
|
||||||
async def test_import_already_configured(hass):
|
|
||||||
"""Test we handle already configured from import."""
|
|
||||||
|
|
||||||
entry = MockConfigEntry(
|
|
||||||
domain=config_flow.DOMAIN,
|
|
||||||
data=VALID_CONFIG,
|
|
||||||
)
|
|
||||||
entry.add_to_hass(hass)
|
|
||||||
|
|
||||||
with patch(
|
|
||||||
"homeassistant.components.foscam.config_flow.FoscamCamera",
|
|
||||||
) as mock_foscam_camera:
|
|
||||||
setup_mock_foscam_camera(mock_foscam_camera)
|
|
||||||
|
|
||||||
result = await hass.config_entries.flow.async_init(
|
|
||||||
config_flow.DOMAIN,
|
|
||||||
context={"source": config_entries.SOURCE_IMPORT},
|
|
||||||
data=VALID_CONFIG,
|
|
||||||
)
|
|
||||||
|
|
||||||
await hass.async_block_till_done()
|
|
||||||
|
|
||||||
assert result["type"] == data_entry_flow.RESULT_TYPE_ABORT
|
|
||||||
assert result["reason"] == "already_configured"
|
|
||||||
|
|
||||||
|
|
||||||
async def test_import_unknown_exception(hass):
|
|
||||||
"""Test we handle unknown exceptions from import."""
|
|
||||||
|
|
||||||
with patch(
|
|
||||||
"homeassistant.components.foscam.config_flow.FoscamCamera",
|
|
||||||
) as mock_foscam_camera:
|
|
||||||
mock_foscam_camera.side_effect = Exception("test")
|
|
||||||
|
|
||||||
result = await hass.config_entries.flow.async_init(
|
|
||||||
config_flow.DOMAIN,
|
|
||||||
context={"source": config_entries.SOURCE_IMPORT},
|
|
||||||
data=VALID_CONFIG,
|
|
||||||
)
|
|
||||||
|
|
||||||
await hass.async_block_till_done()
|
|
||||||
|
|
||||||
assert result["type"] == data_entry_flow.RESULT_TYPE_ABORT
|
|
||||||
assert result["reason"] == "unknown"
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue