"""A sensor platform that give you information about the next space launch."""
from __future__ import annotations

from datetime import timedelta
import logging

from pylaunches import PyLaunches, PyLaunchesException
import voluptuous as vol

from homeassistant.components.sensor import PLATFORM_SCHEMA, SensorEntity
from homeassistant.const import ATTR_ATTRIBUTION, CONF_NAME
from homeassistant.helpers.aiohttp_client import async_get_clientsession
import homeassistant.helpers.config_validation as cv

from .const import (
    ATTR_AGENCY,
    ATTR_AGENCY_COUNTRY_CODE,
    ATTR_LAUNCH_TIME,
    ATTR_STREAM,
    ATTRIBUTION,
    DEFAULT_NAME,
)

_LOGGER = logging.getLogger(__name__)

SCAN_INTERVAL = timedelta(hours=1)

PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
    {vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string}
)


async def async_setup_platform(hass, config, async_add_entities, discovery_info=None):
    """Create the launch sensor."""
    name = config[CONF_NAME]
    session = async_get_clientsession(hass)
    launches = PyLaunches(session)

    async_add_entities([LaunchLibrarySensor(launches, name)], True)


class LaunchLibrarySensor(SensorEntity):
    """Representation of a launch_library Sensor."""

    def __init__(self, launches: PyLaunches, name: str) -> None:
        """Initialize the sensor."""
        self.launches = launches
        self.next_launch = None
        self._name = name

    async def async_update(self) -> None:
        """Get the latest data."""
        try:
            launches = await self.launches.upcoming_launches()
        except PyLaunchesException as exception:
            _LOGGER.error("Error getting data, %s", exception)
        else:
            if launches:
                self.next_launch = launches[0]

    @property
    def name(self) -> str:
        """Return the name of the sensor."""
        return self._name

    @property
    def state(self) -> str | None:
        """Return the state of the sensor."""
        if self.next_launch:
            return self.next_launch.name
        return None

    @property
    def icon(self) -> str:
        """Return the icon of the sensor."""
        return "mdi:rocket"

    @property
    def extra_state_attributes(self) -> dict | None:
        """Return attributes for the sensor."""
        if self.next_launch:
            return {
                ATTR_LAUNCH_TIME: self.next_launch.net,
                ATTR_AGENCY: self.next_launch.launch_service_provider.name,
                ATTR_AGENCY_COUNTRY_CODE: self.next_launch.pad.location.country_code,
                ATTR_STREAM: self.next_launch.webcast_live,
                ATTR_ATTRIBUTION: ATTRIBUTION,
            }
        return None

@@ -1223,16 +1223,47 @@
 or.%22%22%22%0A%0A
+    _attr_icon = %22mdi:rocket%22%0A%0A
     def 
@@ -1277,24 +1277,19 @@
 _(self, 
-launches
+api
 : PyLaun
@@ -1368,73 +1368,36 @@
 elf.
-launches = launches%0A        self.next_launch = None%0A        self.
+api = api%0A        self._attr
 _nam
@@ -1530,24 +1530,19 @@
 it self.
-launches
+api
 .upcomin
@@ -1666,16 +1666,57 @@
 eption)%0A
+            self._attr_available = False%0A
         
@@ -1744,17 +1744,22 @@
 launches
-:
+ and (
 %0A       
@@ -1767,21 +1767,16 @@
         
-self.
 next_lau
@@ -1783,602 +1783,254 @@
 nch 
+:
 = 
-launches%5B0%5D%0A%0A    @property%0A    def name(self) -%3E str:%0A        %22%22%22Return the name of the sensor.%22%22%22%0A        return self._name%0A%0A    @property%0A    def state(self) -%3E str %7C None:%0A        %22%22%22Return the state of the sensor.%22%22%22%0A        if self.next_launch:%0A            return self.next_launch.name%0A        return None%0A%0A    @property%0A    def icon(self) -%3E str:%0A        %22%22%22Return the icon of the sensor.%22%22%22%0A        return %22mdi:rocket%22%0A%0A    @property%0A    def extra_state_attributes(self) -%3E dict %7C None:%0A        %22%22%22Return attributes for the sensor.%22%22%22%0A        if self.next_launch:%0A            return %7B%0A
+next((launch for launch in launches), None)%0A            ):%0A                self._attr_available = True%0A                self._attr_state = next_launch.name%0A                self._attr_extra_state_attributes.update(%0A                    %7B%0A        
     
@@ -2059,21 +2059,16 @@
 H_TIME: 
-self.
 next_lau
@@ -2076,16 +2076,24 @@
 ch.net,%0A
+        
         
@@ -2113,21 +2113,16 @@
 AGENCY: 
-self.
 next_lau
@@ -2163,32 +2163,40 @@
                 
+    
+    
 ATTR_AGENCY_COUN
@@ -2205,21 +2205,16 @@
 Y_CODE: 
-self.
 next_lau
@@ -2256,24 +2256,32 @@
             
+        
 ATTR_STREAM:
@@ -2281,21 +2281,16 @@
 STREAM: 
-self.
 next_lau
@@ -2307,16 +2307,24 @@
 t_live,%0A
+        
         
@@ -2358,24 +2358,32 @@
 TTRIBUTION,%0A
+        
             
@@ -2396,16 +2396,14 @@
     
-return None
+        )
 %0A

Use `_attr_*` for Launch Library (#54388)