Add Minecraft Server Integration (#30992)
* Add Minecraft Server integration * Add unit test for config flow * Fixed some review findings and increased unit test coverage * Fixed docstrings of new test cases * Removed unnecessary debug log messages * Added unique IDs and device infos and removed duplicate name validation * Attempt to fix unit test on CI * Return state OFF instead of UNAVAILABLE in case connection to server drops * Added property decorator to server properties, even less debug messages, improved sensor dispatcher connection and other review findings fixed * Moved special property handling to sensors, fixed name confusion in sensor entity, switch to HA const for scan_interval, simplified building players list string * Improved periodic update, speeded up unit tests * Added type hints, added callback decorator to entity update callback, added const.py to unit test exclusions * Changed state sensor to binary sensor, removed empty unit test file, added constants for icons and units * Let HA handle unknown state, check for None in description and players list sensor * Removed periods at end of log messages, removed constant for default host * Updated requirements_test_pre_commit.txt, fixed codespell findings * Use localhost as default host * Removed passing hass to entities, moved log message from init, moved host lower to vol, use proper patch library, patch library instead of own code * Replaced server properties with global instance attributes, removed config option scan_interval, switch back to async_track_time_interval * Removed description and players list sensors, added players list as state attributes to online players sensor, raise OSError instead of deprecated IOError, other minor review findings fixed * Use MAC address for unique_id in case of an IP address as host, added getmac to manifest.json, added invalid_ip to strings.json, added new test cases for changes in config_flow, replace all IOError's with OSError, other review findings fixed * Removed double assignment * Call get_mac_address async safe * Handle unavailable and unknown states to reach silver quality scale, added quality scale to manifest.json
This commit is contained in:
1 parent
5483de7e25
commit
699d6ad658
14 files changed
+893
No files matched your search
@@ -0,0 +1 @@
|
||||
"""Tests for the Minecraft Server integration."""
|
||||
@@ -0,0 +1,194 @@
|
||||
"""Test the Minecraft Server config flow."""
|
||||
|
||||
from asynctest import patch
|
||||
from mcstatus.pinger import PingResponse
|
||||
|
||||
from homeassistant.components.minecraft_server.const import (
|
||||
DEFAULT_NAME,
|
||||
DEFAULT_PORT,
|
||||
DOMAIN,
|
||||
)
|
||||
from homeassistant.config_entries import SOURCE_USER
|
||||
from homeassistant.const import CONF_HOST, CONF_NAME, CONF_PORT
|
||||
from homeassistant.data_entry_flow import (
|
||||
RESULT_TYPE_ABORT,
|
||||
RESULT_TYPE_CREATE_ENTRY,
|
||||
RESULT_TYPE_FORM,
|
||||
)
|
||||
from homeassistant.helpers.typing import HomeAssistantType
|
||||
|
||||
from tests.common import MockConfigEntry
|
||||
|
||||
STATUS_RESPONSE_RAW = {
|
||||
"description": {"text": "Dummy Description"},
|
||||
"version": {"name": "Dummy Version", "protocol": 123},
|
||||
"players": {
|
||||
"online": 3,
|
||||
"max": 10,
|
||||
"sample": [
|
||||
{"name": "Player 1", "id": "1"},
|
||||
{"name": "Player 2", "id": "2"},
|
||||
{"name": "Player 3", "id": "3"},
|
||||
],
|
||||
},
|
||||
}
|
||||
|
||||
USER_INPUT = {
|
||||
CONF_NAME: DEFAULT_NAME,
|
||||
CONF_HOST: "mc.dummyserver.com",
|
||||
CONF_PORT: DEFAULT_PORT,
|
||||
}
|
||||
|
||||
USER_INPUT_IPV4 = {
|
||||
CONF_NAME: DEFAULT_NAME,
|
||||
CONF_HOST: "1.1.1.1",
|
||||
CONF_PORT: DEFAULT_PORT,
|
||||
}
|
||||
|
||||
USER_INPUT_IPV6 = {
|
||||
CONF_NAME: DEFAULT_NAME,
|
||||
CONF_HOST: "::ffff:0101:0101",
|
||||
CONF_PORT: DEFAULT_PORT,
|
||||
}
|
||||
|
||||
USER_INPUT_PORT_TOO_SMALL = {
|
||||
CONF_NAME: DEFAULT_NAME,
|
||||
CONF_HOST: "mc.dummyserver.com",
|
||||
CONF_PORT: 1023,
|
||||
}
|
||||
|
||||
USER_INPUT_PORT_TOO_LARGE = {
|
||||
CONF_NAME: DEFAULT_NAME,
|
||||
CONF_HOST: "mc.dummyserver.com",
|
||||
CONF_PORT: 65536,
|
||||
}
|
||||
|
||||
|
||||
async def test_show_config_form(hass: HomeAssistantType) -> None:
|
||||
"""Test if initial configuration form is shown."""
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": SOURCE_USER}
|
||||
)
|
||||
|
||||
assert result["type"] == RESULT_TYPE_FORM
|
||||
assert result["step_id"] == "user"
|
||||
|
||||
|
||||
async def test_invalid_ip(hass: HomeAssistantType) -> None:
|
||||
"""Test error in case of an invalid IP address."""
|
||||
with patch("getmac.get_mac_address", return_value=None):
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": SOURCE_USER}, data=USER_INPUT_IPV4
|
||||
)
|
||||
|
||||
assert result["type"] == RESULT_TYPE_FORM
|
||||
assert result["errors"] == {"base": "invalid_ip"}
|
||||
|
||||
|
||||
async def test_same_host(hass: HomeAssistantType) -> None:
|
||||
"""Test abort in case of same host name."""
|
||||
unique_id = f"{USER_INPUT[CONF_HOST]}-{USER_INPUT[CONF_PORT]}"
|
||||
mock_config_entry = MockConfigEntry(
|
||||
domain=DOMAIN, unique_id=unique_id, data=USER_INPUT
|
||||
)
|
||||
mock_config_entry.add_to_hass(hass)
|
||||
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": SOURCE_USER}, data=USER_INPUT
|
||||
)
|
||||
|
||||
assert result["type"] == RESULT_TYPE_ABORT
|
||||
assert result["reason"] == "already_configured"
|
||||
|
||||
|
||||
async def test_port_too_small(hass: HomeAssistantType) -> None:
|
||||
"""Test error in case of a too small port."""
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": SOURCE_USER}, data=USER_INPUT_PORT_TOO_SMALL
|
||||
)
|
||||
|
||||
assert result["type"] == RESULT_TYPE_FORM
|
||||
assert result["errors"] == {"base": "invalid_port"}
|
||||
|
||||
|
||||
async def test_port_too_large(hass: HomeAssistantType) -> None:
|
||||
"""Test error in case of a too large port."""
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": SOURCE_USER}, data=USER_INPUT_PORT_TOO_LARGE
|
||||
)
|
||||
|
||||
assert result["type"] == RESULT_TYPE_FORM
|
||||
assert result["errors"] == {"base": "invalid_port"}
|
||||
|
||||
|
||||
async def test_connection_failed(hass: HomeAssistantType) -> None:
|
||||
"""Test error in case of a failed connection."""
|
||||
with patch("mcstatus.server.MinecraftServer.ping", side_effect=OSError):
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": SOURCE_USER}, data=USER_INPUT
|
||||
)
|
||||
|
||||
assert result["type"] == RESULT_TYPE_FORM
|
||||
assert result["errors"] == {"base": "cannot_connect"}
|
||||
|
||||
|
||||
async def test_connection_succeeded_with_host(hass: HomeAssistantType) -> None:
|
||||
"""Test config entry in case of a successful connection with a host name."""
|
||||
with patch("mcstatus.server.MinecraftServer.ping", return_value=50):
|
||||
with patch(
|
||||
"mcstatus.server.MinecraftServer.status",
|
||||
return_value=PingResponse(STATUS_RESPONSE_RAW),
|
||||
):
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": SOURCE_USER}, data=USER_INPUT
|
||||
)
|
||||
|
||||
assert result["type"] == RESULT_TYPE_CREATE_ENTRY
|
||||
assert result["title"] == f"{USER_INPUT[CONF_HOST]}:{USER_INPUT[CONF_PORT]}"
|
||||
assert result["data"][CONF_NAME] == USER_INPUT[CONF_NAME]
|
||||
assert result["data"][CONF_HOST] == USER_INPUT[CONF_HOST]
|
||||
assert result["data"][CONF_PORT] == USER_INPUT[CONF_PORT]
|
||||
|
||||
|
||||
async def test_connection_succeeded_with_ip4(hass: HomeAssistantType) -> None:
|
||||
"""Test config entry in case of a successful connection with an IPv4 address."""
|
||||
with patch("getmac.get_mac_address", return_value="01:23:45:67:89:ab"):
|
||||
with patch("mcstatus.server.MinecraftServer.ping", return_value=50):
|
||||
with patch(
|
||||
"mcstatus.server.MinecraftServer.status",
|
||||
return_value=PingResponse(STATUS_RESPONSE_RAW),
|
||||
):
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": SOURCE_USER}, data=USER_INPUT_IPV4
|
||||
)
|
||||
|
||||
assert result["type"] == RESULT_TYPE_CREATE_ENTRY
|
||||
assert (
|
||||
result["title"]
|
||||
== f"{USER_INPUT_IPV4[CONF_HOST]}:{USER_INPUT_IPV4[CONF_PORT]}"
|
||||
)
|
||||
assert result["data"][CONF_NAME] == USER_INPUT_IPV4[CONF_NAME]
|
||||
assert result["data"][CONF_HOST] == USER_INPUT_IPV4[CONF_HOST]
|
||||
assert result["data"][CONF_PORT] == USER_INPUT_IPV4[CONF_PORT]
|
||||
|
||||
|
||||
async def test_connection_succeeded_with_ip6(hass: HomeAssistantType) -> None:
|
||||
"""Test config entry in case of a successful connection with an IPv6 address."""
|
||||
with patch("getmac.get_mac_address", return_value="01:23:45:67:89:ab"):
|
||||
with patch("mcstatus.server.MinecraftServer.ping", return_value=50):
|
||||
with patch(
|
||||
"mcstatus.server.MinecraftServer.status",
|
||||
return_value=PingResponse(STATUS_RESPONSE_RAW),
|
||||
):
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": SOURCE_USER}, data=USER_INPUT_IPV6
|
||||
)
|
||||
|
||||
assert result["type"] == RESULT_TYPE_CREATE_ENTRY
|
||||
assert (
|
||||
result["title"]
|
||||
== f"{USER_INPUT_IPV6[CONF_HOST]}:{USER_INPUT_IPV6[CONF_PORT]}"
|
||||
)
|
||||
assert result["data"][CONF_NAME] == USER_INPUT_IPV6[CONF_NAME]
|
||||
assert result["data"][CONF_HOST] == USER_INPUT_IPV6[CONF_HOST]
|
||||
assert result["data"][CONF_PORT] == USER_INPUT_IPV6[CONF_PORT]
|
||||
Reference in new issue
Block a user