def async_send_command(self, command): if isinstance(command, str): command = CecCommand(command) _LOGGER.debug("<< %s", command) if command.src is None or command.src == 0xf: command.src = self._adapter.get_logical_address() self._loop.call_soon_threadsafe(self._adapter.transmit, command)
def _tx(call): """Send CEC command.""" data = call.data if ATTR_RAW in data: command = CecCommand(data[ATTR_RAW]) else: if ATTR_SRC in data: src = data[ATTR_SRC] else: src = ADDR_UNREGISTERED if ATTR_DST in data: dst = data[ATTR_DST] else: dst = ADDR_BROADCAST if ATTR_CMD in data: cmd = data[ATTR_CMD] else: _LOGGER.error("Attribute 'cmd' is missing") return False if ATTR_ATT in data: if isinstance(data[ATTR_ATT], (list, )): att = data[ATTR_ATT] else: att = reduce(lambda x, y: "%s:%x" % (x, y), data[ATTR_ATT]) else: att = "" command = CecCommand(cmd, dst, src, att) hdmi_network.send_command(command)
def test_att(): cc = CecCommand("1f:90:02") assert cc.att == [0x02] cc = CecCommand("52:8f:02:56") assert cc.att == [0x02, 0x56] cc = CecCommand(0x8F, 0x5, 0x3, [0x45, 0x56, 0x98]) assert cc.att == [0x45, 0x56, 0x98] cc = CecCommand(0x8F, 0x5, 0x3, raw="78:a5:89:45") assert cc.att == [0x89, 0x45]
def test_att(self): cc = CecCommand("1f:90:02") self.assertEqual(cc.att, [0x02]) cc = CecCommand("52:8f:02:56") self.assertEqual(cc.att, [0x02, 0x56]) cc = CecCommand(0x8f, 0x5, 0x3, [0x45, 0x56, 0x98]) self.assertEqual(cc.att, [0x45, 0x56, 0x98]) cc = CecCommand(0x8f, 0x5, 0x3, raw="78:a5:89:45") self.assertEqual(cc.att, [0x89, 0x45])
def test_raw(self): cc = CecCommand("1f:90:02:05:89") self.assertEqual(cc.raw, "1f:90:02:05:89") cc = CecCommand("1f:90") self.assertEqual(cc.raw, "1f:90") cc = CecCommand("2c") self.assertEqual(cc.raw, "2c") cc = CecCommand(CMD_POLL, dst=3) self.assertEqual(cc.raw, "f3")
def test_cmd(self): cc = CecCommand("1f:90:02") self.assertEqual(cc.cmd, 0x90) cc = CecCommand("52:8f:02") self.assertEqual(cc.cmd, 0x8f) cc = CecCommand(0x8f, 0x5, 0x3) self.assertEqual(cc.cmd, 0x8f) cc = CecCommand(0x8f, 0x5, 0x3, raw="78:a5:89:45") self.assertEqual(cc.cmd, 0xa5)
def test_cmd(): cc = CecCommand("1f:90:02") assert cc.cmd == 0x90 cc = CecCommand("52:8f:02") assert cc.cmd == 0x8F cc = CecCommand(0x8F, 0x5, 0x3) assert cc.cmd == 0x8F cc = CecCommand(0x8F, 0x5, 0x3, raw="78:a5:89:45") assert cc.cmd == 0xA5
def test_raw(): cc = CecCommand("1f:90:02:05:89") assert cc.raw == "1f:90:02:05:89" cc = CecCommand("1f:90") assert cc.raw == "1f:90" cc = CecCommand("2c") assert cc.raw == "2c" cc = CecCommand(CMD_POLL, dst=3) assert cc.raw == "f3"
def data_received(self, data): self.buffer += bytes.decode(data) for line in self.buffer.splitlines(keepends=True): if line.endswith('\n'): line = line.rstrip() if len(line) == 2: _LOGGER.info("Received poll %s from %s", line, self.transport.get_extra_info('peername')) d = CecCommand(line).dst t = network._adapter.poll_device(d) t.add_done_callback(functools.partial(_after_poll, d)) else: _LOGGER.info("Received command %s from %s", line, self.transport.get_extra_info('peername')) network.send_command(CecCommand(line)) self.buffer = '' else: self.buffer = line
def test_update_callback(self): device = HDMIDevice(3) device.update_callback( CecCommand(CMD_PHYSICAL_ADDRESS[1], att=[0x11, 0x00, 0x02])) self.assertEqual('1.1.0.0', str(device.physical_address)) self.assertEqual(2, device.type) device.update_callback(CecCommand(CMD_POWER_STATUS[1], att=[0x01])) self.assertEqual(1, device.power_status) self.assertTrue(device.is_off) self.assertFalse(device.is_on) device.update_callback(CecCommand(CMD_POWER_STATUS[1], att=[0x00])) self.assertEqual(0, device.power_status) self.assertTrue(device.is_on) self.assertFalse(device.is_off) device.update_callback(CecCommand(CMD_POWER_STATUS[1], att=[0x02])) self.assertEqual(2, device.power_status) self.assertFalse(device.is_on) self.assertFalse(device.is_off) device.update_callback( CecCommand(CMD_OSD_NAME[1], att=list(map(lambda x: ord(x), "Test4")))) self.assertEqual("Test4", device.osd_name) device.update_callback( CecCommand(CMD_VENDOR[1], att=[0x00, 0x80, 0x45])) self.assertEqual(0x008045, device.vendor_id) self.assertEqual("Panasonic", device.vendor)
def test_update(): device = HDMIDevice(2) cmd = CecCommand("02:%02x:4f:6e:6b:79:6f:20:48:54:58:2d:32:32:48:44:58" % CMD_OSD_NAME[1]) device.update_callback(cmd) assert device.osd_name == "Onkyo HTX-22HDX" cmd = CecCommand("02:%02x:01" % CMD_POWER_STATUS[1]) device.update_callback(cmd) assert device.power_status == 1 cmd = CecCommand("02:%02x:02" % CMD_POWER_STATUS[1]) device.update_callback(cmd) assert device.power_status == 2 cmd = CecCommand("02:%02x:18:C0:86" % CMD_VENDOR[1]) device.update_callback(cmd) assert device.vendor_id == 0x18C086 assert device.vendor == "Broadcom" cmd = CecCommand("02:%02x:C0:86:01" % CMD_PHYSICAL_ADDRESS[1]) device.update_callback(cmd) assert device.physical_address.ascmd == "c0:86" assert device.physical_address.asattr == [0xC0, 0x86]
def _async_callback(self, raw_command): command = CecCommand(raw_command[3:]) updated = False if command.src == 15: for i in range(15): updated |= self.get_device(i).update_callback(command) pass elif command.src in self._devices: updated = self.get_device(command.src).update_callback(command) pass if not updated: if self._command_callback: self._loop.call_soon_threadsafe(self._command_callback, command)
def test_update(self): device = HDMIDevice(2) cmd = CecCommand( '02:%02x:4f:6e:6b:79:6f:20:48:54:58:2d:32:32:48:44:58' % CMD_OSD_NAME[1]) device.update_callback(cmd) self.assertEqual(device.osd_name, 'Onkyo HTX-22HDX') cmd = CecCommand('02:%02x:01' % CMD_POWER_STATUS[1]) device.update_callback(cmd) self.assertEqual(device.power_status, 1) cmd = CecCommand('02:%02x:02' % CMD_POWER_STATUS[1]) device.update_callback(cmd) self.assertEqual(device.power_status, 2) cmd = CecCommand('02:%02x:18:C0:86' % CMD_VENDOR[1]) device.update_callback(cmd) self.assertEqual(device.vendor_id, 0x18C086) self.assertEqual(device.vendor, 'Broadcom') cmd = CecCommand('02:%02x:C0:86:01' % CMD_PHYSICAL_ADDRESS[1]) device.update_callback(cmd) self.assertEqual(device.physical_address.ascmd, 'c0:86') self.assertEqual(device.physical_address.asattr, [0xC0, 0x86])
def data_received(self, data: bytes): self.buffer += bytes.decode(data) for line in self.buffer.splitlines(keepends=True): if line.count('\n'): line = line.rstrip() _LOGGER.debug("Received %s from %s", line, self.transport.get_extra_info('peername')) if len(line) == 2: cmd = CecCommand(line) if cmd.src in self._adapter._polling: del self._adapter._polling[cmd.src] else: self._adapter._command_callback("<< " + line) self.buffer = '' else: self.buffer = line
def transmit(self, command): cmd = None att = None if command.cmd == CMD_POWER_STATUS[0]: cmd = CMD_POWER_STATUS[1] att = [2] elif command.cmd == CMD_OSD_NAME[0]: cmd = CMD_OSD_NAME[1] att = (ord(i) for i in ("Test%d" % command.dst)) elif command.cmd == CMD_VENDOR[0]: cmd = CMD_VENDOR[1] att = [0x00, 0x09, 0xB0] elif command.cmd == CMD_PHYSICAL_ADDRESS[0]: cmd = CMD_PHYSICAL_ADDRESS[1] att = [0x09, 0xB0, 0x02] elif command.cmd == CMD_DECK_STATUS[0]: cmd = CMD_DECK_STATUS[1] att = [0x09] elif command.cmd == CMD_AUDIO_STATUS[0]: cmd = CMD_AUDIO_STATUS[1] att = [0x65] response = CecCommand(cmd, src=command.dst, dst=command.src, att=att) self._command_callback(">> " + response.raw)
def test_update_callback(): device = HDMIDevice(3) device.update_callback( CecCommand(CMD_PHYSICAL_ADDRESS[1], att=[0x11, 0x00, 0x02])) assert "1.1.0.0" == str(device.physical_address) assert 2 == device.type device.update_callback(CecCommand(CMD_POWER_STATUS[1], att=[0x01])) assert 1 == device.power_status assert device.is_off is True assert device.is_on is False device.update_callback(CecCommand(CMD_POWER_STATUS[1], att=[0x00])) assert 0 == device.power_status assert device.is_on is True assert device.is_off is False device.update_callback(CecCommand(CMD_POWER_STATUS[1], att=[0x02])) assert 2 == device.power_status assert device.is_on is False assert device.is_off is False device.update_callback( CecCommand(CMD_OSD_NAME[1], att=list(map(lambda x: ord(x), "Test4")))) assert "Test4" == device.osd_name device.update_callback(CecCommand(CMD_VENDOR[1], att=[0x00, 0x80, 0x45])) assert 0x008045 == device.vendor_id assert "Panasonic" == device.vendor
def async_turn_on(self): # pragma: no cover if self.logical_address == TYPE_TV: command = CecCommand(0x04, self.logical_address) else: command = CecCommand(0x44, self.logical_address, att=[0x6d]) yield from self.async_send_command(command)
def async_active_source(self, addr: PhysicalAddress): yield from self.async_send_command( CecCommand(CMD_ACTIVE_SOURCE, ADDR_BROADCAST, att=addr.asattr)) yield from self.async_send_command( CecCommand(CMD_STREAM_PATH, ADDR_BROADCAST, att=addr.asattr))
async def async_turn_off(self): # pragma: no cover command = CecCommand(0x44, self.logical_address, att=[0x6c]) await self.async_send_command(command)
def async_send_command(self, command: CecCommand): command.dst = self._logical_address yield from self._network.async_send_command(command)
def async_request_update(self, cmd: int): self._updates[cmd] = False command = CecCommand(cmd) yield from self.async_send_command(command)
def async_toggle(self): # pragma: no cover command = CecCommand(0x44, self.logical_address, att=[0x40]) yield from self.async_send_command(command)
def async_turn_off(self): # pragma: no cover command = CecCommand(0x36, self.logical_address) yield from self.async_send_command(command)
def test_dst(): cc = CecCommand("1f:90:02") assert cc.dst == 0xF cc = CecCommand("52:90:02") assert cc.dst == 0x2 cc = CecCommand(0x8F, 0x5, 0x3) assert cc.dst == 0x5 cc = CecCommand("52:90:02") cc.dst = 0x4 assert ("%s" % cc) == "54:90:02" cc = CecCommand(0x8F, 0x5, 0x3, raw="78:a5:89:45") assert cc.dst == 0x8
def send_playback(self, key): """Send playback status to CEC adapter.""" from pycec.commands import CecCommand self._device.async_send_command( CecCommand(key, dst=self._logical_address))
def standby_devices(self): self.transmit(CecCommand(CMD_STANDBY))
def test_dst(self): cc = CecCommand("1f:90:02") self.assertEqual(cc.dst, 0xf) cc = CecCommand("52:90:02") self.assertEqual(cc.dst, 0x2) cc = CecCommand(0x8f, 0x5, 0x3) self.assertEqual(cc.dst, 0x5) cc = CecCommand("52:90:02") cc.dst = 0x4 self.assertEqual(("%s" % cc), "54:90:02") cc = CecCommand(0x8f, 0x5, 0x3, raw="78:a5:89:45") self.assertEqual(cc.dst, 0x8)
def test_src(): cc = CecCommand("1f:90:02") assert cc.src == 0x1 cc = CecCommand("52:90:02") assert cc.src == 0x5 cc = CecCommand(0x8F, 0x5, 0x3) assert cc.src == 0x3 cc = CecCommand("52:90:02") cc.src = 0x4 assert ("%s" % cc) == "42:90:02" cc = CecCommand(0x8F, 0x5, 0x3, raw="78:a5:89:45") assert cc.src == 0x7
async def async_send_command(self, command: CecCommand): command.dst = self._logical_address await self._network.async_send_command(command)
def test_src(self): cc = CecCommand("1f:90:02") self.assertEqual(cc.src, 0x1) cc = CecCommand("52:90:02") self.assertEqual(cc.src, 0x5) cc = CecCommand(0x8f, 0x5, 0x3) self.assertEqual(cc.src, 0x3) cc = CecCommand("52:90:02") cc.src = 0x4 self.assertEqual(("%s" % cc), "42:90:02") cc = CecCommand(0x8f, 0x5, 0x3, raw="78:a5:89:45") self.assertEqual(cc.src, 0x7)