Skip to content

Instantly share code, notes, and snippets.

@DasBasti
Created September 5, 2016 18:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DasBasti/b7f5c34bd81517651698abdea8c59192 to your computer and use it in GitHub Desktop.
Save DasBasti/b7f5c34bd81517651698abdea8c59192 to your computer and use it in GitHub Desktop.
"""
Support for monitoring energy usage using the D-link W215 smart switch.
For more details about this platform, please refer to the documentation at
https://home-assistant.io/components/sensor.dlink/
"""
import logging
import voluptuous as vol
from homeassistant.helpers.entity import Entity
from homeassistant.components.sensor import PLATFORM_SCHEMA
import homeassistant.helpers.config_validation as cv
from homeassistant.const import (
CONF_HOST, CONF_NAME, CONF_PASSWORD, CONF_USERNAME)
REQUIREMENTS = ['https://github.com/LinuxChristian/pyW215/archive/'
'v0.1.1.zip#pyW215==0.1.1']
_LOGGER = logging.getLogger(__name__)
DEFAULT_NAME = 'D-link Smart Plug W215'
DEFAULT_PASSWORD = ''
DEFAULT_USERNAME = 'admin'
ICON = 'mdi:flash'
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
vol.Required(CONF_HOST): cv.string,
vol.Required(CONF_USERNAME, default=DEFAULT_USERNAME): cv.string,
vol.Required(CONF_PASSWORD, default=DEFAULT_PASSWORD): cv.string,
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
})
# pylint: disable=unused-argument
def setup_platform(hass, config, add_devices, discovery_info=None):
"""Setup a D-Link Smart Plug."""
from pyW215.pyW215 import SmartPlug
host = config.get(CONF_HOST)
username = config.get(CONF_USERNAME)
password = config.get(CONF_PASSWORD)
name = config.get(CONF_NAME)
add_devices([SmartPlugSensor(SmartPlug(host, password, username), name)])
# pylint: disable=too-many-instance-attributes
class SmartPlugSensor(Entity):
"""Implementation of a DLink Smart Plug sensor."""
def __init__(self, smartplug, name):
"""Initialize the sensor."""
self._unit_of_measurement = "W"
self._state = None
self.smartplug = smartplug
self._name = name
@property
def name(self):
"""Return the name of the sensor."""
return self._name
@property
def state(self):
"""Return the state of the sensor."""
return self._state
@property
def unit_of_measurement(self):
"""Return the unit of measurement of this entity, if any."""
return self._unit_of_measurement
@property
def icon(self):
"""Icon to use in the frontend, if any."""
return ICON
def update(self):
"""Return the current power usage in Watt."""
try:
self._state = float(self.smartplug.current_consumption)
except ValueError:
return None
@gianler
Copy link

gianler commented May 1, 2021

Excuse me, I'm a newbie.. I'm interested to install to my Home Assistant. The plug works fine as switch. How can implement also consumption in HA? I see this file but I don't know hot to do. Thank you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment