Add World Message/MOTD support for MinecraftServer Integration (#66297)
parent
0c6a6c360b
commit
43772b3fa9
|
@ -95,6 +95,7 @@ class MinecraftServer:
|
|||
self.players_online = None
|
||||
self.players_max = None
|
||||
self.players_list = None
|
||||
self.motd = None
|
||||
|
||||
# Dispatcher signal name
|
||||
self.signal_name = f"{SIGNAL_NAME_PREFIX}_{self.unique_id}"
|
||||
|
@ -179,6 +180,7 @@ class MinecraftServer:
|
|||
self.players_online = status_response.players.online
|
||||
self.players_max = status_response.players.max
|
||||
self.latency_time = status_response.latency
|
||||
self.motd = (status_response.description).get("text")
|
||||
self.players_list = []
|
||||
if status_response.players.sample is not None:
|
||||
for player in status_response.players.sample:
|
||||
|
@ -201,6 +203,7 @@ class MinecraftServer:
|
|||
self.players_max = None
|
||||
self.latency_time = None
|
||||
self.players_list = None
|
||||
self.motd = None
|
||||
|
||||
# Inform user once about failed update if necessary.
|
||||
if not self._last_status_request_failed:
|
||||
|
|
|
@ -14,6 +14,7 @@ ICON_PLAYERS_ONLINE = "mdi:account-multiple"
|
|||
ICON_PROTOCOL_VERSION = "mdi:numeric"
|
||||
ICON_STATUS = "mdi:lan"
|
||||
ICON_VERSION = "mdi:numeric"
|
||||
ICON_MOTD = "mdi:minecraft"
|
||||
|
||||
KEY_SERVERS = "servers"
|
||||
|
||||
|
@ -25,6 +26,7 @@ NAME_PLAYERS_ONLINE = "Players Online"
|
|||
NAME_PROTOCOL_VERSION = "Protocol Version"
|
||||
NAME_STATUS = "Status"
|
||||
NAME_VERSION = "Version"
|
||||
NAME_MOTD = "World Message"
|
||||
|
||||
SCAN_INTERVAL = 60
|
||||
|
||||
|
@ -36,3 +38,4 @@ UNIT_PLAYERS_MAX = "players"
|
|||
UNIT_PLAYERS_ONLINE = "players"
|
||||
UNIT_PROTOCOL_VERSION = None
|
||||
UNIT_VERSION = None
|
||||
UNIT_MOTD = None
|
||||
|
|
|
@ -14,15 +14,18 @@ from .const import (
|
|||
ATTR_PLAYERS_LIST,
|
||||
DOMAIN,
|
||||
ICON_LATENCY_TIME,
|
||||
ICON_MOTD,
|
||||
ICON_PLAYERS_MAX,
|
||||
ICON_PLAYERS_ONLINE,
|
||||
ICON_PROTOCOL_VERSION,
|
||||
ICON_VERSION,
|
||||
NAME_LATENCY_TIME,
|
||||
NAME_MOTD,
|
||||
NAME_PLAYERS_MAX,
|
||||
NAME_PLAYERS_ONLINE,
|
||||
NAME_PROTOCOL_VERSION,
|
||||
NAME_VERSION,
|
||||
UNIT_MOTD,
|
||||
UNIT_PLAYERS_MAX,
|
||||
UNIT_PLAYERS_ONLINE,
|
||||
UNIT_PROTOCOL_VERSION,
|
||||
|
@ -45,6 +48,7 @@ async def async_setup_entry(
|
|||
MinecraftServerLatencyTimeSensor(server),
|
||||
MinecraftServerPlayersOnlineSensor(server),
|
||||
MinecraftServerPlayersMaxSensor(server),
|
||||
MinecraftServerMOTDSensor(server),
|
||||
]
|
||||
|
||||
# Add sensor entities.
|
||||
|
@ -176,3 +180,20 @@ class MinecraftServerPlayersMaxSensor(MinecraftServerSensorEntity):
|
|||
async def async_update(self) -> None:
|
||||
"""Update maximum number of players."""
|
||||
self._state = self._server.players_max
|
||||
|
||||
|
||||
class MinecraftServerMOTDSensor(MinecraftServerSensorEntity):
|
||||
"""Representation of a Minecraft Server MOTD sensor."""
|
||||
|
||||
def __init__(self, server: MinecraftServer) -> None:
|
||||
"""Initialize MOTD sensor."""
|
||||
super().__init__(
|
||||
server=server,
|
||||
type_name=NAME_MOTD,
|
||||
icon=ICON_MOTD,
|
||||
unit=UNIT_MOTD,
|
||||
)
|
||||
|
||||
async def async_update(self) -> None:
|
||||
"""Update MOTD."""
|
||||
self._state = self._server.motd
|
||||
|
|
Loading…
Reference in New Issue