Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_decorated_device_notification(self):
plc = pyads.Connection(TEST_SERVER_AMS_NET_ID, TEST_SERVER_AMS_PORT)
@plc.notification(pyads.PLCTYPE_INT)
def callback(handle, name, timestamp, value):
print (handle, name, timestamp, value)
with plc:
plc.add_device_notification("a", pyads.NotificationAttrib(20), callback)
plc.write_by_name("a", 1, pyads.PLCTYPE_INT)
def test_methods_with_closed_port(self):
# type: () -> None
"""Test pyads.Connection methods with no open port."""
with self.plc:
adr = self.plc.get_local_address()
self.assertIsNotNone(adr)
plc = pyads.Connection("127.0.0.1.1.1", 851)
self.assertIsNone(plc.get_local_address())
self.assertIsNone(plc.read_state())
self.assertIsNone(plc.read_device_info())
self.assertIsNone(plc.read_write(1, 2, pyads.PLCTYPE_INT, 1, pyads.PLCTYPE_INT))
self.assertIsNone(plc.read(1, 2, pyads.PLCTYPE_INT))
self.assertIsNone(plc.read_by_name("hello", pyads.PLCTYPE_INT))
self.assertIsNone(plc.get_handle("hello"))
self.assertIsNone(
plc.read_structure_by_name(
"hello", (("", pyads.PLCTYPE_BOOL, 1), ("", pyads.PLCTYPE_BOOL, 1))
)
)
self.assertIsNone(
plc.add_device_notification(
"test", pyads.NotificationAttrib(4), lambda x: x
)
def test_methods_with_closed_port(self):
# type: () -> None
"""Test pyads.Connection methods with no open port."""
with self.plc:
adr = self.plc.get_local_address()
self.assertIsNotNone(adr)
plc = pyads.Connection("127.0.0.1.1.1", 851)
self.assertIsNone(plc.get_local_address())
self.assertIsNone(plc.read_state())
self.assertIsNone(plc.read_device_info())
self.assertIsNone(plc.read_write(1, 2, pyads.PLCTYPE_INT, 1, pyads.PLCTYPE_INT))
self.assertIsNone(plc.read(1, 2, pyads.PLCTYPE_INT))
self.assertIsNone(plc.read_by_name("hello", pyads.PLCTYPE_INT))
self.assertIsNone(plc.get_handle("hello"))
self.assertIsNone(
plc.read_structure_by_name(
"hello", (("", pyads.PLCTYPE_BOOL, 1), ("", pyads.PLCTYPE_BOOL, 1))
)
)
self.assertIsNone(
plc.add_device_notification(
"test", pyads.NotificationAttrib(4), lambda x: x
)
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 119, 105,
116, 104, 32, 80, 121, 116, 104, 111, 110, 32, 117, 115, 105,
110, 103, 32, 112, 121, 97, 100, 115, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 131, 66, 0, 128, 179, 66, 0, 224, 121,
68, 0, 3, 89, 71, 247, 95, 0, 0, 116, 88, 169, 255, 12, 0, 0,
0, 119, 254, 255, 255, 0, 0, 0, 0, 173, 175, 253, 127, 255,
255, 161, 134, 1, 0, 210, 2, 150, 73, 76, 0, 0, 0, 70, 2, 0,
0, 104, 218, 154, 5, 119, 8, 0, 0, 119, 8, 219, 3, 24, 246,
16, 22, 166, 2, 174, 10, 248, 170, 200, 127, 255, 1, 0, 1, 0,
55, 177, 0, 64, 66, 15]
# fmt: on
self.assertEqual(values, pyads.dict_from_bytes(bytes_list, structure_def))
# test for PLC_DEFAULT_STRING_SIZE
structure_def = (
("iVar", pyads.PLCTYPE_INT, 1),
("bVar", pyads.PLCTYPE_BOOL, 1),
("sVar", pyads.PLCTYPE_STRING, 1),
("iVar2", pyads.PLCTYPE_DINT, 1),
)
values = OrderedDict(
[
("iVar", 32767),
("bVar", True),
("sVar", "Testing the default string size of 80"),
("iVar2", -25600000),
]
)
# fmt: off
bytes_list = [255, 127, 1, 84, 101, 115, 116, 105, 110, 103, 32, 116, 104,
101, 32, 100, 101, 102, 97, 117, 108, 116, 32, 115, 116, 114,
105, 110, 103, 32, 115, 105, 122, 101, 32, 111, 102, 32, 56,
def test_dict_from_bytes(self):
# type: () -> None
"""Test dict_from_bytes function"""
# tests for known values
structure_def = (
("rVar", pyads.PLCTYPE_LREAL, 1),
("sVar", pyads.PLCTYPE_STRING, 2, 35),
("rVar1", pyads.PLCTYPE_REAL, 4),
("iVar", pyads.PLCTYPE_DINT, 5),
("iVar1", pyads.PLCTYPE_INT, 3),
("ivar2", pyads.PLCTYPE_UDINT, 6),
("iVar3", pyads.PLCTYPE_UINT, 7),
("iVar4", pyads.PLCTYPE_BYTE, 1),
("iVar5", pyads.PLCTYPE_SINT, 1),
("iVar6", pyads.PLCTYPE_USINT, 1),
("bVar", pyads.PLCTYPE_BOOL, 4),
("iVar7", pyads.PLCTYPE_WORD, 1),
("iVar8", pyads.PLCTYPE_DWORD, 1),
)
values = OrderedDict(
[
("rVar", 1.11),
("sVar", ["Hello", "World"]),
("rVar1", [2.25, 2.25, 2.5, 2.75]),
("iVar", [3, 4, 5, 6, 7]),
("iVar1", [8, 9, 10]),
@plc.notification(pyads.PLCTYPE_INT)
def callback(handle, name, timestamp, value):
print (handle, name, timestamp, value)
def test_ads_stamp_header(self):
dt = datetime.datetime(2017, 9, 1, 10, 10, 10)
filetime = dt_to_filetime(dt)
stamp_header = structs.AdsStampHeader(
timestamp=filetime,
samples=[
structs.AdsNotificationSample(
handle=1,
sample_size=ctypes.sizeof(pyads.PLCTYPE_INT),
data=struct.pack("
client = pyads.Connection(net_id, port, ip_address)
AdsHub.ADS_TYPEMAP = {
ADSTYPE_BOOL: pyads.PLCTYPE_BOOL,
ADSTYPE_BYTE: pyads.PLCTYPE_BYTE,
ADSTYPE_DINT: pyads.PLCTYPE_DINT,
ADSTYPE_INT: pyads.PLCTYPE_INT,
ADSTYPE_UDINT: pyads.PLCTYPE_UDINT,
ADSTYPE_UINT: pyads.PLCTYPE_UINT,
}
AdsHub.ADSError = pyads.ADSError
AdsHub.PLCTYPE_BOOL = pyads.PLCTYPE_BOOL
AdsHub.PLCTYPE_BYTE = pyads.PLCTYPE_BYTE
AdsHub.PLCTYPE_DINT = pyads.PLCTYPE_DINT
AdsHub.PLCTYPE_INT = pyads.PLCTYPE_INT
AdsHub.PLCTYPE_UDINT = pyads.PLCTYPE_UDINT
AdsHub.PLCTYPE_UINT = pyads.PLCTYPE_UINT
try:
ads = AdsHub(client)
except pyads.ADSError:
_LOGGER.error(
"Could not connect to ADS host (netid=%s, ip=%s, port=%s)",
net_id,
ip_address,
port,
)
return False
hass.data[DATA_ADS] = ads
hass.bus.listen(EVENT_HOMEASSISTANT_STOP, ads.shutdown)
"""Set up the ADS component."""
import pyads
conf = config[DOMAIN]
net_id = conf.get(CONF_DEVICE)
ip_address = conf.get(CONF_IP_ADDRESS)
port = conf.get(CONF_PORT)
client = pyads.Connection(net_id, port, ip_address)
AdsHub.ADS_TYPEMAP = {
ADSTYPE_BOOL: pyads.PLCTYPE_BOOL,
ADSTYPE_BYTE: pyads.PLCTYPE_BYTE,
ADSTYPE_DINT: pyads.PLCTYPE_DINT,
ADSTYPE_INT: pyads.PLCTYPE_INT,
ADSTYPE_UDINT: pyads.PLCTYPE_UDINT,
ADSTYPE_UINT: pyads.PLCTYPE_UINT,
}
AdsHub.ADSError = pyads.ADSError
AdsHub.PLCTYPE_BOOL = pyads.PLCTYPE_BOOL
AdsHub.PLCTYPE_BYTE = pyads.PLCTYPE_BYTE
AdsHub.PLCTYPE_DINT = pyads.PLCTYPE_DINT
AdsHub.PLCTYPE_INT = pyads.PLCTYPE_INT
AdsHub.PLCTYPE_UDINT = pyads.PLCTYPE_UDINT
AdsHub.PLCTYPE_UINT = pyads.PLCTYPE_UINT
try:
ads = AdsHub(client)
except pyads.ADSError:
_LOGGER.error(