Skip to content

Commit

Permalink
Fixed stream_ip resolution for kubernetes users (#516)
Browse files Browse the repository at this point in the history
* Fixed stream_ip resolution for kubernetes users
  • Loading branch information
robbert229 committed Jun 25, 2022
1 parent 8977dea commit 3ebec38
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion custom_components/mass/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

import logging
import os
import socket
from urllib.parse import urlparse

from homeassistant.config_entries import ConfigEntry
from homeassistant.const import EVENT_HOMEASSISTANT_STOP
Expand All @@ -11,6 +13,7 @@
from homeassistant.helpers import device_registry as dr
from homeassistant.helpers.aiohttp_client import async_get_clientsession
from homeassistant.helpers.start import async_at_start
from homeassistant.helpers.network import get_url
from music_assistant import MusicAssistant
from music_assistant.models.config import MassConfig, MusicProviderConfig
from music_assistant.models.enums import EventType, ProviderType
Expand Down Expand Up @@ -49,6 +52,15 @@
EventType.QUEUE_TIME_UPDATED,
)

def get_local_ip_from_internal_url(hass: HomeAssistant):
"""Get the stream ip address from the internal_url."""
url = get_url(hass, allow_internal=True, allow_external=False)
parsed_uri = urlparse(url)

if parsed_uri.netloc == '':
return hass.config.api.local_ip

return socket.gethostbyname(parsed_uri.netloc)

async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry):
"""Set up from a config entry."""
Expand Down Expand Up @@ -95,7 +107,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry):
path=conf.get(CONF_FILE_DIRECTORY),
)
)
stream_ip = hass.config.api.local_ip
stream_ip = get_local_ip_from_internal_url(hass)
mass_conf = MassConfig(
database_url=f"sqlite:///{db_file}", providers=providers, stream_ip=stream_ip
)
Expand Down

0 comments on commit 3ebec38

Please sign in to comment.