mirror of
https://github.com/home-assistant/core.git
synced 2025-07-19 03:07:37 +00:00
Improve tests and config flow for Smart Meter Texas (#39089)
Co-authored-by: Martin Hjelmare <marhje52@gmail.com>
This commit is contained in:
parent
52d949ec90
commit
32db1d1eb2
@ -45,21 +45,21 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry):
|
|||||||
password = entry.data[CONF_PASSWORD]
|
password = entry.data[CONF_PASSWORD]
|
||||||
|
|
||||||
account = Account(username, password)
|
account = Account(username, password)
|
||||||
smartmetertexas = SmartMeterTexasData(hass, entry, account)
|
smart_meter_texas_data = SmartMeterTexasData(hass, entry, account)
|
||||||
try:
|
try:
|
||||||
await smartmetertexas.client.authenticate()
|
await smart_meter_texas_data.client.authenticate()
|
||||||
except SmartMeterTexasAuthError:
|
except SmartMeterTexasAuthError:
|
||||||
_LOGGER.error("Username or password was not accepted")
|
_LOGGER.error("Username or password was not accepted")
|
||||||
return False
|
return False
|
||||||
except asyncio.TimeoutError:
|
except asyncio.TimeoutError:
|
||||||
raise ConfigEntryNotReady
|
raise ConfigEntryNotReady
|
||||||
|
|
||||||
await smartmetertexas.setup()
|
await smart_meter_texas_data.setup()
|
||||||
|
|
||||||
async def async_update_data():
|
async def async_update_data():
|
||||||
_LOGGER.debug("Fetching latest data")
|
_LOGGER.debug("Fetching latest data")
|
||||||
await smartmetertexas.read_meters()
|
await smart_meter_texas_data.read_meters()
|
||||||
return smartmetertexas
|
return smart_meter_texas_data
|
||||||
|
|
||||||
# Use a DataUpdateCoordinator to manage the updates. This is due to the
|
# Use a DataUpdateCoordinator to manage the updates. This is due to the
|
||||||
# Smart Meter Texas API which takes around 30 seconds to read a meter.
|
# Smart Meter Texas API which takes around 30 seconds to read a meter.
|
||||||
@ -78,7 +78,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry):
|
|||||||
|
|
||||||
hass.data[DOMAIN][entry.entry_id] = {
|
hass.data[DOMAIN][entry.entry_id] = {
|
||||||
DATA_COORDINATOR: coordinator,
|
DATA_COORDINATOR: coordinator,
|
||||||
DATA_SMART_METER: smartmetertexas,
|
DATA_SMART_METER: smart_meter_texas_data,
|
||||||
}
|
}
|
||||||
|
|
||||||
asyncio.create_task(coordinator.async_refresh())
|
asyncio.create_task(coordinator.async_refresh())
|
||||||
|
@ -50,14 +50,6 @@ class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
|
|||||||
VERSION = 1
|
VERSION = 1
|
||||||
CONNECTION_CLASS = config_entries.CONN_CLASS_CLOUD_POLL
|
CONNECTION_CLASS = config_entries.CONN_CLASS_CLOUD_POLL
|
||||||
|
|
||||||
def _account_already_configured(self, account):
|
|
||||||
existing_accounts = {
|
|
||||||
entry.data[CONF_USERNAME]
|
|
||||||
for entry in self._async_current_entries()
|
|
||||||
if CONF_USERNAME in entry.data
|
|
||||||
}
|
|
||||||
return account in existing_accounts
|
|
||||||
|
|
||||||
async def async_step_user(self, user_input=None):
|
async def async_step_user(self, user_input=None):
|
||||||
"""Handle the initial step."""
|
"""Handle the initial step."""
|
||||||
|
|
||||||
@ -74,8 +66,10 @@ class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
|
|||||||
errors["base"] = "unknown"
|
errors["base"] = "unknown"
|
||||||
else:
|
else:
|
||||||
if not errors:
|
if not errors:
|
||||||
if self._account_already_configured(user_input[CONF_USERNAME]):
|
# Ensure the same account cannot be setup more than once.
|
||||||
return self.async_abort(reason="already_configured")
|
await self.async_set_unique_id(user_input[CONF_USERNAME])
|
||||||
|
self._abort_if_unique_id_configured()
|
||||||
|
|
||||||
return self.async_create_entry(title=info["title"], data=user_input)
|
return self.async_create_entry(title=info["title"], data=user_input)
|
||||||
|
|
||||||
return self.async_show_form(
|
return self.async_show_form(
|
||||||
|
@ -32,9 +32,9 @@ def load_smt_fixture(name):
|
|||||||
return json.loads(json_fixture)
|
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."""
|
"""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.config_entries.async_setup(config_entry.entry_id)
|
||||||
await hass.async_block_till_done()
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
|
@ -1,18 +1,19 @@
|
|||||||
"""Test the Smart Meter Texas module."""
|
"""Test the Smart Meter Texas module."""
|
||||||
import pytest
|
|
||||||
|
|
||||||
from homeassistant.components.homeassistant import (
|
from homeassistant.components.homeassistant import (
|
||||||
DOMAIN as HA_DOMAIN,
|
DOMAIN as HA_DOMAIN,
|
||||||
SERVICE_UPDATE_ENTITY,
|
SERVICE_UPDATE_ENTITY,
|
||||||
)
|
)
|
||||||
from homeassistant.components.smart_meter_texas import async_setup_entry
|
|
||||||
from homeassistant.components.smart_meter_texas.const import DOMAIN
|
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.const import ATTR_ENTITY_ID
|
||||||
from homeassistant.exceptions import ConfigEntryNotReady
|
|
||||||
from homeassistant.setup import async_setup_component
|
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
|
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):
|
async def test_auth_failure(hass, config_entry, aioclient_mock):
|
||||||
"""Test if user's username or password is not accepted."""
|
"""Test if user's username or password is not accepted."""
|
||||||
mock_connection(aioclient_mock, auth_fail=True)
|
await setup_integration(hass, config_entry, aioclient_mock, auth_fail=True)
|
||||||
result = await async_setup_entry(hass, config_entry)
|
|
||||||
|
|
||||||
assert result is False
|
assert config_entry.state == ENTRY_STATE_SETUP_ERROR
|
||||||
|
|
||||||
|
|
||||||
async def test_api_timeout(hass, config_entry, aioclient_mock):
|
async def test_api_timeout(hass, config_entry, aioclient_mock):
|
||||||
"""Test that a timeout results in ConfigEntryNotReady."""
|
"""Test that a timeout results in ConfigEntryNotReady."""
|
||||||
mock_connection(aioclient_mock, auth_timeout=True)
|
await setup_integration(hass, config_entry, aioclient_mock, auth_timeout=True)
|
||||||
with pytest.raises(ConfigEntryNotReady):
|
|
||||||
await async_setup_entry(hass, config_entry)
|
|
||||||
|
|
||||||
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):
|
async def test_update_failure(hass, config_entry, aioclient_mock):
|
||||||
"""Test that the coordinator handles a bad response."""
|
"""Test that the coordinator handles a bad response."""
|
||||||
mock_connection(aioclient_mock, bad_reading=True)
|
await setup_integration(hass, config_entry, aioclient_mock, bad_reading=True)
|
||||||
await setup_integration(hass, config_entry, aioclient_mock)
|
|
||||||
await async_setup_component(hass, HA_DOMAIN, {})
|
await async_setup_component(hass, HA_DOMAIN, {})
|
||||||
with patch("smart_meter_texas.Meter.read_meter") as updater:
|
with patch("smart_meter_texas.Meter.read_meter") as updater:
|
||||||
await hass.services.async_call(
|
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.const import ATTR_ENTITY_ID, CONF_ADDRESS
|
||||||
from homeassistant.setup import async_setup_component
|
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
|
from tests.async_mock import patch
|
||||||
|
|
||||||
|
|
||||||
async def test_sensor(hass, config_entry, aioclient_mock):
|
async def test_sensor(hass, config_entry, aioclient_mock):
|
||||||
"""Test that the sensor is setup."""
|
"""Test that the sensor is setup."""
|
||||||
mock_connection(aioclient_mock)
|
|
||||||
await setup_integration(hass, config_entry, aioclient_mock)
|
await setup_integration(hass, config_entry, aioclient_mock)
|
||||||
await refresh_data(hass, config_entry, aioclient_mock)
|
await refresh_data(hass, config_entry, aioclient_mock)
|
||||||
meter = hass.states.get(TEST_ENTITY_ID)
|
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):
|
async def test_name(hass, config_entry, aioclient_mock):
|
||||||
"""Test sensor name property."""
|
"""Test sensor name property."""
|
||||||
mock_connection(aioclient_mock)
|
|
||||||
await setup_integration(hass, config_entry, aioclient_mock)
|
await setup_integration(hass, config_entry, aioclient_mock)
|
||||||
await refresh_data(hass, config_entry, aioclient_mock)
|
await refresh_data(hass, config_entry, aioclient_mock)
|
||||||
meter = hass.states.get(TEST_ENTITY_ID)
|
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):
|
async def test_attributes(hass, config_entry, aioclient_mock):
|
||||||
"""Test meter attributes."""
|
"""Test meter attributes."""
|
||||||
mock_connection(aioclient_mock)
|
|
||||||
await setup_integration(hass, config_entry, aioclient_mock)
|
await setup_integration(hass, config_entry, aioclient_mock)
|
||||||
await refresh_data(hass, config_entry, aioclient_mock)
|
await refresh_data(hass, config_entry, aioclient_mock)
|
||||||
meter = hass.states.get(TEST_ENTITY_ID)
|
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):
|
async def test_generic_entity_update_service(hass, config_entry, aioclient_mock):
|
||||||
"""Test generic update entity service homeasasistant/update_entity."""
|
"""Test generic update entity service homeasasistant/update_entity."""
|
||||||
mock_connection(aioclient_mock)
|
|
||||||
await setup_integration(hass, config_entry, aioclient_mock)
|
await setup_integration(hass, config_entry, aioclient_mock)
|
||||||
await async_setup_component(hass, HA_DOMAIN, {})
|
await async_setup_component(hass, HA_DOMAIN, {})
|
||||||
with patch("smart_meter_texas.Meter.read_meter") as updater:
|
with patch("smart_meter_texas.Meter.read_meter") as updater:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user