core/homeassistant/components/vera/switch.py

59 lines
1.6 KiB
Python
Raw Normal View History

"""Support for Vera switches."""
2015-03-02 10:09:00 +00:00
import logging
2016-02-19 05:27:50 +00:00
from homeassistant.components.switch import ENTITY_ID_FORMAT, SwitchDevice
from homeassistant.util import convert
from . import VERA_CONTROLLER, VERA_DEVICES, VeraDevice
2015-09-09 03:11:25 +00:00
_LOGGER = logging.getLogger(__name__)
2015-03-02 10:09:00 +00:00
2015-03-08 14:58:11 +00:00
def setup_platform(hass, config, add_entities, discovery_info=None):
"""Set up the Vera switches."""
add_entities(
2019-07-31 19:25:30 +00:00
[
VeraSwitch(device, hass.data[VERA_CONTROLLER])
for device in hass.data[VERA_DEVICES]["switch"]
],
True,
)
2015-03-02 10:09:00 +00:00
2015-03-08 14:58:11 +00:00
2016-03-15 09:17:09 +00:00
class VeraSwitch(VeraDevice, SwitchDevice):
2016-03-08 12:35:39 +00:00
"""Representation of a Vera Switch."""
2015-03-02 10:09:00 +00:00
2016-03-15 09:17:09 +00:00
def __init__(self, vera_device, controller):
2016-03-08 12:35:39 +00:00
"""Initialize the Vera device."""
2016-03-15 09:17:09 +00:00
self._state = False
VeraDevice.__init__(self, vera_device, controller)
self.entity_id = ENTITY_ID_FORMAT.format(self.vera_id)
2015-03-02 10:09:00 +00:00
def turn_on(self, **kwargs):
2016-03-08 12:35:39 +00:00
"""Turn device on."""
2015-03-02 10:09:00 +00:00
self.vera_device.switch_on()
self._state = True
self.schedule_update_ha_state()
2015-03-02 10:09:00 +00:00
def turn_off(self, **kwargs):
2016-03-08 12:35:39 +00:00
"""Turn device off."""
2015-03-02 10:09:00 +00:00
self.vera_device.switch_off()
self._state = False
self.schedule_update_ha_state()
2015-03-02 10:09:00 +00:00
@property
def current_power_w(self):
"""Return the current power usage in W."""
power = self.vera_device.power
if power:
return convert(power, float, 0.0)
2015-03-02 10:09:00 +00:00
@property
def is_on(self):
2016-03-08 12:35:39 +00:00
"""Return true if device is on."""
2016-03-15 09:17:09 +00:00
return self._state
def update(self):
"""Update device state."""
2016-03-15 09:17:09 +00:00
self._state = self.vera_device.is_switched_on()