Move london underground coordinator to its own file (#99550)

This commit is contained in:
Jan-Philipp Benecke 2023-09-04 11:07:08 +02:00 committed by GitHub
parent b9536732bc
commit fa0b61e96a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 62 additions and 50 deletions

View File

@ -707,6 +707,7 @@ build.json @home-assistant/supervisor
/tests/components/logger/ @home-assistant/core /tests/components/logger/ @home-assistant/core
/homeassistant/components/logi_circle/ @evanjd /homeassistant/components/logi_circle/ @evanjd
/tests/components/logi_circle/ @evanjd /tests/components/logi_circle/ @evanjd
/homeassistant/components/london_underground/ @jpbede
/homeassistant/components/lookin/ @ANMalko @bdraco /homeassistant/components/lookin/ @ANMalko @bdraco
/tests/components/lookin/ @ANMalko @bdraco /tests/components/lookin/ @ANMalko @bdraco
/homeassistant/components/loqed/ @mikewoudenberg /homeassistant/components/loqed/ @mikewoudenberg

View File

@ -0,0 +1,26 @@
"""Constants for the London underground integration."""
from datetime import timedelta
DOMAIN = "london_underground"
CONF_LINE = "line"
SCAN_INTERVAL = timedelta(seconds=30)
TUBE_LINES = [
"Bakerloo",
"Central",
"Circle",
"District",
"DLR",
"Elizabeth line",
"Hammersmith & City",
"Jubilee",
"London Overground",
"Metropolitan",
"Northern",
"Piccadilly",
"Victoria",
"Waterloo & City",
]

View File

@ -0,0 +1,30 @@
"""DataUpdateCoordinator for London underground integration."""
from __future__ import annotations
import asyncio
import logging
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator
from .const import DOMAIN, SCAN_INTERVAL
_LOGGER = logging.getLogger(__name__)
class LondonTubeCoordinator(DataUpdateCoordinator):
"""London Underground sensor coordinator."""
def __init__(self, hass, data):
"""Initialize coordinator."""
super().__init__(
hass,
_LOGGER,
name=DOMAIN,
update_interval=SCAN_INTERVAL,
)
self._data = data
async def _async_update_data(self):
async with asyncio.timeout(10):
await self._data.update()
return self._data.data

View File

@ -1,7 +1,7 @@
{ {
"domain": "london_underground", "domain": "london_underground",
"name": "London Underground", "name": "London Underground",
"codeowners": [], "codeowners": ["@jpbede"],
"documentation": "https://www.home-assistant.io/integrations/london_underground", "documentation": "https://www.home-assistant.io/integrations/london_underground",
"iot_class": "cloud_polling", "iot_class": "cloud_polling",
"loggers": ["london_tube_status"], "loggers": ["london_tube_status"],

View File

@ -1,8 +1,6 @@
"""Sensor for checking the status of London Underground tube lines.""" """Sensor for checking the status of London Underground tube lines."""
from __future__ import annotations from __future__ import annotations
import asyncio
from datetime import timedelta
import logging import logging
from london_tube_status import TubeData from london_tube_status import TubeData
@ -15,37 +13,13 @@ from homeassistant.helpers.aiohttp_client import async_get_clientsession
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
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
from homeassistant.helpers.update_coordinator import ( from homeassistant.helpers.update_coordinator import CoordinatorEntity
CoordinatorEntity,
DataUpdateCoordinator, from .const import CONF_LINE, TUBE_LINES
) from .coordinator import LondonTubeCoordinator
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
DOMAIN = "london_underground"
CONF_LINE = "line"
SCAN_INTERVAL = timedelta(seconds=30)
TUBE_LINES = [
"Bakerloo",
"Central",
"Circle",
"District",
"DLR",
"Elizabeth line",
"Hammersmith & City",
"Jubilee",
"London Overground",
"Metropolitan",
"Northern",
"Piccadilly",
"Victoria",
"Waterloo & City",
]
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend( PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
{vol.Required(CONF_LINE): vol.All(cv.ensure_list, [vol.In(list(TUBE_LINES))])} {vol.Required(CONF_LINE): vol.All(cv.ensure_list, [vol.In(list(TUBE_LINES))])}
) )
@ -76,25 +50,6 @@ async def async_setup_platform(
async_add_entities(sensors) async_add_entities(sensors)
class LondonTubeCoordinator(DataUpdateCoordinator):
"""London Underground sensor coordinator."""
def __init__(self, hass, data):
"""Initialize coordinator."""
super().__init__(
hass,
_LOGGER,
name=DOMAIN,
update_interval=SCAN_INTERVAL,
)
self._data = data
async def _async_update_data(self):
async with asyncio.timeout(10):
await self._data.update()
return self._data.data
class LondonTubeSensor(CoordinatorEntity[LondonTubeCoordinator], SensorEntity): class LondonTubeSensor(CoordinatorEntity[LondonTubeCoordinator], SensorEntity):
"""Sensor that reads the status of a line from Tube Data.""" """Sensor that reads the status of a line from Tube Data."""