"""Sensor for checking the status of London Underground tube lines."""
from __future__ import annotations

from datetime import timedelta

from london_tube_status import TubeData
import voluptuous as vol

from homeassistant.components.sensor import PLATFORM_SCHEMA, SensorEntity
from homeassistant.const import ATTR_ATTRIBUTION
from homeassistant.core import HomeAssistant
from homeassistant.helpers.aiohttp_client import async_get_clientsession
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType

ATTRIBUTION = "Powered by TfL Open Data"

CONF_LINE = "line"

ICON = "mdi:subway"

SCAN_INTERVAL = timedelta(seconds=30)

TUBE_LINES = [
    "Bakerloo",
    "Central",
    "Circle",
    "District",
    "DLR",
    "Hammersmith & City",
    "Jubilee",
    "London Overground",
    "Metropolitan",
    "Northern",
    "Piccadilly",
    "TfL Rail",
    "Victoria",
    "Waterloo & City",
]

PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
    {vol.Required(CONF_LINE): vol.All(cv.ensure_list, [vol.In(list(TUBE_LINES))])}
)


async def async_setup_platform(
    hass: HomeAssistant,
    config: ConfigType,
    async_add_entities: AddEntitiesCallback,
    discovery_info: DiscoveryInfoType | None = None,
) -> None:
    """Set up the Tube sensor."""

    session = async_get_clientsession(hass)

    data = TubeData(session)
    await data.update()

    sensors = []
    for line in config[CONF_LINE]:
        sensors.append(LondonTubeSensor(line, data))

    async_add_entities(sensors, True)


class LondonTubeSensor(SensorEntity):
    """Sensor that reads the status of a line from Tube Data."""

    def __init__(self, name, data):
        """Initialize the London Underground sensor."""
        self._data = data
        self._description = None
        self._name = name
        self._state = None
        self.attrs = {ATTR_ATTRIBUTION: ATTRIBUTION}

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

    @property
    def native_value(self):
        """Return the state of the sensor."""
        return self._state

    @property
    def icon(self):
        """Icon to use in the frontend, if any."""
        return ICON

    @property
    def extra_state_attributes(self):
        """Return other details about the sensor state."""
        self.attrs["Description"] = self._description
        return self.attrs

    async def async_update(self):
        """Update the sensor."""
        await self._data.update()
        self._state = self._data.data[self.name]["State"]
        self._description = self._data.data[self.name]["Description"]

@@ -131,16 +131,52 @@
 medelta%0A
+import logging%0A%0Aimport async_timeout
 %0Afrom lo
@@ -402,16 +402,70 @@
 sistant%0A
+from homeassistant.exceptions import PlatformNotReady%0A
 from hom
@@ -722,16 +722,193 @@
 InfoType
+%0Afrom homeassistant.helpers.update_coordinator import (%0A    CoordinatorEntity,%0A    DataUpdateCoordinator,%0A)%0A%0A_LOGGER = logging.getLogger(__name__)%0A%0ADOMAIN = %22london_underground%22
 %0A%0AATTRIB
@@ -1724,27 +1724,170 @@
     
-await data.update()
+coordinator = LondonTubeCoordinator(hass, data)%0A%0A    await coordinator.async_refresh()%0A%0A    if not coordinator.last_update_success:%0A        raise PlatformNotReady
 %0A%0A  
@@ -1980,18 +1980,25 @@
 sor(
-line, data
+coordinator, line
 ))%0A%0A
@@ -2031,41 +2031,584 @@
 sors
-, True)%0A%0A%0Aclass LondonTubeSensor(
+)%0A%0A%0Aclass LondonTubeCoordinator(DataUpdateCoordinator):%0A    %22%22%22London Underground sensor coordinator.%22%22%22%0A%0A    def __init__(self, hass, data):%0A        %22%22%22Initialize coordinator.%22%22%22%0A        super().__init__(%0A            hass,%0A            _LOGGER,%0A            name=DOMAIN,%0A            update_interval=SCAN_INTERVAL,%0A        )%0A        self._data = data%0A%0A    async def _async_update_data(self):%0A        async with async_timeout.timeout(10):%0A            await self._data.update()%0A            return self._data.data%0A%0A%0Aclass LondonTubeSensor(CoordinatorEntity%5BLondonTubeCoordinator%5D, 
 Sens
@@ -2711,18 +2711,25 @@
 lf, 
-name, data
+coordinator, name
 ):%0A 
@@ -2796,109 +2796,61 @@
    s
-elf._data = data%0A        self._description = None%0A        self._name = name%0A        self._state = Non
+uper().__init__(coordinator)%0A        self._name = nam
 e%0A  
@@ -3115,22 +3115,52 @@
 rn self.
-_s
+coordinator.data%5Bself.name%5D%5B%22S
 tate
+%22%5D
 %0A%0A    @p
@@ -3415,273 +3415,73 @@
 elf.
-_description%0A        return self.attrs%0A%0A    async def async_update(self):%0A        %22%22%22Update the sensor.%22%22%22%0A        await self._data.update()%0A        self._state = self._data.data%5Bself.name%5D%5B%22State%22%5D%0A        self._description = self._data.data%5Bself.name%5D%5B%22Description%22%5D
+coordinator.data%5Bself.name%5D%5B%22Description%22%5D%0A        return self.attrs
 %0A

Use DataUpdateCoordinator in london_underground (#75304)