Set up Almond Web to connect to HA (#28603)
* Set up Almond Web to connect to HA * Add missing string * Add type
This commit is contained in:
1 parent
a1f2b6d402
commit
5961215e6e
5 files changed
+245
-14
No files matched your search
@@ -0,0 +1,38 @@
|
||||
"""Network helpers."""
|
||||
from typing import Optional, cast
|
||||
from ipaddress import ip_address
|
||||
|
||||
import yarl
|
||||
|
||||
from homeassistant.core import HomeAssistant, callback
|
||||
from homeassistant.loader import bind_hass
|
||||
from homeassistant.util.network import is_local
|
||||
|
||||
|
||||
@bind_hass
|
||||
@callback
|
||||
def async_get_external_url(hass: HomeAssistant) -> Optional[str]:
|
||||
"""Get external url of this instance.
|
||||
|
||||
Note: currently it takes 30 seconds after Home Assistant starts for
|
||||
cloud.async_remote_ui_url to work.
|
||||
"""
|
||||
if "cloud" in hass.config.components:
|
||||
try:
|
||||
return cast(str, hass.components.cloud.async_remote_ui_url())
|
||||
except hass.components.cloud.CloudNotAvailable:
|
||||
pass
|
||||
|
||||
if hass.config.api is None:
|
||||
return None
|
||||
|
||||
base_url = yarl.URL(hass.config.api.base_url)
|
||||
|
||||
try:
|
||||
if is_local(ip_address(base_url.host)):
|
||||
return None
|
||||
except ValueError:
|
||||
# ip_address raises ValueError if host is not an IP address
|
||||
pass
|
||||
|
||||
return str(base_url)
|
||||
Reference in new issue
Block a user