From fa0b61e96a907d955edf49d9288860f9cddfa984 Mon Sep 17 00:00:00 2001 From: Jan-Philipp Benecke Date: Mon, 4 Sep 2023 11:07:08 +0200 Subject: [PATCH] Move london underground coordinator to its own file (#99550) --- CODEOWNERS | 1 + .../components/london_underground/const.py | 26 +++++++++ .../london_underground/coordinator.py | 30 +++++++++++ .../london_underground/manifest.json | 2 +- .../components/london_underground/sensor.py | 53 ++----------------- 5 files changed, 62 insertions(+), 50 deletions(-) create mode 100644 homeassistant/components/london_underground/const.py create mode 100644 homeassistant/components/london_underground/coordinator.py diff --git a/CODEOWNERS b/CODEOWNERS index b937c2769fce18..42537d4e3f18c4 100644 --- a/CODEOWNERS +++ b/CODEOWNERS @@ -707,6 +707,7 @@ build.json @home-assistant/supervisor /tests/components/logger/ @home-assistant/core /homeassistant/components/logi_circle/ @evanjd /tests/components/logi_circle/ @evanjd +/homeassistant/components/london_underground/ @jpbede /homeassistant/components/lookin/ @ANMalko @bdraco /tests/components/lookin/ @ANMalko @bdraco /homeassistant/components/loqed/ @mikewoudenberg diff --git a/homeassistant/components/london_underground/const.py b/homeassistant/components/london_underground/const.py new file mode 100644 index 00000000000000..4928d3bb1641d5 --- /dev/null +++ b/homeassistant/components/london_underground/const.py @@ -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", +] diff --git a/homeassistant/components/london_underground/coordinator.py b/homeassistant/components/london_underground/coordinator.py new file mode 100644 index 00000000000000..a094d099896835 --- /dev/null +++ b/homeassistant/components/london_underground/coordinator.py @@ -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 diff --git a/homeassistant/components/london_underground/manifest.json b/homeassistant/components/london_underground/manifest.json index acdb83a2359df5..eafc63c6ae7386 100644 --- a/homeassistant/components/london_underground/manifest.json +++ b/homeassistant/components/london_underground/manifest.json @@ -1,7 +1,7 @@ { "domain": "london_underground", "name": "London Underground", - "codeowners": [], + "codeowners": ["@jpbede"], "documentation": "https://www.home-assistant.io/integrations/london_underground", "iot_class": "cloud_polling", "loggers": ["london_tube_status"], diff --git a/homeassistant/components/london_underground/sensor.py b/homeassistant/components/london_underground/sensor.py index 7e52186fa51763..c0d0eeca372f83 100644 --- a/homeassistant/components/london_underground/sensor.py +++ b/homeassistant/components/london_underground/sensor.py @@ -1,8 +1,6 @@ """Sensor for checking the status of London Underground tube lines.""" from __future__ import annotations -import asyncio -from datetime import timedelta import logging from london_tube_status import TubeData @@ -15,36 +13,12 @@ import homeassistant.helpers.config_validation as cv from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType -from homeassistant.helpers.update_coordinator import ( - CoordinatorEntity, - DataUpdateCoordinator, -) - -_LOGGER = logging.getLogger(__name__) - -DOMAIN = "london_underground" - -CONF_LINE = "line" - +from homeassistant.helpers.update_coordinator import CoordinatorEntity -SCAN_INTERVAL = timedelta(seconds=30) +from .const import CONF_LINE, TUBE_LINES +from .coordinator import LondonTubeCoordinator -TUBE_LINES = [ - "Bakerloo", - "Central", - "Circle", - "District", - "DLR", - "Elizabeth line", - "Hammersmith & City", - "Jubilee", - "London Overground", - "Metropolitan", - "Northern", - "Piccadilly", - "Victoria", - "Waterloo & City", -] +_LOGGER = logging.getLogger(__name__) PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend( {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) -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): """Sensor that reads the status of a line from Tube Data."""