2019-02-13 20:21:14 +00:00
|
|
|
"""Support for Ecobee binary sensors."""
|
2024-03-08 13:15:26 +00:00
|
|
|
|
2021-10-23 09:44:51 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2019-09-25 20:38:21 +00:00
|
|
|
from homeassistant.components.binary_sensor import (
|
2021-12-10 07:55:57 +00:00
|
|
|
BinarySensorDeviceClass,
|
2020-04-23 19:57:07 +00:00
|
|
|
BinarySensorEntity,
|
2019-09-25 20:38:21 +00:00
|
|
|
)
|
2022-01-03 12:10:41 +00:00
|
|
|
from homeassistant.config_entries import ConfigEntry
|
|
|
|
from homeassistant.core import HomeAssistant
|
2023-08-11 02:04:26 +00:00
|
|
|
from homeassistant.helpers.device_registry import DeviceInfo
|
2022-01-03 12:10:41 +00:00
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
2016-08-19 07:11:56 +00:00
|
|
|
|
2021-06-17 08:59:13 +00:00
|
|
|
from .const import DOMAIN, ECOBEE_MODEL_TO_NAME, MANUFACTURER
|
2016-08-19 07:11:56 +00:00
|
|
|
|
|
|
|
|
2022-01-03 12:10:41 +00:00
|
|
|
async def async_setup_entry(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
config_entry: ConfigEntry,
|
|
|
|
async_add_entities: AddEntitiesCallback,
|
|
|
|
) -> None:
|
2019-09-25 20:38:21 +00:00
|
|
|
"""Set up ecobee binary (occupancy) sensors."""
|
|
|
|
data = hass.data[DOMAIN]
|
2020-04-04 21:14:47 +00:00
|
|
|
dev = []
|
2016-08-19 07:11:56 +00:00
|
|
|
for index in range(len(data.ecobee.thermostats)):
|
|
|
|
for sensor in data.ecobee.get_remote_sensors(index):
|
2019-07-31 19:25:30 +00:00
|
|
|
for item in sensor["capability"]:
|
|
|
|
if item["type"] != "occupancy":
|
2016-08-19 07:11:56 +00:00
|
|
|
continue
|
|
|
|
|
2019-09-25 20:38:21 +00:00
|
|
|
dev.append(EcobeeBinarySensor(data, sensor["name"], index))
|
2016-08-19 07:11:56 +00:00
|
|
|
|
2019-09-25 20:38:21 +00:00
|
|
|
async_add_entities(dev, True)
|
2016-08-19 07:11:56 +00:00
|
|
|
|
|
|
|
|
2020-04-23 19:57:07 +00:00
|
|
|
class EcobeeBinarySensor(BinarySensorEntity):
|
2016-08-19 07:11:56 +00:00
|
|
|
"""Representation of an Ecobee sensor."""
|
|
|
|
|
2023-06-26 21:12:48 +00:00
|
|
|
_attr_device_class = BinarySensorDeviceClass.OCCUPANCY
|
|
|
|
_attr_has_entity_name = True
|
|
|
|
|
2019-09-25 20:38:21 +00:00
|
|
|
def __init__(self, data, sensor_name, sensor_index):
|
2019-02-13 20:21:14 +00:00
|
|
|
"""Initialize the Ecobee sensor."""
|
2019-09-25 20:38:21 +00:00
|
|
|
self.data = data
|
2024-06-21 11:40:26 +00:00
|
|
|
self.sensor_name = sensor_name
|
2016-08-19 07:11:56 +00:00
|
|
|
self.index = sensor_index
|
|
|
|
|
2019-10-01 21:28:13 +00:00
|
|
|
@property
|
2024-07-30 13:33:57 +00:00
|
|
|
def unique_id(self) -> str | None:
|
2019-10-01 21:28:13 +00:00
|
|
|
"""Return a unique identifier for this sensor."""
|
|
|
|
for sensor in self.data.ecobee.get_remote_sensors(self.index):
|
|
|
|
if sensor["name"] == self.sensor_name:
|
|
|
|
if "code" in sensor:
|
|
|
|
return f"{sensor['code']}-{self.device_class}"
|
2019-10-04 21:15:43 +00:00
|
|
|
thermostat = self.data.ecobee.get_thermostat(self.index)
|
|
|
|
return f"{thermostat['identifier']}-{sensor['id']}-{self.device_class}"
|
2024-07-30 13:33:57 +00:00
|
|
|
return None
|
2019-10-01 21:28:13 +00:00
|
|
|
|
2019-10-04 06:31:45 +00:00
|
|
|
@property
|
2021-10-23 09:44:51 +00:00
|
|
|
def device_info(self) -> DeviceInfo | None:
|
2019-10-04 06:31:45 +00:00
|
|
|
"""Return device information for this sensor."""
|
|
|
|
identifier = None
|
|
|
|
model = None
|
|
|
|
for sensor in self.data.ecobee.get_remote_sensors(self.index):
|
|
|
|
if sensor["name"] != self.sensor_name:
|
|
|
|
continue
|
|
|
|
if "code" in sensor:
|
|
|
|
identifier = sensor["code"]
|
|
|
|
model = "ecobee Room Sensor"
|
|
|
|
else:
|
|
|
|
thermostat = self.data.ecobee.get_thermostat(self.index)
|
|
|
|
identifier = thermostat["identifier"]
|
|
|
|
try:
|
|
|
|
model = (
|
|
|
|
f"{ECOBEE_MODEL_TO_NAME[thermostat['modelNumber']]} Thermostat"
|
|
|
|
)
|
|
|
|
except KeyError:
|
2021-06-17 08:59:13 +00:00
|
|
|
# Ecobee model is not in our list
|
|
|
|
model = None
|
2019-10-04 06:31:45 +00:00
|
|
|
break
|
|
|
|
|
2021-06-17 08:59:13 +00:00
|
|
|
if identifier is not None:
|
2021-10-23 09:44:51 +00:00
|
|
|
return DeviceInfo(
|
|
|
|
identifiers={(DOMAIN, identifier)},
|
|
|
|
manufacturer=MANUFACTURER,
|
|
|
|
model=model,
|
|
|
|
name=self.sensor_name,
|
|
|
|
)
|
2019-10-04 06:31:45 +00:00
|
|
|
return None
|
|
|
|
|
2021-06-18 07:06:31 +00:00
|
|
|
@property
|
2022-08-20 05:52:55 +00:00
|
|
|
def available(self) -> bool:
|
2021-06-18 07:06:31 +00:00
|
|
|
"""Return true if device is available."""
|
|
|
|
thermostat = self.data.ecobee.get_thermostat(self.index)
|
|
|
|
return thermostat["runtime"]["connected"]
|
|
|
|
|
2022-08-20 05:52:55 +00:00
|
|
|
async def async_update(self) -> None:
|
2016-08-19 07:11:56 +00:00
|
|
|
"""Get the latest state of the sensor."""
|
2019-09-25 20:38:21 +00:00
|
|
|
await self.data.update()
|
|
|
|
for sensor in self.data.ecobee.get_remote_sensors(self.index):
|
2019-10-12 20:11:30 +00:00
|
|
|
if sensor["name"] != self.sensor_name:
|
|
|
|
continue
|
2019-07-31 19:25:30 +00:00
|
|
|
for item in sensor["capability"]:
|
2019-10-12 20:11:30 +00:00
|
|
|
if item["type"] != "occupancy":
|
|
|
|
continue
|
2023-08-30 10:11:13 +00:00
|
|
|
self._attr_is_on = item["value"] == "true"
|
2019-10-12 20:11:30 +00:00
|
|
|
break
|