trainman419 / python-cec

Other
171 stars 42 forks source link

list_devices() does not return all of the devices on RaspberyPI #41

Closed Mariusmssj closed 4 years ago

Mariusmssj commented 4 years ago

Hello, Thank you for the great library it has really helped me out :) I ran into an issue and I was hoping you could help?

When I run code below I only get 5 devices back:

import cec

adapters = cec.list_adapters()

if len(adapters) > 0:
    adapter = adapters[0]

    print('Adapter: {0}'.format(adapter))

    cec.init(adapter)

    devices = cec.list_devices()

    for i in range(len(devices)):
        d = cec.Device(i)
        print('{0} : {1} '.format(i, d.osd_string))

Which shows this:

Adapter: RPI 0 : TV 1 : python-cec 2 : Recorder 2 3 : Tuner 1 4 : PlayStation 4

However there is an AV Receiver that the raspberry pi is plugged into and if I run the following:

d = cec.Device(5)
print('{0} | Active: {1} | ON: {2}'.format(d.osd_string, d.is_active(), d.is_on()))

I get:

AVR-X1600H | Active: False | ON: False

Which means it does see the receiver. Is the list_devices limited to how many it can return? Also is there any example code of how to use cec.EVENT_COMMAND callback? I want to have a callback that will run once a device is turned off or on.

Thank you

nforro commented 4 years ago

list_devices() calls GetActiveDevices(), so that's why the AV receiver is not listed.

Also is there any example code of how to use cec.EVENT_COMMAND callback? I want to have a callback that will run once a device is turned off or on.

I'm using something like this with my TV:

def cmd(event, command):
    if command['initiator'] == cec.CECDEVICE_TV and \
       command['opcode'] == cec.CEC_OPCODE_REPORT_POWER_STATUS:
        current_power_status = command['parameters'][0]
        ...

cec.add_callback(cmd, cec.EVENT_COMMAND)
cec.init()
cec.Device(cec.CECDEVICE_TV).transmit(cec.CEC_OPCODE_GIVE_DEVICE_POWER_STATUS)
Mariusmssj commented 4 years ago

That is brilliant. Thank you :)