def __init__(self, opp: OpenPeerPower, unique_id: str, config_data: ConfigType) -> None: """Initialize server instance.""" self._opp = opp # Server data self.unique_id = unique_id self.name = config_data[CONF_NAME] self.host = config_data[CONF_HOST] self.port = config_data[CONF_PORT] self.online = False self._last_status_request_failed = False self.srv_record_checked = False # 3rd party library instance self._mc_status = MCStatus(self.host, self.port) # Data provided by 3rd party library self.version = None self.protocol_version = None self.latency_time = None self.players_online = None self.players_max = None self.players_list = None # Dispatcher signal name self.signal_name = f"{SIGNAL_NAME_PREFIX}_{self.unique_id}" # Callback for stopping periodic update. self._stop_periodic_update = None
async def async_check_connection(self) -> None: """Check server connection using a 'status' request and store connection status.""" # Check if host is a valid SRV record, if not already done. if not self.srv_record_checked: self.srv_record_checked = True srv_record = await helpers.async_check_srv_record( self._opp, self.host) if srv_record is not None: _LOGGER.debug( "'%s' is a valid Minecraft SRV record ('%s:%s')", self.host, srv_record[CONF_HOST], srv_record[CONF_PORT], ) # Overwrite host, port and 3rd party library instance # with data extracted out of SRV record. self.host = srv_record[CONF_HOST] self.port = srv_record[CONF_PORT] self._mc_status = MCStatus(self.host, self.port) # Ping the server with a status request. try: await self._opp.async_add_executor_job(self._mc_status.status, self._MAX_RETRIES_STATUS) self.online = True except OSError as error: _LOGGER.debug( "Error occurred while trying to check the connection to '%s:%s' - OSError: %s", self.host, self.port, error, ) self.online = False
def __init__(self, hass: HomeAssistantType, unique_id: str, config_data: ConfigType) -> None: """Initialize server instance.""" self._hass = hass # Server data self.unique_id = unique_id self.name = config_data[CONF_NAME] self.host = config_data[CONF_HOST] self.port = config_data[CONF_PORT] self.servertype = config_data[CONF_SERVER_TYPE] self.online = False self._last_status_request_failed = False self.srv_record_checked = False # 3rd party library instance if self.servertype == CONF_SERVER_TYPE_BEDROCK: self._mc_status = MCStatusBedrock(self.host, self.port) else: self._mc_status = MCStatus(self.host, self.port) # Data provided by 3rd party library self.version = None self.protocol_version = None self.latency_time = None self.players_online = None self.players_max = None self.players_list = None self.motd = None self.map = None # Dispatcher signal name self.signal_name = f"{SIGNAL_NAME_PREFIX}_{self.unique_id}" # Callback for stopping periodic update. self._stop_periodic_update = None
async def _update_srv_record(self) -> None: srv_record = await helpers.async_check_srv_record(self.host) if srv_record is not None: _LOGGER.debug( "'%s' is a valid Minecraft SRV record ('%s:%s')", self.host, srv_record[CONF_HOST], srv_record[CONF_PORT], ) # SRV record has the port details, no need to pass them on to HASS directly self.port = None # Overwrite host, port and 3rd party library instance # with data extracted out of SRV record. self._mc_status = MCStatus(srv_record[CONF_HOST], srv_record[CONF_PORT])