Skip to content

Commit aa1a667

Browse files
committed
Add auto sensors creation
1 parent 728ae96 commit aa1a667

File tree

2 files changed

+38
-12
lines changed

2 files changed

+38
-12
lines changed

custom_components/sonoff/__init__.py

+14-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from homeassistant.components.binary_sensor import DEVICE_CLASSES
66
from homeassistant.const import CONF_USERNAME, CONF_PASSWORD, CONF_DEVICES, \
77
CONF_NAME, CONF_DEVICE_CLASS, EVENT_HOMEASSISTANT_STOP, CONF_MODE, \
8-
CONF_SCAN_INTERVAL, CONF_FORCE_UPDATE, CONF_EXCLUDE
8+
CONF_SCAN_INTERVAL, CONF_FORCE_UPDATE, CONF_EXCLUDE, CONF_SENSORS
99
from homeassistant.core import ServiceCall
1010
from homeassistant.helpers import config_validation as cv, discovery
1111
from homeassistant.helpers.aiohttp_client import async_get_clientsession
@@ -40,6 +40,7 @@
4040
vol.Optional(CONF_DEFAULT_CLASS, default='switch'): cv.string,
4141
vol.Optional(CONF_SCAN_INTERVAL): cv.time_period,
4242
vol.Optional(CONF_FORCE_UPDATE): cv.ensure_list,
43+
vol.Optional(CONF_SENSORS): cv.ensure_list,
4344
vol.Optional(CONF_DEBUG, default=False): cv.boolean,
4445
vol.Optional(CONF_DEVICES): {
4546
cv.string: vol.Schema({
@@ -110,6 +111,12 @@ async def async_setup(hass: HomeAssistantType, hass_config: dict):
110111
else:
111112
force_update = None
112113

114+
if CONF_SENSORS in config:
115+
sensors = config[CONF_SENSORS]
116+
_LOGGER.debug(f"Init auto sensors for: {sensors}")
117+
else:
118+
sensors = []
119+
113120
def add_device(deviceid: str, state: dict, *args):
114121
device = registry.devices[deviceid]
115122

@@ -166,6 +173,12 @@ def add_device(deviceid: str, state: dict, *args):
166173
hass.async_create_task(discovery.async_load_platform(
167174
hass, info.pop('component'), DOMAIN, info, hass_config))
168175

176+
for attribute in sensors:
177+
if attribute in state:
178+
info = {'deviceid': deviceid, 'attribute': attribute}
179+
hass.async_create_task(discovery.async_load_platform(
180+
hass, 'sensor', DOMAIN, info, hass_config))
181+
169182
async def send_command(call: ServiceCall):
170183
"""Service for send raw command to device.
171184

custom_components/sonoff/sensor.py

+24-11
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,28 @@
11
from typing import Optional
22

3-
from homeassistant.const import DEVICE_CLASS_TEMPERATURE, TEMP_CELSIUS, \
4-
DEVICE_CLASS_HUMIDITY, DEVICE_CLASS_ILLUMINANCE
3+
from homeassistant.const import DEVICE_CLASS_TEMPERATURE, \
4+
DEVICE_CLASS_HUMIDITY, DEVICE_CLASS_ILLUMINANCE, DEVICE_CLASS_POWER, \
5+
DEVICE_CLASS_SIGNAL_STRENGTH
56
from homeassistant.helpers.entity import Entity
67

78
from . import DOMAIN, EWeLinkRegistry
89
from .sonoff_main import EWeLinkDevice
910

10-
SONOFF_SC = {
11-
'temperature': [DEVICE_CLASS_TEMPERATURE, TEMP_CELSIUS, None],
11+
SENSORS = {
12+
'temperature': [DEVICE_CLASS_TEMPERATURE, '°C', None],
1213
# UNIT_PERCENTAGE is not on old versions
13-
'humidity': [DEVICE_CLASS_HUMIDITY, "%", None],
14+
'humidity': [DEVICE_CLASS_HUMIDITY, '%', None],
1415
'dusty': [None, None, 'mdi:cloud'],
1516
'light': [DEVICE_CLASS_ILLUMINANCE, None, None],
16-
'noise': [None, None, 'mdi:bell-ring']
17+
'noise': [None, None, 'mdi:bell-ring'],
18+
'power': [DEVICE_CLASS_POWER, 'W', None],
19+
'current': [DEVICE_CLASS_POWER, 'A', None],
20+
'voltage': [DEVICE_CLASS_POWER, 'V', None],
21+
'rssi': [DEVICE_CLASS_SIGNAL_STRENGTH, 'dBm', None]
1722
}
1823

24+
SONOFF_SC = {'temperature', 'humidity', 'dusty', 'light', 'noise'}
25+
1926

2027
async def async_setup_platform(hass, config, add_entities,
2128
discovery_info=None):
@@ -24,13 +31,19 @@ async def async_setup_platform(hass, config, add_entities,
2431

2532
deviceid = discovery_info['deviceid']
2633
registry = hass.data[DOMAIN]
34+
35+
if 'attribute' in discovery_info:
36+
add_entities([EWeLinkSensor(registry, deviceid,
37+
discovery_info['attribute'])])
38+
return
39+
2740
device = registry.devices[deviceid]
2841
if device.get('uiid') == 18:
29-
add_entities([SonoffSC(registry, deviceid, attr)
42+
add_entities([EWeLinkSensor(registry, deviceid, attr)
3043
for attr in SONOFF_SC])
3144

3245

33-
class SonoffSC(EWeLinkDevice, Entity):
46+
class EWeLinkSensor(EWeLinkDevice, Entity):
3447
_state = None
3548

3649
def __init__(self, registry: EWeLinkRegistry, deviceid: str, attr: str):
@@ -76,12 +89,12 @@ def available(self) -> bool:
7689

7790
@property
7891
def device_class(self):
79-
return SONOFF_SC[self._attr][0]
92+
return SENSORS[self._attr][0] if self._attr in SENSORS else None
8093

8194
@property
8295
def unit_of_measurement(self):
83-
return SONOFF_SC[self._attr][1]
96+
return SENSORS[self._attr][1] if self._attr in SENSORS else None
8497

8598
@property
8699
def icon(self):
87-
return SONOFF_SC[self._attr][2]
100+
return SENSORS[self._attr][2] if self._attr in SENSORS else None

0 commit comments

Comments
 (0)