Home Assistant Unofficial Reference 2024.12.1
binary_sensor.py
Go to the documentation of this file.
1 """A sensor platform which detects underruns and capped status from the official Raspberry Pi Kernel.
2 
3 Minimal Kernel needed is 4.14+
4 """
5 
6 import logging
7 
8 from rpi_bad_power import UnderVoltage, new_under_voltage
9 
11  BinarySensorDeviceClass,
12  BinarySensorEntity,
13 )
14 from homeassistant.config_entries import ConfigEntry
15 from homeassistant.const import EntityCategory
16 from homeassistant.core import HomeAssistant
17 from homeassistant.helpers.entity_platform import AddEntitiesCallback
18 
19 _LOGGER = logging.getLogger(__name__)
20 
21 DESCRIPTION_NORMALIZED = "Voltage normalized. Everything is working as intended."
22 DESCRIPTION_UNDER_VOLTAGE = (
23  "Under-voltage was detected. Consider getting a uninterruptible power supply for"
24  " your Raspberry Pi."
25 )
26 
27 
29  hass: HomeAssistant,
30  config_entry: ConfigEntry,
31  async_add_entities: AddEntitiesCallback,
32 ) -> None:
33  """Set up rpi_power binary sensor."""
34  under_voltage = await hass.async_add_executor_job(new_under_voltage)
35  async_add_entities([RaspberryChargerBinarySensor(under_voltage)], True)
36 
37 
39  """Binary sensor representing the rpi power status."""
40 
41  _attr_device_class = BinarySensorDeviceClass.PROBLEM
42  _attr_entity_category = EntityCategory.DIAGNOSTIC
43  _attr_icon = "mdi:raspberry-pi"
44  _attr_name = "RPi Power status"
45  _attr_unique_id = "rpi_power" # only one sensor possible
46 
47  def __init__(self, under_voltage: UnderVoltage) -> None:
48  """Initialize the binary sensor."""
49  self._under_voltage_under_voltage = under_voltage
50 
51  def update(self) -> None:
52  """Update the state."""
53  value = self._under_voltage_under_voltage.get()
54  if self._attr_is_on_attr_is_on != value:
55  if value:
56  _LOGGER.warning(DESCRIPTION_UNDER_VOLTAGE)
57  else:
58  _LOGGER.debug(DESCRIPTION_NORMALIZED)
59  self._attr_is_on_attr_is_on = value
web.Response get(self, web.Request request, str config_key)
Definition: view.py:88
None async_setup_entry(HomeAssistant hass, ConfigEntry config_entry, AddEntitiesCallback async_add_entities)