Improve tests and config flow for Smart Meter Texas (#39089)

Co-authored-by: Martin Hjelmare <marhje52@gmail.com>
This commit is contained in:
Graham WetzlerandMartin Hjelmare authored and GitHub committed 2020-08-21 19:20:42 +02:00
1 parent 52d949ec90
commit 32db1d1eb2
5 files changed
+25 -38

No files matched your search

@@ -32,9 +32,9 @@ def load_smt_fixture(name):
return json.loads(json_fixture)
async def setup_integration(hass, config_entry, aioclient_mock):
async def setup_integration(hass, config_entry, aioclient_mock, **kwargs):
"""Initialize the Smart Meter Texas integration for testing."""
mock_connection(aioclient_mock)
mock_connection(aioclient_mock, **kwargs)
await hass.config_entries.async_setup(config_entry.entry_id)
await hass.async_block_till_done()
+12 -15
View File
@@ -1,18 +1,19 @@
"""Test the Smart Meter Texas module."""
import pytest
from homeassistant.components.homeassistant import (
DOMAIN as HA_DOMAIN,
SERVICE_UPDATE_ENTITY,
)
from homeassistant.components.smart_meter_texas import async_setup_entry
from homeassistant.components.smart_meter_texas.const import DOMAIN
from homeassistant.config_entries import ENTRY_STATE_LOADED, ENTRY_STATE_NOT_LOADED
from homeassistant.config_entries import (
ENTRY_STATE_LOADED,
ENTRY_STATE_NOT_LOADED,
ENTRY_STATE_SETUP_ERROR,
ENTRY_STATE_SETUP_RETRY,
)
from homeassistant.const import ATTR_ENTITY_ID
from homeassistant.exceptions import ConfigEntryNotReady
from homeassistant.setup import async_setup_component
from .conftest import TEST_ENTITY_ID, mock_connection, setup_integration
from .conftest import TEST_ENTITY_ID, setup_integration
from tests.async_mock import patch
@@ -28,25 +29,21 @@ async def test_setup_with_no_config(hass):
async def test_auth_failure(hass, config_entry, aioclient_mock):
"""Test if user's username or password is not accepted."""
mock_connection(aioclient_mock, auth_fail=True)
result = await async_setup_entry(hass, config_entry)
await setup_integration(hass, config_entry, aioclient_mock, auth_fail=True)
assert result is False
assert config_entry.state == ENTRY_STATE_SETUP_ERROR
async def test_api_timeout(hass, config_entry, aioclient_mock):
"""Test that a timeout results in ConfigEntryNotReady."""
mock_connection(aioclient_mock, auth_timeout=True)
with pytest.raises(ConfigEntryNotReady):
await async_setup_entry(hass, config_entry)
await setup_integration(hass, config_entry, aioclient_mock, auth_timeout=True)
assert config_entry.state == ENTRY_STATE_NOT_LOADED
assert config_entry.state == ENTRY_STATE_SETUP_RETRY
async def test_update_failure(hass, config_entry, aioclient_mock):
"""Test that the coordinator handles a bad response."""
mock_connection(aioclient_mock, bad_reading=True)
await setup_integration(hass, config_entry, aioclient_mock)
await setup_integration(hass, config_entry, aioclient_mock, bad_reading=True)
await async_setup_component(hass, HA_DOMAIN, {})
with patch("smart_meter_texas.Meter.read_meter") as updater:
await hass.services.async_call(
@@ -11,14 +11,13 @@ from homeassistant.components.smart_meter_texas.const import (
from homeassistant.const import ATTR_ENTITY_ID, CONF_ADDRESS
from homeassistant.setup import async_setup_component
from .conftest import TEST_ENTITY_ID, mock_connection, refresh_data, setup_integration
from .conftest import TEST_ENTITY_ID, refresh_data, setup_integration
from tests.async_mock import patch
async def test_sensor(hass, config_entry, aioclient_mock):
"""Test that the sensor is setup."""
mock_connection(aioclient_mock)
await setup_integration(hass, config_entry, aioclient_mock)
await refresh_data(hass, config_entry, aioclient_mock)
meter = hass.states.get(TEST_ENTITY_ID)
@@ -29,7 +28,6 @@ async def test_sensor(hass, config_entry, aioclient_mock):
async def test_name(hass, config_entry, aioclient_mock):
"""Test sensor name property."""
mock_connection(aioclient_mock)
await setup_integration(hass, config_entry, aioclient_mock)
await refresh_data(hass, config_entry, aioclient_mock)
meter = hass.states.get(TEST_ENTITY_ID)
@@ -39,7 +37,6 @@ async def test_name(hass, config_entry, aioclient_mock):
async def test_attributes(hass, config_entry, aioclient_mock):
"""Test meter attributes."""
mock_connection(aioclient_mock)
await setup_integration(hass, config_entry, aioclient_mock)
await refresh_data(hass, config_entry, aioclient_mock)
meter = hass.states.get(TEST_ENTITY_ID)
@@ -51,7 +48,6 @@ async def test_attributes(hass, config_entry, aioclient_mock):
async def test_generic_entity_update_service(hass, config_entry, aioclient_mock):
"""Test generic update entity service homeasasistant/update_entity."""
mock_connection(aioclient_mock)
await setup_integration(hass, config_entry, aioclient_mock)
await async_setup_component(hass, HA_DOMAIN, {})
with patch("smart_meter_texas.Meter.read_meter") as updater: