1 """The Minecraft Server integration."""
3 from __future__
import annotations
26 from .api
import MinecraftServer, MinecraftServerAddressError, MinecraftServerType
27 from .const
import DOMAIN, KEY_LATENCY, KEY_MOTD
28 from .coordinator
import MinecraftServerCoordinator
30 PLATFORMS = [Platform.BINARY_SENSOR, Platform.SENSOR]
32 _LOGGER = logging.getLogger(__name__)
36 """Load dnspython rdata classes used by mcstatus."""
37 for rdtype
in dns.rdatatype.RdataType:
38 if not dns.rdatatype.is_metatype(rdtype)
or rdtype == dns.rdatatype.OPT:
39 dns.rdata.get_rdata_class(dns.rdataclass.IN, rdtype)
43 """Set up Minecraft Server from a config entry."""
46 hass.async_add_executor_job(load_dnspython_rdata_classes)
51 entry.data.get(CONF_TYPE, MinecraftServerType.JAVA_EDITION),
52 entry.data[CONF_ADDRESS],
57 await api.async_initialize()
58 except MinecraftServerAddressError
as error:
63 await coordinator.async_config_entry_first_refresh()
66 domain_data = hass.data.setdefault(DOMAIN, {})
67 domain_data[entry.entry_id] = coordinator
70 await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
76 """Unload Minecraft Server config entry."""
79 unload_ok = await hass.config_entries.async_unload_platforms(
80 config_entry, PLATFORMS
84 hass.data[DOMAIN].pop(config_entry.entry_id)
90 """Migrate old config entry to a new format."""
93 if config_entry.version == 1:
94 _LOGGER.debug(
"Migrating from version 1")
96 old_unique_id = config_entry.unique_id
98 config_entry_id = config_entry.entry_id
101 _LOGGER.debug(
"Migrating config entry. Resetting unique ID: %s", old_unique_id)
102 hass.config_entries.async_update_entry(config_entry, unique_id=
None, version=2)
108 await er.async_migrate_entries(hass, config_entry_id, _migrate_entity_unique_id)
110 _LOGGER.debug(
"Migration to version 2 successful")
113 if config_entry.version == 2:
114 _LOGGER.debug(
"Migrating from version 2")
116 config_data = config_entry.data
119 address = config_data[CONF_HOST]
123 await api.async_initialize()
124 host_only_lookup_success =
True
125 except MinecraftServerAddressError
as error:
126 host_only_lookup_success =
False
128 "Hostname (without port) cannot be parsed, trying again with port: %s",
132 if not host_only_lookup_success:
133 address = f
"{config_data[CONF_HOST]}:{config_data[CONF_PORT]}"
137 await api.async_initialize()
138 except MinecraftServerAddressError:
140 "Can't migrate configuration entry due to error while parsing server address, try again later"
145 "Migrating config entry, replacing host '%s' and port '%s' with address '%s'",
146 config_data[CONF_HOST],
147 config_data[CONF_PORT],
151 new_data = config_data.copy()
152 new_data[CONF_ADDRESS] = address
153 del new_data[CONF_HOST]
154 del new_data[CONF_PORT]
155 hass.config_entries.async_update_entry(config_entry, data=new_data, version=3)
157 _LOGGER.debug(
"Migration to version 3 successful")
163 hass: HomeAssistant, config_entry: ConfigEntry, old_unique_id: str |
None
165 """Migrate the device identifiers to the new format."""
166 device_registry = dr.async_get(hass)
167 device_entry_found =
False
168 for device_entry
in dr.async_entries_for_config_entry(
169 device_registry, config_entry.entry_id
171 for identifier
in device_entry.identifiers:
172 if identifier[1] == old_unique_id:
177 config_entry.entry_id,
181 "Migrating device identifiers from %s to %s",
182 device_entry.identifiers,
185 device_registry.async_update_device(
186 device_id=device_entry.id, new_identifiers=new_identifiers
189 device_entry_found =
True
193 if device_entry_found:
199 """Migrate the unique ID of an entity to the new format."""
205 unique_id_pieces = entity_entry.unique_id.split(
"-")
206 entity_type = unique_id_pieces[2]
210 new_entity_type = entity_type.lower()
211 new_entity_type = new_entity_type.replace(
" ",
"_")
214 if new_entity_type ==
"world_message":
215 new_entity_type = KEY_MOTD
218 if new_entity_type ==
"latency_time":
219 new_entity_type = KEY_LATENCY
221 new_unique_id = f
"{entity_entry.config_entry_id}-{new_entity_type}"
223 "Migrating entity unique ID from %s to %s",
224 entity_entry.unique_id,
228 return {
"new_unique_id": new_unique_id}
dict[str, Any] _migrate_entity_unique_id(er.RegistryEntry entity_entry)
bool async_migrate_entry(HomeAssistant hass, ConfigEntry config_entry)
None _async_migrate_device_identifiers(HomeAssistant hass, ConfigEntry config_entry, str|None old_unique_id)
None load_dnspython_rdata_classes()
bool async_setup_entry(HomeAssistant hass, ConfigEntry entry)
bool async_unload_entry(HomeAssistant hass, ConfigEntry config_entry)