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."""
|
"""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.components.sensor import STATE_CLASS_MEASUREMENT, SensorEntity
|
||||||
|
from homeassistant.config_entries import ConfigEntry
|
||||||
from homeassistant.const import (
|
from homeassistant.const import (
|
||||||
ATTR_BATTERY_LEVEL,
|
ATTR_BATTERY_LEVEL,
|
||||||
CONCENTRATION_PARTS_PER_MILLION,
|
CONCENTRATION_PARTS_PER_MILLION,
|
||||||
|
@ -10,11 +15,19 @@ from homeassistant.const import (
|
||||||
PERCENTAGE,
|
PERCENTAGE,
|
||||||
TEMP_CELSIUS,
|
TEMP_CELSIUS,
|
||||||
)
|
)
|
||||||
|
from homeassistant.core import HomeAssistant
|
||||||
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||||
|
from homeassistant.helpers.typing import ConfigType, StateType
|
||||||
|
|
||||||
from . import DOMAIN
|
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."""
|
"""Set up the Demo sensors."""
|
||||||
async_add_entities(
|
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."""
|
"""Set up the Demo config entry."""
|
||||||
await async_setup_platform(hass, {}, async_add_entities)
|
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):
|
class DemoSensor(SensorEntity):
|
||||||
"""Representation of a Demo sensor."""
|
"""Representation of a Demo sensor."""
|
||||||
|
|
||||||
|
_attr_should_poll = False
|
||||||
|
|
||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
unique_id,
|
unique_id: str,
|
||||||
name,
|
name: str,
|
||||||
state,
|
state: StateType,
|
||||||
device_class,
|
device_class: str | None,
|
||||||
state_class,
|
state_class: str | None,
|
||||||
unit_of_measurement,
|
unit_of_measurement: str | None,
|
||||||
battery,
|
battery: StateType,
|
||||||
):
|
) -> None:
|
||||||
"""Initialize the sensor."""
|
"""Initialize the sensor."""
|
||||||
self._battery = battery
|
self._attr_device_class = device_class
|
||||||
self._device_class = device_class
|
self._attr_name = name
|
||||||
self._name = name
|
self._attr_state = state
|
||||||
self._state = state
|
self._attr_state_class = state_class
|
||||||
self._state_class = state_class
|
self._attr_unique_id = unique_id
|
||||||
self._unique_id = unique_id
|
self._attr_unit_of_measurement = unit_of_measurement
|
||||||
self._unit_of_measurement = unit_of_measurement
|
|
||||||
|
|
||||||
@property
|
self._attr_device_info = {
|
||||||
def device_info(self):
|
"identifiers": {(DOMAIN, unique_id)},
|
||||||
"""Return device info."""
|
"name": name,
|
||||||
return {
|
|
||||||
"identifiers": {
|
|
||||||
# Serial numbers are unique identifiers within a specific domain
|
|
||||||
(DOMAIN, self.unique_id)
|
|
||||||
},
|
|
||||||
"name": self.name,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@property
|
if battery:
|
||||||
def unique_id(self):
|
self._attr_extra_state_attributes = {ATTR_BATTERY_LEVEL: battery}
|
||||||
"""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}
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue