mirror of
https://github.com/home-assistant/core.git
synced 2025-07-25 14:17:45 +00:00
Add device info to Launch Library (#98767)
This commit is contained in:
parent
02567d9bf6
commit
2609394b9f
@ -17,6 +17,7 @@ from homeassistant.components.sensor import (
|
|||||||
from homeassistant.config_entries import ConfigEntry
|
from homeassistant.config_entries import ConfigEntry
|
||||||
from homeassistant.const import CONF_NAME, PERCENTAGE
|
from homeassistant.const import CONF_NAME, PERCENTAGE
|
||||||
from homeassistant.core import HomeAssistant, callback
|
from homeassistant.core import HomeAssistant, callback
|
||||||
|
from homeassistant.helpers.device_registry import DeviceEntryType, DeviceInfo
|
||||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||||
from homeassistant.helpers.update_coordinator import (
|
from homeassistant.helpers.update_coordinator import (
|
||||||
CoordinatorEntity,
|
CoordinatorEntity,
|
||||||
@ -49,7 +50,7 @@ SENSOR_DESCRIPTIONS: tuple[LaunchLibrarySensorEntityDescription, ...] = (
|
|||||||
LaunchLibrarySensorEntityDescription(
|
LaunchLibrarySensorEntityDescription(
|
||||||
key="next_launch",
|
key="next_launch",
|
||||||
icon="mdi:rocket-launch",
|
icon="mdi:rocket-launch",
|
||||||
name="Next launch",
|
translation_key="next_launch",
|
||||||
value_fn=lambda nl: nl.name,
|
value_fn=lambda nl: nl.name,
|
||||||
attributes_fn=lambda nl: {
|
attributes_fn=lambda nl: {
|
||||||
"provider": nl.launch_service_provider.name,
|
"provider": nl.launch_service_provider.name,
|
||||||
@ -61,7 +62,7 @@ SENSOR_DESCRIPTIONS: tuple[LaunchLibrarySensorEntityDescription, ...] = (
|
|||||||
LaunchLibrarySensorEntityDescription(
|
LaunchLibrarySensorEntityDescription(
|
||||||
key="launch_time",
|
key="launch_time",
|
||||||
icon="mdi:clock-outline",
|
icon="mdi:clock-outline",
|
||||||
name="Launch time",
|
translation_key="launch_time",
|
||||||
device_class=SensorDeviceClass.TIMESTAMP,
|
device_class=SensorDeviceClass.TIMESTAMP,
|
||||||
value_fn=lambda nl: parse_datetime(nl.net),
|
value_fn=lambda nl: parse_datetime(nl.net),
|
||||||
attributes_fn=lambda nl: {
|
attributes_fn=lambda nl: {
|
||||||
@ -73,7 +74,7 @@ SENSOR_DESCRIPTIONS: tuple[LaunchLibrarySensorEntityDescription, ...] = (
|
|||||||
LaunchLibrarySensorEntityDescription(
|
LaunchLibrarySensorEntityDescription(
|
||||||
key="launch_probability",
|
key="launch_probability",
|
||||||
icon="mdi:dice-multiple",
|
icon="mdi:dice-multiple",
|
||||||
name="Launch probability",
|
translation_key="next_launch",
|
||||||
native_unit_of_measurement=PERCENTAGE,
|
native_unit_of_measurement=PERCENTAGE,
|
||||||
value_fn=lambda nl: None if nl.probability == -1 else nl.probability,
|
value_fn=lambda nl: None if nl.probability == -1 else nl.probability,
|
||||||
attributes_fn=lambda nl: None,
|
attributes_fn=lambda nl: None,
|
||||||
@ -81,14 +82,14 @@ SENSOR_DESCRIPTIONS: tuple[LaunchLibrarySensorEntityDescription, ...] = (
|
|||||||
LaunchLibrarySensorEntityDescription(
|
LaunchLibrarySensorEntityDescription(
|
||||||
key="launch_status",
|
key="launch_status",
|
||||||
icon="mdi:rocket-launch",
|
icon="mdi:rocket-launch",
|
||||||
name="Launch status",
|
translation_key="next_launch",
|
||||||
value_fn=lambda nl: nl.status.name,
|
value_fn=lambda nl: nl.status.name,
|
||||||
attributes_fn=lambda nl: {"reason": nl.holdreason} if nl.inhold else None,
|
attributes_fn=lambda nl: {"reason": nl.holdreason} if nl.inhold else None,
|
||||||
),
|
),
|
||||||
LaunchLibrarySensorEntityDescription(
|
LaunchLibrarySensorEntityDescription(
|
||||||
key="launch_mission",
|
key="launch_mission",
|
||||||
icon="mdi:orbit",
|
icon="mdi:orbit",
|
||||||
name="Launch mission",
|
translation_key="launch_mission",
|
||||||
value_fn=lambda nl: nl.mission.name,
|
value_fn=lambda nl: nl.mission.name,
|
||||||
attributes_fn=lambda nl: {
|
attributes_fn=lambda nl: {
|
||||||
"mission_type": nl.mission.type,
|
"mission_type": nl.mission.type,
|
||||||
@ -99,7 +100,7 @@ SENSOR_DESCRIPTIONS: tuple[LaunchLibrarySensorEntityDescription, ...] = (
|
|||||||
LaunchLibrarySensorEntityDescription(
|
LaunchLibrarySensorEntityDescription(
|
||||||
key="starship_launch",
|
key="starship_launch",
|
||||||
icon="mdi:rocket",
|
icon="mdi:rocket",
|
||||||
name="Next Starship launch",
|
translation_key="starship_launch",
|
||||||
device_class=SensorDeviceClass.TIMESTAMP,
|
device_class=SensorDeviceClass.TIMESTAMP,
|
||||||
value_fn=lambda sl: parse_datetime(sl.net),
|
value_fn=lambda sl: parse_datetime(sl.net),
|
||||||
attributes_fn=lambda sl: {
|
attributes_fn=lambda sl: {
|
||||||
@ -112,7 +113,7 @@ SENSOR_DESCRIPTIONS: tuple[LaunchLibrarySensorEntityDescription, ...] = (
|
|||||||
LaunchLibrarySensorEntityDescription(
|
LaunchLibrarySensorEntityDescription(
|
||||||
key="starship_event",
|
key="starship_event",
|
||||||
icon="mdi:calendar",
|
icon="mdi:calendar",
|
||||||
name="Next Starship event",
|
translation_key="starship_event",
|
||||||
device_class=SensorDeviceClass.TIMESTAMP,
|
device_class=SensorDeviceClass.TIMESTAMP,
|
||||||
value_fn=lambda se: parse_datetime(se.date),
|
value_fn=lambda se: parse_datetime(se.date),
|
||||||
attributes_fn=lambda se: {
|
attributes_fn=lambda se: {
|
||||||
@ -139,7 +140,7 @@ async def async_setup_entry(
|
|||||||
coordinator=coordinator,
|
coordinator=coordinator,
|
||||||
entry_id=entry.entry_id,
|
entry_id=entry.entry_id,
|
||||||
description=description,
|
description=description,
|
||||||
name=name if description.key == "next_launch" else None,
|
name=name,
|
||||||
)
|
)
|
||||||
for description in SENSOR_DESCRIPTIONS
|
for description in SENSOR_DESCRIPTIONS
|
||||||
)
|
)
|
||||||
@ -151,6 +152,7 @@ class LaunchLibrarySensor(
|
|||||||
"""Representation of the next launch sensors."""
|
"""Representation of the next launch sensors."""
|
||||||
|
|
||||||
_attr_attribution = "Data provided by Launch Library."
|
_attr_attribution = "Data provided by Launch Library."
|
||||||
|
_attr_has_entity_name = True
|
||||||
_next_event: Launch | Event | None = None
|
_next_event: Launch | Event | None = None
|
||||||
entity_description: LaunchLibrarySensorEntityDescription
|
entity_description: LaunchLibrarySensorEntityDescription
|
||||||
|
|
||||||
@ -159,14 +161,17 @@ class LaunchLibrarySensor(
|
|||||||
coordinator: DataUpdateCoordinator[LaunchLibraryData],
|
coordinator: DataUpdateCoordinator[LaunchLibraryData],
|
||||||
entry_id: str,
|
entry_id: str,
|
||||||
description: LaunchLibrarySensorEntityDescription,
|
description: LaunchLibrarySensorEntityDescription,
|
||||||
name: str | None = None,
|
name: str,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Initialize a Launch Library sensor."""
|
"""Initialize a Launch Library sensor."""
|
||||||
super().__init__(coordinator)
|
super().__init__(coordinator)
|
||||||
if name:
|
|
||||||
self._attr_name = name
|
|
||||||
self._attr_unique_id = f"{entry_id}_{description.key}"
|
self._attr_unique_id = f"{entry_id}_{description.key}"
|
||||||
self.entity_description = description
|
self.entity_description = description
|
||||||
|
self._attr_device_info = DeviceInfo(
|
||||||
|
identifiers={(DOMAIN, entry_id)},
|
||||||
|
entry_type=DeviceEntryType.SERVICE,
|
||||||
|
name=name,
|
||||||
|
)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def native_value(self) -> datetime | str | int | None:
|
def native_value(self) -> datetime | str | int | None:
|
||||||
|
@ -8,5 +8,30 @@
|
|||||||
"abort": {
|
"abort": {
|
||||||
"single_instance_allowed": "[%key:common::config_flow::abort::single_instance_allowed%]"
|
"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"
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user