2016-06-24 08:06:58 +00:00
|
|
|
"""
|
2016-06-30 08:33:34 +00:00
|
|
|
Support for Homematic switches.
|
2016-06-24 08:06:58 +00:00
|
|
|
|
|
|
|
For more details about this platform, please refer to the documentation at
|
|
|
|
https://home-assistant.io/components/switch.homematic/
|
|
|
|
"""
|
|
|
|
import logging
|
|
|
|
from homeassistant.components.switch import SwitchDevice
|
2016-11-29 19:53:02 +00:00
|
|
|
from homeassistant.components.homematic import HMDevice
|
2016-06-24 08:06:58 +00:00
|
|
|
from homeassistant.const import STATE_UNKNOWN
|
2016-11-29 19:53:02 +00:00
|
|
|
from homeassistant.loader import get_component
|
2016-06-24 08:06:58 +00:00
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
DEPENDENCIES = ['homematic']
|
|
|
|
|
|
|
|
|
|
|
|
def setup_platform(hass, config, add_callback_devices, discovery_info=None):
|
2016-06-30 08:33:34 +00:00
|
|
|
"""Setup the Homematic switch platform."""
|
2016-06-28 20:53:53 +00:00
|
|
|
if discovery_info is None:
|
|
|
|
return
|
|
|
|
|
2016-11-29 19:53:02 +00:00
|
|
|
homematic = get_component("homematic")
|
2016-09-09 17:33:12 +00:00
|
|
|
return homematic.setup_hmdevice_discovery_helper(
|
2016-11-29 19:53:02 +00:00
|
|
|
hass,
|
2016-09-09 17:33:12 +00:00
|
|
|
HMSwitch,
|
|
|
|
discovery_info,
|
|
|
|
add_callback_devices
|
|
|
|
)
|
2016-06-24 08:06:58 +00:00
|
|
|
|
|
|
|
|
2016-11-29 19:53:02 +00:00
|
|
|
class HMSwitch(HMDevice, SwitchDevice):
|
2016-06-30 08:33:34 +00:00
|
|
|
"""Representation of a Homematic switch."""
|
2016-06-24 08:06:58 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def is_on(self):
|
|
|
|
"""Return True if switch is on."""
|
|
|
|
try:
|
|
|
|
return self._hm_get_state() > 0
|
|
|
|
except TypeError:
|
|
|
|
return False
|
|
|
|
|
|
|
|
@property
|
|
|
|
def current_power_mwh(self):
|
|
|
|
"""Return the current power usage in mWh."""
|
|
|
|
if "ENERGY_COUNTER" in self._data:
|
|
|
|
try:
|
|
|
|
return self._data["ENERGY_COUNTER"] / 1000
|
|
|
|
except ZeroDivisionError:
|
|
|
|
return 0
|
|
|
|
|
|
|
|
return None
|
|
|
|
|
|
|
|
def turn_on(self, **kwargs):
|
|
|
|
"""Turn the switch on."""
|
2017-02-14 22:19:57 +00:00
|
|
|
self._hmdevice.on(self._channel)
|
2016-06-24 08:06:58 +00:00
|
|
|
|
|
|
|
def turn_off(self, **kwargs):
|
|
|
|
"""Turn the switch off."""
|
2017-02-14 22:19:57 +00:00
|
|
|
self._hmdevice.off(self._channel)
|
2016-06-24 08:06:58 +00:00
|
|
|
|
|
|
|
def _init_data_struct(self):
|
2016-06-30 08:33:34 +00:00
|
|
|
"""Generate a data dict (self._data) from the Homematic metadata."""
|
2016-06-24 08:06:58 +00:00
|
|
|
# Use STATE
|
2016-09-09 17:33:12 +00:00
|
|
|
self._state = "STATE"
|
|
|
|
self._data.update({self._state: STATE_UNKNOWN})
|
2016-06-24 08:06:58 +00:00
|
|
|
|
|
|
|
# Need sensor values for SwitchPowermeter
|
2016-09-09 17:33:12 +00:00
|
|
|
for node in self._hmdevice.SENSORNODE:
|
|
|
|
self._data.update({node: STATE_UNKNOWN})
|