Add support for Minecraft SRV records (#32372)

* Added support for Minecraft SRV records

* Switched from dnspython to aiodns, improved server ping and log messages, use address instead of host and port in config flow

* Updated component requirements
This commit is contained in:
elmurato authored and GitHub committed 2020-03-23 16:51:13 -07:00
1 parent cd57b764ce
commit 45241e57ca
10 files changed
+245 -96

No files matched your search

@@ -9,7 +9,7 @@ from homeassistant import config_entries
from homeassistant.config_entries import ConfigFlow
from homeassistant.const import CONF_HOST, CONF_NAME, CONF_PORT
from . import MinecraftServer
from . import MinecraftServer, helpers
from .const import ( # pylint: disable=unused-import
DEFAULT_HOST,
DEFAULT_NAME,
@@ -29,11 +29,24 @@ class MinecraftServerConfigFlow(ConfigFlow, domain=DOMAIN):
errors = {}
if user_input is not None:
# User inputs.
host = user_input[CONF_HOST]
port = user_input[CONF_PORT]
host = None
port = DEFAULT_PORT
# Split address at last occurrence of ':'.
address_left, separator, address_right = user_input[CONF_HOST].rpartition(
":"
)
# If no separator is found, 'rpartition' return ('', '', original_string).
if separator == "":
host = address_right
else:
host = address_left
try:
port = int(address_right)
except ValueError:
pass # 'port' is already set to default value.
unique_id = ""
# Remove '[' and ']' in case of an IPv6 address.
host = host.strip("[]")
# Check if 'host' is a valid IP address and if so, get the MAC address.
ip_address = None
@@ -42,6 +55,7 @@ class MinecraftServerConfigFlow(ConfigFlow, domain=DOMAIN):
ip_address = ipaddress.ip_address(host)
except ValueError:
# Host is not a valid IP address.
# Continue with host and port.
pass
else:
# Host is a valid IP address.
@@ -55,38 +69,56 @@ class MinecraftServerConfigFlow(ConfigFlow, domain=DOMAIN):
partial(getmac.get_mac_address, **params)
)
# Validate IP address via valid MAC address.
# Validate IP address (MAC address must be available).
if ip_address is not None and mac_address is None:
errors["base"] = "invalid_ip"
# Validate port configuration (limit to user and dynamic port range).
elif (port < 1024) or (port > 65535):
errors["base"] = "invalid_port"
# Validate host and port via ping request to server.
# Validate host and port by checking the server connection.
else:
# Build unique_id.
if ip_address is not None:
# Since IP addresses can change and therefore are not allowed in a
# unique_id, fall back to the MAC address.
unique_id = f"{mac_address}-{port}"
else:
# Use host name in unique_id (host names should not change).
unique_id = f"{host}-{port}"
# Abort in case the host was already configured before.
await self.async_set_unique_id(unique_id)
self._abort_if_unique_id_configured()
# Create server instance with configuration data and try pinging the server.
server = MinecraftServer(self.hass, unique_id, user_input)
# Create server instance with configuration data and ping the server.
config_data = {
CONF_NAME: user_input[CONF_NAME],
CONF_HOST: host,
CONF_PORT: port,
}
server = MinecraftServer(self.hass, "dummy_unique_id", config_data)
await server.async_check_connection()
if not server.online:
# Host or port invalid or server not reachable.
errors["base"] = "cannot_connect"
else:
# Build unique_id and config entry title.
unique_id = ""
title = f"{host}:{port}"
if ip_address is not None:
# Since IP addresses can change and therefore are not allowed in a
# unique_id, fall back to the MAC address and port (to support
# servers with same MAC address but different ports).
unique_id = f"{mac_address}-{port}"
if ip_address.version == 6:
title = f"[{host}]:{port}"
else:
# Check if 'host' is a valid SRV record.
srv_record = await helpers.async_check_srv_record(
self.hass, host
)
if srv_record is not None:
# Use only SRV host name in unique_id (does not change).
unique_id = f"{host}-srv"
title = host
else:
# Use host name and port in unique_id (to support servers with
# same host name but different ports).
unique_id = f"{host}-{port}"
# Abort in case the host was already configured before.
await self.async_set_unique_id(unique_id)
self._abort_if_unique_id_configured()
# Configuration data are available and no error was detected, create configuration entry.
return self.async_create_entry(
title=f"{host}:{port}", data=user_input
)
return self.async_create_entry(title=title, data=config_data)
# Show configuration form (default form in case of no user_input,
# form filled with user_input and eventually with errors otherwise).
@@ -107,9 +139,6 @@ class MinecraftServerConfigFlow(ConfigFlow, domain=DOMAIN):
vol.Required(
CONF_HOST, default=user_input.get(CONF_HOST, DEFAULT_HOST)
): vol.All(str, vol.Lower),
vol.Optional(
CONF_PORT, default=user_input.get(CONF_PORT, DEFAULT_PORT)
): int,
}
),
errors=errors,