Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Small improvements for emonitor #48700

Merged
merged 1 commit into from
Apr 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion homeassistant/components/emonitor/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from homeassistant.helpers.device_registry import format_mac

from . import name_short_mac
from .const import DOMAIN # pylint:disable=unused-import
from .const import DOMAIN

_LOGGER = logging.getLogger(__name__)

Expand Down Expand Up @@ -52,6 +52,7 @@ async def async_step_user(self, user_input=None):
await self.async_set_unique_id(
format_mac(info["mac_address"]), raise_on_progress=False
)
self._abort_if_unique_id_configured()
return self.async_create_entry(title=info["title"], data=user_input)

return self.async_show_form(
Expand Down
36 changes: 36 additions & 0 deletions tests/components/emonitor/test_config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,3 +185,39 @@ async def test_dhcp_already_exists(hass):
await hass.async_block_till_done()

assert result["type"] == "abort"
assert result["reason"] == "already_configured"


async def test_user_unique_id_already_exists(hass):
"""Test creating an entry where the unique_id already exists."""
await setup.async_setup_component(hass, "persistent_notification", {})
entry = MockConfigEntry(
domain=DOMAIN,
data={CONF_HOST: "1.2.3.4"},
unique_id="aa:bb:cc:dd:ee:ff",
)
entry.add_to_hass(hass)

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.emonitor.config_flow.Emonitor.async_get_status",
return_value=_mock_emonitor(),
), patch(
"homeassistant.components.emonitor.async_setup_entry",
return_value=True,
):
result2 = await hass.config_entries.flow.async_configure(
result["flow_id"],
{
"host": "1.2.3.4",
},
)
await hass.async_block_till_done()

assert result2["type"] == "abort"
assert result2["reason"] == "already_configured"