Add new integration for Bond hub (#37477)

* create foundation for Bond integration

* add Bond hub integration (fix lint)

* Update homeassistant/components/bond/__init__.py

adding async_unload_entry per PR review suggestion

Co-authored-by: Chris Talkington <chris@talkingtontech.com>

* add Bond hub integration (fix missing import after applying PR suggestion)

* Update tests/components/bond/test_init.py

add a unit for unloading per PR review suggestion

Co-authored-by: Chris Talkington <chris@talkingtontech.com>

* Update tests/components/bond/test_init.py

add unit test for unload per PR review suggestion

Co-authored-by: Chris Talkington <chris@talkingtontech.com>

* Update tests/components/bond/test_init.py

PR review suggestion

Co-authored-by: Chris Talkington <chris@talkingtontech.com>

* Update tests/components/bond/test_init.py

PR review suggestion

Co-authored-by: Chris Talkington <chris@talkingtontech.com>

* Update tests/components/bond/test_init.py

PR review suggestion

Co-authored-by: Chris Talkington <chris@talkingtontech.com>

* add Bond hub integration (fix formatting)

* Update homeassistant/components/bond/manifest.json

PR suggestion

Co-authored-by: Chris Talkington <chris@talkingtontech.com>

* Update homeassistant/components/bond/manifest.json

PR suggestion

Co-authored-by: Chris Talkington <chris@talkingtontech.com>

* Update homeassistant/components/bond/manifest.json

PR suggestion

Co-authored-by: Chris Talkington <chris@talkingtontech.com>

* Update homeassistant/components/bond/strings.json

PR suggestion

Co-authored-by: Chris Talkington <chris@talkingtontech.com>

* Update homeassistant/components/bond/manifest.json

PR suggestion

Co-authored-by: Chris Talkington <chris@talkingtontech.com>

* Update requirements_all.txt

PR suggestion

Co-authored-by: Chris Talkington <chris@talkingtontech.com>

* Update homeassistant/components/bond/manifest.json

PR suggestion

Co-authored-by: Chris Talkington <chris@talkingtontech.com>

* Update requirements_test_all.txt

PR suggestion

Co-authored-by: Chris Talkington <chris@talkingtontech.com>

* add Bond hub integration (remove friendly name from config per PR suggestion)

* Update homeassistant/components/bond/__init__.py

add per PR review feedback

Co-authored-by: Chris Talkington <chris@talkingtontech.com>

* Update homeassistant/components/bond/__init__.py

remove per PR review feedback

Co-authored-by: Chris Talkington <chris@talkingtontech.com>

* Update tests/components/bond/test_init.py

fix unit test

Co-authored-by: Chris Talkington <chris@talkingtontech.com>

Co-authored-by: Chris Talkington <chris@talkingtontech.com>
This commit is contained in:
Eugene PrystupaandChris Talkington authored and GitHub committed 2020-07-05 20:17:53 -05:00
1 parent 6749424508
commit 9b77e16ffc
18 files changed
+617

No files matched your search

+1
View File
@@ -0,0 +1 @@
"""Tests for the Bond integration."""
+22
View File
@@ -0,0 +1,22 @@
"""Common methods used across tests for Bond."""
from homeassistant.components.bond.const import DOMAIN as BOND_DOMAIN
from homeassistant.const import CONF_ACCESS_TOKEN, CONF_HOST
from homeassistant.setup import async_setup_component
from tests.async_mock import patch
from tests.common import MockConfigEntry
async def setup_platform(hass, platform):
"""Set up the specified Bond platform."""
mock_entry = MockConfigEntry(
domain=BOND_DOMAIN,
data={CONF_HOST: "1.1.1.1", CONF_ACCESS_TOKEN: "test-token"},
)
mock_entry.add_to_hass(hass)
with patch("homeassistant.components.bond.PLATFORMS", [platform]):
assert await async_setup_component(hass, BOND_DOMAIN, {})
await hass.async_block_till_done()
return mock_entry
+94
View File
@@ -0,0 +1,94 @@
"""Test the Bond config flow."""
from requests.exceptions import ConnectionError
from simplejson import JSONDecodeError
from homeassistant import config_entries, setup
from homeassistant.components.bond.const import DOMAIN
from homeassistant.const import CONF_ACCESS_TOKEN, CONF_HOST
from tests.async_mock import patch
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.bond.config_flow.Bond.getDeviceIds", return_value=[],
), patch(
"homeassistant.components.bond.async_setup", return_value=True
) as mock_setup, patch(
"homeassistant.components.bond.async_setup_entry", return_value=True,
) as mock_setup_entry:
result2 = await hass.config_entries.flow.async_configure(
result["flow_id"], {CONF_HOST: "1.1.1.1", CONF_ACCESS_TOKEN: "test-token"},
)
assert result2["type"] == "create_entry"
assert result2["title"] == "1.1.1.1"
assert result2["data"] == {
CONF_HOST: "1.1.1.1",
CONF_ACCESS_TOKEN: "test-token",
}
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.bond.config_flow.Bond.getDeviceIds",
side_effect=JSONDecodeError("test-message", "test-doc", 0),
):
result2 = await hass.config_entries.flow.async_configure(
result["flow_id"], {CONF_HOST: "1.1.1.1", CONF_ACCESS_TOKEN: "test-token"},
)
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.bond.config_flow.Bond.getDeviceIds",
side_effect=ConnectionError,
):
result2 = await hass.config_entries.flow.async_configure(
result["flow_id"], {CONF_HOST: "1.1.1.1", CONF_ACCESS_TOKEN: "test-token"},
)
assert result2["type"] == "form"
assert result2["errors"] == {"base": "cannot_connect"}
async def test_form_unexpected_error(hass):
"""Test we handle unexpected error gracefully."""
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_USER}
)
with patch(
"homeassistant.components.bond.config_flow.Bond.getDeviceIds",
side_effect=Exception,
):
result2 = await hass.config_entries.flow.async_configure(
result["flow_id"], {CONF_HOST: "1.1.1.1", CONF_ACCESS_TOKEN: "test-token"},
)
assert result2["type"] == "form"
assert result2["errors"] == {"base": "unknown"}
+113
View File
@@ -0,0 +1,113 @@
"""Tests for the Bond cover device."""
import logging
from bond import BOND_DEVICE_TYPE_MOTORIZED_SHADES
from homeassistant.components.bond.utils import BondDevice
from homeassistant.components.cover import DOMAIN as COVER_DOMAIN
from homeassistant.const import (
ATTR_ENTITY_ID,
SERVICE_CLOSE_COVER,
SERVICE_OPEN_COVER,
SERVICE_STOP_COVER,
)
from homeassistant.helpers.entity_registry import EntityRegistry
from .common import setup_platform
from tests.async_mock import patch
_LOGGER = logging.getLogger(__name__)
async def test_entity_registry(hass):
"""Tests that the devices are registered in the entity registry."""
with patch(
"homeassistant.components.bond.utils.get_bond_devices",
return_value=[
BondDevice(
"device-1",
{"name": "name-1", "type": BOND_DEVICE_TYPE_MOTORIZED_SHADES},
)
],
):
await setup_platform(hass, COVER_DOMAIN)
registry: EntityRegistry = await hass.helpers.entity_registry.async_get_registry()
assert [key for key in registry.entities.keys()] == ["cover.name_1"]
async def test_open_cover(hass):
"""Tests that open cover command delegates to API."""
with patch(
"homeassistant.components.bond.utils.get_bond_devices",
return_value=[
BondDevice(
"device-1",
{"name": "name-1", "type": BOND_DEVICE_TYPE_MOTORIZED_SHADES},
)
],
):
await setup_platform(hass, COVER_DOMAIN)
with patch("bond.Bond.open") as mock_open:
await hass.services.async_call(
COVER_DOMAIN,
SERVICE_OPEN_COVER,
{ATTR_ENTITY_ID: "cover.name_1"},
blocking=True,
)
await hass.async_block_till_done()
mock_open.assert_called_once()
async def test_close_cover(hass):
"""Tests that close cover command delegates to API."""
with patch(
"homeassistant.components.bond.utils.get_bond_devices",
return_value=[
BondDevice(
"device-1",
{"name": "name-1", "type": BOND_DEVICE_TYPE_MOTORIZED_SHADES},
)
],
):
await setup_platform(hass, COVER_DOMAIN)
with patch("bond.Bond.close") as mock_close:
await hass.services.async_call(
COVER_DOMAIN,
SERVICE_CLOSE_COVER,
{ATTR_ENTITY_ID: "cover.name_1"},
blocking=True,
)
await hass.async_block_till_done()
mock_close.assert_called_once()
async def test_stop_cover(hass):
"""Tests that stop cover command delegates to API."""
with patch(
"homeassistant.components.bond.utils.get_bond_devices",
return_value=[
BondDevice(
"device-1",
{"name": "name-1", "type": BOND_DEVICE_TYPE_MOTORIZED_SHADES},
)
],
):
await setup_platform(hass, COVER_DOMAIN)
with patch("bond.Bond.hold") as mock_hold:
await hass.services.async_call(
COVER_DOMAIN,
SERVICE_STOP_COVER,
{ATTR_ENTITY_ID: "cover.name_1"},
blocking=True,
)
await hass.async_block_till_done()
mock_hold.assert_called_once()
+56
View File
@@ -0,0 +1,56 @@
"""Tests for the Bond module."""
from homeassistant.components.bond.const import DOMAIN
from homeassistant.config_entries import ENTRY_STATE_LOADED, ENTRY_STATE_NOT_LOADED
from homeassistant.const import CONF_ACCESS_TOKEN, CONF_HOST
from homeassistant.core import HomeAssistant
from homeassistant.setup import async_setup_component
from tests.async_mock import patch
from tests.common import MockConfigEntry
async def test_async_setup_no_domain_config(hass: HomeAssistant):
"""Test setup without configuration is noop."""
result = await async_setup_component(hass, DOMAIN, {})
assert result is True
async def test_async_setup_entry_sets_up_supported_domains(hass: HomeAssistant):
"""Test that configuring entry sets up cover domain."""
config_entry = MockConfigEntry(
domain=DOMAIN, data={CONF_HOST: "1.1.1.1", CONF_ACCESS_TOKEN: "test-token"},
)
config_entry.add_to_hass(hass)
with patch(
"homeassistant.components.bond.cover.async_setup_entry"
) as mock_cover_async_setup_entry:
result = await hass.config_entries.async_setup(config_entry.entry_id)
assert result is True
await hass.async_block_till_done()
assert len(mock_cover_async_setup_entry.mock_calls) == 1
async def test_unload_config_entry(hass: HomeAssistant):
"""Test that configuration entry supports unloading."""
config_entry = MockConfigEntry(
domain=DOMAIN, data={CONF_HOST: "1.1.1.1", CONF_ACCESS_TOKEN: "test-token"},
)
config_entry.add_to_hass(hass)
with patch(
"homeassistant.components.bond.cover.async_setup_entry", return_value=True
):
result = await hass.config_entries.async_setup(config_entry.entry_id)
assert result is True
await hass.async_block_till_done()
assert config_entry.entry_id in hass.data[DOMAIN]
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.entry_id not in hass.data[DOMAIN]
assert config_entry.state == ENTRY_STATE_NOT_LOADED
+24
View File
@@ -0,0 +1,24 @@
"""Tests for the Bond cover device."""
import logging
from bond import Bond
from homeassistant.components.bond.utils import get_bond_devices
from homeassistant.core import HomeAssistant
from tests.async_mock import patch
_LOGGER = logging.getLogger(__name__)
async def test_get_bond_devices(hass: HomeAssistant):
"""Tests that the querying for devices delegates to API."""
bond: Bond = Bond("1.1.1.1", "test-token")
with patch(
"bond.Bond.getDeviceIds", return_value=["device-1", "device-2"],
) as mock_get_device_ids, patch("bond.Bond.getDevice") as mock_get_device:
await get_bond_devices(hass, bond)
assert len(mock_get_device_ids.mock_calls) == 1
assert len(mock_get_device.mock_calls) == 2