mirror of
https://github.com/home-assistant/core.git
synced 2025-07-17 18:27:09 +00:00
Used validate_config to ensure 'host' parameter in edimax config. Added name option to edimax config
This commit is contained in:
parent
f6811e858a
commit
6a239bf18a
@ -6,9 +6,17 @@ Support for Edimax switches.
|
|||||||
"""
|
"""
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
from homeassistant.components.switch import SwitchDevice
|
from homeassistant.helpers import validate_config
|
||||||
from homeassistant.const import CONF_HOST, CONF_USERNAME, CONF_PASSWORD
|
from homeassistant.components.switch import SwitchDevice, DOMAIN
|
||||||
|
from homeassistant.const import CONF_HOST, CONF_USERNAME, CONF_PASSWORD, CONF_NAME
|
||||||
|
|
||||||
|
# constants
|
||||||
|
DEFAULT_USERNAME = 'admin'
|
||||||
|
DEFAULT_PASSWORD = '1234'
|
||||||
|
DEVICE_DEFAULT_NAME = 'Edimax Smart Plug'
|
||||||
|
|
||||||
|
# setup logger
|
||||||
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
# pylint: disable=unused-argument
|
# pylint: disable=unused-argument
|
||||||
def setup_platform(hass, config, add_devices_callback, discovery_info=None):
|
def setup_platform(hass, config, add_devices_callback, discovery_info=None):
|
||||||
@ -17,33 +25,34 @@ def setup_platform(hass, config, add_devices_callback, discovery_info=None):
|
|||||||
# pylint: disable=no-name-in-module, import-error
|
# pylint: disable=no-name-in-module, import-error
|
||||||
from pyedimax.smartplug import SmartPlug
|
from pyedimax.smartplug import SmartPlug
|
||||||
except ImportError:
|
except ImportError:
|
||||||
logging.getLogger(__name__).exception((
|
_LOGGER.error('Failed to import pyedimax')
|
||||||
"Failed to import pyedimax. "))
|
|
||||||
|
|
||||||
return
|
|
||||||
|
|
||||||
host = config.get(CONF_HOST)
|
|
||||||
auth = (config.get(CONF_USERNAME, 'admin'),
|
|
||||||
config.get(CONF_PASSWORD, '1234'))
|
|
||||||
|
|
||||||
if not host:
|
|
||||||
logging.getLogger(__name__).error(
|
|
||||||
'Missing config variable %s', CONF_HOST)
|
|
||||||
return False
|
return False
|
||||||
|
|
||||||
add_devices_callback([SmartPlugSwitch(SmartPlug(host, auth))])
|
# pylint: disable=global-statement
|
||||||
|
# check for required values in configuration file
|
||||||
|
if not validate_config({DOMAIN: config},
|
||||||
|
{DOMAIN: [CONF_HOST]},
|
||||||
|
_LOGGER):
|
||||||
|
return False
|
||||||
|
|
||||||
|
host = config.get(CONF_HOST)
|
||||||
|
auth = (config.get(CONF_USERNAME, DEFAULT_USERNAME),
|
||||||
|
config.get(CONF_PASSWORD, DEFAULT_PASSWORD))
|
||||||
|
name = config.get(CONF_NAME, DEVICE_DEFAULT_NAME)
|
||||||
|
|
||||||
|
add_devices_callback([SmartPlugSwitch(SmartPlug(host, auth), name)])
|
||||||
|
|
||||||
|
|
||||||
class SmartPlugSwitch(SwitchDevice):
|
class SmartPlugSwitch(SwitchDevice):
|
||||||
""" Represents an Edimax Smart Plug switch within Home Assistant. """
|
""" Represents an Edimax Smart Plug switch within Home Assistant. """
|
||||||
def __init__(self, smartplug):
|
def __init__(self, smartplug, name):
|
||||||
self.smartplug = smartplug
|
self.smartplug = smartplug
|
||||||
|
self._name = name
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def name(self):
|
def name(self):
|
||||||
""" Returns the name of the Smart Plug, if any. """
|
""" Returns the name of the Smart Plug, if any. """
|
||||||
#TODO: dynamically get name from device using requests
|
return self._name
|
||||||
return 'Edimax Smart Plug'
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def current_power_mwh(self):
|
def current_power_mwh(self):
|
||||||
|
Loading…
x
Reference in New Issue
Block a user