From 0fadce751f0e3cfa08e25212f0f70a9ec164b201 Mon Sep 17 00:00:00 2001 From: Simon Hansen <67142049+DurgNomis-drol@users.noreply.github.com> Date: Wed, 12 Jan 2022 14:44:29 +0100 Subject: [PATCH] Use EntityDescription in launch_library (#63685) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Joakim Sørensen --- .../components/launch_library/sensor.py | 40 ++++++++++++++----- 1 file changed, 30 insertions(+), 10 deletions(-) diff --git a/homeassistant/components/launch_library/sensor.py b/homeassistant/components/launch_library/sensor.py index b93c33daa1c..455fa9fe71d 100644 --- a/homeassistant/components/launch_library/sensor.py +++ b/homeassistant/components/launch_library/sensor.py @@ -7,7 +7,11 @@ from typing import Any from pylaunches.objects.launch import Launch 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.const import CONF_NAME 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( hass: HomeAssistant, config: ConfigType, @@ -72,7 +83,12 @@ async def async_setup_entry( 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.""" _attr_attribution = ATTRIBUTION - _attr_icon = "mdi:rocket-launch" _next_launch: Launch | None = None def __init__( - self, coordinator: DataUpdateCoordinator, entry_id: str, name: str + self, + coordinator: DataUpdateCoordinator, + entry_id: str, + name: str, + description: SensorEntityDescription, ) -> None: """Initialize a Launch Library entity.""" super().__init__(coordinator) 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 def native_value(self) -> str | None: @@ -99,11 +124,6 @@ class NextLaunchSensor(CoordinatorEntity, SensorEntity): return None 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 def extra_state_attributes(self) -> dict[str, Any] | None: """Return the attributes of the sensor."""