Move FiveM entity class to separate file (#95348)
This commit is contained in:
parent
4d2fa5bdbc
commit
d8e73b6a6b
5 changed files with 66 additions and 53 deletions
|
@ -358,6 +358,7 @@ omit =
|
|||
homeassistant/components/fivem/__init__.py
|
||||
homeassistant/components/fivem/binary_sensor.py
|
||||
homeassistant/components/fivem/coordinator.py
|
||||
homeassistant/components/fivem/entity.py
|
||||
homeassistant/components/fivem/sensor.py
|
||||
homeassistant/components/fixer/sensor.py
|
||||
homeassistant/components/fjaraskupan/__init__.py
|
||||
|
|
|
@ -1,10 +1,7 @@
|
|||
"""The FiveM integration."""
|
||||
from __future__ import annotations
|
||||
|
||||
from collections.abc import Mapping
|
||||
from dataclasses import dataclass
|
||||
import logging
|
||||
from typing import Any
|
||||
|
||||
from fivem import FiveMServerOfflineError
|
||||
|
||||
|
@ -12,14 +9,9 @@ from homeassistant.config_entries import ConfigEntry
|
|||
from homeassistant.const import CONF_HOST, CONF_PORT, Platform
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.exceptions import ConfigEntryNotReady
|
||||
from homeassistant.helpers.entity import DeviceInfo, EntityDescription
|
||||
from homeassistant.helpers.update_coordinator import (
|
||||
CoordinatorEntity,
|
||||
)
|
||||
|
||||
from .const import (
|
||||
DOMAIN,
|
||||
MANUFACTURER,
|
||||
)
|
||||
from .coordinator import FiveMDataUpdateCoordinator
|
||||
|
||||
|
@ -58,46 +50,3 @@ async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
|||
hass.data[DOMAIN].pop(entry.entry_id)
|
||||
|
||||
return unload_ok
|
||||
|
||||
|
||||
@dataclass
|
||||
class FiveMEntityDescription(EntityDescription):
|
||||
"""Describes FiveM entity."""
|
||||
|
||||
extra_attrs: list[str] | None = None
|
||||
|
||||
|
||||
class FiveMEntity(CoordinatorEntity[FiveMDataUpdateCoordinator]):
|
||||
"""Representation of a FiveM base entity."""
|
||||
|
||||
entity_description: FiveMEntityDescription
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
coordinator: FiveMDataUpdateCoordinator,
|
||||
description: FiveMEntityDescription,
|
||||
) -> None:
|
||||
"""Initialize base entity."""
|
||||
super().__init__(coordinator)
|
||||
self.entity_description = description
|
||||
|
||||
self._attr_name = f"{self.coordinator.host} {description.name}"
|
||||
self._attr_unique_id = f"{self.coordinator.unique_id}-{description.key}".lower()
|
||||
self._attr_device_info = DeviceInfo(
|
||||
identifiers={(DOMAIN, self.coordinator.unique_id)},
|
||||
manufacturer=MANUFACTURER,
|
||||
model=self.coordinator.server,
|
||||
name=self.coordinator.host,
|
||||
sw_version=self.coordinator.version,
|
||||
)
|
||||
|
||||
@property
|
||||
def extra_state_attributes(self) -> Mapping[str, Any] | None:
|
||||
"""Return the extra attributes of the sensor."""
|
||||
if self.entity_description.extra_attrs is None:
|
||||
return None
|
||||
|
||||
return {
|
||||
attr: self.coordinator.data[attr]
|
||||
for attr in self.entity_description.extra_attrs
|
||||
}
|
||||
|
|
|
@ -10,8 +10,8 @@ from homeassistant.config_entries import ConfigEntry
|
|||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||
|
||||
from . import FiveMEntity, FiveMEntityDescription
|
||||
from .const import DOMAIN, NAME_STATUS
|
||||
from .entity import FiveMEntity, FiveMEntityDescription
|
||||
|
||||
|
||||
@dataclass
|
||||
|
|
63
homeassistant/components/fivem/entity.py
Normal file
63
homeassistant/components/fivem/entity.py
Normal file
|
@ -0,0 +1,63 @@
|
|||
"""The FiveM entity."""
|
||||
from __future__ import annotations
|
||||
|
||||
from collections.abc import Mapping
|
||||
from dataclasses import dataclass
|
||||
import logging
|
||||
from typing import Any
|
||||
|
||||
from homeassistant.helpers.entity import DeviceInfo, EntityDescription
|
||||
from homeassistant.helpers.update_coordinator import (
|
||||
CoordinatorEntity,
|
||||
)
|
||||
|
||||
from .const import (
|
||||
DOMAIN,
|
||||
MANUFACTURER,
|
||||
)
|
||||
from .coordinator import FiveMDataUpdateCoordinator
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
|
||||
@dataclass
|
||||
class FiveMEntityDescription(EntityDescription):
|
||||
"""Describes FiveM entity."""
|
||||
|
||||
extra_attrs: list[str] | None = None
|
||||
|
||||
|
||||
class FiveMEntity(CoordinatorEntity[FiveMDataUpdateCoordinator]):
|
||||
"""Representation of a FiveM base entity."""
|
||||
|
||||
entity_description: FiveMEntityDescription
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
coordinator: FiveMDataUpdateCoordinator,
|
||||
description: FiveMEntityDescription,
|
||||
) -> None:
|
||||
"""Initialize base entity."""
|
||||
super().__init__(coordinator)
|
||||
self.entity_description = description
|
||||
|
||||
self._attr_name = f"{self.coordinator.host} {description.name}"
|
||||
self._attr_unique_id = f"{self.coordinator.unique_id}-{description.key}".lower()
|
||||
self._attr_device_info = DeviceInfo(
|
||||
identifiers={(DOMAIN, self.coordinator.unique_id)},
|
||||
manufacturer=MANUFACTURER,
|
||||
model=self.coordinator.server,
|
||||
name=self.coordinator.host,
|
||||
sw_version=self.coordinator.version,
|
||||
)
|
||||
|
||||
@property
|
||||
def extra_state_attributes(self) -> Mapping[str, Any] | None:
|
||||
"""Return the extra attributes of the sensor."""
|
||||
if self.entity_description.extra_attrs is None:
|
||||
return None
|
||||
|
||||
return {
|
||||
attr: self.coordinator.data[attr]
|
||||
for attr in self.entity_description.extra_attrs
|
||||
}
|
|
@ -7,7 +7,6 @@ from homeassistant.core import HomeAssistant
|
|||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||
from homeassistant.helpers.typing import StateType
|
||||
|
||||
from . import FiveMEntity, FiveMEntityDescription
|
||||
from .const import (
|
||||
ATTR_PLAYERS_LIST,
|
||||
ATTR_RESOURCES_LIST,
|
||||
|
@ -22,6 +21,7 @@ from .const import (
|
|||
UNIT_PLAYERS_ONLINE,
|
||||
UNIT_RESOURCES,
|
||||
)
|
||||
from .entity import FiveMEntity, FiveMEntityDescription
|
||||
|
||||
|
||||
@dataclass
|
||||
|
|
Loading…
Add table
Reference in a new issue