Demo: Sensor improvements (#52263)
This commit is contained in:
parent
d37018cf87
commit
8dd545d060
1 changed files with 40 additions and 66 deletions
|
@ -1,5 +1,10 @@
|
|||
"""Demo platform that has a couple of fake sensors."""
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Any
|
||||
|
||||
from homeassistant.components.sensor import STATE_CLASS_MEASUREMENT, SensorEntity
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.const import (
|
||||
ATTR_BATTERY_LEVEL,
|
||||
CONCENTRATION_PARTS_PER_MILLION,
|
||||
|
@ -10,11 +15,19 @@ from homeassistant.const import (
|
|||
PERCENTAGE,
|
||||
TEMP_CELSIUS,
|
||||
)
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||
from homeassistant.helpers.typing import ConfigType, StateType
|
||||
|
||||
from . import DOMAIN
|
||||
|
||||
|
||||
async def async_setup_platform(hass, config, async_add_entities, discovery_info=None):
|
||||
async def async_setup_platform(
|
||||
hass: HomeAssistant,
|
||||
config: ConfigType,
|
||||
async_add_entities: AddEntitiesCallback,
|
||||
discovery_info: dict[str, Any] | None = None,
|
||||
) -> None:
|
||||
"""Set up the Demo sensors."""
|
||||
async_add_entities(
|
||||
[
|
||||
|
@ -58,7 +71,11 @@ async def async_setup_platform(hass, config, async_add_entities, discovery_info=
|
|||
)
|
||||
|
||||
|
||||
async def async_setup_entry(hass, config_entry, async_add_entities):
|
||||
async def async_setup_entry(
|
||||
hass: HomeAssistant,
|
||||
config_entry: ConfigEntry,
|
||||
async_add_entities: AddEntitiesCallback,
|
||||
) -> None:
|
||||
"""Set up the Demo config entry."""
|
||||
await async_setup_platform(hass, {}, async_add_entities)
|
||||
|
||||
|
@ -66,73 +83,30 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
|
|||
class DemoSensor(SensorEntity):
|
||||
"""Representation of a Demo sensor."""
|
||||
|
||||
_attr_should_poll = False
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
unique_id,
|
||||
name,
|
||||
state,
|
||||
device_class,
|
||||
state_class,
|
||||
unit_of_measurement,
|
||||
battery,
|
||||
):
|
||||
unique_id: str,
|
||||
name: str,
|
||||
state: StateType,
|
||||
device_class: str | None,
|
||||
state_class: str | None,
|
||||
unit_of_measurement: str | None,
|
||||
battery: StateType,
|
||||
) -> None:
|
||||
"""Initialize the sensor."""
|
||||
self._battery = battery
|
||||
self._device_class = device_class
|
||||
self._name = name
|
||||
self._state = state
|
||||
self._state_class = state_class
|
||||
self._unique_id = unique_id
|
||||
self._unit_of_measurement = unit_of_measurement
|
||||
self._attr_device_class = device_class
|
||||
self._attr_name = name
|
||||
self._attr_state = state
|
||||
self._attr_state_class = state_class
|
||||
self._attr_unique_id = unique_id
|
||||
self._attr_unit_of_measurement = unit_of_measurement
|
||||
|
||||
@property
|
||||
def device_info(self):
|
||||
"""Return device info."""
|
||||
return {
|
||||
"identifiers": {
|
||||
# Serial numbers are unique identifiers within a specific domain
|
||||
(DOMAIN, self.unique_id)
|
||||
},
|
||||
"name": self.name,
|
||||
self._attr_device_info = {
|
||||
"identifiers": {(DOMAIN, unique_id)},
|
||||
"name": name,
|
||||
}
|
||||
|
||||
@property
|
||||
def unique_id(self):
|
||||
"""Return the unique id."""
|
||||
return self._unique_id
|
||||
|
||||
@property
|
||||
def should_poll(self):
|
||||
"""No polling needed for a demo sensor."""
|
||||
return False
|
||||
|
||||
@property
|
||||
def device_class(self):
|
||||
"""Return the device class of the sensor."""
|
||||
return self._device_class
|
||||
|
||||
@property
|
||||
def state_class(self):
|
||||
"""Return the state class of the sensor."""
|
||||
return self._state_class
|
||||
|
||||
@property
|
||||
def name(self):
|
||||
"""Return the name of the sensor."""
|
||||
return self._name
|
||||
|
||||
@property
|
||||
def state(self):
|
||||
"""Return the state of the sensor."""
|
||||
return self._state
|
||||
|
||||
@property
|
||||
def unit_of_measurement(self):
|
||||
"""Return the unit this state is expressed in."""
|
||||
return self._unit_of_measurement
|
||||
|
||||
@property
|
||||
def extra_state_attributes(self):
|
||||
"""Return the state attributes."""
|
||||
if self._battery:
|
||||
return {ATTR_BATTERY_LEVEL: self._battery}
|
||||
if battery:
|
||||
self._attr_extra_state_attributes = {ATTR_BATTERY_LEVEL: battery}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue