Home Assistant Unofficial Reference 2024.12.1
sensor.py
Go to the documentation of this file.
1 """Pushbullet platform for sensor component."""
2 
3 from __future__ import annotations
4 
5 from homeassistant.components.sensor import SensorEntity, SensorEntityDescription
6 from homeassistant.config_entries import ConfigEntry
7 from homeassistant.const import CONF_NAME
8 from homeassistant.core import HomeAssistant, callback
9 from homeassistant.helpers.device_registry import DeviceEntryType, DeviceInfo
10 from homeassistant.helpers.dispatcher import async_dispatcher_connect
11 from homeassistant.helpers.entity_platform import AddEntitiesCallback
12 
13 from .api import PushBulletNotificationProvider
14 from .const import DATA_UPDATED, DOMAIN
15 
16 SENSOR_TYPES: tuple[SensorEntityDescription, ...] = (
18  key="application_name",
19  translation_key="application_name",
20  entity_registry_enabled_default=False,
21  ),
23  key="body",
24  translation_key="body",
25  ),
27  key="notification_id",
28  translation_key="notification_id",
29  entity_registry_enabled_default=False,
30  ),
32  key="notification_tag",
33  translation_key="notification_tag",
34  entity_registry_enabled_default=False,
35  ),
37  key="package_name",
38  translation_key="package_name",
39  entity_registry_enabled_default=False,
40  ),
42  key="receiver_email",
43  translation_key="receiver_email",
44  entity_registry_enabled_default=False,
45  ),
47  key="sender_email",
48  translation_key="sender_email",
49  entity_registry_enabled_default=False,
50  ),
52  key="source_device_iden",
53  translation_key="source_device_identifier",
54  entity_registry_enabled_default=False,
55  ),
57  key="title",
58  translation_key="title",
59  ),
61  key="type",
62  translation_key="type",
63  entity_registry_enabled_default=False,
64  ),
65 )
66 
67 SENSOR_KEYS: list[str] = [desc.key for desc in SENSOR_TYPES]
68 
69 
71  hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
72 ) -> None:
73  """Set up the Pushbullet sensors from config entry."""
74 
75  pb_provider: PushBulletNotificationProvider = hass.data[DOMAIN][entry.entry_id]
76 
77  entities = [
78  PushBulletNotificationSensor(entry.data[CONF_NAME], pb_provider, description)
79  for description in SENSOR_TYPES
80  ]
81 
82  async_add_entities(entities)
83 
84 
86  """Representation of a Pushbullet Sensor."""
87 
88  _attr_should_poll = False
89  _attr_has_entity_name = True
90 
91  def __init__(
92  self,
93  name: str,
94  pb_provider: PushBulletNotificationProvider,
95  description: SensorEntityDescription,
96  ) -> None:
97  """Initialize the Pushbullet sensor."""
98  self.entity_descriptionentity_description = description
99  self.pb_providerpb_provider = pb_provider
100  self._attr_unique_id_attr_unique_id = (
101  f"{pb_provider.pushbullet.user_info['iden']}-{description.key}"
102  )
103  self._attr_device_info_attr_device_info = DeviceInfo(
104  identifiers={(DOMAIN, pb_provider.pushbullet.user_info["iden"])},
105  name=name,
106  entry_type=DeviceEntryType.SERVICE,
107  )
108 
109  @callback
110  def async_update_callback(self) -> None:
111  """Fetch the latest data from the sensor.
112 
113  This will fetch the 'sensor reading' into self._state but also all
114  attributes into self._state_attributes.
115  """
116  try:
117  self._attr_native_value_attr_native_value = self.pb_providerpb_provider.data[self.entity_descriptionentity_description.key]
118  self._attr_extra_state_attributes_attr_extra_state_attributes = self.pb_providerpb_provider.data
119  except (KeyError, TypeError):
120  pass
121  self.async_write_ha_stateasync_write_ha_state()
122 
123  async def async_added_to_hass(self) -> None:
124  """Register callbacks."""
125  self.async_on_removeasync_on_remove(
127  self.hasshass, DATA_UPDATED, self.async_update_callbackasync_update_callback
128  )
129  )
None __init__(self, str name, PushBulletNotificationProvider pb_provider, SensorEntityDescription description)
Definition: sensor.py:96
None async_on_remove(self, CALLBACK_TYPE func)
Definition: entity.py:1331
None async_setup_entry(HomeAssistant hass, ConfigEntry entry, AddEntitiesCallback async_add_entities)
Definition: sensor.py:72
Callable[[], None] async_dispatcher_connect(HomeAssistant hass, str signal, Callable[..., Any] target)
Definition: dispatcher.py:103