Home Assistant Unofficial Reference 2024.12.1
binary_sensor.py
Go to the documentation of this file.
1 """Support for Samsung Printers with SyncThru web interface."""
2 
3 from __future__ import annotations
4 
5 from pysyncthru import SyncThru, SyncthruState
6 
8  BinarySensorDeviceClass,
9  BinarySensorEntity,
10 )
11 from homeassistant.config_entries import ConfigEntry
12 from homeassistant.const import CONF_NAME
13 from homeassistant.core import HomeAssistant
14 from homeassistant.helpers.device_registry import DeviceInfo
15 from homeassistant.helpers.entity_platform import AddEntitiesCallback
17  CoordinatorEntity,
18  DataUpdateCoordinator,
19 )
20 
21 from . import device_identifiers
22 from .const import DOMAIN
23 
24 SYNCTHRU_STATE_PROBLEM = {
25  SyncthruState.INVALID: True,
26  SyncthruState.OFFLINE: None,
27  SyncthruState.NORMAL: False,
28  SyncthruState.UNKNOWN: True,
29  SyncthruState.WARNING: True,
30  SyncthruState.TESTING: False,
31  SyncthruState.ERROR: True,
32 }
33 
34 
36  hass: HomeAssistant,
37  config_entry: ConfigEntry,
38  async_add_entities: AddEntitiesCallback,
39 ) -> None:
40  """Set up from config entry."""
41 
42  coordinator: DataUpdateCoordinator[SyncThru] = hass.data[DOMAIN][
43  config_entry.entry_id
44  ]
45 
46  name: str = config_entry.data[CONF_NAME]
47  entities = [
48  SyncThruOnlineSensor(coordinator, name),
49  SyncThruProblemSensor(coordinator, name),
50  ]
51 
52  async_add_entities(entities)
53 
54 
56  CoordinatorEntity[DataUpdateCoordinator[SyncThru]], BinarySensorEntity
57 ):
58  """Implementation of an abstract Samsung Printer binary sensor platform."""
59 
60  def __init__(self, coordinator: DataUpdateCoordinator[SyncThru], name: str) -> None:
61  """Initialize the sensor."""
62  super().__init__(coordinator)
63  self.syncthrusyncthru = coordinator.data
64  self._attr_name_attr_name = name
65  self._id_suffix_id_suffix = ""
66 
67  @property
68  def unique_id(self):
69  """Return unique ID for the sensor."""
70  serial = self.syncthrusyncthru.serial_number()
71  return f"{serial}{self._id_suffix}" if serial else None
72 
73  @property
74  def device_info(self) -> DeviceInfo | None:
75  """Return device information."""
76  if (identifiers := device_identifiers(self.syncthrusyncthru)) is None:
77  return None
78  return DeviceInfo(
79  identifiers=identifiers,
80  )
81 
82 
84  """Implementation of a sensor that checks whether is turned on/online."""
85 
86  _attr_device_class = BinarySensorDeviceClass.CONNECTIVITY
87 
88  def __init__(self, coordinator: DataUpdateCoordinator[SyncThru], name: str) -> None:
89  """Initialize the sensor."""
90  super().__init__(coordinator, name)
91  self._id_suffix_id_suffix_id_suffix = "_online"
92 
93  @property
94  def is_on(self):
95  """Set the state to whether the printer is online."""
96  return self.syncthrusyncthru.is_online()
97 
98 
100  """Implementation of a sensor that checks whether the printer works correctly."""
101 
102  _attr_device_class = BinarySensorDeviceClass.PROBLEM
103 
104  def __init__(self, syncthru, name):
105  """Initialize the sensor."""
106  super().__init__(syncthru, name)
107  self._id_suffix_id_suffix_id_suffix = "_problem"
108 
109  @property
110  def is_on(self):
111  """Set the state to whether there is a problem with the printer."""
112  return SYNCTHRU_STATE_PROBLEM[self.syncthrusyncthru.device_status()]
None __init__(self, DataUpdateCoordinator[SyncThru] coordinator, str name)
None __init__(self, DataUpdateCoordinator[SyncThru] coordinator, str name)
None async_setup_entry(HomeAssistant hass, ConfigEntry config_entry, AddEntitiesCallback async_add_entities)
set[tuple[str, str]]|None device_identifiers(SyncThru printer)
Definition: __init__.py:89