Files
homeassistant-core/homeassistant/components/ecobee/binary_sensor.py
T

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

107 lines
3.7 KiB
Python
Raw Normal View History

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