Home Assistant Unofficial Reference 2024.12.1
binary_sensor.py
Go to the documentation of this file.
1 """Support for Ecobee binary sensors."""
2 
3 from __future__ import annotations
4 
6  BinarySensorDeviceClass,
7  BinarySensorEntity,
8 )
9 from homeassistant.config_entries import ConfigEntry
10 from homeassistant.core import HomeAssistant
11 from homeassistant.helpers.device_registry import DeviceInfo
12 from homeassistant.helpers.entity_platform import AddEntitiesCallback
13 
14 from .const import DOMAIN, ECOBEE_MODEL_TO_NAME, MANUFACTURER
15 
16 
18  hass: HomeAssistant,
19  config_entry: ConfigEntry,
20  async_add_entities: AddEntitiesCallback,
21 ) -> None:
22  """Set up ecobee binary (occupancy) sensors."""
23  data = hass.data[DOMAIN]
24  dev = []
25  for index in range(len(data.ecobee.thermostats)):
26  for sensor in data.ecobee.get_remote_sensors(index):
27  for item in sensor["capability"]:
28  if item["type"] != "occupancy":
29  continue
30 
31  dev.append(EcobeeBinarySensor(data, sensor["name"], index))
32 
33  async_add_entities(dev, True)
34 
35 
37  """Representation of an Ecobee sensor."""
38 
39  _attr_device_class = BinarySensorDeviceClass.OCCUPANCY
40  _attr_has_entity_name = True
41 
42  def __init__(self, data, sensor_name, sensor_index):
43  """Initialize the Ecobee sensor."""
44  self.datadata = data
45  self.sensor_namesensor_name = sensor_name
46  self.indexindex = sensor_index
47 
48  @property
49  def unique_id(self) -> str | None:
50  """Return a unique identifier for this sensor."""
51  for sensor in self.datadata.ecobee.get_remote_sensors(self.indexindex):
52  if sensor["name"] == self.sensor_namesensor_name:
53  if "code" in sensor:
54  return f"{sensor['code']}-{self.device_class}"
55  thermostat = self.datadata.ecobee.get_thermostat(self.indexindex)
56  return f"{thermostat['identifier']}-{sensor['id']}-{self.device_class}"
57  return None
58 
59  @property
60  def device_info(self) -> DeviceInfo | None:
61  """Return device information for this sensor."""
62  identifier = None
63  model = None
64  for sensor in self.datadata.ecobee.get_remote_sensors(self.indexindex):
65  if sensor["name"] != self.sensor_namesensor_name:
66  continue
67  if "code" in sensor:
68  identifier = sensor["code"]
69  model = "ecobee Room Sensor"
70  else:
71  thermostat = self.datadata.ecobee.get_thermostat(self.indexindex)
72  identifier = thermostat["identifier"]
73  try:
74  model = (
75  f"{ECOBEE_MODEL_TO_NAME[thermostat['modelNumber']]} Thermostat"
76  )
77  except KeyError:
78  # Ecobee model is not in our list
79  model = None
80  break
81 
82  if identifier is not None:
83  return DeviceInfo(
84  identifiers={(DOMAIN, identifier)},
85  manufacturer=MANUFACTURER,
86  model=model,
87  name=self.sensor_namesensor_name,
88  )
89  return None
90 
91  @property
92  def available(self) -> bool:
93  """Return true if device is available."""
94  thermostat = self.datadata.ecobee.get_thermostat(self.indexindex)
95  return thermostat["runtime"]["connected"]
96 
97  async def async_update(self) -> None:
98  """Get the latest state of the sensor."""
99  await self.datadata.update()
100  for sensor in self.datadata.ecobee.get_remote_sensors(self.indexindex):
101  if sensor["name"] != self.sensor_namesensor_name:
102  continue
103  for item in sensor["capability"]:
104  if item["type"] != "occupancy":
105  continue
106  self._attr_is_on_attr_is_on = item["value"] == "true"
107  break
def __init__(self, data, sensor_name, sensor_index)
None async_setup_entry(HomeAssistant hass, ConfigEntry config_entry, AddEntitiesCallback async_add_entities)
IssData update(pyiss.ISS iss)
Definition: __init__.py:33