Home Assistant Unofficial Reference 2024.12.1
binary_sensor.py
Go to the documentation of this file.
1 """The Minecraft Server binary sensor platform."""
2 
4  BinarySensorDeviceClass,
5  BinarySensorEntity,
6  BinarySensorEntityDescription,
7 )
8 from homeassistant.config_entries import ConfigEntry
9 from homeassistant.core import HomeAssistant
10 from homeassistant.helpers.entity_platform import AddEntitiesCallback
11 
12 from .const import DOMAIN
13 from .coordinator import MinecraftServerCoordinator
14 from .entity import MinecraftServerEntity
15 
16 KEY_STATUS = "status"
17 
18 
19 BINARY_SENSOR_DESCRIPTIONS = [
21  key=KEY_STATUS,
22  translation_key=KEY_STATUS,
23  device_class=BinarySensorDeviceClass.CONNECTIVITY,
24  ),
25 ]
26 
27 
29  hass: HomeAssistant,
30  config_entry: ConfigEntry,
31  async_add_entities: AddEntitiesCallback,
32 ) -> None:
33  """Set up the Minecraft Server binary sensor platform."""
34  coordinator = hass.data[DOMAIN][config_entry.entry_id]
35 
36  # Add binary sensor entities.
38  [
39  MinecraftServerBinarySensorEntity(coordinator, description, config_entry)
40  for description in BINARY_SENSOR_DESCRIPTIONS
41  ]
42  )
43 
44 
46  """Representation of a Minecraft Server binary sensor base entity."""
47 
48  def __init__(
49  self,
50  coordinator: MinecraftServerCoordinator,
51  description: BinarySensorEntityDescription,
52  config_entry: ConfigEntry,
53  ) -> None:
54  """Initialize binary sensor base entity."""
55  super().__init__(coordinator, config_entry)
56  self.entity_descriptionentity_description = description
57  self._attr_unique_id_attr_unique_id = f"{config_entry.entry_id}-{description.key}"
58  self._attr_is_on_attr_is_on = False
59 
60  @property
61  def available(self) -> bool:
62  """Return binary sensor availability."""
63  return True
64 
65  @property
66  def is_on(self) -> bool:
67  """Return binary sensor state."""
68  return self.coordinator.last_update_success
None __init__(self, MinecraftServerCoordinator coordinator, BinarySensorEntityDescription description, ConfigEntry config_entry)
None async_setup_entry(HomeAssistant hass, ConfigEntry config_entry, AddEntitiesCallback async_add_entities)