hass-core/homeassistant/components/fivem/sensor.py
Sander Jochems 0ea82bdbfb
Fivem integration (#65089)
* Initial fivem integration setup

* Use licenseKey for unique ID

* Create FiveMServer class

* Create FiveMStatusBinarySensor

* Fix platform loading

* Create sensor platform

* Remove config flow tests

* Update manifest.json

* Use attr_ instead or properties in sensors.py

* Use entry_id as unique_id

* Move device info to _attr instead of property

* Register callback in FiveMEntity

* Create config flow tests

* Add loggin to fivem

* Use FiveM in config_flow

* Use update_coordinator instead of dispatcher

* Bump fivem-api to 0.1.2

* Remove leftovers

* More tests for config flow

* Add component files to .coveragerc

* Fix simple comments

* Add gamename check to config flow

* Use entity descriptions for sensors

* Move extra attributes to init

* Use [] instead of get() for server info

* Fix error in gamename test
2022-02-08 11:27:11 +02:00

78 lines
2.2 KiB
Python

"""The FiveM sensor platform."""
from dataclasses import dataclass
from typing import Any
from homeassistant.components.sensor import SensorEntity, SensorEntityDescription
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 (
ATTR_PLAYERS_LIST,
ATTR_RESOURCES_LIST,
DOMAIN,
ICON_PLAYERS_MAX,
ICON_PLAYERS_ONLINE,
ICON_RESOURCES,
NAME_PLAYERS_MAX,
NAME_PLAYERS_ONLINE,
NAME_RESOURCES,
UNIT_PLAYERS_MAX,
UNIT_PLAYERS_ONLINE,
UNIT_RESOURCES,
)
@dataclass
class FiveMSensorEntityDescription(SensorEntityDescription, FiveMEntityDescription):
"""Describes FiveM sensor entity."""
SENSORS: tuple[FiveMSensorEntityDescription, ...] = (
FiveMSensorEntityDescription(
key=NAME_PLAYERS_MAX,
name=NAME_PLAYERS_MAX,
icon=ICON_PLAYERS_MAX,
native_unit_of_measurement=UNIT_PLAYERS_MAX,
),
FiveMSensorEntityDescription(
key=NAME_PLAYERS_ONLINE,
name=NAME_PLAYERS_ONLINE,
icon=ICON_PLAYERS_ONLINE,
native_unit_of_measurement=UNIT_PLAYERS_ONLINE,
extra_attrs=[ATTR_PLAYERS_LIST],
),
FiveMSensorEntityDescription(
key=NAME_RESOURCES,
name=NAME_RESOURCES,
icon=ICON_RESOURCES,
native_unit_of_measurement=UNIT_RESOURCES,
extra_attrs=[ATTR_RESOURCES_LIST],
),
)
async def async_setup_entry(
hass: HomeAssistant,
entry: ConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
"""Set up the FiveM sensor platform."""
coordinator = hass.data[DOMAIN][entry.entry_id]
# Add sensor entities.
async_add_entities(
[FiveMSensorEntity(coordinator, description) for description in SENSORS]
)
class FiveMSensorEntity(FiveMEntity, SensorEntity):
"""Representation of a FiveM sensor base entity."""
entity_description: FiveMSensorEntityDescription
@property
def native_value(self) -> Any:
"""Return the state of the sensor."""
return self.coordinator.data[self.entity_description.key]