Skip to content

Commit

Permalink
Improve config None handling for edge case, add test (#1807)
Browse files Browse the repository at this point in the history
  • Loading branch information
jozefKruszynski authored Jan 22, 2024
1 parent ded34c1 commit d59a954
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 5 deletions.
6 changes: 3 additions & 3 deletions custom_components/mass/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ async def async_step_manual(self, user_input: dict[str, Any] | None = None) -> F

try:
self.server_info = await get_server_info(self.hass, user_input[CONF_URL])
self.openai_agent_id = user_input[CONF_OPENAI_AGENT_ID]
self.openai_agent_id = user_input[CONF_OPENAI_AGENT_ID] or ""
self.expose_players_assist = user_input[CONF_ASSIST_AUTO_EXPOSE_PLAYERS]
await self.async_set_unique_id(self.server_info.server_id)
except CannotConnect:
Expand Down Expand Up @@ -258,7 +258,7 @@ async def async_step_discovery_confirm(
if user_input is not None:
# Check that we can connect to the address.
try:
self.openai_agent_id = user_input[CONF_OPENAI_AGENT_ID]
self.openai_agent_id = user_input[CONF_OPENAI_AGENT_ID] or ""
self.expose_players_assist = user_input[CONF_ASSIST_AUTO_EXPOSE_PLAYERS]
await get_server_info(self.hass, self.server_info.base_url)
except CannotConnect:
Expand Down Expand Up @@ -302,7 +302,7 @@ async def async_step_finish_addon_setup(
except CannotConnect:
return self.async_abort(reason="cannot_connect")
if user_input is not None:
self.openai_agent_id = user_input[CONF_OPENAI_AGENT_ID]
self.openai_agent_id = user_input[CONF_OPENAI_AGENT_ID] or ""
self.expose_players_assist = user_input[CONF_ASSIST_AUTO_EXPOSE_PLAYERS]
return await self._async_create_entry_or_abort()

Expand Down
5 changes: 3 additions & 2 deletions requirements_test.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ pylint==3.0.3
pre-commit==3.6.0
music-assistant==2.0.0b83
async_timeout==4.0.3
pytest-homeassistant-custom-component==0.13.88
zeroconf==0.128.0
pytest-homeassistant-custom-component==0.13.91
voluptuous==0.13.1
zeroconf==0.131.0
20 changes: 20 additions & 0 deletions tests/test_config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from homeassistant.helpers.selector import ConversationAgentSelector
from music_assistant.client.exceptions import CannotConnect, InvalidServerVersion
from pytest_homeassistant_custom_component.common import MockConfigEntry
from voluptuous.error import MultipleInvalid

from custom_components.mass import config_flow
from custom_components.mass.config_flow import (
Expand Down Expand Up @@ -221,6 +222,25 @@ async def test_flow_discovery_confirm_creates_config_entry(m_mass, hass): # noq
assert expected == result


@patch("custom_components.mass.config_flow.MusicAssistantClient")
async def test_flow_discovery_confirm_creates_config_entry_with_none_type_conversation_agent(
m_mass, hass # noqa: ARG001
):
"""Test the config entry is successfully created."""
with pytest.raises(MultipleInvalid):
config_flow.ConfigFlow.data = VALID_CONFIG
_result = await hass.config_entries.flow.async_init(
config_flow.DOMAIN, context={"source": "zeroconf"}, data=ZEROCONF_DATA
)
await hass.config_entries.flow.async_configure(
_result["flow_id"],
user_input={
CONF_OPENAI_AGENT_ID: None,
CONF_ASSIST_AUTO_EXPOSE_PLAYERS: False,
},
)


async def test_options_flow_init(hass):
"""Test config flow options."""
config_entry = await setup_mass_integration(hass)
Expand Down

0 comments on commit d59a954

Please sign in to comment.