Components creating and updating groups

How can I add/remove groups and add/remove group members from within a custom component?

Why?
I created a PlexDevices which I’d like to dynamically create the following groups and dynamically keep their memberships in sync:

  • Connected (shows currently connected plex devices)
  • Disconnected (shows disconnected plex devices)
  • All (combination or connected and disconnected plex devices)

This enables you to see and control any device that connects, as they connect, etc. (without needing to know all client names and addd them to a group manually).

Thanks

I imagine this is a simple thing, just need to be pointed int he right direction. I’ve tried create_group and creating a new group via the EntityComponent. I was able to get a group created and members added - it just won’t display in the HA homepage.

Here are the basics of the code I am using:

from homeassistant.helpers.entity_component import (
    EntityComponent, DEFAULT_SCAN_INTERVAL)
from homeassistant.helpers.entity import (Entity, generate_entity_id)

def create_group(hass, groupname):
    return EntityComponent(_LOGGER, 'group', hass,
                            group_name=groupname)
def add_group_members(hass, group_component, member_list):
    group_component.add_entities(member_list)

all_group = create_group(hass, 'all devices)

# later in the code
add_group_members(hass, all_group, new_streamers)

That produces an entity called group.all_devices with state unknown and attributes like this:

hidden: true
auto: true
friendly_name: all devices
entity_id: group.kitchen_apple_tv,group.living_room_apple_tv
order: 2

But I can never see the group on the ha home page:

group:
  default_view:
    name: Home
    view: yes
    entities:
      - group.all_devices

Same results with this code:

import homeassistant.components.group as group
test_group = group.Group.create_group(
            hass, 'test_group', ['media_player.living_room_apple_tv', 'media_player.jesses_iphone_7_plus'])
group:
  default_view:
    name: Home
    view: yes
    entities:
      - group.test_group

Entity group.test_group gets created with a state of unknown and attributes of:

entity_id: media_player.living_room_apple_tv,media_player.jesses_iphone_7_plus
friendly_name: test_group
order: 1

OK, I’m super close and I got it work work by doing this:

import homeassistant.components.group as group
test_group = group.Group.create_group(
            hass, 'test_group', ['media_player.living_room_apple_tv', 'media_player.jesses_iphone_7_plus'])
#configuration.yaml
test_group:

group:

So something is wrong, just don’t know what. Its almost as if I have to add that group to some sort of ha master group

Figured it out (the below will create the group is it doesn’t exist and set it membership):

hass.states.set('group.your_group', 'off', {
        'entity_id': ['media_player.living_room_apple_tv', 'media_player.jesses_iphone_7_plus']})

@JesseWebDotCom: where exactly in your code did you put this last snippet of code? I’m looking to do the same but am still struggling with how everything sits together behind the scenes.

I was told by @balloob to not create/manipulate groups like I was doing. He will provide a way/some sample code on how to do it properly.

@JesseWebDotCom thanks for the quick reply. Was is specified to you if that answer would be made available here or on Github?

to me - when I hear back I will share

1 Like

Did this ever realize ? And if so, would you please share ?

I never heard back from @balloob

@balloob - can you share example code on the right way to create and update HA groups from within an HA custom component?

Does anyone know if this was ever explained or can find an example?

Any news ?

Hello,

Since march, does someone has found a way to create group in component ?

Thanks

I am also looking for a way to create groups. Does someone find a solution?

1 Like