core/homeassistant/components/ripple/sensor.py

69 lines
1.8 KiB
Python
Raw Normal View History

"""Support for Ripple sensors."""
2017-06-07 08:24:07 +00:00
from datetime import timedelta
from pyripple import get_balance
2017-06-07 08:24:07 +00:00
import voluptuous as vol
from homeassistant.components.sensor import PLATFORM_SCHEMA, SensorEntity
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
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
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)
add_entities([RippleSensor(name, address)], True)
2017-06-07 08:24:07 +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
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