1 """Support for monitoring the state of Vultr Subscriptions."""
3 from __future__
import annotations
7 import voluptuous
as vol
10 PLATFORM_SCHEMA
as SENSOR_PLATFORM_SCHEMA,
13 SensorEntityDescription,
22 ATTR_CURRENT_BANDWIDTH_USED,
28 _LOGGER = logging.getLogger(__name__)
30 DEFAULT_NAME =
"Vultr {} {}"
31 SENSOR_TYPES: tuple[SensorEntityDescription, ...] = (
33 key=ATTR_CURRENT_BANDWIDTH_USED,
34 name=
"Current Bandwidth Used",
35 native_unit_of_measurement=UnitOfInformation.GIGABYTES,
36 device_class=SensorDeviceClass.DATA_SIZE,
37 icon=
"mdi:chart-histogram",
40 key=ATTR_PENDING_CHARGES,
41 name=
"Pending Charges",
42 native_unit_of_measurement=
"US$",
43 icon=
"mdi:currency-usd",
46 SENSOR_KEYS: list[str] = [desc.key
for desc
in SENSOR_TYPES]
48 PLATFORM_SCHEMA = SENSOR_PLATFORM_SCHEMA.extend(
50 vol.Required(CONF_SUBSCRIPTION): cv.string,
51 vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
52 vol.Optional(CONF_MONITORED_CONDITIONS, default=SENSOR_KEYS): vol.All(
53 cv.ensure_list, [vol.In(SENSOR_KEYS)]
62 add_entities: AddEntitiesCallback,
63 discovery_info: DiscoveryInfoType |
None =
None,
65 """Set up the Vultr subscription (server) sensor."""
66 vultr = hass.data[DATA_VULTR]
68 subscription = config[CONF_SUBSCRIPTION]
69 name = config[CONF_NAME]
70 monitored_conditions = config[CONF_MONITORED_CONDITIONS]
72 if subscription
not in vultr.data:
73 _LOGGER.error(
"Subscription %s not found", subscription)
78 for description
in SENSOR_TYPES
79 if description.key
in monitored_conditions
86 """Representation of a Vultr subscription sensor."""
89 self, vultr, subscription, name, description: SensorEntityDescription
91 """Initialize a new Vultr sensor."""
101 """Return the name of the sensor."""
106 return self.
_name_name.format(
109 except (KeyError, TypeError):
110 return self.
_name_name
114 """Return the value of this given sensor type."""
117 except (TypeError, ValueError):
121 """Update state of sensor."""
None __init__(self, vultr, subscription, name, SensorEntityDescription description)
web.Response get(self, web.Request request, str config_key)
def add_entities(account, async_add_entities, tracked)
None setup_platform(HomeAssistant hass, ConfigType config, AddEntitiesCallback add_entities, DiscoveryInfoType|None discovery_info=None)