core/homeassistant/components/insteon_hub.py

96 lines
2.5 KiB
Python
Raw Normal View History

2016-01-19 13:10:17 +00:00
"""
Support for Insteon Hub.
2016-01-27 08:34:14 +00:00
For more details about this component, please refer to the documentation at
2016-02-02 23:23:59 +00:00
https://home-assistant.io/components/insteon_hub/
2016-01-27 08:25:44 +00:00
"""
2016-01-19 13:10:17 +00:00
import logging
2016-02-19 05:27:50 +00:00
2016-01-19 13:10:17 +00:00
import homeassistant.bootstrap as bootstrap
2016-02-19 05:27:50 +00:00
from homeassistant.const import (
ATTR_DISCOVERED, ATTR_SERVICE, CONF_API_KEY, CONF_PASSWORD, CONF_USERNAME,
EVENT_PLATFORM_DISCOVERED)
2016-01-19 13:10:17 +00:00
from homeassistant.helpers import validate_config
from homeassistant.helpers.entity import ToggleEntity
2016-02-19 05:27:50 +00:00
from homeassistant.loader import get_component
2016-01-19 13:10:17 +00:00
2016-01-29 05:37:08 +00:00
DOMAIN = "insteon_hub"
2016-01-27 08:25:44 +00:00
REQUIREMENTS = ['insteon_hub==0.4.5']
2016-01-19 13:10:17 +00:00
INSTEON = None
2016-01-29 05:37:08 +00:00
DISCOVER_LIGHTS = "insteon_hub.lights"
2016-01-19 13:10:17 +00:00
_LOGGER = logging.getLogger(__name__)
def setup(hass, config):
2016-03-08 16:55:57 +00:00
"""Setup Insteon Hub component.
2016-01-19 13:10:17 +00:00
This will automatically import associated lights.
"""
if not validate_config(
config,
{DOMAIN: [CONF_USERNAME, CONF_PASSWORD, CONF_API_KEY]},
2016-01-19 13:10:17 +00:00
_LOGGER):
return False
2016-01-29 02:35:04 +00:00
import insteon
username = config[DOMAIN][CONF_USERNAME]
2016-01-19 13:10:17 +00:00
password = config[DOMAIN][CONF_PASSWORD]
api_key = config[DOMAIN][CONF_API_KEY]
2016-01-19 13:10:17 +00:00
global INSTEON
INSTEON = insteon.Insteon(username, password, api_key)
2016-01-19 13:10:17 +00:00
if INSTEON is None:
_LOGGER.error("Could not connect to Insteon service.")
return
2016-01-19 13:10:17 +00:00
comp_name = 'light'
discovery = DISCOVER_LIGHTS
component = get_component(comp_name)
bootstrap.setup_component(hass, component.DOMAIN, config)
hass.bus.fire(
EVENT_PLATFORM_DISCOVERED,
{ATTR_SERVICE: discovery, ATTR_DISCOVERED: {}})
return True
class InsteonToggleDevice(ToggleEntity):
2016-03-08 16:55:57 +00:00
"""An abstract Class for an Insteon node."""
2016-01-19 13:10:17 +00:00
def __init__(self, node):
2016-03-08 16:55:57 +00:00
"""Initialize the device."""
2016-01-19 13:10:17 +00:00
self.node = node
self._value = 0
@property
def name(self):
2016-03-07 17:49:31 +00:00
"""Return the the name of the node."""
2016-01-19 13:10:17 +00:00
return self.node.DeviceName
@property
def unique_id(self):
2016-03-07 17:49:31 +00:00
"""Return the ID of this insteon node."""
2016-01-19 13:10:17 +00:00
return self.node.DeviceID
def update(self):
2016-03-07 17:49:31 +00:00
"""Update state of the sensor."""
2016-01-19 13:10:17 +00:00
resp = self.node.send_command('get_status', wait=True)
try:
self._value = resp['response']['level']
except KeyError:
pass
@property
def is_on(self):
2016-03-07 17:49:31 +00:00
"""Return the boolean response if the node is on."""
2016-01-19 13:10:17 +00:00
return self._value != 0
def turn_on(self, **kwargs):
2016-03-08 16:55:57 +00:00
"""Turn device on."""
2016-01-19 13:10:17 +00:00
self.node.send_command('on')
def turn_off(self, **kwargs):
2016-03-08 16:55:57 +00:00
"""Turn device off."""
2016-01-19 13:10:17 +00:00
self.node.send_command('off')