custom-components / pyscript

Pyscript adds rich Python scripting to HASS
Apache License 2.0
897 stars 47 forks source link

Cant' read entities of a group since 1.6.0 #623

Open jrlacharnay opened 3 months ago

jrlacharnay commented 3 months ago

Hello,

I have a group named group.electrovannes, with the following attributes:

entity_id:
  - switch.ev1_1_gag_haie_nord
  - switch.ev1_2_gag_maison
  - switch.ev2_1_gag_haie_est_sud
  - switch.ev2_2_jets_butte
order: 1
friendly_name: Electrovannes pour l'arrosage du jardin

In this pyscript code, I'm reading the attributes of this group, and then reading the list of entities composing the group:

group_attributes = state.getattr(group.electrovannes)
log.info(f"attributes: {group_attributes}")

valve_list = state.get('group.electrovannes.entity_id')
log.info(f"valve list: {valve_list}")

With pyscript 1.5.0, it displays the expected result :

attributes: {'entity_id': ('switch.ev1_1_gag_haie_nord', 'switch.ev1_2_gag_maison', 'switch.ev2_1_gag_haie_est_sud', 'switch.ev2_2_jets_butte'), 'order': 1, 'friendly_name': "Electrovannes pour l'arrosage du jardin"}
valve list: ('switch.ev1_1_gag_haie_nord', 'switch.ev1_2_gag_maison', 'switch.ev2_1_gag_haie_est_sud', 'switch.ev2_2_jets_butte')

However, with pyscript 1.6.0 / 1.6.1, the entity_id attribute seems to not exist anymore, and I can't read it. The exact same code gives this logs:

attributes: {'order': 1, 'friendly_name': "Electrovannes pour l'arrosage du jardin"}
valve list: group.electrovannes

I guess that it's caused by this change: https://github.com/custom-components/pyscript/pull/555 (@dmamelin)

Is there another way to read the contents of a group?

Thanks

ALERTua commented 3 months ago

This works on my side:

    attrs = state.getattr('group.batteries_group')
    log.debug(f"attrs:\n{pformat(attrs)}")
2024-07-30 10:12:40.952 DEBUG (MainThread) [custom_components.pyscript.file.tryouts.tryouts] attrs:
{'auto': True,
 'entity_id': ('sensor.54ef44e1a1e5_battery',
               'sensor.54ef44e1ad9d_battery',
               'sensor.54ef44e1b134_battery',
               'sensor.582d34826eac_582d34826eac_battery',
               'sensor.582d34826eac_battery',
               'sensor.a4c13853b5ef_battery',
               'sensor.a4c1387fce25_battery',
               'sensor.a4c138beb99d_battery',
               'sensor.alert_s_s24_battery_level',
               'sensor.bedroom_window_reed_battery',
               'sensor.ble_bathroom_battery',
               'sensor.ble_shower_battery',
               'sensor.delta_2_battery_level',
               'sensor.delta_2_main_battery_level',
               'sensor.delta_pro_battery_level',
               'sensor.delta_pro_main_battery_current',
               'sensor.delta_pro_main_battery_level',
               'sensor.flood_1_miot_battery_battery_level',
               'sensor.flood_2_miot_battery_battery_level',
               'sensor.flood_3_miot_battery_battery_level',
               'sensor.flood_4_miot_battery_battery_level',
               'sensor.ijai_v3_4619_battery_level',
               'sensor.iphone_kateryna_battery_level',
               'sensor.mi_multifunction_air_monitor_battery_battery_level',
               'sensor.miaomiaoce_t2_7360_battery_level',
               'sensor.miaomiaoce_t2_b5ef_battery_level',
               'sensor.miaomiaoce_t2_b99d_battery_level',
               'sensor.miaomiaoce_t2_ce25_battery_level',
               'sensor.motion_detector_1_battery',
               'sensor.reed_office_cabinet_battery',
               'sensor.reed_wardrobe_battery',
               'sensor.room_window_reed_battery',
               'sensor.shower_air_meter_miot_battery',
               'sensor.ups_battery_charge',
               'sensor.valve_kitchen_battery',
               'sensor.water_leak_4_bt_battery',
               'sensor.water_leak_zb_1_battery',
               'sensor.water_leak_zb_2_battery',
               'sensor.water_leak_zb_3_battery',
               'sensor.water_leak_zb_4_battery',
               'sensor.xiaomi_wireless_button_2_battery'),
 'friendly_name': 'Batteries',
 'icon': 'mdi:battery',
 'order': 1}
    battery_entity_ids = attrs.get('entity_id', [])
    log.debug(f"battery_entity_ids:\n{pformat(battery_entity_ids)}")
    2024-07-30 10:13:29.901 DEBUG (MainThread) [custom_components.pyscript.file.tryouts.tryouts] battery_entity_ids:
('sensor.54ef44e1a1e5_battery',
 'sensor.54ef44e1ad9d_battery',
 'sensor.54ef44e1b134_battery',
 'sensor.582d34826eac_582d34826eac_battery',
 'sensor.582d34826eac_battery',
 'sensor.a4c13853b5ef_battery',
 'sensor.a4c1387fce25_battery',
 'sensor.a4c138beb99d_battery',
 'sensor.alert_s_s24_battery_level',
 'sensor.bedroom_window_reed_battery',
 'sensor.ble_bathroom_battery',
 'sensor.ble_shower_battery',
 'sensor.delta_2_battery_level',
 'sensor.delta_2_main_battery_level',
 'sensor.delta_pro_battery_level',
 'sensor.delta_pro_main_battery_current',
 'sensor.delta_pro_main_battery_level',
 'sensor.flood_1_miot_battery_battery_level',
 'sensor.flood_2_miot_battery_battery_level',
 'sensor.flood_3_miot_battery_battery_level',
 'sensor.flood_4_miot_battery_battery_level',
 'sensor.ijai_v3_4619_battery_level',
 'sensor.iphone_kateryna_battery_level',
 'sensor.mi_multifunction_air_monitor_battery_battery_level',
 'sensor.miaomiaoce_t2_7360_battery_level',
 'sensor.miaomiaoce_t2_b5ef_battery_level',
 'sensor.miaomiaoce_t2_b99d_battery_level',
 'sensor.miaomiaoce_t2_ce25_battery_level',
 'sensor.motion_detector_1_battery',
 'sensor.reed_office_cabinet_battery',
 'sensor.reed_wardrobe_battery',
 'sensor.room_window_reed_battery',
 'sensor.shower_air_meter_miot_battery',
 'sensor.ups_battery_charge',
 'sensor.valve_kitchen_battery',
 'sensor.water_leak_4_bt_battery',
 'sensor.water_leak_zb_1_battery',
 'sensor.water_leak_zb_2_battery',
 'sensor.water_leak_zb_3_battery',
 'sensor.water_leak_zb_4_battery',
 'sensor.xiaomi_wireless_button_2_battery')
archite commented 3 months ago

I ran into this as well and just settled on:

groups = {k: hass.states.get(k) for k, v in hass.data['entity_info'].items() if v['domain'] == 'group'}

groups['binary_sensor.stairway_motion'].attributes['entity_id']

Not ideal but works for my needs.

archite commented 3 months ago

And don't get me started on gathering ZHA groups ;-) If you run into that, take a look at hass.data["entity_registry"].async_get('my.entity').translation_key which should right something like light_group or other similar items listed in ZHA String.