From 5ddcf7e6599363bc735113ddc156b96d59ab8c9d Mon Sep 17 00:00:00 2001 From: jonochocki Date: Fri, 2 Jul 2021 12:48:39 -0500 Subject: [PATCH 01/11] Create hacs.json --- hacs.json | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 hacs.json diff --git a/hacs.json b/hacs.json new file mode 100644 index 0000000..d8911a3 --- /dev/null +++ b/hacs.json @@ -0,0 +1,5 @@ +{ + "name": "CTA Bus Tracker", + "render_readme": true, + "persistent_directory": "cache" +} From 008b7d9182202c3bf005699d349c698017fbedee Mon Sep 17 00:00:00 2001 From: jonochocki Date: Sun, 4 Jul 2021 22:40:45 -0500 Subject: [PATCH 02/11] Update hacs.json --- hacs.json | 1 + 1 file changed, 1 insertion(+) diff --git a/hacs.json b/hacs.json index d8911a3..de46d7a 100644 --- a/hacs.json +++ b/hacs.json @@ -2,4 +2,5 @@ "name": "CTA Bus Tracker", "render_readme": true, "persistent_directory": "cache" + "domains": ["sensor"], } From 7806fa1ee233234fdca0475b52c12fec9f3d12dd Mon Sep 17 00:00:00 2001 From: jonochocki Date: Sun, 4 Jul 2021 22:44:50 -0500 Subject: [PATCH 03/11] Update hacs.json --- hacs.json | 1 - 1 file changed, 1 deletion(-) diff --git a/hacs.json b/hacs.json index de46d7a..bbfb9d1 100644 --- a/hacs.json +++ b/hacs.json @@ -1,6 +1,5 @@ { "name": "CTA Bus Tracker", "render_readme": true, - "persistent_directory": "cache" "domains": ["sensor"], } From 206c52903b01908ef050cb0edb26b91499770c0c Mon Sep 17 00:00:00 2001 From: jonochocki Date: Sun, 4 Jul 2021 22:49:43 -0500 Subject: [PATCH 04/11] Create info.MD --- info.MD | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 info.MD diff --git a/info.MD b/info.MD new file mode 100644 index 0000000..5badae0 --- /dev/null +++ b/info.MD @@ -0,0 +1,3 @@ +{% if prerelease %} +### ATTN!: This is a BETA version! +{% endif %} From cf6e6682892029d7aac33b4487617d3ad4cd799e Mon Sep 17 00:00:00 2001 From: jonochocki Date: Sun, 4 Jul 2021 22:52:40 -0500 Subject: [PATCH 05/11] Update sensor.py --- custom_components/ctabustracker/sensor.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/custom_components/ctabustracker/sensor.py b/custom_components/ctabustracker/sensor.py index 0ba1c10..ea3cabe 100644 --- a/custom_components/ctabustracker/sensor.py +++ b/custom_components/ctabustracker/sensor.py @@ -2,7 +2,7 @@ Show departure information from ctabustracker (Chicago Transit Authority). For more details about this component, please refer to the documentation at -https://github.com/custom-components/sensor.ctabustracker/ +https://github.com/jonochocki/sensor.ctabustracker/ """ from datetime import timedelta import logging From 860e3ab79a91624ec5066a5fd95a8f8fdc893ce1 Mon Sep 17 00:00:00 2001 From: jonochocki Date: Sun, 4 Jul 2021 22:54:54 -0500 Subject: [PATCH 06/11] Create __init__.py --- custom_components/__init__.py | 1 + 1 file changed, 1 insertion(+) create mode 100644 custom_components/__init__.py diff --git a/custom_components/__init__.py b/custom_components/__init__.py new file mode 100644 index 0000000..01c0f3f --- /dev/null +++ b/custom_components/__init__.py @@ -0,0 +1 @@ +### From 2fcd9e5b67bc92eb83a73e48f79d6efd0fc726ec Mon Sep 17 00:00:00 2001 From: jonochocki Date: Sun, 4 Jul 2021 22:57:14 -0500 Subject: [PATCH 07/11] Create resources.json --- resources.json | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 resources.json diff --git a/resources.json b/resources.json new file mode 100644 index 0000000..b4593dd --- /dev/null +++ b/resources.json @@ -0,0 +1,4 @@ +[ + "https://raw.githubusercontent.com/custom-components/sensor.plex_recently_added/master/jonochocki/ctabustracker/__init__.py", + "https://raw.githubusercontent.com/custom-components/sensor.plex_recently_added/master/jonochocki/ctabustracker/manifest.json" +] From 5742ba47fd252d2eb9e2fa513b33d25207e22535 Mon Sep 17 00:00:00 2001 From: jonochocki Date: Sun, 4 Jul 2021 22:58:39 -0500 Subject: [PATCH 08/11] Update resources.json --- resources.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/resources.json b/resources.json index b4593dd..82c18c9 100644 --- a/resources.json +++ b/resources.json @@ -1,4 +1,4 @@ [ - "https://raw.githubusercontent.com/custom-components/sensor.plex_recently_added/master/jonochocki/ctabustracker/__init__.py", - "https://raw.githubusercontent.com/custom-components/sensor.plex_recently_added/master/jonochocki/ctabustracker/manifest.json" + "https://raw.githubusercontent.com/jonochocki/sensor.ctabustracker/master/custom_components/ctabustracker/__init__.py", + "https://raw.githubusercontent.com/jonochocki/sensor.ctabustracker/master/custom_components/ctabustracker/manifest.json" ] From ffcae610c8a80a0c6d78e4c0bafa74cb23c26319 Mon Sep 17 00:00:00 2001 From: jonochocki Date: Sun, 4 Jul 2021 22:59:24 -0500 Subject: [PATCH 09/11] Rename custom_components/__init__.py to custom_components/ctabustracker/__init__.py --- custom_components/{ => ctabustracker}/__init__.py | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename custom_components/{ => ctabustracker}/__init__.py (100%) diff --git a/custom_components/__init__.py b/custom_components/ctabustracker/__init__.py similarity index 100% rename from custom_components/__init__.py rename to custom_components/ctabustracker/__init__.py From 53c222936c6fdba969ee700ed56d3a9ba5de53e0 Mon Sep 17 00:00:00 2001 From: jonochocki Date: Sun, 4 Jul 2021 23:01:04 -0500 Subject: [PATCH 10/11] Create manifest.json --- custom_components/ctabustracker/manifest.json | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 custom_components/ctabustracker/manifest.json diff --git a/custom_components/ctabustracker/manifest.json b/custom_components/ctabustracker/manifest.json new file mode 100644 index 0000000..c9f6d43 --- /dev/null +++ b/custom_components/ctabustracker/manifest.json @@ -0,0 +1,9 @@ +{ + "domain": "ctabustracker", + "name": "CTA Bus Tracker", + "version": "1.0.1", + "documentation": "https://github.com/jonochocki/sensor.ctabustracker", + "dependencies": [], + "codeowners": ["@jonochocki"], + "requirements": [] + } From 39a37d8bb274121037f572d0b59172604198f430 Mon Sep 17 00:00:00 2001 From: shaun Date: Mon, 10 Jan 2022 21:22:46 -0600 Subject: [PATCH 11/11] Support CTA train arrival API + update docs and other pointers --- README.md | 50 +++++++--- custom_components/ctabustracker/manifest.json | 6 +- custom_components/ctabustracker/sensor.py | 95 +++++++++++++++---- info.MD | 3 - resources.json | 4 +- 5 files changed, 114 insertions(+), 44 deletions(-) delete mode 100644 info.MD diff --git a/README.md b/README.md index 7bf82ba..38231d5 100644 --- a/README.md +++ b/README.md @@ -5,48 +5,68 @@ Show departure information from ctabustracker (Chicago Transit Authority). This plafrom is based of the findings of [@SilvrrGIT](https://github.com/SilvrrGIT) in this [forum post](https://community.home-assistant.io/t/cta-bus-tracker-sensor/92416) -**HA 0.86.0 or newer:** +## Installation +### HACS +Add this as a custom repository and restart your HA instance. +### Manual To get started put `/custom_components/ctabustracker/sensor.py` here: `/custom_components/ctabustracker/sensor.py` -**Older versions** - -To get started put `/custom_components/ctabustracker/sensor.py` here: -`/custom_components/sensor/ctabustracker.py` - ## Example configuration.yaml ```yaml sensor: platform: ctabustracker api_key: 'dfshkdkf7333ykgdfk73' + type: 'bus' lines: - - route: 151 - stop_id: 77 + - stop_id: 77 departures: 2 name: 'Union Station' + +sensor: + platform: ctabustracker + api_key: 'lk38vjklrj4nj' + type: 'train' + lines: + - stop_id: 3045 + departures: 2 + name: 'Logan Square' ``` -**Configuration variables:** +### Configuration variables key | type | description :--- | :--- | :--- **platform (Required)** | string | The platform name. -**api_key (Required)** | string | Your [API key](https://www.transitchicago.com/developers/bustracker/) +**api_key (Required)** | string | [CTA Bus API Key](https://www.transitchicago.com/developers/bustracker/) *or* [CTA Train API Key](https://www.transitchicago.com/developers/traintracker/) +**type** (Required) | string | Transit type: ["bus", "train"] **lines (Required)** | list | List of lines you want to track. +> **Note:** Bus times and train times each required a different API key. -**Lines configuration** +### Lines configuration key | type | description :--- | :--- | :--- -**route (Required)** | string | Route number (`rt`) **stop_id (Required)** | string | Stop ID (`stpid`) **departures (Optional)** | int | Number of future departures. -**name (Optional)** | list | List of lines you want to track. +**name (Optional)** | list | Name of the HA sensor -*** +## Change Log +### 0.0.3 +[@ludeeus](https://github.com/ludeeus): Initial release. -[![BuyMeCoffee](https://camo.githubusercontent.com/cd005dca0ef55d7725912ec03a936d3a7c8de5b5/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6275792532306d6525323061253230636f666665652d646f6e6174652d79656c6c6f772e737667)](https://www.buymeacoffee.com/ludeeus) +### 0.0.3 (on HACS) +[@jonochocki](https://github.com/jonochocki): Added easy install via HACS support. +### 0.0.4 +[@smcpeck](https://github.com/smcpeck): Added support for train lines. +- `route` config variable is no longer supported since the APIs don't need it was a "bus only" API parameter. + +*** +contributor | support +:--- | :--- +[@ludeeus](https://github.com/ludeeus) | [![BuyMeCoffee](https://camo.githubusercontent.com/cd005dca0ef55d7725912ec03a936d3a7c8de5b5/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6275792532306d6525323061253230636f666665652d646f6e6174652d79656c6c6f772e737667)](https://www.buymeacoffee.com/ludeeus) +[@smcpeck](https://github.com/smcpeck) | [![BuyMeCoffee](https://camo.githubusercontent.com/cd005dca0ef55d7725912ec03a936d3a7c8de5b5/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6275792532306d6525323061253230636f666665652d646f6e6174652d79656c6c6f772e737667)](https://www.buymeacoffee.com/shaunmcpeck) diff --git a/custom_components/ctabustracker/manifest.json b/custom_components/ctabustracker/manifest.json index c9f6d43..8db2b2a 100644 --- a/custom_components/ctabustracker/manifest.json +++ b/custom_components/ctabustracker/manifest.json @@ -1,9 +1,9 @@ { "domain": "ctabustracker", "name": "CTA Bus Tracker", - "version": "1.0.1", - "documentation": "https://github.com/jonochocki/sensor.ctabustracker", + "version": "0.0.4", + "documentation": "https://github.com/smcpeck/sensor.ctabustracker", "dependencies": [], - "codeowners": ["@jonochocki"], + "codeowners": ["@ludeeus","@jonochocki","@smcpeck"], "requirements": [] } diff --git a/custom_components/ctabustracker/sensor.py b/custom_components/ctabustracker/sensor.py index ea3cabe..97fa904 100644 --- a/custom_components/ctabustracker/sensor.py +++ b/custom_components/ctabustracker/sensor.py @@ -2,9 +2,9 @@ Show departure information from ctabustracker (Chicago Transit Authority). For more details about this component, please refer to the documentation at -https://github.com/jonochocki/sensor.ctabustracker/ +https://github.com/smcpeck/sensor.ctabustracker/ """ -from datetime import timedelta +import datetime as dt import logging import requests @@ -16,30 +16,32 @@ from homeassistant.helpers.entity import Entity from homeassistant.util import Throttle -__version__ = '0.0.3' +__version__ = '0.0.4' _LOGGER = logging.getLogger(__name__) CONF_API_KEY = 'api_key' CONF_DEPARTURES = 'departures' CONF_LINES = 'lines' -CONF_ROUTE = 'route' CONF_STOP_ID = 'stop_id' +CONF_TYPE = 'type' -RESOURCE = "http://ctabustracker.com/bustime/api/v2/" -ENDPOINT = "getpredictions?key={}&rt={}&stpid={}&format=json" +BUS_RESOURCE = "http://ctabustracker.com/bustime/api/v2/" +BUS_ENDPOINT = "getpredictions?key={}&stpid={}&format=json" +TRAIN_RESOURCE = "http://lapi.transitchicago.com/api/1.0/" +TRAIN_ENDPOINT = "ttarrivals.aspx?key={}&stpid={}&max={}&outputType=JSON" -TIME_BETWEEN_UPDATES = timedelta(seconds=60) +TIME_BETWEEN_UPDATES = dt.timedelta(seconds=60) LINES = vol.Schema({ vol.Required(CONF_STOP_ID): cv.string, - vol.Required(CONF_ROUTE): cv.string, vol.Optional(CONF_DEPARTURES, default=1): cv.positive_int, vol.Optional(CONF_NAME): cv.string, }) PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({ vol.Required(CONF_API_KEY): cv.string, + vol.Required(CONF_TYPE): vol.In(["bus", "train"]), vol.Required(CONF_LINES): vol.All(cv.ensure_list, [LINES]), }) @@ -49,40 +51,63 @@ async def async_setup_platform( """Set up the sensor platform.""" api_key = config[CONF_API_KEY] lines = config[CONF_LINES] + transit_type = config[CONF_TYPE] dev = [] for line in lines: - api = CtaBusData(api_key, line) + api = None + if transit_type == "bus": + api = CtaBusData(api_key, line) + elif transit_type == "train": + api = CtaTrainData(api_key, line) + for departure in range(0, line['departures']): - dev.append(CtaBusSensor(api, departure, line)) + dev.append(CtaSensor(api, transit_type, departure, line)) + async_add_entities(dev, True) -class CtaBusSensor(Entity): +class CtaSensor(Entity): """Representation of a Home Assistant sensor.""" - def __init__(self, api, departure, config): + def __init__(self, api, transit_type, departure, config): """Initialize the sensor.""" self.api = api self.departure = departure self.config = config self._state = None + self.transit_type = transit_type postfix = '' if departure == 0 else str(departure) self._name = "{} {}".format( - self.config.get('name', 'CTA '+self.config['route']), postfix) + self.config.get('name', 'CTA ' + self.config['stop_id']), postfix) def update(self): """Get the latest information.""" try: self.api.update() data = self.api.data + _LOGGER.debug(f"[{self.transit_type}] SENSOR.update() data = {data}") + if data: - self._state = data[self.departure].get('prdctdn') + if self.transit_type == "bus": + self._state = data[self.departure].get('prdctdn') + elif self.transit_type == "train": + prediction = data[self.departure] + pred_time = dt.datetime.strptime(prediction["prdt"], "%Y-%m-%dT%H:%M:%S") + arr_time = dt.datetime.strptime(prediction["arrT"], "%Y-%m-%dT%H:%M:%S") + minutes_left = int((arr_time-pred_time).total_seconds()/60) + if minutes_left <= 1: + minutes_left = "DUE" + self._state = minutes_left + else: + self._state = "Bad type" + _LOGGER.warning("Bad transit_type configured") else: - self._state = self._state - except Exception: # pylint: disable=W0703 + self._state = "No data" + _LOGGER.warning("No CTA data") + except Exception as ex: # pylint: disable=W0703 self._state = None - _LOGGER.debug(self._state) + _LOGGER.error(ex) @property def name(self): @@ -97,8 +122,36 @@ def state(self): @property def icon(self): """Set sensor icon.""" - return 'mdi:bus-clock' + return {'bus':'mdi:bus-clock','train':'mdi:train'}[self.transit_type] + +class CtaTrainData: + """Get the latest data and update the states.""" + + def __init__(self, api_key, config): + """Initialize the data object.""" + self.api_key = api_key + self.config = config + self.api = "{}{}".format( + TRAIN_RESOURCE, TRAIN_ENDPOINT.format( + self.api_key, self.config['stop_id'], self.config['departures'])) + self._data = None + + @Throttle(TIME_BETWEEN_UPDATES) + def update(self): + """Get the latest data from ctabustracker.""" + try: + self._data = requests.get( + self.api).json().get('ctatt', {}).get('eta', {}) + except Exception as error: # pylint: disable=W0703 + _LOGGER.error(error) + self._data = self._data + return self._data + @property + def data(self): + """Holds data.""" + return self._data + class CtaBusData: """Get the latest data and update the states.""" @@ -108,8 +161,8 @@ def __init__(self, api_key, config): self.api_key = api_key self.config = config self.api = "{}{}".format( - RESOURCE, ENDPOINT.format( - self.api_key, self.config['route'], self.config['stop_id'])) + BUS_RESOURCE, BUS_ENDPOINT.format( + self.api_key, self.config['stop_id'])) self._data = None @Throttle(TIME_BETWEEN_UPDATES) @@ -118,10 +171,10 @@ def update(self): try: self._data = requests.get( self.api).json().get('bustime-response', {}).get('prd', {}) - _LOGGER.debug(self._data) except Exception as error: # pylint: disable=W0703 _LOGGER.error(error) self._data = self._data + @property def data(self): diff --git a/info.MD b/info.MD deleted file mode 100644 index 5badae0..0000000 --- a/info.MD +++ /dev/null @@ -1,3 +0,0 @@ -{% if prerelease %} -### ATTN!: This is a BETA version! -{% endif %} diff --git a/resources.json b/resources.json index 82c18c9..f849cd8 100644 --- a/resources.json +++ b/resources.json @@ -1,4 +1,4 @@ [ - "https://raw.githubusercontent.com/jonochocki/sensor.ctabustracker/master/custom_components/ctabustracker/__init__.py", - "https://raw.githubusercontent.com/jonochocki/sensor.ctabustracker/master/custom_components/ctabustracker/manifest.json" + "https://raw.githubusercontent.com/smcpeck/sensor.ctabustracker/master/custom_components/ctabustracker/__init__.py", + "https://raw.githubusercontent.com/smcpeck/sensor.ctabustracker/master/custom_components/ctabustracker/manifest.json" ]