Increase timeouts in Minecraft Server (#101784)

This commit is contained in:
elmurato 2023-10-23 15:49:48 +02:00 committed by GitHub
parent c7d2499a52
commit 54bcd70878
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 23 additions and 8 deletions

View file

@ -39,7 +39,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
)
except MinecraftServerAddressError as error:
raise ConfigEntryError(
f"Server address in configuration entry is invalid (error: {error})"
f"Server address in configuration entry is invalid: {error}"
) from error
# Create coordinator instance.
@ -109,7 +109,7 @@ async def async_migrate_entry(hass: HomeAssistant, config_entry: ConfigEntry) ->
except MinecraftServerAddressError as error:
host_only_lookup_success = False
_LOGGER.debug(
"Hostname (without port) cannot be parsed (error: %s), trying again with port",
"Hostname (without port) cannot be parsed, trying again with port: %s",
error,
)
@ -119,7 +119,7 @@ async def async_migrate_entry(hass: HomeAssistant, config_entry: ConfigEntry) ->
MinecraftServer(MinecraftServerType.JAVA_EDITION, address)
except MinecraftServerAddressError as error:
_LOGGER.exception(
"Can't migrate configuration entry due to error while parsing server address (error: %s), try again later",
"Can't migrate configuration entry due to error while parsing server address, try again later: %s",
error,
)
return False

View file

@ -11,6 +11,10 @@ from mcstatus.status_response import BedrockStatusResponse, JavaStatusResponse
_LOGGER = logging.getLogger(__name__)
LOOKUP_TIMEOUT: float = 10
DATA_UPDATE_TIMEOUT: float = 10
DATA_UPDATE_RETRIES: int = 3
@dataclass
class MinecraftServerData:
@ -57,14 +61,17 @@ class MinecraftServer:
"""Initialize server instance."""
try:
if server_type == MinecraftServerType.JAVA_EDITION:
self._server = JavaServer.lookup(address)
self._server = JavaServer.lookup(address, timeout=LOOKUP_TIMEOUT)
else:
self._server = BedrockServer.lookup(address)
self._server = BedrockServer.lookup(address, timeout=LOOKUP_TIMEOUT)
except (ValueError, LifetimeTimeout) as error:
raise MinecraftServerAddressError(
f"{server_type} server address '{address}' is invalid (error: {error})"
f"Lookup of '{address}' failed: {self._get_error_message(error)}"
) from error
self._server.timeout = DATA_UPDATE_TIMEOUT
self._address = address
_LOGGER.debug(
"%s server instance created with address '%s'", server_type, address
)
@ -83,10 +90,10 @@ class MinecraftServer:
status_response: BedrockStatusResponse | JavaStatusResponse
try:
status_response = await self._server.async_status()
status_response = await self._server.async_status(tries=DATA_UPDATE_RETRIES)
except OSError as error:
raise MinecraftServerConnectionError(
f"Fetching data from the server failed (error: {error})"
f"Status request to '{self._address}' failed: {self._get_error_message(error)}"
) from error
if isinstance(status_response, JavaStatusResponse):
@ -132,3 +139,11 @@ class MinecraftServer:
game_mode=status_response.gamemode,
map_name=status_response.map_name,
)
def _get_error_message(self, error: BaseException) -> str:
"""Get error message of an exception."""
if not str(error):
# Fallback to error type in case of an empty error message.
return repr(error)
return str(error)