Move Minecraft Server base entity to its own file (#97187)

pull/96399/head^2
elmurato 2023-07-25 10:14:01 +02:00 committed by GitHub
parent 06f97679ee
commit 90bf2d3076
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 67 additions and 58 deletions

View File

@ -706,6 +706,9 @@ omit =
homeassistant/components/mill/climate.py
homeassistant/components/mill/sensor.py
homeassistant/components/minecraft_server/__init__.py
homeassistant/components/minecraft_server/binary_sensor.py
homeassistant/components/minecraft_server/entity.py
homeassistant/components/minecraft_server/sensor.py
homeassistant/components/minio/minio_helper.py
homeassistant/components/mjpeg/camera.py
homeassistant/components/mjpeg/util.py

View File

@ -10,16 +10,12 @@ from mcstatus.server import JavaServer
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import CONF_HOST, CONF_NAME, CONF_PORT, Platform
from homeassistant.core import CALLBACK_TYPE, HomeAssistant, callback
from homeassistant.helpers.dispatcher import (
async_dispatcher_connect,
async_dispatcher_send,
)
from homeassistant.helpers.entity import DeviceInfo, Entity
from homeassistant.core import CALLBACK_TYPE, HomeAssistant
from homeassistant.helpers.dispatcher import async_dispatcher_send
from homeassistant.helpers.event import async_track_time_interval
from . import helpers
from .const import DOMAIN, MANUFACTURER, SCAN_INTERVAL, SIGNAL_NAME_PREFIX
from .const import DOMAIN, SCAN_INTERVAL, SIGNAL_NAME_PREFIX
PLATFORMS = [Platform.BINARY_SENSOR, Platform.SENSOR]
@ -214,52 +210,3 @@ class MinecraftServer:
error,
)
self._last_status_request_failed = True
class MinecraftServerEntity(Entity):
"""Representation of a Minecraft Server base entity."""
_attr_has_entity_name = True
_attr_should_poll = False
def __init__(
self,
server: MinecraftServer,
type_name: str,
icon: str,
device_class: str | None,
) -> None:
"""Initialize base entity."""
self._server = server
self._attr_icon = icon
self._attr_unique_id = f"{self._server.unique_id}-{type_name}"
self._attr_device_info = DeviceInfo(
identifiers={(DOMAIN, self._server.unique_id)},
manufacturer=MANUFACTURER,
model=f"Minecraft Server ({self._server.version})",
name=self._server.name,
sw_version=str(self._server.protocol_version),
)
self._attr_device_class = device_class
self._extra_state_attributes = None
self._disconnect_dispatcher: CALLBACK_TYPE | None = None
async def async_update(self) -> None:
"""Fetch data from the server."""
raise NotImplementedError()
async def async_added_to_hass(self) -> None:
"""Connect dispatcher to signal from server."""
self._disconnect_dispatcher = async_dispatcher_connect(
self.hass, self._server.signal_name, self._update_callback
)
async def async_will_remove_from_hass(self) -> None:
"""Disconnect dispatcher before removal."""
if self._disconnect_dispatcher:
self._disconnect_dispatcher()
@callback
def _update_callback(self) -> None:
"""Triggers update of properties after receiving signal from server."""
self.async_schedule_update_ha_state(force_refresh=True)

View File

@ -7,8 +7,9 @@ from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from . import MinecraftServer, MinecraftServerEntity
from . import MinecraftServer
from .const import DOMAIN, ICON_STATUS, NAME_STATUS
from .entity import MinecraftServerEntity
async def async_setup_entry(

View File

@ -0,0 +1,57 @@
"""Base entity for the Minecraft Server integration."""
from homeassistant.core import CALLBACK_TYPE, callback
from homeassistant.helpers.dispatcher import async_dispatcher_connect
from homeassistant.helpers.entity import DeviceInfo, Entity
from . import MinecraftServer
from .const import DOMAIN, MANUFACTURER
class MinecraftServerEntity(Entity):
"""Representation of a Minecraft Server base entity."""
_attr_has_entity_name = True
_attr_should_poll = False
def __init__(
self,
server: MinecraftServer,
type_name: str,
icon: str,
device_class: str | None,
) -> None:
"""Initialize base entity."""
self._server = server
self._attr_icon = icon
self._attr_unique_id = f"{self._server.unique_id}-{type_name}"
self._attr_device_info = DeviceInfo(
identifiers={(DOMAIN, self._server.unique_id)},
manufacturer=MANUFACTURER,
model=f"Minecraft Server ({self._server.version})",
name=self._server.name,
sw_version=str(self._server.protocol_version),
)
self._attr_device_class = device_class
self._extra_state_attributes = None
self._disconnect_dispatcher: CALLBACK_TYPE | None = None
async def async_update(self) -> None:
"""Fetch data from the server."""
raise NotImplementedError()
async def async_added_to_hass(self) -> None:
"""Connect dispatcher to signal from server."""
self._disconnect_dispatcher = async_dispatcher_connect(
self.hass, self._server.signal_name, self._update_callback
)
async def async_will_remove_from_hass(self) -> None:
"""Disconnect dispatcher before removal."""
if self._disconnect_dispatcher:
self._disconnect_dispatcher()
@callback
def _update_callback(self) -> None:
"""Triggers update of properties after receiving signal from server."""
self.async_schedule_update_ha_state(force_refresh=True)

View File

@ -7,7 +7,7 @@ from homeassistant.const import UnitOfTime
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from . import MinecraftServer, MinecraftServerEntity
from . import MinecraftServer
from .const import (
ATTR_PLAYERS_LIST,
DOMAIN,
@ -26,6 +26,7 @@ from .const import (
UNIT_PLAYERS_MAX,
UNIT_PLAYERS_ONLINE,
)
from .entity import MinecraftServerEntity
async def async_setup_entry(