Skip to content

Commit

Permalink
Move london underground coordinator to its own file (home-assistant#9…
Browse files Browse the repository at this point in the history
  • Loading branch information
jpbede authored Sep 4, 2023
1 parent b953673 commit fa0b61e
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 50 deletions.
1 change: 1 addition & 0 deletions CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
26 changes: 26 additions & 0 deletions homeassistant/components/london_underground/const.py
Original file line number Diff line number Diff line change
@@ -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",
]
30 changes: 30 additions & 0 deletions homeassistant/components/london_underground/coordinator.py
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion homeassistant/components/london_underground/manifest.json
Original file line number Diff line number Diff line change
@@ -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"],
Expand Down
53 changes: 4 additions & 49 deletions homeassistant/components/london_underground/sensor.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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))])}
Expand Down Expand Up @@ -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."""

Expand Down

0 comments on commit fa0b61e

Please sign in to comment.