diff --git a/homeassistant/components/voip/assist_satellite.py b/homeassistant/components/voip/assist_satellite.py index 1026df8d0d9..a0aeaaf38d3 100644 --- a/homeassistant/components/voip/assist_satellite.py +++ b/homeassistant/components/voip/assist_satellite.py @@ -30,7 +30,15 @@ from homeassistant.config_entries import ConfigEntry from homeassistant.core import Context, HomeAssistant, callback from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback -from .const import CHANNELS, CONF_SIP_PORT, DOMAIN, RATE, RTP_AUDIO_SETTINGS, WIDTH +from .const import ( + CHANNELS, + CONF_SIP_PORT, + CONF_SIP_USER, + DOMAIN, + RATE, + RTP_AUDIO_SETTINGS, + WIDTH, +) from .devices import VoIPDevice from .entity import VoIPEntity @@ -199,7 +207,10 @@ class VoipAssistSatellite(VoIPEntity, AssistSatelliteEntity, RtpDatagramProtocol # HA SIP server source_ip = await async_get_source_ip(self.hass) sip_port = self.config_entry.options.get(CONF_SIP_PORT, SIP_PORT) - source_endpoint = get_sip_endpoint(host=source_ip, port=sip_port) + sip_user = self.config_entry.options.get(CONF_SIP_USER) + source_endpoint = get_sip_endpoint( + host=source_ip, port=sip_port, username=sip_user + ) try: # VoIP ID is SIP header diff --git a/homeassistant/components/voip/config_flow.py b/homeassistant/components/voip/config_flow.py index 63dcb8f86ee..7ae603f0f6a 100644 --- a/homeassistant/components/voip/config_flow.py +++ b/homeassistant/components/voip/config_flow.py @@ -16,7 +16,7 @@ from homeassistant.config_entries import ( from homeassistant.core import callback from homeassistant.helpers import config_validation as cv -from .const import CONF_SIP_PORT, DOMAIN +from .const import CONF_SIP_PORT, CONF_SIP_USER, DOMAIN class VoIPConfigFlow(ConfigFlow, domain=DOMAIN): @@ -58,7 +58,15 @@ class VoipOptionsFlowHandler(OptionsFlow): ) -> ConfigFlowResult: """Manage the options.""" if user_input is not None: - return self.async_create_entry(title="", data=user_input) + if CONF_SIP_USER in user_input and not user_input[CONF_SIP_USER]: + del user_input[CONF_SIP_USER] + self.hass.config_entries.async_update_entry( + self.config_entry, options=user_input + ) + return self.async_create_entry( + title="", + data=user_input, + ) return self.async_show_form( step_id="init", @@ -70,7 +78,15 @@ class VoipOptionsFlowHandler(OptionsFlow): CONF_SIP_PORT, SIP_PORT, ), - ): cv.port + ): cv.port, + vol.Optional( + CONF_SIP_USER, + description={ + "suggested_value": self.config_entry.options.get( + CONF_SIP_USER, None + ) + }, + ): vol.Any(None, cv.string), } ), ) diff --git a/homeassistant/components/voip/const.py b/homeassistant/components/voip/const.py index b4ee5d8ce7a..9a4403f9df2 100644 --- a/homeassistant/components/voip/const.py +++ b/homeassistant/components/voip/const.py @@ -13,3 +13,4 @@ RTP_AUDIO_SETTINGS = { } CONF_SIP_PORT = "sip_port" +CONF_SIP_USER = "sip_user" diff --git a/homeassistant/components/voip/strings.json b/homeassistant/components/voip/strings.json index c25c22f3f80..96c902bf39a 100644 --- a/homeassistant/components/voip/strings.json +++ b/homeassistant/components/voip/strings.json @@ -53,7 +53,8 @@ "step": { "init": { "data": { - "sip_port": "SIP port" + "sip_port": "SIP port", + "sip_user": "SIP user" } } } diff --git a/tests/components/voip/test_config_flow.py b/tests/components/voip/test_config_flow.py index 1b7aaad7c03..05f14afa4e7 100644 --- a/tests/components/voip/test_config_flow.py +++ b/tests/components/voip/test_config_flow.py @@ -80,3 +80,30 @@ async def test_options_flow(hass: HomeAssistant) -> None: ) assert result["type"] is FlowResultType.CREATE_ENTRY assert config_entry.options == {"sip_port": 5061} + + # Manual with user + result = await hass.config_entries.options.async_init( + config_entry.entry_id, + ) + result = await hass.config_entries.options.async_configure( + result["flow_id"], + user_input={"sip_port": 5061, "sip_user": "HA"}, + ) + assert result["type"] is FlowResultType.CREATE_ENTRY + assert config_entry.options == {"sip_port": 5061, "sip_user": "HA"} + + # Manual remove user + result = await hass.config_entries.options.async_init( + config_entry.entry_id, + ) + + assert config_entry.options == {"sip_port": 5061, "sip_user": "HA"} + + result = await hass.config_entries.options.async_configure( + result["flow_id"], + user_input={"sip_port": 5060, "sip_user": ""}, + ) + await hass.async_block_till_done() + + assert result["type"] is FlowResultType.CREATE_ENTRY + assert config_entry.options == {"sip_port": 5060}