Add device info to Launch Library (#98767)

This commit is contained in:
Joost Lekkerkerker 2023-10-13 15:28:58 +02:00 committed by GitHub
parent 02567d9bf6
commit 2609394b9f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 41 additions and 11 deletions

View File

@ -17,6 +17,7 @@ from homeassistant.components.sensor import (
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import CONF_NAME, PERCENTAGE
from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers.device_registry import DeviceEntryType, DeviceInfo
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.update_coordinator import (
CoordinatorEntity,
@ -49,7 +50,7 @@ SENSOR_DESCRIPTIONS: tuple[LaunchLibrarySensorEntityDescription, ...] = (
LaunchLibrarySensorEntityDescription(
key="next_launch",
icon="mdi:rocket-launch",
name="Next launch",
translation_key="next_launch",
value_fn=lambda nl: nl.name,
attributes_fn=lambda nl: {
"provider": nl.launch_service_provider.name,
@ -61,7 +62,7 @@ SENSOR_DESCRIPTIONS: tuple[LaunchLibrarySensorEntityDescription, ...] = (
LaunchLibrarySensorEntityDescription(
key="launch_time",
icon="mdi:clock-outline",
name="Launch time",
translation_key="launch_time",
device_class=SensorDeviceClass.TIMESTAMP,
value_fn=lambda nl: parse_datetime(nl.net),
attributes_fn=lambda nl: {
@ -73,7 +74,7 @@ SENSOR_DESCRIPTIONS: tuple[LaunchLibrarySensorEntityDescription, ...] = (
LaunchLibrarySensorEntityDescription(
key="launch_probability",
icon="mdi:dice-multiple",
name="Launch probability",
translation_key="next_launch",
native_unit_of_measurement=PERCENTAGE,
value_fn=lambda nl: None if nl.probability == -1 else nl.probability,
attributes_fn=lambda nl: None,
@ -81,14 +82,14 @@ SENSOR_DESCRIPTIONS: tuple[LaunchLibrarySensorEntityDescription, ...] = (
LaunchLibrarySensorEntityDescription(
key="launch_status",
icon="mdi:rocket-launch",
name="Launch status",
translation_key="next_launch",
value_fn=lambda nl: nl.status.name,
attributes_fn=lambda nl: {"reason": nl.holdreason} if nl.inhold else None,
),
LaunchLibrarySensorEntityDescription(
key="launch_mission",
icon="mdi:orbit",
name="Launch mission",
translation_key="launch_mission",
value_fn=lambda nl: nl.mission.name,
attributes_fn=lambda nl: {
"mission_type": nl.mission.type,
@ -99,7 +100,7 @@ SENSOR_DESCRIPTIONS: tuple[LaunchLibrarySensorEntityDescription, ...] = (
LaunchLibrarySensorEntityDescription(
key="starship_launch",
icon="mdi:rocket",
name="Next Starship launch",
translation_key="starship_launch",
device_class=SensorDeviceClass.TIMESTAMP,
value_fn=lambda sl: parse_datetime(sl.net),
attributes_fn=lambda sl: {
@ -112,7 +113,7 @@ SENSOR_DESCRIPTIONS: tuple[LaunchLibrarySensorEntityDescription, ...] = (
LaunchLibrarySensorEntityDescription(
key="starship_event",
icon="mdi:calendar",
name="Next Starship event",
translation_key="starship_event",
device_class=SensorDeviceClass.TIMESTAMP,
value_fn=lambda se: parse_datetime(se.date),
attributes_fn=lambda se: {
@ -139,7 +140,7 @@ async def async_setup_entry(
coordinator=coordinator,
entry_id=entry.entry_id,
description=description,
name=name if description.key == "next_launch" else None,
name=name,
)
for description in SENSOR_DESCRIPTIONS
)
@ -151,6 +152,7 @@ class LaunchLibrarySensor(
"""Representation of the next launch sensors."""
_attr_attribution = "Data provided by Launch Library."
_attr_has_entity_name = True
_next_event: Launch | Event | None = None
entity_description: LaunchLibrarySensorEntityDescription
@ -159,14 +161,17 @@ class LaunchLibrarySensor(
coordinator: DataUpdateCoordinator[LaunchLibraryData],
entry_id: str,
description: LaunchLibrarySensorEntityDescription,
name: str | None = None,
name: str,
) -> None:
"""Initialize a Launch Library sensor."""
super().__init__(coordinator)
if name:
self._attr_name = name
self._attr_unique_id = f"{entry_id}_{description.key}"
self.entity_description = description
self._attr_device_info = DeviceInfo(
identifiers={(DOMAIN, entry_id)},
entry_type=DeviceEntryType.SERVICE,
name=name,
)
@property
def native_value(self) -> datetime | str | int | None:

View File

@ -8,5 +8,30 @@
"abort": {
"single_instance_allowed": "[%key:common::config_flow::abort::single_instance_allowed%]"
}
},
"entity": {
"sensor": {
"next_launch": {
"name": "Next launch"
},
"launch_time": {
"name": "Launch time"
},
"launch_probability": {
"name": "Launch probability"
},
"launch_status": {
"name": "Launch status"
},
"launch_mission": {
"name": "Launch mission"
},
"starship_launch": {
"name": "Next Starship launch"
},
"starship_event": {
"name": "Next Starship event"
}
}
}
}