| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126 |
- """Interfaces with the Integration 101 Template api sensors."""
- import logging
- from homeassistant.components.sensor import (
- SensorDeviceClass,
- SensorEntity,
- SensorStateClass,
- )
- from homeassistant.const import UnitOfTemperature
- from homeassistant.core import HomeAssistant, callback
- from homeassistant.helpers.device_registry import DeviceInfo
- from homeassistant.helpers.entity_platform import AddEntitiesCallback
- from homeassistant.helpers.update_coordinator import CoordinatorEntity
- from . import MyConfigEntry
- from .api import Device, DeviceType
- from .const import DOMAIN
- from .coordinator import ExampleCoordinator
- _LOGGER = logging.getLogger(__name__)
- async def async_setup_entry(
- hass: HomeAssistant,
- config_entry: MyConfigEntry,
- async_add_entities: AddEntitiesCallback,
- ):
- """Set up the Sensors."""
- # This gets the data update coordinator from the config entry runtime data as specified in your __init__.py
- coordinator: ExampleCoordinator = config_entry.runtime_data.coordinator
- # Enumerate all the sensors in your data value from your DataUpdateCoordinator and add an instance of your sensor class
- # to a list for each one.
- # This maybe different in your specific case, depending on how your data is structured
- sensors = [
- ExampleSensor(coordinator, device)
- for device in coordinator.data.devices
- if device.device_type == DeviceType.TEMP_SENSOR
- ]
- # Create the sensors.
- async_add_entities(sensors)
- class ExampleSensor(CoordinatorEntity, SensorEntity):
- """Implementation of a sensor."""
- def __init__(self, coordinator: ExampleCoordinator, device: Device) -> None:
- """Initialise sensor."""
- super().__init__(coordinator)
- self.device = device
- self.device_id = device.device_id
- @callback
- def _handle_coordinator_update(self) -> None:
- """Update sensor with latest data from coordinator."""
- # This method is called by your DataUpdateCoordinator when a successful update runs.
- self.device = self.coordinator.get_device_by_id(
- self.device.device_type, self.device_id
- )
- _LOGGER.debug("Device: %s", self.device)
- self.async_write_ha_state()
- @property
- def device_class(self) -> str:
- """Return device class."""
- # https://developers.home-assistant.io/docs/core/entity/sensor/#available-device-classes
- return SensorDeviceClass.TEMPERATURE
- @property
- def device_info(self) -> DeviceInfo:
- """Return device information."""
- # Identifiers are what group entities into the same device.
- # If your device is created elsewhere, you can just specify the indentifiers parameter.
- # If your device connects via another device, add via_device parameter with the indentifiers of that device.
- return DeviceInfo(
- name=f"ExampleDevice{self.device.device_id}",
- manufacturer="ACME Manufacturer",
- model="Door&Temp v1",
- sw_version="1.0",
- identifiers={
- (
- DOMAIN,
- f"{self.coordinator.data.controller_name}-{self.device.device_id}",
- )
- },
- )
- @property
- def name(self) -> str:
- """Return the name of the sensor."""
- return self.device.name
- @property
- def native_value(self) -> int | float:
- """Return the state of the entity."""
- # Using native value and native unit of measurement, allows you to change units
- # in Lovelace and HA will automatically calculate the correct value.
- return float(self.device.state)
- @property
- def native_unit_of_measurement(self) -> str | None:
- """Return unit of temperature."""
- return UnitOfTemperature.CELSIUS
- @property
- def state_class(self) -> str | None:
- """Return state class."""
- # https://developers.home-assistant.io/docs/core/entity/sensor/#available-state-classes
- return SensorStateClass.MEASUREMENT
- @property
- def unique_id(self) -> str:
- """Return unique id."""
- # All entities must have a unique id. Think carefully what you want this to be as
- # changing it later will cause HA to create new entities.
- return f"{DOMAIN}-{self.device.device_unique_id}"
- @property
- def extra_state_attributes(self):
- """Return the extra state attributes."""
- # Add any additional attributes you want on your sensor.
- attrs = {}
- attrs["extra_info"] = "Extra Info"
- return attrs
|