core/homeassistant/components/demo/sensor.py

136 lines
3.7 KiB
Python
Raw Normal View History

"""Demo platform that has a couple of fake sensors."""
2021-06-29 06:39:40 +00:00
from __future__ import annotations
from typing import Any
from homeassistant.components.sensor import STATE_CLASS_MEASUREMENT, SensorEntity
2021-06-29 06:39:40 +00:00
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import (
2019-07-31 19:25:30 +00:00
ATTR_BATTERY_LEVEL,
CONCENTRATION_PARTS_PER_MILLION,
DEVICE_CLASS_CO,
DEVICE_CLASS_CO2,
DEVICE_CLASS_ENERGY,
2019-07-31 19:25:30 +00:00
DEVICE_CLASS_HUMIDITY,
DEVICE_CLASS_POWER,
2019-07-31 19:25:30 +00:00
DEVICE_CLASS_TEMPERATURE,
ENERGY_KILO_WATT_HOUR,
PERCENTAGE,
POWER_WATT,
TEMP_CELSIUS,
2019-07-31 19:25:30 +00:00
)
2021-06-29 06:39:40 +00:00
from homeassistant.core import HomeAssistant
2021-10-22 15:00:00 +00:00
from homeassistant.helpers.entity import DeviceInfo
2021-06-29 06:39:40 +00:00
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import ConfigType, StateType
from . import DOMAIN
2021-06-29 06:39:40 +00:00
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(
2019-07-31 19:25:30 +00:00
[
DemoSensor(
"sensor_1",
"Outside Temperature",
15.6,
DEVICE_CLASS_TEMPERATURE,
STATE_CLASS_MEASUREMENT,
TEMP_CELSIUS,
12,
),
DemoSensor(
"sensor_2",
"Outside Humidity",
54,
DEVICE_CLASS_HUMIDITY,
STATE_CLASS_MEASUREMENT,
PERCENTAGE,
None,
2019-07-31 19:25:30 +00:00
),
DemoSensor(
"sensor_3",
"Carbon monoxide",
54,
DEVICE_CLASS_CO,
STATE_CLASS_MEASUREMENT,
CONCENTRATION_PARTS_PER_MILLION,
None,
),
DemoSensor(
"sensor_4",
"Carbon dioxide",
54,
DEVICE_CLASS_CO2,
STATE_CLASS_MEASUREMENT,
CONCENTRATION_PARTS_PER_MILLION,
14,
),
DemoSensor(
"sensor_5",
"Power consumption",
100,
DEVICE_CLASS_POWER,
STATE_CLASS_MEASUREMENT,
POWER_WATT,
None,
),
DemoSensor(
"sensor_6",
"Today energy",
15,
DEVICE_CLASS_ENERGY,
STATE_CLASS_MEASUREMENT,
ENERGY_KILO_WATT_HOUR,
None,
),
2019-07-31 19:25:30 +00:00
]
)
2021-06-29 06:39:40 +00:00
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)
class DemoSensor(SensorEntity):
2016-03-08 15:46:34 +00:00
"""Representation of a Demo sensor."""
2021-06-29 06:39:40 +00:00
_attr_should_poll = False
def __init__(
self,
2021-06-29 06:39:40 +00:00
unique_id: str,
name: str,
state: StateType,
device_class: str | None,
state_class: str | None,
unit_of_measurement: str | None,
battery: StateType,
) -> None:
2016-03-08 15:46:34 +00:00
"""Initialize the sensor."""
2021-06-29 06:39:40 +00:00
self._attr_device_class = device_class
self._attr_name = name
self._attr_native_unit_of_measurement = unit_of_measurement
self._attr_native_value = state
2021-06-29 06:39:40 +00:00
self._attr_state_class = state_class
self._attr_unique_id = unique_id
2021-10-22 15:00:00 +00:00
self._attr_device_info = DeviceInfo(
identifiers={(DOMAIN, unique_id)},
name=name,
)
2021-06-29 06:39:40 +00:00
if battery:
self._attr_extra_state_attributes = {ATTR_BATTERY_LEVEL: battery}