diff --git a/.github/workflows/hacs-validate.yml b/.github/workflows/hacs-validate.yml index e3f1ba8..6cb1b9c 100644 --- a/.github/workflows/hacs-validate.yml +++ b/.github/workflows/hacs-validate.yml @@ -1,17 +1,17 @@ -name: Validate with HACS - -on: - push: - pull_request: - schedule: - - cron: '54 0 * * THU' - -jobs: - validate: - runs-on: 'ubuntu-latest' - steps: - - uses: 'actions/checkout@v2' - - name: HACS validation - uses: 'hacs/action@main' - with: - category: 'integration' +name: Validate with HACS + +on: + push: + pull_request: + schedule: + - cron: '54 0 * * THU' + +jobs: + validate: + runs-on: 'ubuntu-latest' + steps: + - uses: 'actions/checkout@v2' + - name: HACS validation + uses: 'hacs/action@main' + with: + category: 'integration' diff --git a/README.md b/README.md index f8ab7ee..04b0688 100644 --- a/README.md +++ b/README.md @@ -1,63 +1,63 @@ -# Metlink Wellington Transport for Home Assistant - -This is a custom component for Home Assistant which uses -the [Metlink opendata API](https://opendata.metlink.org.nz/) to obtain -realtime departure info for Greater Wellington public transport, which can -then be displayed on your Home Assistant dashboard or used in automations. - - -## Installation - -[![hacs_badge](https://img.shields.io/badge/HACS-Default-orange.svg?style=for-the-badge)](https://github.com/custom-components/hacs) - -The integration can be installed using HACS. -If you prefer to instal manually, copy `custom_components/metlink` to your -installation's `config/custom_components` directory. - -### Configuration - -After installing the custom component, you still need to configure it before -it will do anything. - -The integration can be added from the Integrations configuration screen. - -API keys can be obtained by registering on the -[Metlink Developer Portal](https://opendata.metlink.org.nz/). Be sure to subscribe to the "Metlink Open Data API". Currently this is the only API they offer, and is free, but is still unsubscribed by default. - -If you prefer to configure using yaml instead of the UI, add something like -the following to your `config/configuration.yaml`: - -``` -sensor: - - platform: metlink - api_key: abcdefg1234xzy - stops: - - stop_id: 9999 - - stop_id: WELL - route: HVL - - stop_id: 5016 - destination: 3451 - num_departures: 3 -``` - - -`stop_id` for Train and Cable Car stops is a 4 character alphabetic -code, and for bus and ferry stops, is a 4 digit numeric code. -The IDs are on bus stop signs, or can be looked up on the -[Metlink](https://metlink.org.nz) main web site. - - -If your stop is busy with multiple routes, you can filter by route and/or destination. Currently exact matches are expected and only a single route or destination can be specified, though the destination does check the name as well as the stop id for a match, though some of the names that come through the API are abbreviated in ways that do not match the main web site so stop id will be more reliable. The destination filter is only available on the final destination, not any intermediate stops. - - -Each stop will create a sensor in Home Assistant, which will return the next departure time as its status. - -It will also return attributes for departure time, service, service -name, destination name, stop id for the destination, and status. The -duplication of departure time in the attributes makes more sense when -there is more than one result being returned. - -If more than 1 result is requested in `num_departures`, the attributes -will be suffixed with a number for second and subsequent departures. - -[![BuyMeCoffee](https://www.buymeacoffee.com/assets/img/custom_images/orange_img.png)](https://www.buymeacoffee.com/jasonrumney) +# Metlink Wellington Transport for Home Assistant + +This is a custom component for Home Assistant which uses +the [Metlink opendata API](https://opendata.metlink.org.nz/) to obtain +realtime departure info for Greater Wellington public transport, which can +then be displayed on your Home Assistant dashboard or used in automations. + + +## Installation + +[![hacs_badge](https://img.shields.io/badge/HACS-Default-orange.svg?style=for-the-badge)](https://github.com/custom-components/hacs) + +The integration can be installed using HACS. +If you prefer to instal manually, copy `custom_components/metlink` to your +installation's `config/custom_components` directory. + +### Configuration + +After installing the custom component, you still need to configure it before +it will do anything. + +The integration can be added from the Integrations configuration screen. + +API keys can be obtained by registering on the +[Metlink Developer Portal](https://opendata.metlink.org.nz/). Be sure to subscribe to the "Metlink Open Data API". Currently this is the only API they offer, and is free, but is still unsubscribed by default. + +If you prefer to configure using yaml instead of the UI, add something like +the following to your `config/configuration.yaml`: + +``` +sensor: + - platform: metlink + api_key: abcdefg1234xzy + stops: + - stop_id: 9999 + - stop_id: WELL + route: HVL + - stop_id: 5016 + destination: 3451 + num_departures: 3 +``` + + +`stop_id` for Train and Cable Car stops is a 4 character alphabetic +code, and for bus and ferry stops, is a 4 digit numeric code. +The IDs are on bus stop signs, or can be looked up on the +[Metlink](https://metlink.org.nz) main web site. + + +If your stop is busy with multiple routes, you can filter by route and/or destination. Currently exact matches are expected and only a single route or destination can be specified, though the destination does check the name as well as the stop id for a match, though some of the names that come through the API are abbreviated in ways that do not match the main web site so stop id will be more reliable. The destination filter is only available on the final destination, not any intermediate stops. + + +Each stop will create a sensor in Home Assistant, which will return the next departure time as its status. + +It will also return attributes for departure time, service, service +name, destination name, stop id for the destination, and status. The +duplication of departure time in the attributes makes more sense when +there is more than one result being returned. + +If more than 1 result is requested in `num_departures`, the attributes +will be suffixed with a number for second and subsequent departures. + +[![BuyMeCoffee](https://www.buymeacoffee.com/assets/img/custom_images/orange_img.png)](https://www.buymeacoffee.com/jasonrumney) diff --git a/custom_components/metlink/__init__.py b/custom_components/metlink/__init__.py index a9f08e5..907a4ee 100644 --- a/custom_components/metlink/__init__.py +++ b/custom_components/metlink/__init__.py @@ -1,65 +1,65 @@ -"""Metlink Wellington Transport integration.""" -import asyncio -import logging - -from homeassistant import config_entries, core - -from .const import DOMAIN - -_LOGGER = logging.getLogger(__name__) - - -async def async_setup_entry( - hass: core.HomeAssistant, entry: config_entries.ConfigEntry -) -> bool: - """Set up platform from a ConfigEntry.""" - hass.data.setdefault(DOMAIN, {}) - data = {**entry.data, **entry.options} - # Register an update listener to refresh when options are updated. - update_listener = entry.add_update_listener(options_update_listener) - data["unsub_options_update_listener"] = update_listener - hass.data[DOMAIN][entry.entry_id] = data - - # Forward the setup to the sensor platform. - _LOGGER.debug(f"Setting up based on {entry.data}") - hass.async_create_task( - hass.config_entries.async_forward_entry_setup(entry, "sensor") - ) - return True - - -async def options_update_listener( - hass: core.HomeAssistant, config_entry: config_entries.ConfigEntry -): - """Handle options update.""" - _LOGGER.debug("Reloading config after options update") - await hass.config_entries.async_reload(config_entry.entry_id) - - -async def async_unload_entry( - hass: core.HomeAssistant, entry: config_entries.ConfigEntry -) -> bool: - """Unload a config entry.""" - _LOGGER.debug("Unloading") - unload_ok = all( - await asyncio.gather( - *[hass.config_entries.async_forward_entry_unload(entry, "sensor")] - ) - ) - # Remove the listener - hass.data[DOMAIN][entry.entry_id]["unsub_options_update_listener"]() - - # Remove from domain - if unload_ok: - hass.data[DOMAIN].pop(entry.entry_id) - else: - _LOGGER.warning("Unload failed") - - return unload_ok - - -async def async_setup(hass: core.HomeAssistant, config: dict) -> bool: - """Setup the Metlink component from yaml configuration.""" - _LOGGER.debug("Setting up from YAML config") - hass.data.setdefault(DOMAIN, {}) - return True +"""Metlink Wellington Transport integration.""" +import asyncio +import logging + +from homeassistant import config_entries, core + +from .const import DOMAIN + +_LOGGER = logging.getLogger(__name__) + + +async def async_setup_entry( + hass: core.HomeAssistant, entry: config_entries.ConfigEntry +) -> bool: + """Set up platform from a ConfigEntry.""" + hass.data.setdefault(DOMAIN, {}) + data = {**entry.data, **entry.options} + # Register an update listener to refresh when options are updated. + update_listener = entry.add_update_listener(options_update_listener) + data["unsub_options_update_listener"] = update_listener + hass.data[DOMAIN][entry.entry_id] = data + + # Forward the setup to the sensor platform. + _LOGGER.debug(f"Setting up based on {entry.data}") + hass.async_create_task( + hass.config_entries.async_forward_entry_setup(entry, "sensor") + ) + return True + + +async def options_update_listener( + hass: core.HomeAssistant, config_entry: config_entries.ConfigEntry +): + """Handle options update.""" + _LOGGER.debug("Reloading config after options update") + await hass.config_entries.async_reload(config_entry.entry_id) + + +async def async_unload_entry( + hass: core.HomeAssistant, entry: config_entries.ConfigEntry +) -> bool: + """Unload a config entry.""" + _LOGGER.debug("Unloading") + unload_ok = all( + await asyncio.gather( + *[hass.config_entries.async_forward_entry_unload(entry, "sensor")] + ) + ) + # Remove the listener + hass.data[DOMAIN][entry.entry_id]["unsub_options_update_listener"]() + + # Remove from domain + if unload_ok: + hass.data[DOMAIN].pop(entry.entry_id) + else: + _LOGGER.warning("Unload failed") + + return unload_ok + + +async def async_setup(hass: core.HomeAssistant, config: dict) -> bool: + """Setup the Metlink component from yaml configuration.""" + _LOGGER.debug("Setting up from YAML config") + hass.data.setdefault(DOMAIN, {}) + return True diff --git a/custom_components/metlink/const.py b/custom_components/metlink/const.py index efcbafe..32372a8 100644 --- a/custom_components/metlink/const.py +++ b/custom_components/metlink/const.py @@ -1,45 +1,45 @@ -# Copyright 2021 Jason Rumney -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# https://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -DOMAIN = "metlink" -ATTRIBUTION = "Data provided by Greater Wellington Regional Council" - -CONF_STOPS = "stops" -CONF_STOP_ID = "stop_id" -CONF_DEST = "destination" -CONF_ROUTE = "route" -CONF_NUM_DEPARTURES = "num_departures" - -ATTR_ACCESSIBLE = "wheelchair_accessible" -ATTR_AIMED = "aimed" -ATTR_ARRIVAL = "arrival" -ATTR_CLOSED = "closed" -ATTR_DELAY = "delay" -ATTR_DEPARTURE = "departure" -ATTR_DEPARTURES = "departures" -ATTR_DESCRIPTION = "description" -ATTR_DESTINATION = "destination" -ATTR_DESTINATION_ID = "destination_id" -ATTR_DIRECTION = "direction" -ATTR_EXPECTED = "expected" -ATTR_FAREZONE = "farezone" -ATTR_MONITORED = "monitored" -ATTR_NAME = "name" -ATTR_OPERATOR = "operator" -ATTR_ORIGIN = "origin" -ATTR_SERVICE = "service_id" -ATTR_STATUS = "status" -ATTR_STOP = "stop_id" -ATTR_STOP_NAME = "stop_name" -ATTR_VEHICLE = "vehicle_id" +# Copyright 2021 Jason Rumney +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +DOMAIN = "metlink" +ATTRIBUTION = "Data provided by Greater Wellington Regional Council" + +CONF_STOPS = "stops" +CONF_STOP_ID = "stop_id" +CONF_DEST = "destination" +CONF_ROUTE = "route" +CONF_NUM_DEPARTURES = "num_departures" + +ATTR_ACCESSIBLE = "wheelchair_accessible" +ATTR_AIMED = "aimed" +ATTR_ARRIVAL = "arrival" +ATTR_CLOSED = "closed" +ATTR_DELAY = "delay" +ATTR_DEPARTURE = "departure" +ATTR_DEPARTURES = "departures" +ATTR_DESCRIPTION = "description" +ATTR_DESTINATION = "destination" +ATTR_DESTINATION_ID = "destination_id" +ATTR_DIRECTION = "direction" +ATTR_EXPECTED = "expected" +ATTR_FAREZONE = "farezone" +ATTR_MONITORED = "monitored" +ATTR_NAME = "name" +ATTR_OPERATOR = "operator" +ATTR_ORIGIN = "origin" +ATTR_SERVICE = "service_id" +ATTR_STATUS = "status" +ATTR_STOP = "stop_id" +ATTR_STOP_NAME = "stop_name" +ATTR_VEHICLE = "vehicle_id" diff --git a/custom_components/metlink/manifest.json b/custom_components/metlink/manifest.json index 1fcaccc..fa1247f 100644 --- a/custom_components/metlink/manifest.json +++ b/custom_components/metlink/manifest.json @@ -1,12 +1,12 @@ -{ - "codeowners": ["@make-all"], - "config_flow": true, - "dependencies": [], - "documentation": "https://github.com/make-all/metlink-nz/wiki", - "domain": "metlink", - "iot_class": "cloud_polling", - "issue_tracker": "https://github.com/make-all/metlink-nz/issues", - "name": "Metlink Wellington Transport", - "requirements": ["isodate==0.6.0"], - "version": "1.0.3" -} +{ + "codeowners": ["@make-all"], + "config_flow": true, + "dependencies": [], + "documentation": "https://github.com/make-all/metlink-nz/wiki", + "domain": "metlink", + "iot_class": "cloud_polling", + "issue_tracker": "https://github.com/make-all/metlink-nz/issues", + "name": "Metlink Wellington Transport", + "requirements": ["isodate~=0.6.0"], + "version": "1.0.3" +} diff --git a/hacs.json b/hacs.json index 5dbcf9d..d8248e5 100644 --- a/hacs.json +++ b/hacs.json @@ -1,8 +1,8 @@ -{ - "name": "Metlink Wellington Transport", - "render_readme": true, - "domains": ["sensor"], - "country": "NZ", - "homeassistant": "2021.12.0", - "iot_class": "Cloud Polling" -} +{ + "name": "Metlink Wellington Transport", + "render_readme": true, + "domains": ["sensor"], + "country": "NZ", + "homeassistant": "2021.12.0", + "iot_class": "Cloud Polling" +} diff --git a/requirements.test.txt b/requirements.test.txt index 760eea1..59e1292 100644 --- a/requirements.test.txt +++ b/requirements.test.txt @@ -1,5 +1,5 @@ -pytest -pytest-cov -pytest-homeassistant-custom-component -homeassistant -isodate==0.6.0 +pytest +pytest-cov +pytest-homeassistant-custom-component +homeassistant +isodate~=0.6.0 diff --git a/setup.cfg b/setup.cfg index 092a7b3..511288f 100644 --- a/setup.cfg +++ b/setup.cfg @@ -1,61 +1,61 @@ -[coverage:run] -source = - custom_components - -[coverage:report] -exclude_lines = - pragma: no cover - raise NotImplemented() - if __name__ == '__main__': - main() -fail_under = 95 -show_missing = true - -[tool:pytest] -testpaths = tests -norecursedirs = .git -addopts = - --strict-markers - --cov=custom_components - -[flake8] -# https://github.com/ambv/black#line-length -max-line-length = 88 -# E501: line too long -# W503: Line break occurred before a binary operator -# E203: Whitespace before ':' -# D202 No blank lines allowed after function docstring -# W504 line break after binary operator -ignore = - E501, - W503, - E203, - D202, - W504 - -[isort] -# https://github.com/timothycrosley/isort -# https://github.com/timothycrosley/isort/wiki/isort-Settings -# splits long import on multiple lines indented by 4 spaces -multi_line_output = 3 -include_trailing_comma=True -force_grid_wrap=0 -use_parentheses=True -line_length=88 -indent = " " -# will group `import x` and `from x import` of the same module. -force_sort_within_sections = true -sections = FUTURE,STDLIB,THIRDPARTY,FIRSTPARTY,LOCALFOLDER -default_section = THIRDPARTY -known_first_party = custom_components,tests -forced_separate = tests -combine_as_imports = true - -[mypy] -python_version = 3.7 -ignore_errors = true -follow_imports = silent -ignore_missing_imports = true -warn_incomplete_stub = true -warn_redundant_casts = true -warn_unused_configs = true +[coverage:run] +source = + custom_components + +[coverage:report] +exclude_lines = + pragma: no cover + raise NotImplemented() + if __name__ == '__main__': + main() +fail_under = 95 +show_missing = true + +[tool:pytest] +testpaths = tests +norecursedirs = .git +addopts = + --strict-markers + --cov=custom_components + +[flake8] +# https://github.com/ambv/black#line-length +max-line-length = 88 +# E501: line too long +# W503: Line break occurred before a binary operator +# E203: Whitespace before ':' +# D202 No blank lines allowed after function docstring +# W504 line break after binary operator +ignore = + E501, + W503, + E203, + D202, + W504 + +[isort] +# https://github.com/timothycrosley/isort +# https://github.com/timothycrosley/isort/wiki/isort-Settings +# splits long import on multiple lines indented by 4 spaces +multi_line_output = 3 +include_trailing_comma=True +force_grid_wrap=0 +use_parentheses=True +line_length=88 +indent = " " +# will group `import x` and `from x import` of the same module. +force_sort_within_sections = true +sections = FUTURE,STDLIB,THIRDPARTY,FIRSTPARTY,LOCALFOLDER +default_section = THIRDPARTY +known_first_party = custom_components,tests +forced_separate = tests +combine_as_imports = true + +[mypy] +python_version = 3.7 +ignore_errors = true +follow_imports = silent +ignore_missing_imports = true +warn_incomplete_stub = true +warn_redundant_casts = true +warn_unused_configs = true diff --git a/tests/bandit.yaml b/tests/bandit.yaml index ebd284e..40d4837 100644 --- a/tests/bandit.yaml +++ b/tests/bandit.yaml @@ -1,17 +1,17 @@ -# https://bandit.readthedocs.io/en/latest/config.html - -tests: - - B108 - - B306 - - B307 - - B313 - - B314 - - B315 - - B316 - - B317 - - B318 - - B319 - - B320 - - B325 - - B602 - - B604 +# https://bandit.readthedocs.io/en/latest/config.html + +tests: + - B108 + - B306 + - B307 + - B313 + - B314 + - B315 + - B316 + - B317 + - B318 + - B319 + - B320 + - B325 + - B602 + - B604