Make asuswrt sensor optional (#19736) @kennedyshead

* Dont load if not in config

* Adding config options for sensors

* Fixed mistake with iterating over wrong things

* lint

* lint

* Setting None state

* Using .get when fetching optional config
This commit is contained in:
kennedyshead 2019-01-08 15:14:16 +01:00 committed by Eric Nagley
parent 410f19c777
commit 377b129c9c
2 changed files with 23 additions and 8 deletions

View File

@ -26,6 +26,8 @@ CONF_SSH_KEY = 'ssh_key'
CONF_REQUIRE_IP = 'require_ip'
DEFAULT_SSH_PORT = 22
SECRET_GROUP = 'Password or SSH Key'
CONF_SENSORS = 'sensors'
SENSOR_TYPES = ['upload_speed', 'download_speed', 'download', 'upload']
CONFIG_SCHEMA = vol.Schema({
DOMAIN: vol.Schema({
@ -37,7 +39,9 @@ CONFIG_SCHEMA = vol.Schema({
vol.Optional(CONF_REQUIRE_IP, default=True): cv.boolean,
vol.Exclusive(CONF_PASSWORD, SECRET_GROUP): cv.string,
vol.Exclusive(CONF_SSH_KEY, SECRET_GROUP): cv.isfile,
vol.Exclusive(CONF_PUB_KEY, SECRET_GROUP): cv.isfile
vol.Exclusive(CONF_PUB_KEY, SECRET_GROUP): cv.isfile,
vol.Optional(CONF_SENSORS): vol.All(
cv.ensure_list, [vol.In(SENSOR_TYPES)]),
}),
}, extra=vol.ALLOW_EXTRA)
@ -62,7 +66,8 @@ async def async_setup(hass, config):
hass.data[DATA_ASUSWRT] = api
hass.async_create_task(async_load_platform(
hass, 'sensor', DOMAIN, {}, config))
hass, 'sensor', DOMAIN, config[DOMAIN].get(CONF_SENSORS), config))
hass.async_create_task(async_load_platform(
hass, 'device_tracker', DOMAIN, {}, config))
return True

View File

@ -17,13 +17,23 @@ _LOGGER = logging.getLogger(__name__)
async def async_setup_platform(
hass, config, add_entities, discovery_info=None):
"""Set up the asuswrt sensors."""
if discovery_info is None:
return
api = hass.data[DATA_ASUSWRT]
add_entities([
AsuswrtRXSensor(api),
AsuswrtTXSensor(api),
AsuswrtTotalRXSensor(api),
AsuswrtTotalTXSensor(api)
])
devices = []
if 'download' in discovery_info:
devices.append(AsuswrtTotalRXSensor(api))
if 'upload' in discovery_info:
devices.append(AsuswrtTotalTXSensor(api))
if 'download_speed' in discovery_info:
devices.append(AsuswrtRXSensor(api))
if 'upload_speed' in discovery_info:
devices.append(AsuswrtTXSensor(api))
add_entities(devices)
class AsuswrtSensor(Entity):