Home Assistant Unofficial Reference 2024.12.1
sensor.py
Go to the documentation of this file.
1 """Sensor support for Skybell Doorbells."""
2 
3 from __future__ import annotations
4 
5 from collections.abc import Callable
6 from dataclasses import dataclass
7 from datetime import datetime
8 
9 from aioskybell import SkybellDevice
10 from aioskybell.helpers import const as CONST
11 
13  SensorDeviceClass,
14  SensorEntity,
15  SensorEntityDescription,
16 )
17 from homeassistant.config_entries import ConfigEntry
18 from homeassistant.const import EntityCategory
19 from homeassistant.core import HomeAssistant
20 from homeassistant.helpers.entity_platform import AddEntitiesCallback
21 from homeassistant.helpers.typing import StateType
22 
23 from .entity import DOMAIN, SkybellEntity
24 
25 
26 @dataclass(frozen=True, kw_only=True)
28  """Class to describe a Skybell sensor."""
29 
30  value_fn: Callable[[SkybellDevice], StateType | datetime]
31 
32 
33 SENSOR_TYPES: tuple[SkybellSensorEntityDescription, ...] = (
35  key="chime_level",
36  translation_key="chime_level",
37  value_fn=lambda device: device.outdoor_chime_level,
38  ),
40  key="last_button_event",
41  translation_key="last_button_event",
42  device_class=SensorDeviceClass.TIMESTAMP,
43  value_fn=lambda device: device.latest("button").get(CONST.CREATED_AT),
44  ),
46  key="last_motion_event",
47  translation_key="last_motion_event",
48  device_class=SensorDeviceClass.TIMESTAMP,
49  value_fn=lambda device: device.latest("motion").get(CONST.CREATED_AT),
50  ),
52  key=CONST.ATTR_LAST_CHECK_IN,
53  translation_key="last_check_in",
54  entity_registry_enabled_default=False,
55  device_class=SensorDeviceClass.TIMESTAMP,
56  entity_category=EntityCategory.DIAGNOSTIC,
57  value_fn=lambda device: device.last_check_in,
58  ),
60  key="motion_threshold",
61  translation_key="motion_threshold",
62  entity_registry_enabled_default=False,
63  entity_category=EntityCategory.DIAGNOSTIC,
64  value_fn=lambda device: device.motion_threshold,
65  ),
67  key="video_profile",
68  translation_key="video_profile",
69  entity_registry_enabled_default=False,
70  entity_category=EntityCategory.DIAGNOSTIC,
71  value_fn=lambda device: device.video_profile,
72  ),
74  key=CONST.ATTR_WIFI_SSID,
75  translation_key="wifi_ssid",
76  entity_registry_enabled_default=False,
77  entity_category=EntityCategory.DIAGNOSTIC,
78  value_fn=lambda device: device.wifi_ssid,
79  ),
81  key=CONST.ATTR_WIFI_STATUS,
82  translation_key="wifi_status",
83  entity_registry_enabled_default=False,
84  entity_category=EntityCategory.DIAGNOSTIC,
85  value_fn=lambda device: device.wifi_status,
86  ),
87 )
88 
89 
91  hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
92 ) -> None:
93  """Set up Skybell sensor."""
95  SkybellSensor(coordinator, description)
96  for coordinator in hass.data[DOMAIN][entry.entry_id]
97  for description in SENSOR_TYPES
98  if coordinator.device.owner or description.key not in CONST.ATTR_OWNER_STATS
99  )
100 
101 
103  """A sensor implementation for Skybell devices."""
104 
105  entity_description: SkybellSensorEntityDescription
106 
107  @property
108  def native_value(self) -> StateType | datetime:
109  """Return the state of the sensor."""
110  return self.entity_descriptionentity_description.value_fn(self._device_device)
web.Response get(self, web.Request request, str config_key)
Definition: view.py:88
None async_setup_entry(HomeAssistant hass, ConfigEntry entry, AddEntitiesCallback async_add_entities)
Definition: sensor.py:92