class pdu(utils): """ Power Distribution Manager """ def __init__( self, pduip="192.168.86.10", outlet=1, pdu_type="cyberpower", username="******", password="******", yamlfilename=None, board_name=None, ): self.pduip = pduip self.outlet = outlet self.pdu_type = pdu_type self.username = username self.password = password self.update_defaults_from_yaml(yamlfilename, __class__.__name__, board_name=board_name) if self.pdu_type == "cyberpower": if not self.pduip: raise Exception("pduip must be set for cyberpower config") self.pdu_dev = cpdu.CyberPowerPdu(self.pduip) elif self.pdu_type == "vesync": if not self.username: raise Exception("username must be set for vesync config") if not self.password: raise Exception("password must be set for vesync config") self.pdu_dev = VeSync(self.username, self.password) self.pdu_dev.login() self.pdu_dev.update() else: raise Exception("Unknown PDU type") def power_cycle_board(self): """ Power Cycle Board: OFF, wait 5 seconds, ON """ if self.pdu_type == "cyberpower": self.pdu_dev.set_outlet_on(self.outlet, False) elif self.pdu_type == "vesync": self.pdu_dev.outlets[self.outlet].turn_off() time.sleep(5) if self.pdu_type == "cyberpower": self.pdu_dev.set_outlet_on(self.outlet, True) elif self.pdu_type == "vesync": self.pdu_dev.outlets[self.outlet].turn_on()
class TestVesync(unittest.TestCase): """Test VeSync object initialization.""" def setUp(self): """Setup VeSync argument cases.""" self.vesync_1 = VeSync('*****@*****.**', 'password', 'America/New_York') self.vesync_2 = VeSync('*****@*****.**', 'password') self.vesync_3 = VeSync('*****@*****.**', 'password', None) self.vesync_4 = VeSync('*****@*****.**', 'password') self.vesync_5 = VeSync('', '') self.vesync_6 = VeSync(None, None, None) self.vesync_7 = VeSync(None, 'password') self.vesync_8 = VeSync('*****@*****.**', None) self.vesync_9 = VeSync('*****@*****.**', 'password', 1) def tearDown(self): """Clean up test.""" pass def test_instance(self): """Test VeSync object is successfully initialized.""" self.assertIsInstance(self.vesync_1, VeSync) def test_username(self): """Test invalid username arguments.""" self.assertEqual(self.vesync_1.username, '*****@*****.**') self.assertEqual(self.vesync_5.username, '') self.assertEqual(self.vesync_6.username, None) self.vesync_1.username = '******' self.assertEqual(self.vesync_1.username, '*****@*****.**') def test_password(self): """Test invalid password arguments.""" self.assertEqual(self.vesync_1.password, 'password') self.assertEqual(self.vesync_5.password, '') self.assertEqual(self.vesync_6.password, None) self.vesync_1.password = '******' self.assertEqual(self.vesync_1.password, 'other') def test_hash_password(self): """Test password hash method.""" self.assertEqual(Helpers.hash_password(self.vesync_1.password), '5f4dcc3b5aa765d61d8327deb882cf99') self.assertEqual(Helpers.hash_password(self.vesync_5.password), 'd41d8cd98f00b204e9800998ecf8427e') with self.assertRaises(AttributeError): Helpers.hash_password(self.vesync_6.password) def test_time_zone(self): """Test time zone argument handling.""" self.assertEqual(self.vesync_1.time_zone, 'America/New_York') self.assertEqual(self.vesync_2.time_zone, 'America/New_York') self.assertEqual(self.vesync_3.time_zone, 'America/New_York') self.assertEqual(self.vesync_9.time_zone, 'America/New_York') self.vesync_1.time_zone = 'America/East' self.assertEqual(self.vesync_1.time_zone, 'America/East') def test_login(self): """Test login method.""" mock_vesync = mock.Mock() mock_vesync.login.return_value = True self.assertTrue(mock_vesync.login()) mock_vesync.login.return_value = False self.assertFalse(mock_vesync.login()) with patch('pyvesync_v2.helpers.Helpers.call_api') as mocked_post: d = {"result": {"accountID": "12346536", "userType": "1", "token": "somevaluehere"}, "code": 0} mocked_post.return_value = (d, 200) data = self.vesync_1.login() body = Helpers.req_body(self.vesync_1, 'login') body['email'] = self.vesync_1.username body['password'] = Helpers.hash_password(self.vesync_1.password) mocked_post.assert_called_with('/cloud/v1/user/login', 'post', json=body) self.assertTrue(data)
from pyvesync_v2 import VeSync import os USER = os.getenv('VESYNC_USER') PASSWORD= os.getenv('VESYNC_PASSWORD') manager = VeSync(USER, PASSWORD, "America/New_York") manager.login() # Get/Update Devices from server - populate device lists manager.update() # Display outlet device information for device in manager.outlets: print(device.display_json())