core/homeassistant/components/homematic/switch.py

63 lines
1.6 KiB
Python
Raw Normal View History

"""Support for HomeMatic switches."""
2016-06-24 08:06:58 +00:00
import logging
2018-08-21 19:25:16 +00:00
2016-06-24 08:06:58 +00:00
from homeassistant.components.switch import SwitchDevice
from .const import ATTR_DISCOVER_DEVICES
from .entity import HMDevice
2016-06-24 08:06:58 +00:00
_LOGGER = logging.getLogger(__name__)
def setup_platform(hass, config, add_entities, discovery_info=None):
"""Set up the HomeMatic switch platform."""
2016-06-28 20:53:53 +00:00
if discovery_info is None:
return
devices = []
for conf in discovery_info[ATTR_DISCOVER_DEVICES]:
new_device = HMSwitch(conf)
devices.append(new_device)
add_entities(devices, True)
2016-06-24 08:06:58 +00:00
class HMSwitch(HMDevice, SwitchDevice):
"""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 today_energy_kwh(self):
"""Return the current power usage in kWh."""
2016-06-24 08:06:58 +00:00
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."""
self._hmdevice.on(self._channel)
2016-06-24 08:06:58 +00:00
def turn_off(self, **kwargs):
"""Turn the switch off."""
self._hmdevice.off(self._channel)
2016-06-24 08:06:58 +00:00
def _init_data_struct(self):
"""Generate the data dictionary (self._data) from metadata."""
self._state = "STATE"
self._data.update({self._state: None})
2016-06-24 08:06:58 +00:00
# Need sensor values for SwitchPowermeter
for node in self._hmdevice.SENSORNODE:
self._data.update({node: None})