binary_sensor.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. """Interfaces with the Integration 101 Template api sensors."""
  2. import logging
  3. from homeassistant.components.binary_sensor import (
  4. BinarySensorDeviceClass,
  5. BinarySensorEntity,
  6. )
  7. from homeassistant.core import HomeAssistant, callback
  8. from homeassistant.helpers.device_registry import DeviceInfo
  9. from homeassistant.helpers.entity_platform import AddEntitiesCallback
  10. from homeassistant.helpers.update_coordinator import CoordinatorEntity
  11. from . import MyConfigEntry
  12. from .api import Device, DeviceType
  13. from .const import DOMAIN
  14. from .coordinator import ExampleCoordinator
  15. _LOGGER = logging.getLogger(__name__)
  16. async def async_setup_entry(
  17. hass: HomeAssistant,
  18. config_entry: MyConfigEntry,
  19. async_add_entities: AddEntitiesCallback,
  20. ):
  21. """Set up the Binary Sensors."""
  22. # This gets the data update coordinator from the config entry runtime data as specified in your __init__.py
  23. coordinator: ExampleCoordinator = config_entry.runtime_data.coordinator
  24. # Enumerate all the binary sensors in your data value from your DataUpdateCoordinator and add an instance of your binary sensor class
  25. # to a list for each one.
  26. # This maybe different in your specific case, depending on how your data is structured
  27. binary_sensors = [
  28. ExampleBinarySensor(coordinator, device)
  29. for device in coordinator.data.devices
  30. if device.device_type == DeviceType.DOOR_SENSOR
  31. ]
  32. # Create the binary sensors.
  33. async_add_entities(binary_sensors)
  34. class ExampleBinarySensor(CoordinatorEntity, BinarySensorEntity):
  35. """Implementation of a sensor."""
  36. def __init__(self, coordinator: ExampleCoordinator, device: Device) -> None:
  37. """Initialise sensor."""
  38. super().__init__(coordinator)
  39. self.device = device
  40. self.device_id = device.device_id
  41. @callback
  42. def _handle_coordinator_update(self) -> None:
  43. """Update sensor with latest data from coordinator."""
  44. # This method is called by your DataUpdateCoordinator when a successful update runs.
  45. self.device = self.coordinator.get_device_by_id(
  46. self.device.device_type, self.device_id
  47. )
  48. _LOGGER.debug("Device: %s", self.device)
  49. self.async_write_ha_state()
  50. @property
  51. def device_class(self) -> str:
  52. """Return device class."""
  53. # https://developers.home-assistant.io/docs/core/entity/binary-sensor#available-device-classes
  54. return BinarySensorDeviceClass.DOOR
  55. @property
  56. def device_info(self) -> DeviceInfo:
  57. """Return device information."""
  58. # Identifiers are what group entities into the same device.
  59. # If your device is created elsewhere, you can just specify the indentifiers parameter.
  60. # If your device connects via another device, add via_device parameter with the indentifiers of that device.
  61. return DeviceInfo(
  62. name=f"ExampleDevice{self.device.device_id}",
  63. manufacturer="ACME Manufacturer",
  64. model="Door&Temp v1",
  65. sw_version="1.0",
  66. identifiers={
  67. (
  68. DOMAIN,
  69. f"{self.coordinator.data.controller_name}-{self.device.device_id}",
  70. )
  71. },
  72. )
  73. @property
  74. def name(self) -> str:
  75. """Return the name of the sensor."""
  76. return self.device.name
  77. @property
  78. def is_on(self) -> bool | None:
  79. """Return if the binary sensor is on."""
  80. # This needs to enumerate to true or false
  81. return self.device.state
  82. @property
  83. def unique_id(self) -> str:
  84. """Return unique id."""
  85. # All entities must have a unique id. Think carefully what you want this to be as
  86. # changing it later will cause HA to create new entities.
  87. return f"{DOMAIN}-{self.device.device_unique_id}"
  88. @property
  89. def extra_state_attributes(self):
  90. """Return the extra state attributes."""
  91. # Add any additional attributes you want on your sensor.
  92. attrs = {}
  93. attrs["extra_info"] = "Extra Info"
  94. return attrs