2019-02-19 13:09:06 +00:00
|
|
|
"""Support for Ripple sensors."""
|
2017-06-07 08:24:07 +00:00
|
|
|
from datetime import timedelta
|
|
|
|
|
2019-12-01 14:23:18 +00:00
|
|
|
from pyripple import get_balance
|
2017-06-07 08:24:07 +00:00
|
|
|
import voluptuous as vol
|
|
|
|
|
2021-03-22 18:54:14 +00:00
|
|
|
from homeassistant.components.sensor import PLATFORM_SCHEMA, SensorEntity
|
2019-02-19 13:09:06 +00:00
|
|
|
from homeassistant.const import ATTR_ATTRIBUTION, CONF_ADDRESS, CONF_NAME
|
|
|
|
import homeassistant.helpers.config_validation as cv
|
2017-06-07 08:24:07 +00:00
|
|
|
|
2019-02-14 21:09:22 +00:00
|
|
|
ATTRIBUTION = "Data provided by ripple.com"
|
2017-06-07 08:24:07 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
DEFAULT_NAME = "Ripple Balance"
|
2017-06-07 08:24:07 +00:00
|
|
|
|
|
|
|
SCAN_INTERVAL = timedelta(minutes=5)
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
|
|
|
{
|
|
|
|
vol.Required(CONF_ADDRESS): cv.string,
|
|
|
|
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
|
|
|
|
}
|
|
|
|
)
|
2017-06-07 08:24:07 +00:00
|
|
|
|
|
|
|
|
2018-08-24 14:37:30 +00:00
|
|
|
def setup_platform(hass, config, add_entities, discovery_info=None):
|
2017-06-07 08:24:07 +00:00
|
|
|
"""Set up the Ripple.com sensors."""
|
|
|
|
address = config.get(CONF_ADDRESS)
|
|
|
|
name = config.get(CONF_NAME)
|
|
|
|
|
2018-08-24 14:37:30 +00:00
|
|
|
add_entities([RippleSensor(name, address)], True)
|
2017-06-07 08:24:07 +00:00
|
|
|
|
|
|
|
|
2021-03-22 18:54:14 +00:00
|
|
|
class RippleSensor(SensorEntity):
|
2017-06-07 08:24:07 +00:00
|
|
|
"""Representation of an Ripple.com sensor."""
|
|
|
|
|
|
|
|
def __init__(self, name, address):
|
|
|
|
"""Initialize the sensor."""
|
|
|
|
self._name = name
|
|
|
|
self.address = address
|
|
|
|
self._state = None
|
2019-07-31 19:25:30 +00:00
|
|
|
self._unit_of_measurement = "XRP"
|
2017-06-07 08:24:07 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
|
|
|
"""Return the name of the sensor."""
|
|
|
|
return self._name
|
|
|
|
|
|
|
|
@property
|
|
|
|
def state(self):
|
|
|
|
"""Return the state of the sensor."""
|
|
|
|
return self._state
|
|
|
|
|
|
|
|
@property
|
|
|
|
def unit_of_measurement(self):
|
|
|
|
"""Return the unit of measurement this sensor expresses itself in."""
|
|
|
|
return self._unit_of_measurement
|
|
|
|
|
|
|
|
@property
|
2021-03-11 20:23:20 +00:00
|
|
|
def extra_state_attributes(self):
|
2017-06-07 08:24:07 +00:00
|
|
|
"""Return the state attributes of the sensor."""
|
2019-07-31 19:25:30 +00:00
|
|
|
return {ATTR_ATTRIBUTION: ATTRIBUTION}
|
2017-06-07 08:24:07 +00:00
|
|
|
|
|
|
|
def update(self):
|
|
|
|
"""Get the latest state of the sensor."""
|
2019-07-31 19:25:30 +00:00
|
|
|
|
2017-12-13 20:21:14 +00:00
|
|
|
balance = get_balance(self.address)
|
|
|
|
if balance is not None:
|
|
|
|
self._state = balance
|