2020-03-23 23:51:13 +00:00
|
|
|
"""Helper functions for the Minecraft Server integration."""
|
2021-03-18 12:07:04 +00:00
|
|
|
from __future__ import annotations
|
2020-03-23 23:51:13 +00:00
|
|
|
|
2021-03-18 12:07:04 +00:00
|
|
|
from typing import Any
|
2020-03-23 23:51:13 +00:00
|
|
|
|
|
|
|
import aiodns
|
|
|
|
|
|
|
|
from homeassistant.const import CONF_HOST, CONF_PORT
|
2021-04-22 20:23:36 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
2020-03-23 23:51:13 +00:00
|
|
|
|
|
|
|
from .const import SRV_RECORD_PREFIX
|
|
|
|
|
|
|
|
|
2022-07-14 16:06:33 +00:00
|
|
|
async def async_check_srv_record(
|
|
|
|
hass: HomeAssistant, host: str
|
|
|
|
) -> dict[str, Any] | None:
|
2020-03-23 23:51:13 +00:00
|
|
|
"""Check if the given host is a valid Minecraft SRV record."""
|
|
|
|
# Check if 'host' is a valid SRV record.
|
|
|
|
return_value = None
|
|
|
|
srv_records = None
|
|
|
|
try:
|
|
|
|
srv_records = await aiodns.DNSResolver().query(
|
|
|
|
host=f"{SRV_RECORD_PREFIX}.{host}", qtype="SRV"
|
|
|
|
)
|
2023-02-02 17:35:24 +00:00
|
|
|
except aiodns.error.DNSError:
|
2020-03-23 23:51:13 +00:00
|
|
|
# 'host' is not a SRV record.
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
# 'host' is a valid SRV record, extract the data.
|
|
|
|
return_value = {
|
|
|
|
CONF_HOST: srv_records[0].host,
|
|
|
|
CONF_PORT: srv_records[0].port,
|
|
|
|
}
|
|
|
|
|
|
|
|
return return_value
|