2019-04-03 15:40:03 +00:00
|
|
|
"""Demo platform that has a couple of fake sensors."""
|
2021-03-22 11:52:29 +00:00
|
|
|
from homeassistant.components.sensor import SensorEntity
|
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
|
|
|
)
|
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
|
|
|
|
|
|
|
|
2019-11-13 15:37:31 +00:00
|
|
|
async def async_setup_platform(hass, config, async_add_entities, discovery_info=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,
|
|
|
|
TEMP_CELSIUS,
|
|
|
|
12,
|
|
|
|
),
|
|
|
|
DemoSensor(
|
2020-02-28 19:46:48 +00:00
|
|
|
"sensor_2",
|
|
|
|
"Outside Humidity",
|
|
|
|
54,
|
|
|
|
DEVICE_CLASS_HUMIDITY,
|
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,
|
|
|
|
CONCENTRATION_PARTS_PER_MILLION,
|
|
|
|
None,
|
|
|
|
),
|
|
|
|
DemoSensor(
|
|
|
|
"sensor_4",
|
|
|
|
"Carbon dioxide",
|
|
|
|
54,
|
|
|
|
DEVICE_CLASS_CO2,
|
|
|
|
CONCENTRATION_PARTS_PER_MILLION,
|
|
|
|
14,
|
|
|
|
),
|
2019-07-31 19:25:30 +00:00
|
|
|
]
|
|
|
|
)
|
2015-02-28 15:31:39 +00:00
|
|
|
|
|
|
|
|
2019-11-13 15:37:31 +00:00
|
|
|
async def async_setup_entry(hass, config_entry, async_add_entities):
|
|
|
|
"""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."""
|
|
|
|
|
2019-11-13 15:37:31 +00:00
|
|
|
def __init__(
|
|
|
|
self, unique_id, name, state, device_class, unit_of_measurement, battery
|
|
|
|
):
|
2016-03-08 15:46:34 +00:00
|
|
|
"""Initialize the sensor."""
|
2019-11-13 15:37:31 +00:00
|
|
|
self._unique_id = unique_id
|
2015-02-28 15:31:39 +00:00
|
|
|
self._name = name
|
|
|
|
self._state = state
|
2018-05-03 17:51:36 +00:00
|
|
|
self._device_class = device_class
|
2015-02-28 15:31:39 +00:00
|
|
|
self._unit_of_measurement = unit_of_measurement
|
2015-03-19 06:02:58 +00:00
|
|
|
self._battery = battery
|
2015-02-28 15:31:39 +00:00
|
|
|
|
2019-11-13 15:37:31 +00:00
|
|
|
@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,
|
|
|
|
}
|
|
|
|
|
|
|
|
@property
|
|
|
|
def unique_id(self):
|
|
|
|
"""Return the unique id."""
|
|
|
|
return self._unique_id
|
|
|
|
|
2015-03-17 05:20:31 +00:00
|
|
|
@property
|
|
|
|
def should_poll(self):
|
2016-02-23 05:21:49 +00:00
|
|
|
"""No polling needed for a demo sensor."""
|
2015-03-17 05:20:31 +00:00
|
|
|
return False
|
|
|
|
|
2018-05-03 17:51:36 +00:00
|
|
|
@property
|
|
|
|
def device_class(self):
|
|
|
|
"""Return the device class of the sensor."""
|
|
|
|
return self._device_class
|
|
|
|
|
2015-02-28 15:31:39 +00:00
|
|
|
@property
|
|
|
|
def name(self):
|
2016-02-24 09:38:06 +00:00
|
|
|
"""Return the name of the sensor."""
|
2015-02-28 15:31:39 +00:00
|
|
|
return self._name
|
|
|
|
|
|
|
|
@property
|
|
|
|
def state(self):
|
2016-02-24 09:38:06 +00:00
|
|
|
"""Return the state of the sensor."""
|
2015-02-28 15:31:39 +00:00
|
|
|
return self._state
|
|
|
|
|
2015-03-19 06:02:58 +00:00
|
|
|
@property
|
|
|
|
def unit_of_measurement(self):
|
2016-02-24 09:38:06 +00:00
|
|
|
"""Return the unit this state is expressed in."""
|
2015-03-19 06:02:58 +00:00
|
|
|
return self._unit_of_measurement
|
|
|
|
|
2015-02-28 15:31:39 +00:00
|
|
|
@property
|
2021-03-11 15:51:03 +00:00
|
|
|
def extra_state_attributes(self):
|
2016-02-24 09:38:06 +00:00
|
|
|
"""Return the state attributes."""
|
2015-03-19 06:02:58 +00:00
|
|
|
if self._battery:
|
2019-07-31 19:25:30 +00:00
|
|
|
return {ATTR_BATTERY_LEVEL: self._battery}
|