def test_known(self): """Test 'known' property.""" self.assertTrue(Position(Parameter(raw=b'\x12\x00')).known) self.assertTrue(Position(Parameter(raw=b'\xC8\x00')).known) self.assertFalse(Position(Parameter(raw=b'\xF7\xFF')).known) # Well, not really know. But at least not unknown: self.assertTrue(Position(Parameter(raw=b'\xD2\x00')).known)
def test_parsing_on_off(self): """Test parsing OnOFf from raw.""" parameter_on = SwitchParameter(Parameter(raw=b'\x00\x00')) self.assertTrue(parameter_on.is_on()) self.assertFalse(parameter_on.is_off()) parameter_off = SwitchParameter(Parameter(raw=b'\xC8\x00')) self.assertFalse(parameter_off.is_on()) self.assertTrue(parameter_off.is_off())
def test_roller_shutter_str(self): """Test string representation of RolllerShutter object.""" pyvlx = PyVLX() roller_shutter = RollerShutter( pyvlx=pyvlx, node_id=23, name="Test Roller Shutter", serial_number="aa:bb:aa:bb:aa:bb:aa:23", position_parameter=Parameter( Parameter.from_int(int(0.97 * Parameter.MAX))), ) self.assertEqual( str(roller_shutter), '<RollerShutter name="Test Roller Shutter" node_id="23" serial_number="aa:bb:aa:bb:aa:bb:aa:23" position="97 %"/>', )
def test_exception(self): """Test wrong initialization of Position.""" with self.assertRaises(PyVLXException): Position(position=2.5) with self.assertRaises(PyVLXException): Position(position=-1) with self.assertRaises(PyVLXException): Position(position=51201) with self.assertRaises(PyVLXException): Position(position_percent=2.5) with self.assertRaises(PyVLXException): Position(position_percent=-1) with self.assertRaises(PyVLXException): Position(position_percent=101) with self.assertRaises(PyVLXException): Parameter(raw=b"\x00\x00\x00") with self.assertRaises(PyVLXException): Parameter(raw="\x00\x00")
def test_str(self): """Test string representation.""" self.assertEqual(str(Position(Parameter(raw=b'\xF7\xFF'))), "UNKNOWN") self.assertEqual(str(Position(position_percent=50)), "50 %")
def test_conversion(self): """Test conversion from one form to other.""" self.assertEqual(Position(Parameter(raw=b'\x0A\x05')).position, 2565) self.assertEqual(Position(position_percent=50).position, 25600) self.assertEqual(Position(position=12345).position_percent, 24)
def test_raw(self): """Test raw assignement.""" self.assertEqual(Position(Parameter(raw=b'\x00\x00')).raw, b'\x00\x00') self.assertEqual(Position(Parameter(raw=b'\x0A\x05')).raw, b'\x0A\x05') self.assertEqual(Position(Parameter(raw=b'\xC8\x00')).raw, b'\xC8\x00')
def test_to_percent(self): """Test position percent output from position int input""" self.assertEqual(Position.to_percent(Parameter.from_int(0)), 0) self.assertEqual(Position.to_percent(Parameter.from_int(1)), 0) self.assertEqual(Position.to_percent(Parameter.from_int(3)), 0) self.assertEqual(Position.to_percent(Parameter.from_int(256)), 1) self.assertEqual(Position.to_percent(Parameter.from_int(512)), 1) self.assertEqual(Position.to_percent(Parameter.from_int(5120)), 10) self.assertEqual(Position.to_percent(Parameter.from_int(12800)), 25) self.assertEqual(Position.to_percent(Parameter.from_int(25600)), 50) self.assertEqual(Position.to_percent(Parameter.from_int(38400)), 75) self.assertEqual(Position.to_percent(Parameter.from_int(50688)), 99) self.assertEqual(Position.to_percent(Parameter.from_int(51050)), 100) self.assertEqual(Position.to_percent(Parameter.from_int(51200)), 100)
def test_fallback_to_unknown(self): self.assertEqual( Parameter(raw=b"\xC8\x01"), Parameter(raw=Parameter.from_int(Parameter.UNKNOWN_VALUE))) self.assertEqual( Parameter(raw=b"\xC9\x00"), Parameter(raw=Parameter.from_int(Parameter.UNKNOWN_VALUE))) self.assertEqual( Parameter(raw=b"\xD8\x00"), Parameter(raw=Parameter.from_int(Parameter.UNKNOWN_VALUE))) self.assertEqual( Parameter(raw=b"\xfe\x01"), Parameter(raw=Parameter.from_int(Parameter.UNKNOWN_VALUE)))