Use EntityDescription - pushbullet (#54999)
parent
99465f53c7
commit
20f94d7ad4
|
@ -1,34 +1,72 @@
|
||||||
"""Pushbullet platform for sensor component."""
|
"""Pushbullet platform for sensor component."""
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
import logging
|
import logging
|
||||||
import threading
|
import threading
|
||||||
|
|
||||||
from pushbullet import InvalidKeyError, Listener, PushBullet
|
from pushbullet import InvalidKeyError, Listener, PushBullet
|
||||||
import voluptuous as vol
|
import voluptuous as vol
|
||||||
|
|
||||||
from homeassistant.components.sensor import PLATFORM_SCHEMA, SensorEntity
|
from homeassistant.components.sensor import (
|
||||||
|
PLATFORM_SCHEMA,
|
||||||
|
SensorEntity,
|
||||||
|
SensorEntityDescription,
|
||||||
|
)
|
||||||
from homeassistant.const import CONF_API_KEY, CONF_MONITORED_CONDITIONS
|
from homeassistant.const import CONF_API_KEY, CONF_MONITORED_CONDITIONS
|
||||||
import homeassistant.helpers.config_validation as cv
|
import homeassistant.helpers.config_validation as cv
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
SENSOR_TYPES = {
|
SENSOR_TYPES: tuple[SensorEntityDescription, ...] = (
|
||||||
"application_name": ["Application name"],
|
SensorEntityDescription(
|
||||||
"body": ["Body"],
|
key="application_name",
|
||||||
"notification_id": ["Notification ID"],
|
name="Application name",
|
||||||
"notification_tag": ["Notification tag"],
|
),
|
||||||
"package_name": ["Package name"],
|
SensorEntityDescription(
|
||||||
"receiver_email": ["Receiver email"],
|
key="body",
|
||||||
"sender_email": ["Sender email"],
|
name="Body",
|
||||||
"source_device_iden": ["Sender device ID"],
|
),
|
||||||
"title": ["Title"],
|
SensorEntityDescription(
|
||||||
"type": ["Type"],
|
key="notification_id",
|
||||||
}
|
name="Notification ID",
|
||||||
|
),
|
||||||
|
SensorEntityDescription(
|
||||||
|
key="notification_tag",
|
||||||
|
name="Notification tag",
|
||||||
|
),
|
||||||
|
SensorEntityDescription(
|
||||||
|
key="package_name",
|
||||||
|
name="Package name",
|
||||||
|
),
|
||||||
|
SensorEntityDescription(
|
||||||
|
key="receiver_email",
|
||||||
|
name="Receiver email",
|
||||||
|
),
|
||||||
|
SensorEntityDescription(
|
||||||
|
key="sender_email",
|
||||||
|
name="Sender email",
|
||||||
|
),
|
||||||
|
SensorEntityDescription(
|
||||||
|
key="source_device_iden",
|
||||||
|
name="Sender device ID",
|
||||||
|
),
|
||||||
|
SensorEntityDescription(
|
||||||
|
key="title",
|
||||||
|
name="Title",
|
||||||
|
),
|
||||||
|
SensorEntityDescription(
|
||||||
|
key="type",
|
||||||
|
name="Type",
|
||||||
|
),
|
||||||
|
)
|
||||||
|
|
||||||
|
SENSOR_KEYS: list[str] = [desc.key for desc in SENSOR_TYPES]
|
||||||
|
|
||||||
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
||||||
{
|
{
|
||||||
vol.Required(CONF_API_KEY): cv.string,
|
vol.Required(CONF_API_KEY): cv.string,
|
||||||
vol.Optional(CONF_MONITORED_CONDITIONS, default=["title", "body"]): vol.All(
|
vol.Optional(CONF_MONITORED_CONDITIONS, default=["title", "body"]): vol.All(
|
||||||
cv.ensure_list, vol.Length(min=1), [vol.In(SENSOR_TYPES)]
|
cv.ensure_list, vol.Length(min=1), [vol.In(SENSOR_KEYS)]
|
||||||
),
|
),
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
@ -45,21 +83,24 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
|
||||||
|
|
||||||
pbprovider = PushBulletNotificationProvider(pushbullet)
|
pbprovider = PushBulletNotificationProvider(pushbullet)
|
||||||
|
|
||||||
devices = []
|
monitored_conditions = config[CONF_MONITORED_CONDITIONS]
|
||||||
for sensor_type in config[CONF_MONITORED_CONDITIONS]:
|
entities = [
|
||||||
devices.append(PushBulletNotificationSensor(pbprovider, sensor_type))
|
PushBulletNotificationSensor(pbprovider, description)
|
||||||
add_entities(devices)
|
for description in SENSOR_TYPES
|
||||||
|
if description.key in monitored_conditions
|
||||||
|
]
|
||||||
|
add_entities(entities)
|
||||||
|
|
||||||
|
|
||||||
class PushBulletNotificationSensor(SensorEntity):
|
class PushBulletNotificationSensor(SensorEntity):
|
||||||
"""Representation of a Pushbullet Sensor."""
|
"""Representation of a Pushbullet Sensor."""
|
||||||
|
|
||||||
def __init__(self, pb, element):
|
def __init__(self, pb, description: SensorEntityDescription):
|
||||||
"""Initialize the Pushbullet sensor."""
|
"""Initialize the Pushbullet sensor."""
|
||||||
|
self.entity_description = description
|
||||||
self.pushbullet = pb
|
self.pushbullet = pb
|
||||||
self._element = element
|
|
||||||
self._state = None
|
self._attr_name = f"Pushbullet {description.key}"
|
||||||
self._state_attributes = None
|
|
||||||
|
|
||||||
def update(self):
|
def update(self):
|
||||||
"""Fetch the latest data from the sensor.
|
"""Fetch the latest data from the sensor.
|
||||||
|
@ -68,26 +109,11 @@ class PushBulletNotificationSensor(SensorEntity):
|
||||||
attributes into self._state_attributes.
|
attributes into self._state_attributes.
|
||||||
"""
|
"""
|
||||||
try:
|
try:
|
||||||
self._state = self.pushbullet.data[self._element]
|
self._attr_native_value = self.pushbullet.data[self.entity_description.key]
|
||||||
self._state_attributes = self.pushbullet.data
|
self._attr_extra_state_attributes = self.pushbullet.data
|
||||||
except (KeyError, TypeError):
|
except (KeyError, TypeError):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
@property
|
|
||||||
def name(self):
|
|
||||||
"""Return the name of the sensor."""
|
|
||||||
return f"Pushbullet {self._element}"
|
|
||||||
|
|
||||||
@property
|
|
||||||
def native_value(self):
|
|
||||||
"""Return the current state of the sensor."""
|
|
||||||
return self._state
|
|
||||||
|
|
||||||
@property
|
|
||||||
def extra_state_attributes(self):
|
|
||||||
"""Return all known attributes of the sensor."""
|
|
||||||
return self._state_attributes
|
|
||||||
|
|
||||||
|
|
||||||
class PushBulletNotificationProvider:
|
class PushBulletNotificationProvider:
|
||||||
"""Provider for an account, leading to one or more sensors."""
|
"""Provider for an account, leading to one or more sensors."""
|
||||||
|
|
Loading…
Reference in New Issue