2019-04-03 17:40:03 +02:00
|
|
|
"""Demo platform that has a couple of fake sensors."""
|
2018-05-05 15:37:40 +02:00
|
|
|
from homeassistant.const import (
|
2019-07-31 12:25:30 -07:00
|
|
|
ATTR_BATTERY_LEVEL,
|
|
|
|
DEVICE_CLASS_HUMIDITY,
|
|
|
|
DEVICE_CLASS_TEMPERATURE,
|
2020-09-05 21:09:14 +02:00
|
|
|
PERCENTAGE,
|
2019-12-08 17:59:27 +01:00
|
|
|
TEMP_CELSIUS,
|
2019-07-31 12:25:30 -07:00
|
|
|
)
|
2015-03-21 19:16:13 -07:00
|
|
|
from homeassistant.helpers.entity import Entity
|
2019-12-08 17:59:27 +01:00
|
|
|
|
2019-11-13 16:37:31 +01:00
|
|
|
from . import DOMAIN
|
2015-02-28 07:31:39 -08:00
|
|
|
|
|
|
|
|
2019-11-13 16:37:31 +01:00
|
|
|
async def async_setup_platform(hass, config, async_add_entities, discovery_info=None):
|
2017-05-02 18:18:47 +02:00
|
|
|
"""Set up the Demo sensors."""
|
2019-11-13 16:37:31 +01:00
|
|
|
async_add_entities(
|
2019-07-31 12:25:30 -07:00
|
|
|
[
|
|
|
|
DemoSensor(
|
2019-11-13 16:37:31 +01:00
|
|
|
"sensor_1",
|
|
|
|
"Outside Temperature",
|
|
|
|
15.6,
|
|
|
|
DEVICE_CLASS_TEMPERATURE,
|
|
|
|
TEMP_CELSIUS,
|
|
|
|
12,
|
|
|
|
),
|
|
|
|
DemoSensor(
|
2020-02-28 20:46:48 +01:00
|
|
|
"sensor_2",
|
|
|
|
"Outside Humidity",
|
|
|
|
54,
|
|
|
|
DEVICE_CLASS_HUMIDITY,
|
2020-09-05 21:09:14 +02:00
|
|
|
PERCENTAGE,
|
2020-02-28 20:46:48 +01:00
|
|
|
None,
|
2019-07-31 12:25:30 -07:00
|
|
|
),
|
|
|
|
]
|
|
|
|
)
|
2015-02-28 07:31:39 -08:00
|
|
|
|
|
|
|
|
2019-11-13 16:37:31 +01: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)
|
|
|
|
|
|
|
|
|
2015-03-21 19:16:13 -07:00
|
|
|
class DemoSensor(Entity):
|
2016-03-08 16:46:34 +01:00
|
|
|
"""Representation of a Demo sensor."""
|
|
|
|
|
2019-11-13 16:37:31 +01:00
|
|
|
def __init__(
|
|
|
|
self, unique_id, name, state, device_class, unit_of_measurement, battery
|
|
|
|
):
|
2016-03-08 16:46:34 +01:00
|
|
|
"""Initialize the sensor."""
|
2019-11-13 16:37:31 +01:00
|
|
|
self._unique_id = unique_id
|
2015-02-28 07:31:39 -08:00
|
|
|
self._name = name
|
|
|
|
self._state = state
|
2018-05-03 19:51:36 +02:00
|
|
|
self._device_class = device_class
|
2015-02-28 07:31:39 -08:00
|
|
|
self._unit_of_measurement = unit_of_measurement
|
2015-03-18 23:02:58 -07:00
|
|
|
self._battery = battery
|
2015-02-28 07:31:39 -08:00
|
|
|
|
2019-11-13 16:37:31 +01: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-16 22:20:31 -07:00
|
|
|
@property
|
|
|
|
def should_poll(self):
|
2016-02-23 06:21:49 +01:00
|
|
|
"""No polling needed for a demo sensor."""
|
2015-03-16 22:20:31 -07:00
|
|
|
return False
|
|
|
|
|
2018-05-03 19:51:36 +02:00
|
|
|
@property
|
|
|
|
def device_class(self):
|
|
|
|
"""Return the device class of the sensor."""
|
|
|
|
return self._device_class
|
|
|
|
|
2015-02-28 07:31:39 -08:00
|
|
|
@property
|
|
|
|
def name(self):
|
2016-02-24 10:38:06 +01:00
|
|
|
"""Return the name of the sensor."""
|
2015-02-28 07:31:39 -08:00
|
|
|
return self._name
|
|
|
|
|
|
|
|
@property
|
|
|
|
def state(self):
|
2016-02-24 10:38:06 +01:00
|
|
|
"""Return the state of the sensor."""
|
2015-02-28 07:31:39 -08:00
|
|
|
return self._state
|
|
|
|
|
2015-03-18 23:02:58 -07:00
|
|
|
@property
|
|
|
|
def unit_of_measurement(self):
|
2016-02-24 10:38:06 +01:00
|
|
|
"""Return the unit this state is expressed in."""
|
2015-03-18 23:02:58 -07:00
|
|
|
return self._unit_of_measurement
|
|
|
|
|
2015-02-28 07:31:39 -08:00
|
|
|
@property
|
2016-02-06 22:28:29 -08:00
|
|
|
def device_state_attributes(self):
|
2016-02-24 10:38:06 +01:00
|
|
|
"""Return the state attributes."""
|
2015-03-18 23:02:58 -07:00
|
|
|
if self._battery:
|
2019-07-31 12:25:30 -07:00
|
|
|
return {ATTR_BATTERY_LEVEL: self._battery}
|