* Add support for Numato 32 port USB GPIO boards Included are a binary_sensor, sensor and switch component implementations. The binary_sensor interface pushes updates via registered callback functions, so no need to poll here. Unit tests are included to test against a Numato device mockup. * Refactor numato configuration due to PR finding * Resolve minor review findings * Bump numato-gpio requirement * Load numato platforms during domain setup According to review finding * Guard from platform setup without discovery_info According to review finding * Move numato API state into hass.data According to review finding. * Avoid side effects in numato entity constructors According to review finding * Keep only first line of numato module docstrings Removed reference to the documentation. Requested by reviewer. * Minor improvements inspired by review findings * Fix async tests Pytest fixture was returning from the yield too early executing teardown code during test execution. * Improve test coverage * Configure GPIO ports early Review finding * Move read_gpio callback to outside the loop Also continue on failed switch setup, resolve other minor review findings and correct some error messages * Bump numato-gpio requirement This fixes a crash during cleanup. When any device had a communication problem, its cleanup would raise an exception which was not handled, fell through to the caller and prevented the remaining devices from being cleaned up. * Call services directly Define local helper functions for better readability. Resolves a review finding. * Assert something in every test So not only coverage is satisfied but things are actually tested to be in the expected state. Resolves a review finding. * Clarify scope of notification tests Make unit test for hass NumatoAPI independent of Home Assistant (very basic test of notifications). Improve the regular operations test for notifications. * Test for hass.states after operating switches Resolves a review finding. * Check for wrong port directions * WIP: Split numato tests to multiple files test_hass_binary_sensor_notification still fails. * Remove pytest asyncio decorator Apears to be redundant. Resolves a review finding. * Call switch services directly. Resolves a review finding. * Remove obsolete inline pylint config Co-Authored-By: Martin Hjelmare <marhje52@gmail.com> * Improve the numato_gpio module mockup Resolves a review finding. * Remove needless explicit conversions to str Resolves review findings. * Test setup of binary_sensor callbacks * Fix test_hass_binary_sensor_notification * Add forgotten await Review finding. Co-authored-by: Martin Hjelmare <marhje52@gmail.com>
114 lines
4.4 KiB
Python
114 lines
4.4 KiB
Python
"""Tests for the numato switch platform."""
|
|
from homeassistant.components import switch
|
|
from homeassistant.const import ATTR_ENTITY_ID, SERVICE_TURN_OFF, SERVICE_TURN_ON
|
|
from homeassistant.helpers import discovery
|
|
from homeassistant.setup import async_setup_component
|
|
|
|
from .common import NUMATO_CFG, mockup_raise
|
|
|
|
MOCKUP_ENTITY_IDS = {
|
|
"switch.numato_switch_mock_port5",
|
|
"switch.numato_switch_mock_port6",
|
|
}
|
|
|
|
|
|
async def test_failing_setups_no_entities(hass, numato_fixture, monkeypatch):
|
|
"""When port setup fails, no entity shall be created."""
|
|
monkeypatch.setattr(numato_fixture.NumatoDeviceMock, "setup", mockup_raise)
|
|
assert await async_setup_component(hass, "numato", NUMATO_CFG)
|
|
await hass.async_block_till_done()
|
|
for entity_id in MOCKUP_ENTITY_IDS:
|
|
assert entity_id not in hass.states.async_entity_ids()
|
|
|
|
|
|
async def test_regular_hass_operations(hass, numato_fixture):
|
|
"""Test regular operations from within Home Assistant."""
|
|
assert await async_setup_component(hass, "numato", NUMATO_CFG)
|
|
await hass.async_block_till_done() # wait until services are registered
|
|
await hass.services.async_call(
|
|
switch.DOMAIN,
|
|
SERVICE_TURN_ON,
|
|
{ATTR_ENTITY_ID: "switch.numato_switch_mock_port5"},
|
|
blocking=True,
|
|
)
|
|
assert hass.states.get("switch.numato_switch_mock_port5").state == "on"
|
|
assert numato_fixture.devices[0].values[5] == 1
|
|
await hass.services.async_call(
|
|
switch.DOMAIN,
|
|
SERVICE_TURN_ON,
|
|
{ATTR_ENTITY_ID: "switch.numato_switch_mock_port6"},
|
|
blocking=True,
|
|
)
|
|
assert hass.states.get("switch.numato_switch_mock_port6").state == "on"
|
|
assert numato_fixture.devices[0].values[6] == 1
|
|
await hass.services.async_call(
|
|
switch.DOMAIN,
|
|
SERVICE_TURN_OFF,
|
|
{ATTR_ENTITY_ID: "switch.numato_switch_mock_port5"},
|
|
blocking=True,
|
|
)
|
|
assert hass.states.get("switch.numato_switch_mock_port5").state == "off"
|
|
assert numato_fixture.devices[0].values[5] == 0
|
|
await hass.services.async_call(
|
|
switch.DOMAIN,
|
|
SERVICE_TURN_OFF,
|
|
{ATTR_ENTITY_ID: "switch.numato_switch_mock_port6"},
|
|
blocking=True,
|
|
)
|
|
assert hass.states.get("switch.numato_switch_mock_port6").state == "off"
|
|
assert numato_fixture.devices[0].values[6] == 0
|
|
|
|
|
|
async def test_failing_hass_operations(hass, numato_fixture, monkeypatch):
|
|
"""Test failing operations called from within Home Assistant.
|
|
|
|
Switches remain in their initial 'off' state when the device can't
|
|
be written to.
|
|
"""
|
|
assert await async_setup_component(hass, "numato", NUMATO_CFG)
|
|
|
|
await hass.async_block_till_done() # wait until services are registered
|
|
monkeypatch.setattr(numato_fixture.devices[0], "write", mockup_raise)
|
|
await hass.services.async_call(
|
|
switch.DOMAIN,
|
|
SERVICE_TURN_ON,
|
|
{ATTR_ENTITY_ID: "switch.numato_switch_mock_port5"},
|
|
blocking=True,
|
|
)
|
|
assert hass.states.get("switch.numato_switch_mock_port5").state == "off"
|
|
assert not numato_fixture.devices[0].values[5]
|
|
await hass.services.async_call(
|
|
switch.DOMAIN,
|
|
SERVICE_TURN_ON,
|
|
{ATTR_ENTITY_ID: "switch.numato_switch_mock_port6"},
|
|
blocking=True,
|
|
)
|
|
assert hass.states.get("switch.numato_switch_mock_port6").state == "off"
|
|
assert not numato_fixture.devices[0].values[6]
|
|
await hass.services.async_call(
|
|
switch.DOMAIN,
|
|
SERVICE_TURN_OFF,
|
|
{ATTR_ENTITY_ID: "switch.numato_switch_mock_port5"},
|
|
blocking=True,
|
|
)
|
|
assert hass.states.get("switch.numato_switch_mock_port5").state == "off"
|
|
assert not numato_fixture.devices[0].values[5]
|
|
await hass.services.async_call(
|
|
switch.DOMAIN,
|
|
SERVICE_TURN_OFF,
|
|
{ATTR_ENTITY_ID: "switch.numato_switch_mock_port6"},
|
|
blocking=True,
|
|
)
|
|
assert hass.states.get("switch.numato_switch_mock_port6").state == "off"
|
|
assert not numato_fixture.devices[0].values[6]
|
|
|
|
|
|
async def test_switch_setup_without_discovery_info(hass, config, numato_fixture):
|
|
"""Test handling of empty discovery_info."""
|
|
numato_fixture.discover()
|
|
await discovery.async_load_platform(hass, "switch", "numato", None, config)
|
|
for entity_id in MOCKUP_ENTITY_IDS:
|
|
assert entity_id not in hass.states.async_entity_ids()
|
|
await hass.async_block_till_done() # wait for numato platform to be loaded
|
|
for entity_id in MOCKUP_ENTITY_IDS:
|
|
assert entity_id in hass.states.async_entity_ids()
|