Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Restore old style config. #48

Merged
merged 1 commit into from
Sep 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
100 changes: 63 additions & 37 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,39 +1,55 @@
# hass-momentary
![icon](images/momentary-icon.png)

### Momentary Switch Component for Home Assistant
A simple switch component that once turned on will turn itself off.

### NOTES!
**This documentation is for the 0.7x version, you can find the
0.6.x version** [here](https://github.com/twrecked/hass-momentary/blob/version-0.6.x/README.md).
# **Momentary devices for Home Asistant**

This is a big update that moves the component over to the `config_flow`
system. The update should be seamless but if you run into any problems:

- You can revert back to the previous version (0.6) and it will still work.
- But if you can re-run the upgrade operation with debug enabled and create a
bug report I would greatly appreciate it.
_Momentary_ is a component that provides momentary switches for _Home
Assistant_. That is, it provides a simple switch that once turned on will
turn itself off.

![icon](images/momentary-icon.png)

## Table Of Contents
# !!!BREAKING CHANGES!!!
Version 0.7 supports adding momentary devices using _config flow_. By default it
will move your existing devices into a single file `momentary.yaml`. If you
**DO NOT** want this behaviour add this to your `momentary` configuration.

1. [Notes](#Notes)
1. [Thanks](#Thanks)
1. [Installation](#Installation)
1. [HACS](#HACS)
1. [Manually](#Manually)
1. [From Script](#From-Script)
1. [Component Configuration](#Component-Configuration)
2. [Upgrade Notes](#Upgrade-notes)
```yaml
momentary:
yaml_config: True
```

<!-- TOC -->
* [**Momentary devices for Home Asistant**](#momentary-devices-for-home-asistant)
* [!!!BREAKING CHANGES!!!](#breaking-changes)
* [Introduction](#introduction)
* [Notes](#notes)
* [Version 0.6 Documentation](#version-06-documentation)
* [Thanks](#thanks)
* [Installation](#installation)
* [HACS](#hacs)
* [Manually](#manually)
* [From Script](#from-script)
* [Component Configuration](#component-configuration)
* [Entity Configuration](#entity-configuration)
* [Options](#options)
* [Upgrade Notes](#upgrade-notes)
* [After the Upgrade](#after-the-upgrade)
* [Names](#names)
* [`unique_id`](#unique_id)
* [Known Issues](#known-issues)
* [Turning on Debug](#turning-on-debug)
<!-- TOC -->

# Introduction

## Notes

Wherever you see `/config` in this README it refers to your home-assistant
configuration directory. For me, for example, it's `/home/steve/ha` that is
mapped to `/config` inside my docker container.

## Version 0.6 Documentation
**This documentation is for the 0.7x version, you can find the
0.6.x version** [here](https://github.com/twrecked/hass-momentary/blob/version-0.6.x/README.md).


## Thanks

Expand All @@ -48,18 +64,18 @@ Many thanks to:
and is licensed by CC BY 4.0


## Installation
# Installation

### HACS
## HACS
[![hacs_badge](https://img.shields.io/badge/HACS-Default-orange.svg?style=for-the-badge)](https://github.com/hacs/integration)

Momentary is part of the default HACS store. If you're not interested in
development branches this is the easiest way to install.

### Manually
## Manually
Copy the `momentary` directory into your `/config/custom_components` directory.

### From Script
## From Script
Run the install script. Run it once to make sure the operations look sane and
run it a second time with the `go` parameter to do the actual work. If you
update just rerun the script, it will overwrite all installed files.
Expand All @@ -70,8 +86,19 @@ install /config
install go /config
```

# Component Configuration

- `yaml_config`; set to `True` to enable backwards compatability, set to `False`
to disable it. The default is `False`.

For example, this enable backwards compatability.

## Component Configuration
```yaml
virtual:
yaml_config: True
```

# Entity Configuration
Add the component using the standard _Home Assistant_ _Integration_ method.

Because this component creates fake entries and because I'm still figuring out
Expand Down Expand Up @@ -147,7 +174,7 @@ reload.
The integration supports multiple installs, you can use this group similar
momentary devices together.

### Options
## Options

The following additional parameters can be specified against the switches:

Expand All @@ -159,15 +186,15 @@ The following additional parameters can be specified against the switches:
| cancellable | Boolean | False | Allow switched to be untoggled manually. |


## Upgrade Notes
# Upgrade Notes

### After the Upgrade
## After the Upgrade

Check the devices and entities on the _Integrations_ page, if everything looks
good then you can comment out the old configuration. _Don't delete it yet, this
is alpha and I might change some settings..._

### Names
## Names

The qualifier applied to the name has changed. The old qualify of `!` has been
removed, not adding _momentary_ to the device/entity name is the default. A
Expand All @@ -181,7 +208,7 @@ The upgrade code will change this for you.
| Empty House Trigger | +Empty House Trigger | switch.momentary_empty_house_trigger |
| !Empty House Trigger | Empty House Trigger | switch.empty_house_trigger |

### `unique_id`
## `unique_id`

During an upgrade the original name based unique id will be created. For a new
install a _UUID_ based unique id will be created.
Expand All @@ -199,7 +226,7 @@ new _UUID_ based unique ids.
_I will look at how this can be made cleaner... maybe a config flow button
or as part of the upgrade._

### Known Issues
## Known Issues

You will see errors like this on upgrade. They don't seem to cause an issue
and they disappear once you remove the legacy configuration.
Expand All @@ -208,7 +235,7 @@ and they disappear once you remove the legacy configuration.
2023-10-13 17:28:09.154 ERROR (MainThread) [homeassistant.components.switch] The momentary platform for the switch integration does not support platform setup. Please remove it from your config.
```

### Turning on Debug
# Turning on Debug

If you do run into issues you can enable debug with the following configration.

Expand All @@ -221,4 +248,3 @@ logger:

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

1 change: 1 addition & 0 deletions changelog
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
0.7.0b12:
Use async calls
add github actions
allow old config
0.7.0b11:
Fix race writing meta data
0.7.0b10:
Expand Down
89 changes: 59 additions & 30 deletions custom_components/momentary/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from __future__ import annotations

import logging
import voluptuous as vol

from homeassistant.config_entries import ConfigEntry, SOURCE_IMPORT
from homeassistant.const import CONF_SOURCE, Platform
Expand All @@ -13,6 +14,7 @@
HomeAssistant,
callback
)
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.issue_registry import (
async_create_issue,
IssueSeverity
Expand All @@ -28,41 +30,64 @@

_LOGGER = logging.getLogger(__name__)

CONFIG_SCHEMA = vol.Schema({
COMPONENT_DOMAIN: vol.Schema({
vol.Optional(CONF_YAML_CONFIG, default=False): cv.boolean,
}),
},
extra=vol.ALLOW_EXTRA,
)


async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
"""Set up an momentary component.
"""
hass.data.setdefault(COMPONENT_DOMAIN, {})

# See if we have already imported the data. If we haven't then do it now.
config_entry = _async_find_matching_config_entry(hass)
if not config_entry:
_LOGGER.debug('importing a YAML setup')
hass.async_create_task(
hass.config_entries.flow.async_init(
COMPONENT_DOMAIN,
context={CONF_SOURCE: SOURCE_IMPORT},
data=config.get(Platform.SWITCH, [])
# hass.data.setdefault(COMPONENT_DOMAIN, {})

if COMPONENT_DOMAIN not in hass.data:
hass.data[COMPONENT_DOMAIN] = {}
hass.data[COMPONENT_CONFIG] = {}

# See if yaml support was enabled.
if not config.get(COMPONENT_DOMAIN, {}).get(CONF_YAML_CONFIG, False):

# New style. We import old config if needed.
_LOGGER.debug("setting up new momentary components")
hass.data[COMPONENT_CONFIG][CONF_YAML_CONFIG] = False

# See if we have already imported the data. If we haven't then do it now.
config_entry = _async_find_matching_config_entry(hass)
if not config_entry:
_LOGGER.debug('importing a YAML setup')
hass.async_create_task(
hass.config_entries.flow.async_init(
COMPONENT_DOMAIN,
context={CONF_SOURCE: SOURCE_IMPORT},
data=config.get(Platform.SWITCH, [])
)
)
)

async_create_issue(
hass,
HOMEASSISTANT_DOMAIN,
f"deprecated_yaml_{COMPONENT_DOMAIN}",
is_fixable=False,
issue_domain=COMPONENT_DOMAIN,
severity=IssueSeverity.WARNING,
translation_key="deprecated_yaml",
translation_placeholders={
"domain": COMPONENT_DOMAIN,
"integration_title": "Momentary",
},
)

return True

_LOGGER.debug('ignoring a YAML setup')

async_create_issue(
hass,
HOMEASSISTANT_DOMAIN,
f"deprecated_yaml_{COMPONENT_DOMAIN}",
is_fixable=False,
issue_domain=COMPONENT_DOMAIN,
severity=IssueSeverity.WARNING,
translation_key="deprecated_yaml",
translation_placeholders={
"domain": COMPONENT_DOMAIN,
"integration_title": "Momentary",
},
)

else:
_LOGGER.debug('ignoring a YAML setup')

else:
_LOGGER.debug("setting up old momentary components")
hass.data[COMPONENT_CONFIG][CONF_YAML_CONFIG] = True

return True


Expand All @@ -78,6 +103,10 @@ def _async_find_matching_config_entry(hass):
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
_LOGGER.debug(f'async setup {entry.data}')

if COMPONENT_DOMAIN not in hass.data:
hass.data[COMPONENT_DOMAIN] = {}
hass.data[COMPONENT_CONFIG] = {}

# Database of devices
group_name = entry.data[ATTR_GROUP_NAME]
file_name = entry.data[ATTR_FILE_NAME]
Expand Down
2 changes: 2 additions & 0 deletions custom_components/momentary/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from datetime import timedelta

COMPONENT_DOMAIN = "momentary"
COMPONENT_CONFIG = "momentary-config"
COMPONENT_MANUFACTURER = "twrecked"
COMPONENT_MODEL = "momentary"

Expand All @@ -24,6 +25,7 @@
CONF_MODE = "mode"
CONF_NAME = "name"
CONF_TOGGLE_FOR = "toggle_for"
CONF_YAML_CONFIG = "yaml_config"

DEFAULT_CANCELLABLE = False
DEFAULT_IMPORTED_NAME = "import"
Expand Down
Loading
Loading