def __init__(self, xknx, name, group_address_long=None, group_address_short=None, group_address_position=None, group_address_position_feedback=None, travel_time_down=DEFAULT_TRAVEL_TIME_DOWN, travel_time_up=DEFAULT_TRAVEL_TIME_UP): # pylint: disable=too-many-arguments Device.__init__(self, xknx, name) if isinstance(group_address_long, (str, int)): group_address_long = Address(group_address_long) if isinstance(group_address_short, (str, int)): group_address_short = Address(group_address_short) if isinstance(group_address_position, (str, int)): group_address_position = Address(group_address_position) if isinstance(group_address_position_feedback, (str, int)): group_address_position_feedback = \ Address(group_address_position_feedback) self.group_address_long = group_address_long self.group_address_short = group_address_short self.group_address_position = group_address_position self.group_address_position_feedback = group_address_position_feedback self.travel_time_down = travel_time_down self.travel_time_up = travel_time_up self.travelcalculator = TravelCalculator(travel_time_down, travel_time_up)
async def main(): """Connect to a tunnel, send 2 telegrams and disconnect.""" xknx = XKNX() gatewayscanner = GatewayScanner(xknx) await gatewayscanner.start() if not gatewayscanner.found: print("No Gateways found") return src_address = Address("15.15.249") print("Connecting to {}:{} from {}".format(gatewayscanner.found_ip_addr, gatewayscanner.found_port, gatewayscanner.found_local_ip)) tunnel = Tunnel(xknx, src_address, local_ip=gatewayscanner.found_local_ip, gateway_ip=gatewayscanner.found_ip_addr, gateway_port=gatewayscanner.found_port) await tunnel.connect_udp() await tunnel.connect() await tunnel.send_telegram( Telegram(Address('1/0/15'), payload=DPTBinary(1))) await asyncio.sleep(2) await tunnel.send_telegram( Telegram(Address('1/0/15'), payload=DPTBinary(0))) await asyncio.sleep(2) await tunnel.connectionstate() await tunnel.disconnect()
def __init__(self, xknx, name, group_address_switch=None, group_address_switch_state=None, group_address_brightness=None, group_address_brightness_state=None, device_updated_cb=None): """Initialize Light class.""" # pylint: disable=too-many-arguments Device.__init__(self, xknx, name, device_updated_cb) if isinstance(group_address_brightness, (str, int)): group_address_brightness = Address(group_address_brightness) if isinstance(group_address_brightness_state, (str, int)): group_address_brightness_state = Address(group_address_brightness_state) self.switch = RemoteValueSwitch1001( xknx, group_address_switch, group_address_switch_state, after_update_cb=self.after_update) self.group_address_brightness = group_address_brightness self.group_address_brightness_state = group_address_brightness_state self.brightness = 0 self.supports_dimming = \ group_address_brightness is not None
def test_telegram_set(self): """Test parsing and streaming CEMIFrame KNX/IP packet with DPTArray/DPTTime as payload.""" xknx = XKNX(loop=self.loop) knxipframe = KNXIPFrame(xknx) knxipframe.init(KNXIPServiceType.ROUTING_INDICATION) knxipframe.body.src_addr = Address("1.2.2") telegram = Telegram() telegram.group_address = Address(337) telegram.payload = DPTArray(DPTTime().to_knx({ 'hours': 13, 'minutes': 23, 'seconds': 42 })) knxipframe.body.telegram = telegram knxipframe.body.set_hops(5) knxipframe.normalize() raw = ((0x06, 0x10, 0x05, 0x30, 0x00, 0x14, 0x29, 0x00, 0xbc, 0xd0, 0x12, 0x02, 0x01, 0x51, 0x04, 0x00, 0x80, 13, 23, 42)) self.assertEqual(knxipframe.header.to_knx(), list(raw[0:6])) self.assertEqual(knxipframe.body.to_knx(), list(raw[6:])) self.assertEqual(knxipframe.to_knx(), list(raw))
def build_and_destroy_tunnel(xknx): gatewayscanner = GatewayScanner(xknx) yield from gatewayscanner.async_start() if not gatewayscanner.found: print("No Gateways found") return src_address = Address("15.15.249") print("Connecting to {}:{} from {}".format(gatewayscanner.found_ip_addr, gatewayscanner.found_port, gatewayscanner.found_local_ip)) tunnel = Tunnel(xknx, src_address, local_ip=gatewayscanner.found_local_ip, gateway_ip=gatewayscanner.found_ip_addr, gateway_port=gatewayscanner.found_port) yield from tunnel.connect_udp() yield from tunnel.connect() yield from tunnel.send_telegram( Telegram(Address('1/0/15'), payload=DPTBinary(0))) yield from asyncio.sleep(2) yield from tunnel.send_telegram( Telegram(Address('1/0/15'), payload=DPTBinary(1))) yield from asyncio.sleep(2) yield from tunnel.connectionstate() yield from tunnel.disconnect()
def test_EndTOEnd_group_response(self): """Test parsing and streaming CEMIFrame KNX/IP packet, group response.""" # Incoming state raw = ((0x06, 0x10, 0x05, 0x30, 0x00, 0x11, 0x29, 0x00, 0xbc, 0xd0, 0x13, 0x01, 0x01, 0x88, 0x01, 0x00, 0x41)) xknx = XKNX(loop=self.loop) knxipframe = KNXIPFrame(xknx) knxipframe.from_knx(raw) telegram = knxipframe.body.telegram self.assertEqual( telegram, Telegram(Address("392"), TelegramType.GROUP_RESPONSE, payload=DPTBinary(1))) knxipframe2 = KNXIPFrame(xknx) knxipframe2.init(KNXIPServiceType.ROUTING_INDICATION) knxipframe2.body.src_addr = Address("1.3.1") knxipframe2.body.telegram = telegram knxipframe2.body.set_hops(5) knxipframe2.normalize() self.assertEqual(knxipframe2.header.to_knx(), list(raw[0:6])) self.assertEqual(knxipframe2.body.to_knx(), list(raw[6:])) self.assertEqual(knxipframe2.to_knx(), list(raw))
def __init__(self, xknx, name, group_address_switch=None, group_address_state=None, group_address_dimm=None, group_address_brightness=None): # pylint: disable=too-many-arguments Device.__init__(self, xknx, name) if isinstance(group_address_switch, (str, int)): group_address_switch = Address(group_address_switch) if isinstance(group_address_state, (str, int)): group_address_state = Address(group_address_state) if isinstance(group_address_dimm, (str, int)): group_address_dimm = Address(group_address_dimm) if isinstance(group_address_brightness, (str, int)): group_address_brightness = Address(group_address_brightness) self.group_address_switch = group_address_switch self.group_address_dimm = group_address_dimm self.group_address_brightness = group_address_brightness self.group_address_state = group_address_state self.state = False self.brightness = 0 self.supports_dimming = \ group_address_brightness is not None \ or group_address_dimm is not None
def test_connect_request(self): """Test parsing and streaming connection tunneling request KNX/IP packet.""" raw = ((0x06, 0x10, 0x04, 0x20, 0x00, 0x15, 0x04, 0x01, 0x17, 0x00, 0x11, 0x00, 0xbc, 0xe0, 0x00, 0x00, 0x48, 0x08, 0x01, 0x00, 0x81)) xknx = XKNX(loop=self.loop) knxipframe = KNXIPFrame(xknx) knxipframe.from_knx(raw) self.assertTrue(isinstance(knxipframe.body, TunnellingRequest)) self.assertEqual(knxipframe.body.communication_channel_id, 1) self.assertEqual(knxipframe.body.sequence_counter, 23) self.assertTrue(isinstance(knxipframe.body.cemi, CEMIFrame)) self.assertEqual(knxipframe.body.cemi.telegram, Telegram(Address('9/0/8'), payload=DPTBinary(1))) knxipframe2 = KNXIPFrame(xknx) knxipframe2.init(KNXIPServiceType.TUNNELLING_REQUEST) knxipframe2.body.cemi.telegram = Telegram(Address('9/0/8'), payload=DPTBinary(1)) knxipframe2.body.sequence_counter = 23 knxipframe2.normalize() self.assertEqual(knxipframe2.to_knx(), list(raw))
def __init__(self): """CEMIFrame __init__ object.""" super(CEMIFrame, self).__init__() self.code = CEMIMessageCode.L_DATA_IND self.flags = 0 self.cmd = APCICommand.GROUP_READ self.src_addr = Address() self.dst_addr = Address() self.mpdu_len = 0 self.payload = None
def __init__(self): super(DIBDeviceInformation, self).__init__() self.knx_medium = KNXMedium.TP1 self.programming_mode = False self.individual_address = Address() self.installation_number = 0 self.project_number = 0 self.serial_number = "" self.multicast_address = "224.0.23.12" self.mac_address = "" self.name = ""
def test_from_knx(self): raw = ((0x06, 0x10, 0x05, 0x30, 0x00, 0x12, 0x29, 0x00, 0xbc, 0xd0, 0x12, 0x02, 0x01, 0x51, 0x02, 0x00, 0x40, 0xf0)) knxipframe = KNXIPFrame() knxipframe.from_knx(raw) self.assertEqual(knxipframe.body.src_addr, Address("1.2.2")) self.assertEqual(knxipframe.body.dst_addr, Address(337)) self.assertEqual(len(knxipframe.body.payload.value), 1) self.assertEqual(knxipframe.body.payload.value[0], 0xf0)
def test_from_knx(self): """Test parsing and streaming CEMIFrame KNX/IP packet (payload=0xf0).""" raw = ((0x06, 0x10, 0x05, 0x30, 0x00, 0x12, 0x29, 0x00, 0xbc, 0xd0, 0x12, 0x02, 0x01, 0x51, 0x02, 0x00, 0x40, 0xf0)) xknx = XKNX(loop=self.loop) knxipframe = KNXIPFrame(xknx) knxipframe.from_knx(raw) self.assertTrue(isinstance(knxipframe.body, CEMIFrame)) self.assertEqual(knxipframe.body.src_addr, Address("1.2.2")) self.assertEqual(knxipframe.body.dst_addr, Address(337)) self.assertEqual(len(knxipframe.body.payload.value), 1) self.assertEqual(knxipframe.body.payload.value[0], 0xf0)
def test_modification_of_device(self): """ This test should verify that devices only stores the references of an object and all accessing functions only return referecenes of the same object""" xknx = XKNX(self.loop, start=False) devices = Devices() light1 = Light(xknx, 'Living-Room.Light_1', group_address_switch='1/6/7') devices.add(light1) for device in devices: device.set_on() self.assertTrue(light1.state) device2 = devices["Living-Room.Light_1"] device2.set_off() self.assertFalse(light1.state) for device in devices.devices_by_group_address(Address('1/6/7')): device.set_on() self.assertTrue(light1.state)
def parse_general(self, doc): """Parse the general section of xknx.yaml.""" if "general" in doc: if "own_address" in doc["general"]: self.xknx.own_address = \ Address(doc["general"]["own_address"], AddressType.PHYSICAL)
def __init__(self, xknx, group_address=None, group_address_state=None, after_update_cb=None): """Initialize RemoteValue class.""" self.xknx = xknx if isinstance(group_address, (str, int)): group_address = Address(group_address) if isinstance(group_address_state, (str, int)): group_address_state = Address(group_address_state) self.group_address = group_address self.group_address_state = group_address_state self.after_update_cb = after_update_cb self.payload = None
def __init__(self, xknx, name, group_address=None, device_class=None, significant_bit=1, actions=None, device_updated_cb=None): """Initialize BinarySensor class.""" # pylint: disable=too-many-arguments Device.__init__(self, xknx, name, device_updated_cb) if isinstance(group_address, (str, int)): group_address = Address(group_address) if not isinstance(significant_bit, int): raise TypeError() if actions is None: actions = [] self.group_address = group_address self.device_class = device_class self.significant_bit = significant_bit self.state = BinarySensorState.OFF self.actions = actions self.last_set = None self.count_set_on = 0 self.count_set_off = 0
def __init__(self, config=None, loop=None, own_address=Address(DEFAULT_ADDRESS), address_format=AddressFormat.LEVEL3, telegram_received_cb=None, device_updated_cb=None): """Initialize XKNX class.""" # pylint: disable=too-many-arguments self.devices = Devices() self.telegrams = asyncio.Queue() self.loop = loop or asyncio.get_event_loop() self.sigint_received = asyncio.Event() self.telegram_queue = TelegramQueue(self) self.state_updater = None self.knxip_interface = None self.started = False self.address_format = address_format self.own_address = own_address self.logger = logging.getLogger('xknx.log') self.knx_logger = logging.getLogger('xknx.knx') self.telegram_logger = logging.getLogger('xknx.telegram') if config is not None: Config(self).read(config) if telegram_received_cb is not None: self.telegram_queue.register_telegram_received_cb( telegram_received_cb) if device_updated_cb is not None: self.devices.register_device_updated_cb(device_updated_cb)
def test_process_switch(self): xknx = XKNX(self.loop, start=False) light = Light(xknx, name="TestLight", group_address_switch='1/2/3', group_address_dimm='1/2/4', group_address_brightness='1/2/5') self.assertEqual(light.state, False) telegram = Telegram(Address('1/2/3'), payload=DPTBinary(1)) light.process(telegram) self.assertEqual(light.state, True) telegram = Telegram(Address('1/2/3'), payload=DPTBinary(0)) light.process(telegram) self.assertEqual(light.state, False)
def test_process_switch(self): """Test process / reading telegrams from telegram queue. Test if switch position is processed correctly.""" xknx = XKNX(loop=self.loop) light = Light(xknx, name="TestLight", group_address_switch='1/2/3', group_address_brightness='1/2/5') self.assertEqual(light.state, False) telegram = Telegram(Address('1/2/3'), payload=DPTBinary(1)) self.loop.run_until_complete(asyncio.Task(light.process(telegram))) self.assertEqual(light.state, True) telegram = Telegram(Address('1/2/3'), payload=DPTBinary(0)) self.loop.run_until_complete(asyncio.Task(light.process(telegram))) self.assertEqual(light.state, False)
def parse_general(self, doc): if "general" in doc: if "own_address" in doc["general"]: self.xknx.globals.own_address = \ Address(doc["general"]["own_address"], AddressType.PHYSICAL) if "own_ip" in doc["general"]: self.xknx.globals.own_ip = doc["general"]["own_ip"]
def __init__(self, xknx, name, group_address=None, device_updated_cb=None): """Initialize Time class.""" Device.__init__(self, xknx, name, device_updated_cb) if isinstance(group_address, (str, int)): group_address = Address(group_address) self.group_address = group_address
def __init__(self, xknx, name, group_address=None): Device.__init__(self, xknx, name) if isinstance(group_address, (str, int)): group_address = Address(group_address) self.group_address = group_address
def test_telegram(self): """Test string representation of Telegram.""" telegram = Telegram(group_address=Address('1/2/3'), payload=DPTBinary(7)) self.assertEqual( str(telegram), '<Telegram group_address="<Address str="1/2/3" />", payload="<DPTBinary value="7" />" telegramtype="TelegramType.GROUP_WRITE" direction=' '"TelegramDirection.OUTGOING" />')
def test_sync_state(self): xknx = XKNX(self.loop, start=False) thermostat = Thermostat(xknx, 'TestThermostat', group_address_temperature='1/2/3', group_address_setpoint='1/2/4') thermostat.sync_state() self.assertEqual(xknx.telegrams.qsize(), 2) telegram1 = xknx.telegrams.get() self.assertEqual(telegram1, Telegram(Address('1/2/3'), TelegramType.GROUP_READ)) telegram2 = xknx.telegrams.get() self.assertEqual(telegram2, Telegram(Address('1/2/4'), TelegramType.GROUP_READ))
def test_set_off(self): xknx = XKNX(self.loop, start=False) outlet = Outlet(xknx, 'TestOutlet', group_address='1/2/3') outlet.set_off() self.assertEqual(xknx.telegrams.qsize(), 1) telegram = xknx.telegrams.get_nowait() self.assertEqual(telegram, Telegram(Address('1/2/3'), payload=DPTBinary(0)))
def test_sync_angle(self): """Test sync function for cover with angle.""" xknx = XKNX(loop=self.loop) cover = Cover(xknx, 'TestCover', group_address_long='1/2/1', group_address_short='1/2/2', group_address_position='1/2/3', group_address_angle='1/2/4') self.loop.run_until_complete(asyncio.Task(cover.sync(False))) self.assertEqual(xknx.telegrams.qsize(), 2) telegram1 = xknx.telegrams.get_nowait() self.assertEqual(telegram1, Telegram(Address('1/2/3'), TelegramType.GROUP_READ)) telegram2 = xknx.telegrams.get_nowait() self.assertEqual(telegram2, Telegram(Address('1/2/4'), TelegramType.GROUP_READ))
def __init__(self, xknx, name, group_address=None, device_updated_cb=None): """Initialize notification class.""" # pylint: disable=too-many-arguments Device.__init__(self, xknx, name, device_updated_cb) if isinstance(group_address, (str, int)): group_address = Address(group_address) self.group_address = group_address self.message = ""
def test_sync_state(self): xknx = XKNX(self.loop, start=False) light = Light(xknx, name="TestLight", group_address_switch='1/2/3', group_address_dimm='1/2/4', group_address_brightness='1/2/5') light.sync_state() self.assertEqual(xknx.telegrams.qsize(), 2) telegram1 = xknx.telegrams.get_nowait() self.assertEqual(telegram1, Telegram(Address('1/2/3'), TelegramType.GROUP_READ)) telegram2 = xknx.telegrams.get_nowait() self.assertEqual(telegram2, Telegram(Address('1/2/5'), TelegramType.GROUP_READ))
def test_process(self): """Test process / reading telegrams from telegram queue.""" xknx = XKNX(loop=self.loop) sensor = Sensor(xknx, 'TestSensor', group_address='1/2/3') telegram = Telegram(Address('1/2/3')) telegram.payload = DPTArray((0x01, 0x02, 0x03)) self.loop.run_until_complete(asyncio.Task(sensor.process(telegram))) self.assertEqual(sensor.state, DPTArray((0x01, 0x02, 0x03)))
def test_sync(self): """Test sync function / sending group reads to KNX bus. Testing with a Light without dimm functionality.""" xknx = XKNX(loop=self.loop) light = Light(xknx, name="TestLight", group_address_switch='1/2/3', group_address_brightness='1/2/5') self.loop.run_until_complete(asyncio.Task(light.sync(False))) self.assertEqual(xknx.telegrams.qsize(), 2) telegram1 = xknx.telegrams.get_nowait() self.assertEqual(telegram1, Telegram(Address('1/2/3'), TelegramType.GROUP_READ)) telegram2 = xknx.telegrams.get_nowait() self.assertEqual(telegram2, Telegram(Address('1/2/5'), TelegramType.GROUP_READ))