mirror of
https://github.com/home-assistant/core.git
synced 2026-11-30 01:33:12 -04:00
Add KMTronic Integration (#41682)
Co-authored-by: J. Nick Koston <nick@koston.org>
This commit is contained in:
co-authored by
J. Nick Koston
parent
5cd022a683
commit
5c29adea3d
@@ -0,0 +1 @@
|
||||
"""Tests for the kmtronic integration."""
|
||||
@@ -0,0 +1,145 @@
|
||||
"""Test the kmtronic config flow."""
|
||||
from unittest.mock import Mock, patch
|
||||
|
||||
from aiohttp import ClientConnectorError, ClientResponseError
|
||||
|
||||
from homeassistant import config_entries, setup
|
||||
from homeassistant.components.kmtronic.const import DOMAIN
|
||||
from homeassistant.config_entries import ENTRY_STATE_LOADED, ENTRY_STATE_NOT_LOADED
|
||||
|
||||
from tests.common import MockConfigEntry
|
||||
|
||||
|
||||
async def test_form(hass):
|
||||
"""Test we get the form."""
|
||||
await setup.async_setup_component(hass, "persistent_notification", {})
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": config_entries.SOURCE_USER}
|
||||
)
|
||||
assert result["type"] == "form"
|
||||
assert result["errors"] == {}
|
||||
|
||||
with patch(
|
||||
"homeassistant.components.kmtronic.config_flow.KMTronicHubAPI.async_get_status",
|
||||
return_value=[Mock()],
|
||||
), patch(
|
||||
"homeassistant.components.kmtronic.async_setup", return_value=True
|
||||
) as mock_setup, patch(
|
||||
"homeassistant.components.kmtronic.async_setup_entry",
|
||||
return_value=True,
|
||||
) as mock_setup_entry:
|
||||
result2 = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
{
|
||||
"host": "1.1.1.1",
|
||||
"username": "test-username",
|
||||
"password": "test-password",
|
||||
},
|
||||
)
|
||||
|
||||
assert result2["type"] == "create_entry"
|
||||
assert result2["title"] == "1.1.1.1"
|
||||
assert result2["data"] == {
|
||||
"host": "1.1.1.1",
|
||||
"username": "test-username",
|
||||
"password": "test-password",
|
||||
}
|
||||
await hass.async_block_till_done()
|
||||
assert len(mock_setup.mock_calls) == 1
|
||||
assert len(mock_setup_entry.mock_calls) == 1
|
||||
|
||||
|
||||
async def test_form_invalid_auth(hass):
|
||||
"""Test we handle invalid auth."""
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": config_entries.SOURCE_USER}
|
||||
)
|
||||
|
||||
with patch(
|
||||
"homeassistant.components.kmtronic.config_flow.KMTronicHubAPI.async_get_status",
|
||||
side_effect=ClientResponseError(None, None, status=401),
|
||||
):
|
||||
result2 = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
{
|
||||
"host": "1.1.1.1",
|
||||
"username": "test-username",
|
||||
"password": "test-password",
|
||||
},
|
||||
)
|
||||
|
||||
assert result2["type"] == "form"
|
||||
assert result2["errors"] == {"base": "invalid_auth"}
|
||||
|
||||
|
||||
async def test_form_cannot_connect(hass):
|
||||
"""Test we handle cannot connect error."""
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": config_entries.SOURCE_USER}
|
||||
)
|
||||
|
||||
with patch(
|
||||
"homeassistant.components.kmtronic.config_flow.KMTronicHubAPI.async_get_status",
|
||||
side_effect=ClientConnectorError(None, Mock()),
|
||||
):
|
||||
result2 = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
{
|
||||
"host": "1.1.1.1",
|
||||
"username": "test-username",
|
||||
"password": "test-password",
|
||||
},
|
||||
)
|
||||
|
||||
assert result2["type"] == "form"
|
||||
assert result2["errors"] == {"base": "cannot_connect"}
|
||||
|
||||
|
||||
async def test_form_unknown_error(hass):
|
||||
"""Test we handle unknown errors."""
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": config_entries.SOURCE_USER}
|
||||
)
|
||||
|
||||
with patch(
|
||||
"homeassistant.components.kmtronic.config_flow.KMTronicHubAPI.async_get_status",
|
||||
side_effect=Exception(),
|
||||
):
|
||||
result2 = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
{
|
||||
"host": "1.1.1.1",
|
||||
"username": "test-username",
|
||||
"password": "test-password",
|
||||
},
|
||||
)
|
||||
|
||||
assert result2["type"] == "form"
|
||||
assert result2["errors"] == {"base": "unknown"}
|
||||
|
||||
|
||||
async def test_unload_config_entry(hass, aioclient_mock):
|
||||
"""Test entry unloading."""
|
||||
|
||||
config_entry = MockConfigEntry(
|
||||
domain=DOMAIN,
|
||||
data={"host": "1.1.1.1", "username": "admin", "password": "admin"},
|
||||
)
|
||||
config_entry.add_to_hass(hass)
|
||||
|
||||
aioclient_mock.get(
|
||||
"http://1.1.1.1/status.xml",
|
||||
text="<response><relay0>0</relay0><relay1>0</relay1></response>",
|
||||
)
|
||||
await hass.config_entries.async_setup(config_entry.entry_id)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
config_entries = hass.config_entries.async_entries(DOMAIN)
|
||||
assert len(config_entries) == 1
|
||||
assert config_entries[0] is config_entry
|
||||
assert config_entry.state == ENTRY_STATE_LOADED
|
||||
|
||||
await hass.config_entries.async_unload(config_entry.entry_id)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert config_entry.state == ENTRY_STATE_NOT_LOADED
|
||||
@@ -0,0 +1,150 @@
|
||||
"""The tests for the KMtronic switch platform."""
|
||||
import asyncio
|
||||
from datetime import timedelta
|
||||
|
||||
from homeassistant.components.kmtronic.const import DOMAIN
|
||||
from homeassistant.config_entries import ENTRY_STATE_SETUP_RETRY
|
||||
from homeassistant.const import STATE_UNAVAILABLE
|
||||
from homeassistant.setup import async_setup_component
|
||||
from homeassistant.util import dt as dt_util
|
||||
|
||||
from tests.common import MockConfigEntry, async_fire_time_changed
|
||||
|
||||
|
||||
async def test_relay_on_off(hass, aioclient_mock):
|
||||
"""Tests the relay turns on correctly."""
|
||||
|
||||
aioclient_mock.get(
|
||||
"http://1.1.1.1/status.xml",
|
||||
text="<response><relay0>0</relay0><relay1>0</relay1></response>",
|
||||
)
|
||||
|
||||
MockConfigEntry(
|
||||
domain=DOMAIN, data={"host": "1.1.1.1", "username": "foo", "password": "bar"}
|
||||
).add_to_hass(hass)
|
||||
assert await async_setup_component(hass, DOMAIN, {})
|
||||
await hass.async_block_till_done()
|
||||
|
||||
# Mocks the response for turning a relay1 on
|
||||
aioclient_mock.get(
|
||||
"http://1.1.1.1/FF0101",
|
||||
text="",
|
||||
)
|
||||
|
||||
state = hass.states.get("switch.relay1")
|
||||
assert state.state == "off"
|
||||
|
||||
await hass.services.async_call(
|
||||
"switch", "turn_on", {"entity_id": "switch.relay1"}, blocking=True
|
||||
)
|
||||
|
||||
await hass.async_block_till_done()
|
||||
state = hass.states.get("switch.relay1")
|
||||
assert state.state == "on"
|
||||
|
||||
# Mocks the response for turning a relay1 off
|
||||
aioclient_mock.get(
|
||||
"http://1.1.1.1/FF0100",
|
||||
text="",
|
||||
)
|
||||
|
||||
await hass.services.async_call(
|
||||
"switch", "turn_off", {"entity_id": "switch.relay1"}, blocking=True
|
||||
)
|
||||
|
||||
await hass.async_block_till_done()
|
||||
state = hass.states.get("switch.relay1")
|
||||
assert state.state == "off"
|
||||
|
||||
|
||||
async def test_update(hass, aioclient_mock):
|
||||
"""Tests switch refreshes status periodically."""
|
||||
now = dt_util.utcnow()
|
||||
future = now + timedelta(minutes=10)
|
||||
|
||||
aioclient_mock.get(
|
||||
"http://1.1.1.1/status.xml",
|
||||
text="<response><relay0>0</relay0><relay1>0</relay1></response>",
|
||||
)
|
||||
|
||||
MockConfigEntry(
|
||||
domain=DOMAIN, data={"host": "1.1.1.1", "username": "foo", "password": "bar"}
|
||||
).add_to_hass(hass)
|
||||
assert await async_setup_component(hass, DOMAIN, {})
|
||||
|
||||
await hass.async_block_till_done()
|
||||
state = hass.states.get("switch.relay1")
|
||||
assert state.state == "off"
|
||||
|
||||
aioclient_mock.clear_requests()
|
||||
aioclient_mock.get(
|
||||
"http://1.1.1.1/status.xml",
|
||||
text="<response><relay0>1</relay0><relay1>1</relay1></response>",
|
||||
)
|
||||
async_fire_time_changed(hass, future)
|
||||
|
||||
await hass.async_block_till_done()
|
||||
state = hass.states.get("switch.relay1")
|
||||
assert state.state == "on"
|
||||
|
||||
|
||||
async def test_config_entry_not_ready(hass, aioclient_mock):
|
||||
"""Tests configuration entry not ready."""
|
||||
|
||||
aioclient_mock.get(
|
||||
"http://1.1.1.1/status.xml",
|
||||
exc=asyncio.TimeoutError(),
|
||||
)
|
||||
|
||||
config_entry = MockConfigEntry(
|
||||
domain=DOMAIN, data={"host": "1.1.1.1", "username": "foo", "password": "bar"}
|
||||
)
|
||||
config_entry.add_to_hass(hass)
|
||||
await hass.config_entries.async_setup(config_entry.entry_id)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert config_entry.state == ENTRY_STATE_SETUP_RETRY
|
||||
|
||||
|
||||
async def test_failed_update(hass, aioclient_mock):
|
||||
"""Tests coordinator update fails."""
|
||||
now = dt_util.utcnow()
|
||||
future = now + timedelta(minutes=10)
|
||||
|
||||
aioclient_mock.get(
|
||||
"http://1.1.1.1/status.xml",
|
||||
text="<response><relay0>0</relay0><relay1>0</relay1></response>",
|
||||
)
|
||||
|
||||
MockConfigEntry(
|
||||
domain=DOMAIN, data={"host": "1.1.1.1", "username": "foo", "password": "bar"}
|
||||
).add_to_hass(hass)
|
||||
assert await async_setup_component(hass, DOMAIN, {})
|
||||
|
||||
await hass.async_block_till_done()
|
||||
state = hass.states.get("switch.relay1")
|
||||
assert state.state == "off"
|
||||
|
||||
aioclient_mock.clear_requests()
|
||||
aioclient_mock.get(
|
||||
"http://1.1.1.1/status.xml",
|
||||
text="401 Unauthorized: Password required",
|
||||
status=401,
|
||||
)
|
||||
async_fire_time_changed(hass, future)
|
||||
|
||||
await hass.async_block_till_done()
|
||||
state = hass.states.get("switch.relay1")
|
||||
assert state.state == STATE_UNAVAILABLE
|
||||
|
||||
future += timedelta(minutes=10)
|
||||
aioclient_mock.clear_requests()
|
||||
aioclient_mock.get(
|
||||
"http://1.1.1.1/status.xml",
|
||||
exc=asyncio.TimeoutError(),
|
||||
)
|
||||
async_fire_time_changed(hass, future)
|
||||
|
||||
await hass.async_block_till_done()
|
||||
state = hass.states.get("switch.relay1")
|
||||
assert state.state == STATE_UNAVAILABLE
|
||||
Reference in New Issue
Block a user