Address after merge review comments on IntelliFire (#64553)
* Remove unneeded coordinator update method parameter * Remove unused error * Remove unused translation strings * Remove leftover debug print from tests * Improve tests * Cleanup unused entry ID * Typing completionspull/61237/head
parent
8b30751839
commit
8b8f750c41
|
@ -70,9 +70,7 @@ async def async_setup_entry(
|
||||||
coordinator: IntellifireDataUpdateCoordinator = hass.data[DOMAIN][entry.entry_id]
|
coordinator: IntellifireDataUpdateCoordinator = hass.data[DOMAIN][entry.entry_id]
|
||||||
|
|
||||||
async_add_entities(
|
async_add_entities(
|
||||||
IntellifireBinarySensor(
|
IntellifireBinarySensor(coordinator=coordinator, description=description)
|
||||||
coordinator=coordinator, entry_id=entry.entry_id, description=description
|
|
||||||
)
|
|
||||||
for description in INTELLIFIRE_BINARY_SENSORS
|
for description in INTELLIFIRE_BINARY_SENSORS
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -87,7 +85,6 @@ class IntellifireBinarySensor(CoordinatorEntity, BinarySensorEntity):
|
||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
coordinator: IntellifireDataUpdateCoordinator,
|
coordinator: IntellifireDataUpdateCoordinator,
|
||||||
entry_id: str,
|
|
||||||
description: IntellifireBinarySensorEntityDescription,
|
description: IntellifireBinarySensorEntityDescription,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Class initializer."""
|
"""Class initializer."""
|
||||||
|
|
|
@ -11,7 +11,6 @@ from homeassistant import config_entries
|
||||||
from homeassistant.const import CONF_HOST
|
from homeassistant.const import CONF_HOST
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
from homeassistant.data_entry_flow import FlowResult
|
from homeassistant.data_entry_flow import FlowResult
|
||||||
from homeassistant.exceptions import HomeAssistantError
|
|
||||||
|
|
||||||
from .const import DOMAIN
|
from .const import DOMAIN
|
||||||
|
|
||||||
|
@ -62,7 +61,3 @@ class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
|
||||||
return self.async_show_form(
|
return self.async_show_form(
|
||||||
step_id="user", data_schema=STEP_USER_DATA_SCHEMA, errors=errors
|
step_id="user", data_schema=STEP_USER_DATA_SCHEMA, errors=errors
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
class CannotConnect(HomeAssistantError):
|
|
||||||
"""Error to indicate we cannot connect."""
|
|
||||||
|
|
|
@ -24,11 +24,10 @@ class IntellifireDataUpdateCoordinator(DataUpdateCoordinator[IntellifirePollData
|
||||||
LOGGER,
|
LOGGER,
|
||||||
name=DOMAIN,
|
name=DOMAIN,
|
||||||
update_interval=timedelta(seconds=15),
|
update_interval=timedelta(seconds=15),
|
||||||
update_method=self._async_update_data,
|
|
||||||
)
|
)
|
||||||
self._api = api
|
self._api = api
|
||||||
|
|
||||||
async def _async_update_data(self):
|
async def _async_update_data(self) -> IntellifirePollData:
|
||||||
LOGGER.debug("Calling update loop on IntelliFire")
|
LOGGER.debug("Calling update loop on IntelliFire")
|
||||||
async with timeout(100):
|
async with timeout(100):
|
||||||
try:
|
try:
|
||||||
|
@ -38,12 +37,12 @@ class IntellifireDataUpdateCoordinator(DataUpdateCoordinator[IntellifirePollData
|
||||||
return self._api.data
|
return self._api.data
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def api(self):
|
def api(self) -> IntellifireAsync:
|
||||||
"""Return the API pointer."""
|
"""Return the API pointer."""
|
||||||
return self._api
|
return self._api
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def device_info(self):
|
def device_info(self) -> DeviceInfo:
|
||||||
"""Return the device info."""
|
"""Return the device info."""
|
||||||
return DeviceInfo(
|
return DeviceInfo(
|
||||||
manufacturer="Hearth and Home",
|
manufacturer="Hearth and Home",
|
||||||
|
|
|
@ -8,8 +8,7 @@
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"error": {
|
"error": {
|
||||||
"cannot_connect": "[%key:common::config_flow::error::cannot_connect%]",
|
"cannot_connect": "[%key:common::config_flow::error::cannot_connect%]"
|
||||||
"unknown": "[%key:common::config_flow::error::unknown%]"
|
|
||||||
},
|
},
|
||||||
"abort": {
|
"abort": {
|
||||||
"already_configured": "[%key:common::config_flow::abort::already_configured_device%]"
|
"already_configured": "[%key:common::config_flow::abort::already_configured_device%]"
|
||||||
|
|
|
@ -4,8 +4,7 @@
|
||||||
"already_configured": "Device is already configured"
|
"already_configured": "Device is already configured"
|
||||||
},
|
},
|
||||||
"error": {
|
"error": {
|
||||||
"cannot_connect": "Failed to connect",
|
"cannot_connect": "Failed to connect"
|
||||||
"unknown": "Unexpected error"
|
|
||||||
},
|
},
|
||||||
"step": {
|
"step": {
|
||||||
"user": {
|
"user": {
|
||||||
|
|
|
@ -0,0 +1,29 @@
|
||||||
|
"""Fixtures for IntelliFire integration tests."""
|
||||||
|
from collections.abc import Generator
|
||||||
|
from unittest.mock import AsyncMock, MagicMock, Mock, patch
|
||||||
|
|
||||||
|
import pytest
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def mock_setup_entry() -> Generator[AsyncMock, None, None]:
|
||||||
|
"""Mock setting up a config entry."""
|
||||||
|
with patch(
|
||||||
|
"homeassistant.components.intellifire.async_setup_entry", return_value=True
|
||||||
|
) as mock_setup:
|
||||||
|
yield mock_setup
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def mock_intellifire_config_flow() -> Generator[None, MagicMock, None]:
|
||||||
|
"""Return a mocked IntelliFire client."""
|
||||||
|
data_mock = Mock()
|
||||||
|
data_mock.serial = "12345"
|
||||||
|
|
||||||
|
with patch(
|
||||||
|
"homeassistant.components.intellifire.config_flow.IntellifireAsync",
|
||||||
|
autospec=True,
|
||||||
|
) as intellifire_mock:
|
||||||
|
intellifire = intellifire_mock.return_value
|
||||||
|
intellifire.data = data_mock
|
||||||
|
yield intellifire
|
|
@ -1,14 +1,17 @@
|
||||||
"""Test the IntelliFire config flow."""
|
"""Test the IntelliFire config flow."""
|
||||||
from unittest.mock import Mock, patch
|
from unittest.mock import AsyncMock, MagicMock
|
||||||
|
|
||||||
from homeassistant import config_entries
|
from homeassistant import config_entries
|
||||||
from homeassistant.components.intellifire.config_flow import validate_input
|
|
||||||
from homeassistant.components.intellifire.const import DOMAIN
|
from homeassistant.components.intellifire.const import DOMAIN
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
from homeassistant.data_entry_flow import RESULT_TYPE_CREATE_ENTRY, RESULT_TYPE_FORM
|
from homeassistant.data_entry_flow import RESULT_TYPE_CREATE_ENTRY, RESULT_TYPE_FORM
|
||||||
|
|
||||||
|
|
||||||
async def test_form(hass: HomeAssistant) -> None:
|
async def test_form(
|
||||||
|
hass: HomeAssistant,
|
||||||
|
mock_setup_entry: AsyncMock,
|
||||||
|
mock_intellifire_config_flow: MagicMock,
|
||||||
|
) -> None:
|
||||||
"""Test we get the form."""
|
"""Test we get the form."""
|
||||||
result = await hass.config_entries.flow.async_init(
|
result = await hass.config_entries.flow.async_init(
|
||||||
DOMAIN, context={"source": config_entries.SOURCE_USER}
|
DOMAIN, context={"source": config_entries.SOURCE_USER}
|
||||||
|
@ -16,98 +19,37 @@ async def test_form(hass: HomeAssistant) -> None:
|
||||||
assert result["type"] == RESULT_TYPE_FORM
|
assert result["type"] == RESULT_TYPE_FORM
|
||||||
assert result["errors"] is None
|
assert result["errors"] is None
|
||||||
|
|
||||||
with patch(
|
result2 = await hass.config_entries.flow.async_configure(
|
||||||
"homeassistant.components.intellifire.config_flow.validate_input",
|
result["flow_id"],
|
||||||
return_value={
|
{
|
||||||
"title": "Living Room Fireplace",
|
|
||||||
"type": "Fireplace",
|
|
||||||
"serial": "abcd1234",
|
|
||||||
"host": "1.1.1.1",
|
"host": "1.1.1.1",
|
||||||
},
|
},
|
||||||
), patch(
|
)
|
||||||
"homeassistant.components.intellifire.async_setup_entry", return_value=True
|
await hass.async_block_till_done()
|
||||||
) as mock_setup_entry:
|
|
||||||
print("mock_setup_entry", mock_setup_entry)
|
|
||||||
result2 = await hass.config_entries.flow.async_configure(
|
|
||||||
result["flow_id"],
|
|
||||||
{
|
|
||||||
"host": "1.1.1.1",
|
|
||||||
},
|
|
||||||
)
|
|
||||||
await hass.async_block_till_done()
|
|
||||||
|
|
||||||
assert result2["type"] == RESULT_TYPE_CREATE_ENTRY
|
assert result2["type"] == RESULT_TYPE_CREATE_ENTRY
|
||||||
assert result2["title"] == "Fireplace"
|
assert result2["title"] == "Fireplace"
|
||||||
assert result2["data"] == {"host": "1.1.1.1"}
|
assert result2["data"] == {"host": "1.1.1.1"}
|
||||||
|
|
||||||
|
assert len(mock_setup_entry.mock_calls) == 1
|
||||||
|
|
||||||
async def test_form_cannot_connect(hass: HomeAssistant) -> None:
|
|
||||||
|
async def test_form_cannot_connect(
|
||||||
|
hass: HomeAssistant, mock_intellifire_config_flow: MagicMock
|
||||||
|
) -> None:
|
||||||
"""Test we handle cannot connect error."""
|
"""Test we handle cannot connect error."""
|
||||||
|
mock_intellifire_config_flow.poll.side_effect = ConnectionError
|
||||||
|
|
||||||
result = await hass.config_entries.flow.async_init(
|
result = await hass.config_entries.flow.async_init(
|
||||||
DOMAIN, context={"source": config_entries.SOURCE_USER}
|
DOMAIN, context={"source": config_entries.SOURCE_USER}
|
||||||
)
|
)
|
||||||
|
|
||||||
with patch(
|
result2 = await hass.config_entries.flow.async_configure(
|
||||||
"intellifire4py.IntellifireAsync.poll",
|
result["flow_id"],
|
||||||
side_effect=ConnectionError,
|
{
|
||||||
):
|
"host": "1.1.1.1",
|
||||||
result2 = await hass.config_entries.flow.async_configure(
|
},
|
||||||
result["flow_id"],
|
)
|
||||||
{
|
|
||||||
"host": "1.1.1.1",
|
|
||||||
},
|
|
||||||
)
|
|
||||||
|
|
||||||
assert result2["type"] == RESULT_TYPE_FORM
|
assert result2["type"] == RESULT_TYPE_FORM
|
||||||
assert result2["errors"] == {"base": "cannot_connect"}
|
assert result2["errors"] == {"base": "cannot_connect"}
|
||||||
|
|
||||||
|
|
||||||
async def test_form_good(hass: HomeAssistant) -> None:
|
|
||||||
"""Test we handle cannot connect error."""
|
|
||||||
result = await hass.config_entries.flow.async_init(
|
|
||||||
DOMAIN, context={"source": config_entries.SOURCE_USER}
|
|
||||||
)
|
|
||||||
|
|
||||||
with patch(
|
|
||||||
"intellifire4py.IntellifireAsync.poll",
|
|
||||||
side_effect=ConnectionError,
|
|
||||||
):
|
|
||||||
result2 = await hass.config_entries.flow.async_configure(
|
|
||||||
result["flow_id"],
|
|
||||||
{
|
|
||||||
"host": "1.1.1.1",
|
|
||||||
},
|
|
||||||
)
|
|
||||||
|
|
||||||
assert result2["type"] == RESULT_TYPE_FORM
|
|
||||||
assert result2["errors"] == {"base": "cannot_connect"}
|
|
||||||
|
|
||||||
|
|
||||||
async def test_validate_input(hass: HomeAssistant) -> None:
|
|
||||||
"""Test for the ideal case."""
|
|
||||||
# Define a mock object
|
|
||||||
data_mock = Mock()
|
|
||||||
data_mock.serial = "12345"
|
|
||||||
|
|
||||||
result = await hass.config_entries.flow.async_init(
|
|
||||||
DOMAIN, context={"source": config_entries.SOURCE_USER}
|
|
||||||
)
|
|
||||||
with patch(
|
|
||||||
"homeassistant.components.intellifire.config_flow.validate_input",
|
|
||||||
return_value="abcd1234",
|
|
||||||
), patch("intellifire4py.IntellifireAsync.poll", return_value=3), patch(
|
|
||||||
"intellifire4py.IntellifireAsync.data", return_value="something"
|
|
||||||
), patch(
|
|
||||||
"intellifire4py.IntellifireAsync.data.serial", return_value="1234"
|
|
||||||
), patch(
|
|
||||||
"intellifire4py.intellifire_async.IntellifireAsync", return_value="1111"
|
|
||||||
), patch(
|
|
||||||
"intellifire4py.IntellifireAsync", return_value=True
|
|
||||||
), patch(
|
|
||||||
"intellifire4py.model.IntellifirePollData", new=data_mock
|
|
||||||
) as mobj:
|
|
||||||
assert mobj.serial == "12345"
|
|
||||||
|
|
||||||
result = await validate_input(hass, {"host": "127.0.0.1"})
|
|
||||||
|
|
||||||
assert result() == "1234"
|
|
||||||
|
|
Loading…
Reference in New Issue