From dc7976b31ff22855d1d094c6c917d553169e6f3c Mon Sep 17 00:00:00 2001 From: Aaron Godfrey Date: Tue, 26 May 2020 22:28:57 -0700 Subject: [PATCH] Added scan_interval configuration option. Changed default update time to 1 hour. --- README.md | 21 +++++++++++++++---- .../nintendo_wishlist/__init__.py | 9 ++++++-- .../nintendo_wishlist/binary_sensor.py | 5 ++++- custom_components/nintendo_wishlist/const.py | 2 +- 4 files changed, 29 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index f976670..632f5f9 100644 --- a/README.md +++ b/README.md @@ -21,10 +21,23 @@ sale on your wish list in home assistant. ## Configuration -| Name | Required | Description | -| -------- | -------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -| country | yes | The 2 letter country code. See [Supported Countries](#supported-countries) below. | -| wishlist | yes | A list of Nintendo Switch titles. Each item can be the exact title or just the beginning of a title in order to match multiple games. For example `Shantae` would trigger when any one of the many `Shantae` games goes on sale. Read [how wishlist matching works](#how-wish-list-matching-works) below. | +| Name | Required | Description | +| ------------- | -------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| country | yes | The 2 letter country code. See [Supported Countries](#supported-countries) below. | +| wishlist | yes | A list of Nintendo Switch titles. Each item can be the exact title or just the beginning of a title in order to match multiple games. For example `Shantae` would trigger when any one of the many `Shantae` games goes on sale. Read [how wishlist matching works](#how-wish-list-matching-works) below. | +| scan_interval | no | The minimum time interval between updates. See [time period dictionary](#time-period-dictionary) below for an example of accepted values. _Defaults to 1 hour._ | + +### Time Period Dictionary + +```yaml +scan_interval: + # at least one of the following must be specified: + days: 0 + hours: 0 + minutes: 3 + seconds: 30 + milliseconds: 0 +``` ## Sample Configuration diff --git a/custom_components/nintendo_wishlist/__init__.py b/custom_components/nintendo_wishlist/__init__.py index 92fb80b..e112971 100644 --- a/custom_components/nintendo_wishlist/__init__.py +++ b/custom_components/nintendo_wishlist/__init__.py @@ -5,11 +5,12 @@ import homeassistant.helpers.config_validation as cv from homeassistant import core +from homeassistant.const import CONF_SCAN_INTERVAL from homeassistant.helpers.aiohttp_client import async_get_clientsession from homeassistant.helpers.discovery import async_load_platform from homeassistant.helpers.update_coordinator import DataUpdateCoordinator -from .const import CONF_COUNTRY, CONF_WISHLIST, DOMAIN, SCAN_INTERVAL +from .const import CONF_COUNTRY, CONF_WISHLIST, DOMAIN, DEFAULT_SCAN_INTERVAL from .eshop import Country, EShop @@ -20,6 +21,9 @@ { vol.Required(CONF_WISHLIST): cv.ensure_list, vol.Required(CONF_COUNTRY): cv.enum(Country), + vol.Optional( + CONF_SCAN_INTERVAL, default=DEFAULT_SCAN_INTERVAL + ): vol.All(cv.time_period, cv.positive_timedelta), } ) }, @@ -39,6 +43,7 @@ async def async_setup(hass: core.HomeAssistant, config: dict) -> bool: conf = config[DOMAIN] country = conf[CONF_COUNTRY].name wishlist = conf[CONF_WISHLIST] + scan_interval = conf[CONF_SCAN_INTERVAL] eshop = EShop(country, async_get_clientsession(hass), wishlist) coordinator = DataUpdateCoordinator( hass, @@ -47,7 +52,7 @@ async def async_setup(hass: core.HomeAssistant, config: dict) -> bool: name=DOMAIN, update_method=eshop.fetch_on_sale, # Polling interval. Will only be polled if there are subscribers. - update_interval=SCAN_INTERVAL, + update_interval=scan_interval, ) # Fetch initial data so we have data when entities subscribe diff --git a/custom_components/nintendo_wishlist/binary_sensor.py b/custom_components/nintendo_wishlist/binary_sensor.py index 86545f6..053df68 100644 --- a/custom_components/nintendo_wishlist/binary_sensor.py +++ b/custom_components/nintendo_wishlist/binary_sensor.py @@ -7,7 +7,10 @@ try: from homeassistant.components.binary_sensor import BinarySensorEntity except ImportError: - from homeassistant.components.binary_sensor import BinarySensorDevice as BinarySensorEntity + # Prior to HA 0.110 + from homeassistant.components.binary_sensor import ( + BinarySensorDevice as BinarySensorEntity, + ) from .const import DOMAIN from .types import SwitchGame diff --git a/custom_components/nintendo_wishlist/const.py b/custom_components/nintendo_wishlist/const.py index c1841af..e51257e 100644 --- a/custom_components/nintendo_wishlist/const.py +++ b/custom_components/nintendo_wishlist/const.py @@ -3,4 +3,4 @@ CONF_COUNTRY = "country" CONF_WISHLIST = "wishlist" DOMAIN = "nintendo_wishlist" -SCAN_INTERVAL = timedelta(minutes=10) +DEFAULT_SCAN_INTERVAL = timedelta(hours=1)