2015-07-21 03:16:54 +00:00
|
|
|
"""
|
|
|
|
homeassistant.components.switch.edimax
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
Support for Edimax switches.
|
|
|
|
"""
|
|
|
|
import logging
|
|
|
|
|
|
|
|
from homeassistant.components.switch import SwitchDevice
|
2015-07-21 06:27:25 +00:00
|
|
|
from homeassistant.const import CONF_HOST, CONF_USERNAME, CONF_PASSWORD
|
2015-07-21 03:16:54 +00:00
|
|
|
|
2015-07-26 01:46:47 +00:00
|
|
|
|
2015-07-21 03:16:54 +00:00
|
|
|
# pylint: disable=unused-argument
|
|
|
|
def setup_platform(hass, config, add_devices_callback, discovery_info=None):
|
|
|
|
""" Find and return Edimax Smart Plugs. """
|
|
|
|
try:
|
|
|
|
# pylint: disable=no-name-in-module, import-error
|
2015-07-29 07:24:42 +00:00
|
|
|
from pyedimax.smartplug import SmartPlug
|
2015-07-21 03:16:54 +00:00
|
|
|
except ImportError:
|
|
|
|
logging.getLogger(__name__).exception((
|
2015-07-29 07:24:42 +00:00
|
|
|
"Failed to import pyedimax. "))
|
2015-07-21 03:16:54 +00:00
|
|
|
|
|
|
|
return
|
|
|
|
|
2015-07-21 06:27:25 +00:00
|
|
|
host = config.get(CONF_HOST)
|
2015-07-26 01:46:47 +00:00
|
|
|
auth = (config.get(CONF_USERNAME, 'admin'),
|
|
|
|
config.get(CONF_PASSWORD, '1234'))
|
2015-07-21 06:27:25 +00:00
|
|
|
|
|
|
|
if not host:
|
2015-07-26 01:46:47 +00:00
|
|
|
logging.getLogger(__name__).error(
|
|
|
|
'Missing config variable %s', CONF_HOST)
|
2015-07-21 06:27:25 +00:00
|
|
|
return False
|
|
|
|
|
|
|
|
add_devices_callback([SmartPlugSwitch(SmartPlug(host, auth))])
|
2015-07-21 03:16:54 +00:00
|
|
|
|
|
|
|
|
|
|
|
class SmartPlugSwitch(SwitchDevice):
|
2015-07-26 06:59:48 +00:00
|
|
|
""" Represents an Edimax Smart Plug switch within Home Assistant. """
|
2015-07-21 03:16:54 +00:00
|
|
|
def __init__(self, smartplug):
|
|
|
|
self.smartplug = smartplug
|
|
|
|
|
2015-07-21 06:27:25 +00:00
|
|
|
@property
|
|
|
|
def name(self):
|
|
|
|
""" Returns the name of the Smart Plug, if any. """
|
|
|
|
#TODO: dynamically get name from device using requests
|
|
|
|
return 'Edimax Smart Plug'
|
|
|
|
|
2015-07-26 01:46:47 +00:00
|
|
|
@property
|
|
|
|
def current_power_mwh(self):
|
|
|
|
""" Current power usage in mwh. """
|
|
|
|
try:
|
|
|
|
return float(self.smartplug.now_power) / 1000000.0
|
|
|
|
except ValueError:
|
|
|
|
return None
|
|
|
|
|
|
|
|
@property
|
|
|
|
def today_power_mw(self):
|
|
|
|
""" Today total power usage in mw. """
|
|
|
|
try:
|
|
|
|
return float(self.smartplug.now_energy_day) / 1000.0
|
|
|
|
except ValueError:
|
|
|
|
return None
|
|
|
|
|
2015-07-21 03:16:54 +00:00
|
|
|
@property
|
|
|
|
def is_on(self):
|
|
|
|
""" True if switch is on. """
|
2015-07-21 06:27:25 +00:00
|
|
|
return self.smartplug.state == 'ON'
|
2015-07-21 03:16:54 +00:00
|
|
|
|
|
|
|
def turn_on(self, **kwargs):
|
|
|
|
""" Turns the switch on. """
|
|
|
|
self.smartplug.state = 'ON'
|
|
|
|
|
|
|
|
def turn_off(self):
|
|
|
|
""" Turns the switch off. """
|
2015-07-26 01:46:47 +00:00
|
|
|
self.smartplug.state = 'OFF'
|