Home Assistant Unofficial Reference 2024.12.1
sensor.py
Go to the documentation of this file.
1 """Support for Ripple sensors."""
2 
3 from __future__ import annotations
4 
5 from datetime import timedelta
6 
7 from pyripple import get_balance
8 import voluptuous as vol
9 
11  PLATFORM_SCHEMA as SENSOR_PLATFORM_SCHEMA,
12  SensorEntity,
13 )
14 from homeassistant.const import CONF_ADDRESS, CONF_NAME
15 from homeassistant.core import HomeAssistant
17 from homeassistant.helpers.entity_platform import AddEntitiesCallback
18 from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
19 
20 DEFAULT_NAME = "Ripple Balance"
21 
22 SCAN_INTERVAL = timedelta(minutes=5)
23 
24 PLATFORM_SCHEMA = SENSOR_PLATFORM_SCHEMA.extend(
25  {
26  vol.Required(CONF_ADDRESS): cv.string,
27  vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
28  }
29 )
30 
31 
33  hass: HomeAssistant,
34  config: ConfigType,
35  add_entities: AddEntitiesCallback,
36  discovery_info: DiscoveryInfoType | None = None,
37 ) -> None:
38  """Set up the Ripple.com sensors."""
39  address = config.get(CONF_ADDRESS)
40  name = config.get(CONF_NAME)
41 
42  add_entities([RippleSensor(name, address)], True)
43 
44 
46  """Representation of an Ripple.com sensor."""
47 
48  _attr_attribution = "Data provided by ripple.com"
49 
50  def __init__(self, name, address):
51  """Initialize the sensor."""
52  self._name_name = name
53  self.addressaddress = address
54  self._state_state = None
55  self._unit_of_measurement_unit_of_measurement = "XRP"
56 
57  @property
58  def name(self):
59  """Return the name of the sensor."""
60  return self._name_name
61 
62  @property
63  def native_value(self):
64  """Return the state of the sensor."""
65  return self._state_state
66 
67  @property
69  """Return the unit of measurement this sensor expresses itself in."""
70  return self._unit_of_measurement_unit_of_measurement
71 
72  def update(self) -> None:
73  """Get the latest state of the sensor."""
74  if (balance := get_balance(self.addressaddress)) is not None:
75  self._state_state = balance
def add_entities(account, async_add_entities, tracked)
Definition: sensor.py:40
None setup_platform(HomeAssistant hass, ConfigType config, AddEntitiesCallback add_entities, DiscoveryInfoType|None discovery_info=None)
Definition: sensor.py:37