1 """The Minecraft Server sensor platform."""
3 from __future__
import annotations
5 from collections.abc
import Callable
6 from dataclasses
import dataclass
16 from .api
import MinecraftServerData, MinecraftServerType
17 from .const
import DOMAIN, KEY_LATENCY, KEY_MOTD
18 from .coordinator
import MinecraftServerCoordinator
19 from .entity
import MinecraftServerEntity
21 ATTR_PLAYERS_LIST =
"players_list"
23 KEY_EDITION =
"edition"
24 KEY_GAME_MODE =
"game_mode"
25 KEY_MAP_NAME =
"map_name"
26 KEY_PLAYERS_MAX =
"players_max"
27 KEY_PLAYERS_ONLINE =
"players_online"
28 KEY_PROTOCOL_VERSION =
"protocol_version"
29 KEY_VERSION =
"version"
31 UNIT_PLAYERS_MAX =
"players"
32 UNIT_PLAYERS_ONLINE =
"players"
35 @dataclass(frozen=True, kw_only=True)
37 """Class describing Minecraft Server sensor entities."""
39 value_fn: Callable[[MinecraftServerData], StateType]
40 attributes_fn: Callable[[MinecraftServerData], dict[str, Any]] |
None
41 supported_server_types: set[MinecraftServerType]
45 data: MinecraftServerData,
46 ) -> dict[str, list[str]]:
47 """Return players list as extra state attributes, if available."""
48 extra_state_attributes: dict[str, Any] = {}
49 players_list = data.players_list
51 if players_list
is not None and len(players_list) != 0:
52 extra_state_attributes[ATTR_PLAYERS_LIST] = players_list
54 return extra_state_attributes
57 SENSOR_DESCRIPTIONS = [
60 translation_key=KEY_VERSION,
61 value_fn=
lambda data: data.version,
63 supported_server_types={
64 MinecraftServerType.JAVA_EDITION,
65 MinecraftServerType.BEDROCK_EDITION,
67 entity_category=EntityCategory.DIAGNOSTIC,
70 key=KEY_PROTOCOL_VERSION,
71 translation_key=KEY_PROTOCOL_VERSION,
72 value_fn=
lambda data: data.protocol_version,
74 supported_server_types={
75 MinecraftServerType.JAVA_EDITION,
76 MinecraftServerType.BEDROCK_EDITION,
78 entity_category=EntityCategory.DIAGNOSTIC,
79 entity_registry_enabled_default=
False,
83 translation_key=KEY_PLAYERS_MAX,
84 native_unit_of_measurement=UNIT_PLAYERS_MAX,
85 value_fn=
lambda data: data.players_max,
87 supported_server_types={
88 MinecraftServerType.JAVA_EDITION,
89 MinecraftServerType.BEDROCK_EDITION,
91 entity_registry_enabled_default=
False,
95 translation_key=KEY_LATENCY,
96 native_unit_of_measurement=UnitOfTime.MILLISECONDS,
97 suggested_display_precision=0,
98 value_fn=
lambda data: data.latency,
100 supported_server_types={
101 MinecraftServerType.JAVA_EDITION,
102 MinecraftServerType.BEDROCK_EDITION,
104 entity_category=EntityCategory.DIAGNOSTIC,
108 translation_key=KEY_MOTD,
109 value_fn=
lambda data: data.motd,
111 supported_server_types={
112 MinecraftServerType.JAVA_EDITION,
113 MinecraftServerType.BEDROCK_EDITION,
117 key=KEY_PLAYERS_ONLINE,
118 translation_key=KEY_PLAYERS_ONLINE,
119 native_unit_of_measurement=UNIT_PLAYERS_ONLINE,
120 value_fn=
lambda data: data.players_online,
121 attributes_fn=get_extra_state_attributes_players_list,
122 supported_server_types={
123 MinecraftServerType.JAVA_EDITION,
124 MinecraftServerType.BEDROCK_EDITION,
129 translation_key=KEY_EDITION,
130 value_fn=
lambda data: data.edition,
132 supported_server_types={
133 MinecraftServerType.BEDROCK_EDITION,
135 entity_category=EntityCategory.DIAGNOSTIC,
136 entity_registry_enabled_default=
False,
140 translation_key=KEY_GAME_MODE,
141 value_fn=
lambda data: data.game_mode,
143 supported_server_types={
144 MinecraftServerType.BEDROCK_EDITION,
149 translation_key=KEY_MAP_NAME,
150 value_fn=
lambda data: data.map_name,
152 supported_server_types={
153 MinecraftServerType.BEDROCK_EDITION,
161 config_entry: ConfigEntry,
162 async_add_entities: AddEntitiesCallback,
164 """Set up the Minecraft Server sensor platform."""
165 coordinator = hass.data[DOMAIN][config_entry.entry_id]
171 for description
in SENSOR_DESCRIPTIONS
172 if config_entry.data.get(CONF_TYPE, MinecraftServerType.JAVA_EDITION)
173 in description.supported_server_types
179 """Representation of a Minecraft Server sensor base entity."""
181 entity_description: MinecraftServerSensorEntityDescription
185 coordinator: MinecraftServerCoordinator,
186 description: MinecraftServerSensorEntityDescription,
187 config_entry: ConfigEntry,
189 """Initialize sensor base entity."""
190 super().
__init__(coordinator, config_entry)
197 """Handle updated data from the coordinator."""
203 """Update sensor properties."""
205 self.coordinator.data
None _handle_coordinator_update(self)
None _update_properties(self)
_attr_extra_state_attributes
None __init__(self, MinecraftServerCoordinator coordinator, MinecraftServerSensorEntityDescription description, ConfigEntry config_entry)
None async_write_ha_state(self)
dict[str, list[str]] get_extra_state_attributes_players_list(MinecraftServerData data)
None async_setup_entry(HomeAssistant hass, ConfigEntry config_entry, AddEntitiesCallback async_add_entities)