Set state_class for demo sensor (#50523)

This commit is contained in:
Erik Montnemery 2021-05-12 18:38:26 +02:00 committed by GitHub
parent 38a0cf6650
commit 216b0df908
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 33 additions and 9 deletions

View File

@ -1,5 +1,5 @@
"""Demo platform that has a couple of fake sensors."""
from homeassistant.components.sensor import SensorEntity
from homeassistant.components.sensor import STATE_CLASS_MEASUREMENT, SensorEntity
from homeassistant.const import (
ATTR_BATTERY_LEVEL,
CONCENTRATION_PARTS_PER_MILLION,
@ -23,6 +23,7 @@ async def async_setup_platform(hass, config, async_add_entities, discovery_info=
"Outside Temperature",
15.6,
DEVICE_CLASS_TEMPERATURE,
STATE_CLASS_MEASUREMENT,
TEMP_CELSIUS,
12,
),
@ -31,6 +32,7 @@ async def async_setup_platform(hass, config, async_add_entities, discovery_info=
"Outside Humidity",
54,
DEVICE_CLASS_HUMIDITY,
STATE_CLASS_MEASUREMENT,
PERCENTAGE,
None,
),
@ -39,6 +41,7 @@ async def async_setup_platform(hass, config, async_add_entities, discovery_info=
"Carbon monoxide",
54,
DEVICE_CLASS_CO,
STATE_CLASS_MEASUREMENT,
CONCENTRATION_PARTS_PER_MILLION,
None,
),
@ -47,6 +50,7 @@ async def async_setup_platform(hass, config, async_add_entities, discovery_info=
"Carbon dioxide",
54,
DEVICE_CLASS_CO2,
STATE_CLASS_MEASUREMENT,
CONCENTRATION_PARTS_PER_MILLION,
14,
),
@ -63,15 +67,23 @@ class DemoSensor(SensorEntity):
"""Representation of a Demo sensor."""
def __init__(
self, unique_id, name, state, device_class, unit_of_measurement, battery
self,
unique_id,
name,
state,
device_class,
state_class,
unit_of_measurement,
battery,
):
"""Initialize the sensor."""
self._unique_id = unique_id
self._battery = battery
self._device_class = device_class
self._name = name
self._state = state
self._device_class = device_class
self._state_class = state_class
self._unique_id = unique_id
self._unit_of_measurement = unit_of_measurement
self._battery = battery
@property
def device_info(self):
@ -99,6 +111,11 @@ class DemoSensor(SensorEntity):
"""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."""

View File

@ -47,14 +47,14 @@ async def prometheus_client(hass, hass_client):
)
sensor1 = DemoSensor(
None, "Television Energy", 74, None, ENERGY_KILO_WATT_HOUR, None
None, "Television Energy", 74, None, None, ENERGY_KILO_WATT_HOUR, None
)
sensor1.hass = hass
sensor1.entity_id = "sensor.television_energy"
await sensor1.async_update_ha_state()
sensor2 = DemoSensor(
None, "Radio Energy", 14, DEVICE_CLASS_POWER, ENERGY_KILO_WATT_HOUR, None
None, "Radio Energy", 14, DEVICE_CLASS_POWER, None, ENERGY_KILO_WATT_HOUR, None
)
sensor2.hass = hass
sensor2.entity_id = "sensor.radio_energy"
@ -65,13 +65,19 @@ async def prometheus_client(hass, hass_client):
await sensor2.async_update_ha_state()
sensor3 = DemoSensor(
None, "Electricity price", 0.123, None, f"SEK/{ENERGY_KILO_WATT_HOUR}", None
None,
"Electricity price",
0.123,
None,
None,
f"SEK/{ENERGY_KILO_WATT_HOUR}",
None,
)
sensor3.hass = hass
sensor3.entity_id = "sensor.electricity_price"
await sensor3.async_update_ha_state()
sensor4 = DemoSensor(None, "Wind Direction", 25, None, DEGREE, None)
sensor4 = DemoSensor(None, "Wind Direction", 25, None, None, DEGREE, None)
sensor4.hass = hass
sensor4.entity_id = "sensor.wind_direction"
await sensor4.async_update_ha_state()
@ -81,6 +87,7 @@ async def prometheus_client(hass, hass_client):
"SPS30 PM <1µm Weight concentration",
3.7069,
None,
None,
CONCENTRATION_MICROGRAMS_PER_CUBIC_METER,
None,
)