Use EntityDescription in launch_library (#63685)

Co-authored-by: Joakim Sørensen <hi@ludeeus.dev>
This commit is contained in:
Simon Hansen 2022-01-12 14:44:29 +01:00 committed by GitHub
parent 2df3c85ba6
commit 0fadce751f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -7,7 +7,11 @@ from typing import Any
from pylaunches.objects.launch import Launch from pylaunches.objects.launch import Launch
import voluptuous as vol import voluptuous as vol
from homeassistant.components.sensor import PLATFORM_SCHEMA, SensorEntity from homeassistant.components.sensor import (
PLATFORM_SCHEMA,
SensorEntity,
SensorEntityDescription,
)
from homeassistant.config_entries import SOURCE_IMPORT, ConfigEntry from homeassistant.config_entries import SOURCE_IMPORT, ConfigEntry
from homeassistant.const import CONF_NAME from homeassistant.const import CONF_NAME
from homeassistant.core import HomeAssistant, callback from homeassistant.core import HomeAssistant, callback
@ -37,6 +41,13 @@ PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
) )
SENSOR_DESCRIPTION = SensorEntityDescription(
key="next_launch",
icon="mdi:rocket-launch",
name=DEFAULT_NAME,
)
async def async_setup_platform( async def async_setup_platform(
hass: HomeAssistant, hass: HomeAssistant,
config: ConfigType, config: ConfigType,
@ -72,7 +83,12 @@ async def async_setup_entry(
async_add_entities( async_add_entities(
[ [
NextLaunchSensor(coordinator, entry.entry_id, name), NextLaunchSensor(
coordinator,
entry.entry_id,
name,
SENSOR_DESCRIPTION,
),
] ]
) )
@ -81,16 +97,25 @@ class NextLaunchSensor(CoordinatorEntity, SensorEntity):
"""Representation of the next launch sensor.""" """Representation of the next launch sensor."""
_attr_attribution = ATTRIBUTION _attr_attribution = ATTRIBUTION
_attr_icon = "mdi:rocket-launch"
_next_launch: Launch | None = None _next_launch: Launch | None = None
def __init__( def __init__(
self, coordinator: DataUpdateCoordinator, entry_id: str, name: str self,
coordinator: DataUpdateCoordinator,
entry_id: str,
name: str,
description: SensorEntityDescription,
) -> None: ) -> None:
"""Initialize a Launch Library entity.""" """Initialize a Launch Library entity."""
super().__init__(coordinator) super().__init__(coordinator)
self._attr_name = name self._attr_name = name
self._attr_unique_id = f"{entry_id}_next_launch" self._attr_unique_id = f"{entry_id}_{description.key}"
self.entity_description = description
@property
def available(self) -> bool:
"""Return if the sensor is available."""
return super().available and self._next_launch is not None
@property @property
def native_value(self) -> str | None: def native_value(self) -> str | None:
@ -99,11 +124,6 @@ class NextLaunchSensor(CoordinatorEntity, SensorEntity):
return None return None
return self._next_launch.name return self._next_launch.name
@property
def available(self) -> bool:
"""Return if the sensor is available."""
return super().available and self._next_launch is not None
@property @property
def extra_state_attributes(self) -> dict[str, Any] | None: def extra_state_attributes(self) -> dict[str, Any] | None:
"""Return the attributes of the sensor.""" """Return the attributes of the sensor."""