Add support for Minecraft Server Bedrock Edition (#100925)

This commit is contained in:
elmurato 2023-10-10 08:42:35 +02:00 committed by GitHub
parent deffa50142
commit c6a3fa30f0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
12 changed files with 447 additions and 144 deletions

View file

@ -7,17 +7,21 @@ from typing import Any
from homeassistant.components.sensor import SensorEntity, SensorEntityDescription
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import UnitOfTime
from homeassistant.const import CONF_TYPE, UnitOfTime
from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import StateType
from .api import MinecraftServerData, MinecraftServerType
from .const import DOMAIN, KEY_LATENCY, KEY_MOTD
from .coordinator import MinecraftServerCoordinator, MinecraftServerData
from .coordinator import MinecraftServerCoordinator
from .entity import MinecraftServerEntity
ATTR_PLAYERS_LIST = "players_list"
ICON_EDITION = "mdi:minecraft"
ICON_GAME_MODE = "mdi:cog"
ICON_MAP_NAME = "mdi:map"
ICON_LATENCY = "mdi:signal"
ICON_PLAYERS_MAX = "mdi:account-multiple"
ICON_PLAYERS_ONLINE = "mdi:account-multiple"
@ -25,6 +29,9 @@ ICON_PROTOCOL_VERSION = "mdi:numeric"
ICON_VERSION = "mdi:numeric"
ICON_MOTD = "mdi:minecraft"
KEY_EDITION = "edition"
KEY_GAME_MODE = "game_mode"
KEY_MAP_NAME = "map_name"
KEY_PLAYERS_MAX = "players_max"
KEY_PLAYERS_ONLINE = "players_online"
KEY_PROTOCOL_VERSION = "protocol_version"
@ -40,6 +47,7 @@ class MinecraftServerEntityDescriptionMixin:
value_fn: Callable[[MinecraftServerData], StateType]
attributes_fn: Callable[[MinecraftServerData], MutableMapping[str, Any]] | None
supported_server_types: list[MinecraftServerType]
@dataclass
@ -69,6 +77,10 @@ SENSOR_DESCRIPTIONS = [
icon=ICON_VERSION,
value_fn=lambda data: data.version,
attributes_fn=None,
supported_server_types=[
MinecraftServerType.JAVA_EDITION,
MinecraftServerType.BEDROCK_EDITION,
],
),
MinecraftServerSensorEntityDescription(
key=KEY_PROTOCOL_VERSION,
@ -76,6 +88,10 @@ SENSOR_DESCRIPTIONS = [
icon=ICON_PROTOCOL_VERSION,
value_fn=lambda data: data.protocol_version,
attributes_fn=None,
supported_server_types=[
MinecraftServerType.JAVA_EDITION,
MinecraftServerType.BEDROCK_EDITION,
],
),
MinecraftServerSensorEntityDescription(
key=KEY_PLAYERS_MAX,
@ -84,6 +100,10 @@ SENSOR_DESCRIPTIONS = [
icon=ICON_PLAYERS_MAX,
value_fn=lambda data: data.players_max,
attributes_fn=None,
supported_server_types=[
MinecraftServerType.JAVA_EDITION,
MinecraftServerType.BEDROCK_EDITION,
],
),
MinecraftServerSensorEntityDescription(
key=KEY_LATENCY,
@ -93,6 +113,10 @@ SENSOR_DESCRIPTIONS = [
icon=ICON_LATENCY,
value_fn=lambda data: data.latency,
attributes_fn=None,
supported_server_types=[
MinecraftServerType.JAVA_EDITION,
MinecraftServerType.BEDROCK_EDITION,
],
),
MinecraftServerSensorEntityDescription(
key=KEY_MOTD,
@ -100,6 +124,10 @@ SENSOR_DESCRIPTIONS = [
icon=ICON_MOTD,
value_fn=lambda data: data.motd,
attributes_fn=None,
supported_server_types=[
MinecraftServerType.JAVA_EDITION,
MinecraftServerType.BEDROCK_EDITION,
],
),
MinecraftServerSensorEntityDescription(
key=KEY_PLAYERS_ONLINE,
@ -108,6 +136,40 @@ SENSOR_DESCRIPTIONS = [
icon=ICON_PLAYERS_ONLINE,
value_fn=lambda data: data.players_online,
attributes_fn=get_extra_state_attributes_players_list,
supported_server_types=[
MinecraftServerType.JAVA_EDITION,
MinecraftServerType.BEDROCK_EDITION,
],
),
MinecraftServerSensorEntityDescription(
key=KEY_EDITION,
translation_key=KEY_EDITION,
icon=ICON_EDITION,
value_fn=lambda data: data.edition,
attributes_fn=None,
supported_server_types=[
MinecraftServerType.BEDROCK_EDITION,
],
),
MinecraftServerSensorEntityDescription(
key=KEY_GAME_MODE,
translation_key=KEY_GAME_MODE,
icon=ICON_GAME_MODE,
value_fn=lambda data: data.game_mode,
attributes_fn=None,
supported_server_types=[
MinecraftServerType.BEDROCK_EDITION,
],
),
MinecraftServerSensorEntityDescription(
key=KEY_MAP_NAME,
translation_key=KEY_MAP_NAME,
icon=ICON_MAP_NAME,
value_fn=lambda data: data.map_name,
attributes_fn=None,
supported_server_types=[
MinecraftServerType.BEDROCK_EDITION,
],
),
]
@ -123,8 +185,10 @@ async def async_setup_entry(
# Add sensor entities.
async_add_entities(
[
MinecraftServerSensorEntity(coordinator, description)
MinecraftServerSensorEntity(coordinator, description, config_entry)
for description in SENSOR_DESCRIPTIONS
if config_entry.data.get(CONF_TYPE, MinecraftServerType.JAVA_EDITION)
in description.supported_server_types
]
)
@ -138,11 +202,12 @@ class MinecraftServerSensorEntity(MinecraftServerEntity, SensorEntity):
self,
coordinator: MinecraftServerCoordinator,
description: MinecraftServerSensorEntityDescription,
config_entry: ConfigEntry,
) -> None:
"""Initialize sensor base entity."""
super().__init__(coordinator)
super().__init__(coordinator, config_entry)
self.entity_description = description
self._attr_unique_id = f"{coordinator.unique_id}-{description.key}"
self._attr_unique_id = f"{config_entry.entry_id}-{description.key}"
self._update_properties()
@callback