Home Assistant Unofficial Reference 2024.12.1
binary_sensor.py
Go to the documentation of this file.
1 """Support for powerwall binary sensors."""
2 
3 from typing import TYPE_CHECKING
4 
5 from tesla_powerwall import GridStatus, MeterType
6 
8  BinarySensorDeviceClass,
9  BinarySensorEntity,
10 )
11 from homeassistant.core import HomeAssistant
12 from homeassistant.helpers.entity_platform import AddEntitiesCallback
13 
14 from .entity import PowerWallEntity
15 from .models import PowerwallConfigEntry
16 
17 CONNECTED_GRID_STATUSES = {
18  GridStatus.TRANSITION_TO_GRID,
19  GridStatus.CONNECTED,
20 }
21 
22 
24  hass: HomeAssistant,
25  entry: PowerwallConfigEntry,
26  async_add_entities: AddEntitiesCallback,
27 ) -> None:
28  """Set up the powerwall sensors."""
29  powerwall_data = entry.runtime_data
31  [
32  sensor_class(powerwall_data)
33  for sensor_class in (
34  PowerWallRunningSensor,
35  PowerWallGridServicesActiveSensor,
36  PowerWallGridStatusSensor,
37  PowerWallConnectedSensor,
38  PowerWallChargingStatusSensor,
39  )
40  ]
41  )
42 
43 
45  """Representation of an Powerwall running sensor."""
46 
47  _attr_translation_key = "status"
48  _attr_device_class = BinarySensorDeviceClass.POWER
49 
50  @property
51  def unique_id(self) -> str:
52  """Device Uniqueid."""
53  return f"{self.base_unique_id}_running"
54 
55  @property
56  def is_on(self) -> bool:
57  """Get the powerwall running state."""
58  return self.datadatadata.site_master.is_running
59 
60 
62  """Representation of an Powerwall connected sensor."""
63 
64  _attr_translation_key = "connected_to_tesla"
65  _attr_device_class = BinarySensorDeviceClass.CONNECTIVITY
66 
67  @property
68  def unique_id(self) -> str:
69  """Device Uniqueid."""
70  return f"{self.base_unique_id}_connected_to_tesla"
71 
72  @property
73  def is_on(self) -> bool:
74  """Get the powerwall connected to tesla state."""
75  return self.datadatadata.site_master.is_connected_to_tesla
76 
77 
79  """Representation of a Powerwall grid services active sensor."""
80 
81  _attr_translation_key = "grid_services_active"
82  _attr_device_class = BinarySensorDeviceClass.POWER
83 
84  @property
85  def unique_id(self) -> str:
86  """Device Uniqueid."""
87  return f"{self.base_unique_id}_grid_services_active"
88 
89  @property
90  def is_on(self) -> bool:
91  """Grid services is active."""
92  return self.datadatadata.grid_services_active
93 
94 
96  """Representation of an Powerwall grid status sensor."""
97 
98  _attr_translation_key = "grid_status"
99  _attr_device_class = BinarySensorDeviceClass.POWER
100 
101  @property
102  def unique_id(self) -> str:
103  """Device Uniqueid."""
104  return f"{self.base_unique_id}_grid_status"
105 
106  @property
107  def is_on(self) -> bool:
108  """Grid is online."""
109  return self.datadatadata.grid_status in CONNECTED_GRID_STATUSES
110 
111 
113  """Representation of an Powerwall charging status sensor."""
114 
115  _attr_device_class = BinarySensorDeviceClass.BATTERY_CHARGING
116 
117  @property
118  def available(self) -> bool:
119  """Powerwall is available."""
120  # Return False if no battery is installed
121  return (
122  super().available
123  and self.datadatadata.meters.get_meter(MeterType.BATTERY) is not None
124  )
125 
126  @property
127  def unique_id(self) -> str:
128  """Device Uniqueid."""
129  return f"{self.base_unique_id}_powerwall_charging"
130 
131  @property
132  def is_on(self) -> bool:
133  """Powerwall is charging."""
134  meter = self.datadatadata.meters.get_meter(MeterType.BATTERY)
135  # Meter cannot be None because of the available property
136  if TYPE_CHECKING:
137  assert meter is not None
138  # is_sending_to returns true for values greater than 100 watts
139  return meter.is_sending_to()
None async_setup_entry(HomeAssistant hass, PowerwallConfigEntry entry, AddEntitiesCallback async_add_entities)