-
Notifications
You must be signed in to change notification settings - Fork 79
/
switch.py
104 lines (80 loc) · 3.02 KB
/
switch.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
"""Support for Etekcity VeSync switches."""
import logging
from homeassistant.components.switch import (SwitchDevice)
from . import DOMAIN
_LOGGER = logging.getLogger(__name__)
ENERGY_UPDATE_INT = 21600
def setup_platform(hass, config, add_entities, discovery_info=None):
"""Set up the VeSync switch platform."""
if discovery_info is None:
return
switches = []
manager = hass.data[DOMAIN]['manager']
if manager.outlets is not None and manager.outlets:
if len(manager.outlets) == 1:
count_string = 'switch'
else:
count_string = 'switches'
_LOGGER.info("Discovered %d VeSync %s",
len(manager.outlets), count_string)
if len(manager.outlets) > 1:
for switch in manager.outlets:
switch._energy_update_interval = ENERGY_UPDATE_INT
switches.append(VeSyncSwitchHA(switch))
_LOGGER.info("Added a VeSync switch named '%s'",
switch.device_name)
else:
switches.append(VeSyncSwitchHA(manager.outlets))
else:
_LOGGER.info("No VeSync switches found")
add_entities(switches)
class VeSyncSwitchHA(SwitchDevice):
"""Representation of a VeSync switch."""
def __init__(self, plug):
"""Initialize the VeSync switch device."""
self.smartplug = plug
@property
def unique_id(self):
"""Return the ID of this switch."""
return self.smartplug.cid
@property
def name(self):
"""Return the name of the switch."""
return self.smartplug.device_name
@property
def device_state_attributes(self):
"""Return the state attributes of the device."""
attr = {}
attr['active_time'] = self.smartplug.active_time
attr['voltage'] = self.smartplug.voltage
attr['active_time'] = self.smartplug.active_time
attr['weekly_energy_total'] = self.smartplug.weekly_energy_total
attr['monthly_energy_total'] = self.smartplug.monthly_energy_total
attr['yearly_energy_total'] = self.smartplug.yearly_energy_total
return attr
@property
def current_power_w(self):
"""Return the current power usage in W."""
return self.smartplug.power
@property
def today_energy_kwh(self):
"""Return the today total energy usage in kWh."""
return self.smartplug.energy_today
@property
def available(self) -> bool:
"""Return True if switch is available."""
return self.smartplug.connection_status == "online"
@property
def is_on(self):
"""Return True if switch is on."""
return self.smartplug.device_status == "on"
def turn_on(self, **kwargs):
"""Turn the switch on."""
self.smartplug.turn_on()
def turn_off(self, **kwargs):
"""Turn the switch off."""
self.smartplug.turn_off()
def update(self):
"""Handle data changes for node values."""
self.smartplug.update()
self.smartplug.update_energy()