-
Notifications
You must be signed in to change notification settings - Fork 1
/
sensor.py
66 lines (53 loc) · 1.59 KB
/
sensor.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import logging
import asyncio
from datetime import timedelta
import voluptuous as vol
import homeassistant.helpers.config_validation as cv
from homeassistant.components.sensor import PLATFORM_SCHEMA
from homeassistant.const import (
CONF_NAME,
CONF_ICON,
CONF_UNIT_OF_MEASUREMENT,
STATE_UNKNOWN,
)
from homeassistant.helpers.entity import Entity
_LOGGER = logging.getLogger(__name__)
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
{
vol.Optional(CONF_NAME): cv.string,
vol.Optional(CONF_ICON): cv.string,
vol.Optional(CONF_UNIT_OF_MEASUREMENT): cv.string,
}
)
def setup_platform(hass, config, add_entities, discovery_info=None):
"""Set up the Custom Sensor platform."""
name = config.get(CONF_NAME)
icon = config.get(CONF_ICON)
unit = config.get(CONF_UNIT_OF_MEASUREMENT)
add_entities([KartaxBasicSensor(name, icon, unit)], True)
class KartaxBasicSensor(Entity):
def __init__(self, name, icon, unit):
self._name = name
self._icon = icon
self._unit = unit
self._state = STATE_UNKNOWN
self._counter = 0
@property
def name(self):
return self._name
@property
def icon(self):
return self._icon
@property
def state(self):
return self._state
@property
def unit_of_measurement(self):
return self._unit
async def async_added_to_hass(self):
self.hass.helpers.event.async_track_time_interval(
self.update, timedelta(seconds=10)
)
def update(self, now=None):
self._counter += 0.1
self._state = self._counter