Skip to content

Commit

Permalink
feature: configurable update_interval in seconds
Browse files Browse the repository at this point in the history
  • Loading branch information
Kartax authored Jan 9, 2025
1 parent bb6dc2b commit bf8a29c
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 7 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ Configure the sensor(s) in ``configuration.yaml``.
sensor:
- platform: binance
decimals: 8
update_interval: 60
symbols:
- BTCUSDT
- ETHUSDT
Expand Down
9 changes: 7 additions & 2 deletions custom_components/binance/binance_ticker_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,12 @@

class BinanceTickerSensor(Entity):

def __init__(self, symbol, decimals):
def __init__(self, symbol, decimals, updateInterval):
self._attr_device_class = SensorDeviceClass.MONETARY
self._name = "Binance Ticker "+symbol.upper()
self._symbol = symbol
self._decimals = decimals
self._updateInterval = updateInterval
self._state = STATE_UNKNOWN
self._data = {}

Expand All @@ -37,6 +38,10 @@ def symbol(self):
def decimals(self):
return self._decimals

@property
def updateInterval(self):
return self._updateInterval

@property
def state(self):
return self._state
Expand All @@ -47,7 +52,7 @@ def extra_state_attributes(self):

async def async_added_to_hass(self):
self.hass.helpers.event.async_track_time_interval(
self.update, timedelta(seconds=60)
self.update, timedelta(seconds=updateInterval)
)

def update(self, *args):
Expand Down
1 change: 1 addition & 0 deletions custom_components/binance/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@

CONF_SYMBOLS = "symbols"
CONF_DECIMALS = "decimals"
CONF_UPDATE_INVERVAL = "update_interval"
2 changes: 1 addition & 1 deletion custom_components/binance/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@
"integration_type": "hub",
"iot_class": "cloud_polling",
"issue_tracker": "https://github.com/Kartax/home-assistant-binance/issues",
"version": "1.1.4"
"version": "1.2.0"
}
11 changes: 7 additions & 4 deletions custom_components/binance/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
from homeassistant.components.sensor import PLATFORM_SCHEMA
from .const import (
CONF_SYMBOLS,
CONF_DECIMALS
CONF_DECIMALS,
CONF_UPDATE_INVERVAL
)

from .binance_ticker_sensor import BinanceTickerSensor
Expand All @@ -17,14 +18,16 @@
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
{
vol.Required(CONF_SYMBOLS): vol.All(cv.ensure_list, [cv.string]),
vol.Optional(CONF_DECIMALS, default=8): cv.positive_int
vol.Optional(CONF_DECIMALS, default=8): cv.positive_int,
vol.Optional(CONF_UPDATE_INVERVAL, default=60): cv.positive_int
}
)

def setup_platform(hass, config, add_entities, discovery_info=None):
symbols = config.get(CONF_SYMBOLS)
decimals = config.get(CONF_DECIMALS)
updateInterval = config.get(CONF_UPDATE_INVERVAL)

for symbol in symbols:
logger.debug("Setup BinanceTickerSensor %s %s", symbol, decimals)
add_entities([BinanceTickerSensor(symbol, decimals)], True)
logger.debug("Setup BinanceTickerSensor %s %s %s", symbol, decimals, updateInterval)
add_entities([BinanceTickerSensor(symbol, decimals, updateInterval)], True)

0 comments on commit bf8a29c

Please sign in to comment.