Clean up Launch Library consts (#64420)

pull/64424/head
Joakim Sørensen 2022-01-19 09:52:22 +01:00 committed by GitHub
parent f1435b1385
commit b82a57d861
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 29 additions and 68 deletions

View File

@ -1,25 +1,3 @@
"""Constants for launch_library.""" """Constants for launch_library."""
DOMAIN = "launch_library" DOMAIN = "launch_library"
ATTR_DESCRIPTION = "description"
ATTR_LAUNCH_FACILITY = "facility"
ATTR_LAUNCH_PAD = "pad"
ATTR_LAUNCH_PAD_COUNTRY_CODE = "provider_country_code"
ATTR_LAUNCH_PROVIDER = "provider"
ATTR_ORBIT = "target_orbit"
ATTR_REASON = "reason"
ATTR_STREAM_LIVE = "stream_live"
ATTR_TYPE = "mission_type"
ATTR_WINDOW_END = "window_end"
ATTR_WINDOW_START = "window_start"
ATTRIBUTION = "Data provided by Launch Library."
DEFAULT_NAME = "Next launch"
NEXT_LAUNCH = "next_launch"
LAUNCH_TIME = "launch_time"
LAUNCH_PROBABILITY = "launch_probability"
LAUNCH_STATUS = "launch_status"
LAUNCH_MISSION = "launch_mission"

View File

@ -17,7 +17,7 @@ from homeassistant.components.sensor import (
SensorEntityDescription, SensorEntityDescription,
) )
from homeassistant.config_entries import SOURCE_IMPORT, ConfigEntry from homeassistant.config_entries import SOURCE_IMPORT, ConfigEntry
from homeassistant.const import CONF_NAME, PERCENTAGE, STATE_UNKNOWN from homeassistant.const import CONF_NAME, PERCENTAGE
from homeassistant.core import HomeAssistant, callback from homeassistant.core import HomeAssistant, callback
import homeassistant.helpers.config_validation as cv import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.entity_platform import AddEntitiesCallback
@ -28,33 +28,15 @@ from homeassistant.helpers.update_coordinator import (
) )
from homeassistant.util.dt import parse_datetime from homeassistant.util.dt import parse_datetime
from .const import ( from .const import DOMAIN
ATTR_DESCRIPTION,
ATTR_LAUNCH_FACILITY, DEFAULT_NEXT_LAUNCH_NAME = "Next launch"
ATTR_LAUNCH_PAD,
ATTR_LAUNCH_PAD_COUNTRY_CODE,
ATTR_LAUNCH_PROVIDER,
ATTR_ORBIT,
ATTR_REASON,
ATTR_STREAM_LIVE,
ATTR_TYPE,
ATTR_WINDOW_END,
ATTR_WINDOW_START,
ATTRIBUTION,
DEFAULT_NAME,
DOMAIN,
LAUNCH_MISSION,
LAUNCH_PROBABILITY,
LAUNCH_STATUS,
LAUNCH_TIME,
NEXT_LAUNCH,
)
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend( PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
{vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string} {vol.Optional(CONF_NAME, default=DEFAULT_NEXT_LAUNCH_NAME): cv.string}
) )
@ -75,59 +57,59 @@ class NextLaunchSensorEntityDescription(
SENSOR_DESCRIPTIONS: tuple[NextLaunchSensorEntityDescription, ...] = ( SENSOR_DESCRIPTIONS: tuple[NextLaunchSensorEntityDescription, ...] = (
NextLaunchSensorEntityDescription( NextLaunchSensorEntityDescription(
key=NEXT_LAUNCH, key="next_launch",
icon="mdi:rocket-launch", icon="mdi:rocket-launch",
name=DEFAULT_NAME, name="Next launch",
value_fn=lambda next_launch: next_launch.name, value_fn=lambda next_launch: next_launch.name,
attributes_fn=lambda next_launch: { attributes_fn=lambda next_launch: {
ATTR_LAUNCH_PROVIDER: next_launch.launch_service_provider.name, "provider": next_launch.launch_service_provider.name,
ATTR_LAUNCH_PAD: next_launch.pad.name, "pad": next_launch.pad.name,
ATTR_LAUNCH_FACILITY: next_launch.pad.location.name, "facility": next_launch.pad.location.name,
ATTR_LAUNCH_PAD_COUNTRY_CODE: next_launch.pad.location.country_code, "provider_country_code": next_launch.pad.location.country_code,
}, },
), ),
NextLaunchSensorEntityDescription( NextLaunchSensorEntityDescription(
key=LAUNCH_TIME, key="launch_time",
icon="mdi:clock-outline", icon="mdi:clock-outline",
name="Launch time", name="Launch time",
device_class=SensorDeviceClass.TIMESTAMP, device_class=SensorDeviceClass.TIMESTAMP,
value_fn=lambda next_launch: parse_datetime(next_launch.net), value_fn=lambda next_launch: parse_datetime(next_launch.net),
attributes_fn=lambda next_launch: { attributes_fn=lambda next_launch: {
ATTR_WINDOW_START: next_launch.window_start, "window_start": next_launch.window_start,
ATTR_WINDOW_END: next_launch.window_end, "window_end": next_launch.window_end,
ATTR_STREAM_LIVE: next_launch.webcast_live, "stream_live": next_launch.webcast_live,
}, },
), ),
NextLaunchSensorEntityDescription( NextLaunchSensorEntityDescription(
key=LAUNCH_PROBABILITY, key="launch_probability",
icon="mdi:dice-multiple", icon="mdi:dice-multiple",
name="Launch Probability", name="Launch Probability",
native_unit_of_measurement=PERCENTAGE, native_unit_of_measurement=PERCENTAGE,
value_fn=lambda next_launch: next_launch.probability value_fn=lambda next_launch: next_launch.probability
if next_launch.probability != -1 if next_launch.probability != -1
else STATE_UNKNOWN, else None,
attributes_fn=lambda next_launch: None, attributes_fn=lambda next_launch: None,
), ),
NextLaunchSensorEntityDescription( NextLaunchSensorEntityDescription(
key=LAUNCH_STATUS, key="launch_status",
icon="mdi:rocket-launch", icon="mdi:rocket-launch",
name="Launch status", name="Launch status",
value_fn=lambda next_launch: next_launch.status.name, value_fn=lambda next_launch: next_launch.status.name,
attributes_fn=lambda next_launch: { attributes_fn=lambda next_launch: {
ATTR_REASON: next_launch.holdreason, "reason": next_launch.holdreason,
} }
if next_launch.inhold if next_launch.inhold
else None, else None,
), ),
NextLaunchSensorEntityDescription( NextLaunchSensorEntityDescription(
key=LAUNCH_MISSION, key="launch_mission",
icon="mdi:orbit", icon="mdi:orbit",
name="Launch mission", name="Launch mission",
value_fn=lambda next_launch: next_launch.mission.name, value_fn=lambda next_launch: next_launch.mission.name,
attributes_fn=lambda next_launch: { attributes_fn=lambda next_launch: {
ATTR_TYPE: next_launch.mission.type, "mission_type": next_launch.mission.type,
ATTR_ORBIT: next_launch.mission.orbit.name, "target_orbit": next_launch.mission.orbit.name,
ATTR_DESCRIPTION: next_launch.mission.description, "description": next_launch.mission.description,
}, },
), ),
) )
@ -161,7 +143,7 @@ async def async_setup_entry(
async_add_entities: AddEntitiesCallback, async_add_entities: AddEntitiesCallback,
) -> None: ) -> None:
"""Set up the sensor platform.""" """Set up the sensor platform."""
name = entry.data.get(CONF_NAME, DEFAULT_NAME) name = entry.data.get(CONF_NAME, DEFAULT_NEXT_LAUNCH_NAME)
coordinator = hass.data[DOMAIN] coordinator = hass.data[DOMAIN]
async_add_entities( async_add_entities(
@ -169,7 +151,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 if description.key == "next_launch" else None,
) )
for description in SENSOR_DESCRIPTIONS for description in SENSOR_DESCRIPTIONS
) )
@ -178,7 +160,7 @@ async def async_setup_entry(
class NextLaunchSensor(CoordinatorEntity, SensorEntity): class NextLaunchSensor(CoordinatorEntity, SensorEntity):
"""Representation of the next launch sensors.""" """Representation of the next launch sensors."""
_attr_attribution = ATTRIBUTION _attr_attribution = "Data provided by Launch Library."
_next_launch: Launch | None = None _next_launch: Launch | None = None
entity_description: NextLaunchSensorEntityDescription entity_description: NextLaunchSensorEntityDescription

View File

@ -2,7 +2,8 @@
from unittest.mock import patch from unittest.mock import patch
from homeassistant import data_entry_flow from homeassistant import data_entry_flow
from homeassistant.components.launch_library.const import DEFAULT_NAME, DOMAIN from homeassistant.components.launch_library.const import DOMAIN
from homeassistant.components.launch_library.sensor import DEFAULT_NEXT_LAUNCH_NAME
from homeassistant.config_entries import SOURCE_IMPORT, SOURCE_USER from homeassistant.config_entries import SOURCE_IMPORT, SOURCE_USER
from homeassistant.const import CONF_NAME from homeassistant.const import CONF_NAME
@ -12,7 +13,7 @@ from tests.common import MockConfigEntry
async def test_import(hass): async def test_import(hass):
"""Test entry will be imported.""" """Test entry will be imported."""
imported_config = {CONF_NAME: DEFAULT_NAME} imported_config = {CONF_NAME: DEFAULT_NEXT_LAUNCH_NAME}
with patch( with patch(
"homeassistant.components.launch_library.async_setup_entry", return_value=True "homeassistant.components.launch_library.async_setup_entry", return_value=True