2019-04-03 15:40:03 +00:00
|
|
|
"""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
|
|
|
|
|
2021-05-12 16:38:26 +00:00
|
|
|
from homeassistant.components.sensor import STATE_CLASS_MEASUREMENT, SensorEntity
|
2021-06-29 06:39:40 +00:00
|
|
|
from homeassistant.config_entries import ConfigEntry
|
2018-05-05 13:37:40 +00:00
|
|
|
from homeassistant.const import (
|
2019-07-31 19:25:30 +00:00
|
|
|
ATTR_BATTERY_LEVEL,
|
2021-03-12 19:03:47 +00:00
|
|
|
CONCENTRATION_PARTS_PER_MILLION,
|
|
|
|
DEVICE_CLASS_CO,
|
|
|
|
DEVICE_CLASS_CO2,
|
2019-07-31 19:25:30 +00:00
|
|
|
DEVICE_CLASS_HUMIDITY,
|
|
|
|
DEVICE_CLASS_TEMPERATURE,
|
2020-09-05 19:09:14 +00:00
|
|
|
PERCENTAGE,
|
2019-12-08 16:59:27 +00:00
|
|
|
TEMP_CELSIUS,
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2021-06-29 06:39:40 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
|
|
|
from homeassistant.helpers.typing import ConfigType, StateType
|
2019-12-08 16:59:27 +00:00
|
|
|
|
2019-11-13 15:37:31 +00:00
|
|
|
from . import DOMAIN
|
2015-02-28 15:31:39 +00:00
|
|
|
|
|
|
|
|
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:
|
2017-05-02 16:18:47 +00:00
|
|
|
"""Set up the Demo sensors."""
|
2019-11-13 15:37:31 +00:00
|
|
|
async_add_entities(
|
2019-07-31 19:25:30 +00:00
|
|
|
[
|
|
|
|
DemoSensor(
|
2019-11-13 15:37:31 +00:00
|
|
|
"sensor_1",
|
|
|
|
"Outside Temperature",
|
|
|
|
15.6,
|
|
|
|
DEVICE_CLASS_TEMPERATURE,
|
2021-05-12 16:38:26 +00:00
|
|
|
STATE_CLASS_MEASUREMENT,
|
2019-11-13 15:37:31 +00:00
|
|
|
TEMP_CELSIUS,
|
|
|
|
12,
|
|
|
|
),
|
|
|
|
DemoSensor(
|
2020-02-28 19:46:48 +00:00
|
|
|
"sensor_2",
|
|
|
|
"Outside Humidity",
|
|
|
|
54,
|
|
|
|
DEVICE_CLASS_HUMIDITY,
|
2021-05-12 16:38:26 +00:00
|
|
|
STATE_CLASS_MEASUREMENT,
|
2020-09-05 19:09:14 +00:00
|
|
|
PERCENTAGE,
|
2020-02-28 19:46:48 +00:00
|
|
|
None,
|
2019-07-31 19:25:30 +00:00
|
|
|
),
|
2021-03-12 19:03:47 +00:00
|
|
|
DemoSensor(
|
|
|
|
"sensor_3",
|
|
|
|
"Carbon monoxide",
|
|
|
|
54,
|
|
|
|
DEVICE_CLASS_CO,
|
2021-05-12 16:38:26 +00:00
|
|
|
STATE_CLASS_MEASUREMENT,
|
2021-03-12 19:03:47 +00:00
|
|
|
CONCENTRATION_PARTS_PER_MILLION,
|
|
|
|
None,
|
|
|
|
),
|
|
|
|
DemoSensor(
|
|
|
|
"sensor_4",
|
|
|
|
"Carbon dioxide",
|
|
|
|
54,
|
|
|
|
DEVICE_CLASS_CO2,
|
2021-05-12 16:38:26 +00:00
|
|
|
STATE_CLASS_MEASUREMENT,
|
2021-03-12 19:03:47 +00:00
|
|
|
CONCENTRATION_PARTS_PER_MILLION,
|
|
|
|
14,
|
|
|
|
),
|
2019-07-31 19:25:30 +00:00
|
|
|
]
|
|
|
|
)
|
2015-02-28 15:31:39 +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:
|
2019-11-13 15:37:31 +00:00
|
|
|
"""Set up the Demo config entry."""
|
|
|
|
await async_setup_platform(hass, {}, async_add_entities)
|
|
|
|
|
|
|
|
|
2021-03-22 11:52:29 +00:00
|
|
|
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
|
|
|
|
|
2019-11-13 15:37:31 +00:00
|
|
|
def __init__(
|
2021-05-12 16:38:26 +00:00
|
|
|
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_state = state
|
|
|
|
self._attr_state_class = state_class
|
|
|
|
self._attr_unique_id = unique_id
|
|
|
|
self._attr_unit_of_measurement = unit_of_measurement
|
|
|
|
|
|
|
|
self._attr_device_info = {
|
|
|
|
"identifiers": {(DOMAIN, unique_id)},
|
|
|
|
"name": name,
|
2019-11-13 15:37:31 +00:00
|
|
|
}
|
|
|
|
|
2021-06-29 06:39:40 +00:00
|
|
|
if battery:
|
|
|
|
self._attr_extra_state_attributes = {ATTR_BATTERY_LEVEL: battery}
|