Skip to content

Commit

Permalink
Alpha 04 (#32)
Browse files Browse the repository at this point in the history
* Allow groups of switches per device.

* Allow groups of switches per device.

* Allow groups of switches per device.

* Allow groups of switches per device.

* Allow groups of switches per device.

* Allow groups of switches per device.

* Allow groups of switches per device.
  • Loading branch information
twrecked authored Nov 14, 2023
1 parent a12c275 commit 4d8b3c9
Show file tree
Hide file tree
Showing 7 changed files with 226 additions and 119 deletions.
25 changes: 21 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,14 +121,30 @@ switches:
toggle_for: 2
```
You can group switches together under an umbrella device. To do this use a
device name instead of `name:` as the first line of the switch entry. This
config create a device with 3 entities:

```yaml
version: 1
switches:
- House Alarm:
- name: House to Armed Trigger
toggle_for: 1
- name: House to Disarmed Trigger
toggle_for: 1
- name: House to Home Trigger
toggle_for: 1
```

Once you've updated the file you will need to reload the component from its
integration setting.

The integration uses the name to distinguish different switches. If you can
the name of a switch the old name will be deleted and the new name created
on reload.
The integration uses the name to distinguish different switches. If you update
the name of a switch the old name will be deleted and the new name created on
reload.

The integration supports mutliple entries, you can use this group similar
The integration supports multiple installs, you can use this group similar
momentary devices together.

### Options
Expand Down Expand Up @@ -205,3 +221,4 @@ logger:

If you can create a bug report with this debug output it would help me track
down the problem.

2 changes: 2 additions & 0 deletions changelog
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
0.7.0a4:
allow multiple switches per device
0.7.0a3:
tidy up code
0.7.0a2:
Expand Down
34 changes: 18 additions & 16 deletions custom_components/momentary/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from .cfg import BlendedCfg


__version__ = '0.7.0a3'
__version__ = '0.7.0a4'

_LOGGER = logging.getLogger(__name__)

Expand Down Expand Up @@ -62,19 +62,19 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
cfg.load()

# Load and create devices.
for switch, values in cfg.switches.items():
_LOGGER.debug(f"would try to add {switch}")
# _LOGGER.debug(f"would try to add {values}")
await _async_get_or_create_momentary_device_in_registry(hass, entry, switch, values)

# Delete orphaned entries.
for switch, values in cfg.orphaned_switches.items():
_LOGGER.debug(f"would try to delete {switch}")
for device, name in cfg.devices.items():
_LOGGER.debug(f"would try to add device {device}/{name}")
await _async_get_or_create_momentary_device_in_registry(hass, entry, device, name)

# Delete orphaned devices.
for switch, values in cfg.orphaned_devices.items():
_LOGGER.debug(f"would try to delete {switch}/{values}")
await _async_delete_momentary_device_from_registry(hass, entry, switch, values)

# Update hass data and queue entry creation.
hass.data[COMPONENT_DOMAIN].update({
group_name: {
ATTR_DEVICES: cfg.devices,
ATTR_SWITCHES: cfg.switches,
ATTR_FILE_NAME: file_name
}
Expand All @@ -90,36 +90,38 @@ async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
_LOGGER.debug(f"unloading it {entry.data[ATTR_GROUP_NAME]}")
unload_ok = await hass.config_entries.async_unload_platforms(entry, [Platform.SWITCH])
if unload_ok:
hass.data[COMPONENT_DOMAIN].pop(entry.data[ATTR_GROUP_NAME])
BlendedCfg.delete_group(entry.data[ATTR_GROUP_NAME])
cfg = hass.data[COMPONENT_DOMAIN].pop(entry.data[ATTR_GROUP_NAME])
for device, name in cfg[ATTR_DEVICES].items():
await _async_delete_momentary_device_from_registry(hass, entry, device, name)
_LOGGER.debug(f"after hass={hass.data[COMPONENT_DOMAIN]}")

return unload_ok


async def _async_get_or_create_momentary_device_in_registry(
hass: HomeAssistant, entry: ConfigEntry, unique_id, switch
hass: HomeAssistant, entry: ConfigEntry, device_id, name
) -> None:
device_registry = dr.async_get(hass)
device_registry.async_get_or_create(
config_entry_id=entry.entry_id,
identifiers={(COMPONENT_DOMAIN, unique_id)},
identifiers={(COMPONENT_DOMAIN, device_id)},
manufacturer=COMPONENT_MANUFACTURER,
name=switch[CONF_NAME],
name=name,
model=COMPONENT_MODEL,
sw_version=__version__
)


async def _async_delete_momentary_device_from_registry(
hass: HomeAssistant, _entry: ConfigEntry, unique_id, _switch
hass: HomeAssistant, _entry: ConfigEntry, device_id, _name
) -> None:
device_registry = dr.async_get(hass)
device = device_registry.async_get_device(
identifiers={(COMPONENT_DOMAIN, unique_id)},
identifiers={(COMPONENT_DOMAIN, device_id)},
)
if device:
_LOGGER.debug(f"found something to delete! {device.id}")
device_registry.async_remove_device(device.id)
else:
_LOGGER.info(f"have orphaned device in meta {unique_id}")
_LOGGER.info(f"have orphaned device in meta {device_id}")
Loading

0 comments on commit 4d8b3c9

Please sign in to comment.