Skip to content

Commit

Permalink
Test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
IceBotYT committed May 27, 2023
1 parent 64ab604 commit ddd3296
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 109 deletions.
8 changes: 5 additions & 3 deletions homeassistant/components/peco/config_flow.py
Expand Up @@ -81,13 +81,15 @@ async def async_step_user(
)

county = user_input[CONF_COUNTY]
phone_number = user_input[CONF_PHONE_NUMBER]

if phone_number is None:
if CONF_PHONE_NUMBER not in user_input:
return self.async_create_entry(
title=user_input[CONF_COUNTY].capitalize(), data=user_input
title=f"{user_input[CONF_COUNTY].capitalize()} Outage Count",
data=user_input,
)

phone_number = user_input[CONF_PHONE_NUMBER]

await self.async_set_unique_id(f"{county}-{phone_number}")
self._abort_if_unique_id_configured()

Expand Down
140 changes: 35 additions & 105 deletions tests/components/peco/test_config_flow.py
Expand Up @@ -2,8 +2,6 @@
from unittest.mock import patch

from peco import HttpError, IncompatibleMeterError, UnresponsiveMeterError
import pytest
from voluptuous.error import MultipleInvalid

from homeassistant import config_entries
from homeassistant.components.peco.const import DOMAIN
Expand All @@ -16,31 +14,24 @@ async def test_form(hass: HomeAssistant) -> None:
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_USER}
)
assert result["type"] == FlowResultType.MENU

result2 = await hass.config_entries.flow.async_configure(
result["flow_id"],
{"next_step_id": "outage_counter"},
)
await hass.async_block_till_done()
assert result2["type"] == FlowResultType.FORM
assert result2["step_id"] == "outage_counter"
assert result["type"] == FlowResultType.FORM
assert result["step_id"] == "user"

with patch(
"homeassistant.components.peco.async_setup_entry",
return_value=True,
):
result3 = await hass.config_entries.flow.async_configure(
result2 = await hass.config_entries.flow.async_configure(
result["flow_id"],
{
"county": "PHILADELPHIA",
},
)
await hass.async_block_till_done()

assert result3["type"] == FlowResultType.CREATE_ENTRY
assert result3["title"] == "Philadelphia Outage Count"
assert result3["data"] == {
assert result2["type"] == FlowResultType.CREATE_ENTRY
assert result2["title"] == "Philadelphia Outage Count"
assert result2["data"] == {
"county": "PHILADELPHIA",
}

Expand All @@ -50,45 +41,24 @@ async def test_invalid_county(hass: HomeAssistant) -> None:
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_USER}
)
assert result["type"] == FlowResultType.MENU

with pytest.raises(MultipleInvalid):
await hass.config_entries.flow.async_configure(
result["flow_id"],
{
"county": "INVALID_COUNTY_THAT_SHOULD_NOT_EXIST",
},
)
await hass.async_block_till_done()

second_result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_USER}
)
assert second_result["type"] == FlowResultType.MENU

second_result2 = await hass.config_entries.flow.async_configure(
second_result["flow_id"],
{"next_step_id": "outage_counter"},
)
await hass.async_block_till_done()
assert second_result2["type"] == FlowResultType.FORM
assert second_result2["step_id"] == "outage_counter"
assert result["type"] == FlowResultType.FORM
assert result["step_id"] == "user"

with patch(
"homeassistant.components.peco.async_setup_entry",
return_value=True,
):
second_result3 = await hass.config_entries.flow.async_configure(
second_result["flow_id"],
result2 = await hass.config_entries.flow.async_configure(
result["flow_id"],
{
"county": "PHILADELPHIA",
},
)
await hass.async_block_till_done()

assert second_result3["type"] == FlowResultType.CREATE_ENTRY
assert second_result3["title"] == "Philadelphia Outage Count"
assert second_result3["data"] == {
assert result2["type"] == FlowResultType.CREATE_ENTRY
assert result2["title"] == "Philadelphia Outage Count"
assert result2["data"] == {
"county": "PHILADELPHIA",
}

Expand All @@ -98,65 +68,49 @@ async def test_meter_value_error(hass: HomeAssistant) -> None:
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_USER}
)
assert result["type"] == FlowResultType.MENU

result = await hass.config_entries.flow.async_configure(
result["flow_id"],
{
"next_step_id": "smart_meter",
},
)
await hass.async_block_till_done()
assert result["type"] == FlowResultType.FORM
assert result["step_id"] == "smart_meter"
assert result["step_id"] == "user"

result = await hass.config_entries.flow.async_configure(
result["flow_id"],
{
"county": "PHILADELPHIA",
"phone_number": "INVALID_SMART_METER_THAT_SHOULD_NOT_EXIST",
},
)
await hass.async_block_till_done()

assert result["type"] == FlowResultType.SHOW_PROGRESS
assert result["step_id"] == "smart_meter"
assert result["step_id"] == "user"
assert result["progress_action"] == "verifying_meter"

result = await hass.config_entries.flow.async_configure(result["flow_id"])

assert result["type"] == FlowResultType.FORM
assert result["step_id"] == "smart_meter"
assert result["errors"] == {"base": "invalid_phone_number"}
assert result["step_id"] == "user"
assert result["errors"] == {"phone_number": "invalid_phone_number"}


async def test_incompatible_meter_error(hass: HomeAssistant) -> None:
"""Test if the IncompatibleMeter error works."""
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_USER}
)
assert result["type"] == FlowResultType.MENU

result = await hass.config_entries.flow.async_configure(
result["flow_id"],
{
"next_step_id": "smart_meter",
},
)
await hass.async_block_till_done()
assert result["type"] == FlowResultType.FORM
assert result["step_id"] == "smart_meter"
assert result["step_id"] == "user"

with patch("peco.PecoOutageApi.meter_check", side_effect=IncompatibleMeterError()):
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
{
"county": "PHILADELPHIA",
"phone_number": "1234567890",
},
)
await hass.async_block_till_done()

assert result["type"] == FlowResultType.SHOW_PROGRESS
assert result["step_id"] == "smart_meter"
assert result["step_id"] == "user"
assert result["progress_action"] == "verifying_meter"

result = await hass.config_entries.flow.async_configure(result["flow_id"])
Expand All @@ -170,107 +124,83 @@ async def test_unresponsive_meter_error(hass: HomeAssistant) -> None:
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_USER}
)
assert result["type"] == FlowResultType.MENU

result = await hass.config_entries.flow.async_configure(
result["flow_id"],
{
"next_step_id": "smart_meter",
},
)
await hass.async_block_till_done()
assert result["type"] == FlowResultType.FORM
assert result["step_id"] == "smart_meter"
assert result["step_id"] == "user"

with patch("peco.PecoOutageApi.meter_check", side_effect=UnresponsiveMeterError()):
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
{
"county": "PHILADELPHIA",
"phone_number": "1234567890",
},
)
await hass.async_block_till_done()

assert result["type"] == FlowResultType.SHOW_PROGRESS
assert result["step_id"] == "smart_meter"
assert result["step_id"] == "user"
assert result["progress_action"] == "verifying_meter"

result = await hass.config_entries.flow.async_configure(result["flow_id"])

assert result["type"] == FlowResultType.FORM
assert result["step_id"] == "smart_meter"
assert result["errors"] == {"base": "unresponsive_meter"}
assert result["step_id"] == "user"
assert result["errors"] == {"phone_number": "unresponsive_meter"}


async def test_meter_http_error(hass: HomeAssistant) -> None:
"""Test if the InvalidMeter error works."""
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_USER}
)
assert result["type"] == FlowResultType.MENU

result = await hass.config_entries.flow.async_configure(
result["flow_id"],
{
"next_step_id": "smart_meter",
},
)
await hass.async_block_till_done()
assert result["type"] == FlowResultType.FORM
assert result["step_id"] == "smart_meter"
assert result["step_id"] == "user"

with patch("peco.PecoOutageApi.meter_check", side_effect=HttpError):
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
{
"county": "PHILADELPHIA",
"phone_number": "1234567890",
},
)
await hass.async_block_till_done()

assert result["type"] == FlowResultType.SHOW_PROGRESS
assert result["step_id"] == "smart_meter"
assert result["step_id"] == "user"
assert result["progress_action"] == "verifying_meter"

result = await hass.config_entries.flow.async_configure(result["flow_id"])

assert result["type"] == FlowResultType.FORM
assert result["step_id"] == "smart_meter"
assert result["errors"] == {"base": "http_error"}
assert result["step_id"] == "user"
assert result["errors"] == {"phone_number": "http_error"}


async def test_smart_meter(hass: HomeAssistant) -> None:
"""Test if the Smart Meter step works."""
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_USER}
)
assert result["type"] == FlowResultType.MENU

result = await hass.config_entries.flow.async_configure(
result["flow_id"],
{
"next_step_id": "smart_meter",
},
)
await hass.async_block_till_done()
assert result["type"] == FlowResultType.FORM
assert result["step_id"] == "smart_meter"
assert result["step_id"] == "user"

with patch("peco.PecoOutageApi.meter_check", return_value=True):
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
{
"county": "PHILADELPHIA",
"phone_number": "1234567890",
},
)
await hass.async_block_till_done()

assert result["type"] == FlowResultType.SHOW_PROGRESS
assert result["step_id"] == "smart_meter"
assert result["step_id"] == "user"
assert result["progress_action"] == "verifying_meter"

result = await hass.config_entries.flow.async_configure(result["flow_id"])

assert result["type"] == FlowResultType.CREATE_ENTRY
assert result["title"] == "1234567890 Smart Meter"
assert result["title"] == "Philadelphia - 1234567890"
assert result["data"]["phone_number"] == "1234567890"

0 comments on commit ddd3296

Please sign in to comment.