Home Assistant Unofficial Reference 2024.12.1
binary_sensor.py
Go to the documentation of this file.
1 """Amber Electric Binary Sensor definitions."""
2 
3 from __future__ import annotations
4 
5 from typing import Any
6 
8  BinarySensorEntity,
9  BinarySensorEntityDescription,
10 )
11 from homeassistant.core import HomeAssistant
12 from homeassistant.helpers.entity_platform import AddEntitiesCallback
13 from homeassistant.helpers.update_coordinator import CoordinatorEntity
14 
15 from . import AmberConfigEntry
16 from .const import ATTRIBUTION
17 from .coordinator import AmberUpdateCoordinator
18 
19 PRICE_SPIKE_ICONS = {
20  "none": "mdi:power-plug",
21  "potential": "mdi:power-plug-outline",
22  "spike": "mdi:power-plug-off",
23 }
24 
25 
27  CoordinatorEntity[AmberUpdateCoordinator], BinarySensorEntity
28 ):
29  """Sensor to show single grid binary values."""
30 
31  _attr_attribution = ATTRIBUTION
32 
33  def __init__(
34  self,
35  coordinator: AmberUpdateCoordinator,
36  description: BinarySensorEntityDescription,
37  ) -> None:
38  """Initialize the Sensor."""
39  super().__init__(coordinator)
40  self.site_idsite_idsite_id = coordinator.site_id
41  self.entity_descriptionentity_description = description
42  self._attr_unique_id_attr_unique_id = f"{coordinator.site_id}-{description.key}"
43 
44  @property
45  def is_on(self) -> bool | None:
46  """Return true if the binary sensor is on."""
47  return self.coordinator.data["grid"][self.entity_descriptionentity_description.key] # type: ignore[no-any-return]
48 
49 
51  """Sensor to show single grid binary values."""
52 
53  @property
54  def icon(self) -> str:
55  """Return the sensor icon."""
56  status = self.coordinator.data["grid"]["price_spike"]
57  return PRICE_SPIKE_ICONS[status]
58 
59  @property
60  def is_on(self) -> bool | None:
61  """Return true if the binary sensor is on."""
62  return self.coordinator.data["grid"]["price_spike"] == "spike" # type: ignore[no-any-return]
63 
64  @property
65  def extra_state_attributes(self) -> dict[str, Any]:
66  """Return additional pieces of information about the price spike."""
67 
68  spike_status = self.coordinator.data["grid"]["price_spike"]
69  return {
70  "spike_status": spike_status,
71  }
72 
73 
75  """Sensor to show whether demand window is active."""
76 
77  @property
78  def is_on(self) -> bool | None:
79  """Return true if the binary sensor is on."""
80  grid = self.coordinator.data["grid"]
81  if "demand_window" in grid:
82  return grid["demand_window"] # type: ignore[no-any-return]
83  return None
84 
85 
87  hass: HomeAssistant,
88  entry: AmberConfigEntry,
89  async_add_entities: AddEntitiesCallback,
90 ) -> None:
91  """Set up a config entry."""
92  coordinator = entry.runtime_data
93 
94  price_spike_description = BinarySensorEntityDescription(
95  key="price_spike",
96  name=f"{entry.title} - Price Spike",
97  )
98  demand_window_description = BinarySensorEntityDescription(
99  key="demand_window",
100  name=f"{entry.title} - Demand Window",
101  translation_key="demand_window",
102  )
104  [
105  AmberPriceSpikeBinarySensor(coordinator, price_spike_description),
106  AmberDemandWindowBinarySensor(coordinator, demand_window_description),
107  ]
108  )
None __init__(self, AmberUpdateCoordinator coordinator, BinarySensorEntityDescription description)
None async_setup_entry(HomeAssistant hass, AmberConfigEntry entry, AddEntitiesCallback async_add_entities)