Skip to content

Commit 87ab50a

Browse files
committed
First commit
1 parent f94ad56 commit 87ab50a

File tree

10 files changed

+890
-0
lines changed

10 files changed

+890
-0
lines changed

.gitignore

Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
#
2+
.vscode
3+
4+
5+
# Byte-compiled / optimized / DLL files
6+
__pycache__/
7+
*.py[cod]
8+
*$py.class
9+
10+
# C extensions
11+
*.so
12+
13+
# Distribution / packaging
14+
.Python
15+
build/
16+
develop-eggs/
17+
dist/
18+
downloads/
19+
eggs/
20+
.eggs/
21+
lib/
22+
lib64/
23+
parts/
24+
sdist/
25+
var/
26+
wheels/
27+
share/python-wheels/
28+
*.egg-info/
29+
.installed.cfg
30+
*.egg
31+
MANIFEST
32+
33+
# PyInstaller
34+
# Usually these files are written by a python script from a template
35+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
36+
*.manifest
37+
*.spec
38+
39+
# Installer logs
40+
pip-log.txt
41+
pip-delete-this-directory.txt
42+
43+
# Unit test / coverage reports
44+
htmlcov/
45+
.tox/
46+
.nox/
47+
.coverage
48+
.coverage.*
49+
.cache
50+
nosetests.xml
51+
coverage.xml
52+
*.cover
53+
*.py,cover
54+
.hypothesis/
55+
.pytest_cache/
56+
cover/
57+
58+
# Translations
59+
*.mo
60+
*.pot
61+
62+
# Django stuff:
63+
*.log
64+
local_settings.py
65+
db.sqlite3
66+
db.sqlite3-journal
67+
68+
# Flask stuff:
69+
instance/
70+
.webassets-cache
71+
72+
# Scrapy stuff:
73+
.scrapy
74+
75+
# Sphinx documentation
76+
docs/_build/
77+
78+
# PyBuilder
79+
.pybuilder/
80+
target/
81+
82+
# Jupyter Notebook
83+
.ipynb_checkpoints
84+
85+
# IPython
86+
profile_default/
87+
ipython_config.py
88+
89+
# pyenv
90+
# For a library or package, you might want to ignore these files since the code is
91+
# intended to run in multiple environments; otherwise, check them in:
92+
# .python-version
93+
94+
# pipenv
95+
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
96+
# However, in case of collaboration, if having platform-specific dependencies or dependencies
97+
# having no cross-platform support, pipenv may install dependencies that don't work, or not
98+
# install all needed dependencies.
99+
#Pipfile.lock
100+
101+
# PEP 582; used by e.g. github.com/David-OConnor/pyflow
102+
__pypackages__/
103+
104+
# Celery stuff
105+
celerybeat-schedule
106+
celerybeat.pid
107+
108+
# SageMath parsed files
109+
*.sage.py
110+
111+
# Environments
112+
.env
113+
.venv
114+
env/
115+
venv/
116+
ENV/
117+
env.bak/
118+
venv.bak/
119+
120+
# Spyder project settings
121+
.spyderproject
122+
.spyproject
123+
124+
# Rope project settings
125+
.ropeproject
126+
127+
# mkdocs documentation
128+
/site
129+
130+
# mypy
131+
.mypy_cache/
132+
.dmypy.json
133+
dmypy.json
134+
135+
# Pyre type checker
136+
.pyre/
137+
138+
# pytype static type analyzer
139+
.pytype/
140+
141+
# Cython debug symbols
142+
cython_debug/
143+
144+
# Additional
145+
.storage/
146+
www/
147+
blueprints/
148+
secrets.yaml
149+
scripts.yaml
150+
scenes.yaml
151+
home-assistant_v2.db
152+
home-assistant.log.1
153+
configuration.yaml
154+
automations.yaml
155+
.HA_VERSION
156+
home-assistant.log.fault
157+
home-assistant_v2.db-shm
158+
home-assistant_v2.db-wal
159+
160+
# OS generated files #
161+
######################
162+
.DS_Store
163+
.DS_Store?
164+
._*
165+
.Spotlight-V100
166+
.Trashes
167+
ehthumbs.db
168+
Thumbs.db
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
## 1a. Install via HACS (recommended)
2+
Will add to HACS if there's any interest. Create an [issue](https://github.com/benjamin-dcs/home-assistant/issues) to indicate interest.
3+
4+
## 1b. Manual install
5+
1) Download the latest release of the File++ from this repository;
6+
2) In Home Assistant, create a folder `config/custom_components`;
7+
3) Add the File++ files to the `custom_components` folder (you should end up it with a `config/custom_components/file_plusplus` folder);
8+
4) Restart Home Assistant;
9+
5) Add a File++ entity as an Integration in Home Assistant `(menu: settings -> devices & services -> add integration)`;
10+
11+
For updates, repeat step 1 to 4. Home Assistant will not delete any configuration.
Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
"""The file++ component."""
2+
3+
from copy import deepcopy
4+
from typing import Any
5+
6+
from homeassistant.components.notify import migrate_notify_issue
7+
from homeassistant.config_entries import SOURCE_IMPORT, ConfigEntry
8+
from homeassistant.const import (
9+
CONF_FILE_PATH,
10+
CONF_NAME,
11+
CONF_PLATFORM,
12+
CONF_SCAN_INTERVAL,
13+
Platform,
14+
)
15+
from homeassistant.core import DOMAIN as HOMEASSISTANT_DOMAIN, HomeAssistant
16+
from homeassistant.exceptions import ConfigEntryNotReady
17+
from homeassistant.helpers import (
18+
config_validation as cv,
19+
discovery,
20+
issue_registry as ir,
21+
)
22+
from homeassistant.helpers.typing import ConfigType
23+
24+
from .const import DOMAIN
25+
from .notify import PLATFORM_SCHEMA as NOTIFY_PLATFORM_SCHEMA
26+
from .sensor import PLATFORM_SCHEMA as SENSOR_PLATFORM_SCHEMA
27+
28+
IMPORT_SCHEMA = {
29+
Platform.SENSOR: SENSOR_PLATFORM_SCHEMA,
30+
Platform.NOTIFY: NOTIFY_PLATFORM_SCHEMA,
31+
}
32+
33+
CONFIG_SCHEMA = cv.config_entry_only_config_schema(DOMAIN)
34+
35+
PLATFORMS = [Platform.NOTIFY, Platform.SENSOR]
36+
37+
38+
async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
39+
"""Set up the file integration."""
40+
41+
hass.data[DOMAIN] = config
42+
if hass.config_entries.async_entries(DOMAIN):
43+
# We skip import in case we already have config entries
44+
return True
45+
# The use of the legacy notify service was deprecated with HA Core 2024.6.0
46+
# and will be removed with HA Core 2024.12
47+
migrate_notify_issue(hass, DOMAIN, "File (Large)", "2024.12.0")
48+
# The YAML config was imported with HA Core 2024.6.0 and will be removed with
49+
# HA Core 2024.12
50+
ir.async_create_issue(
51+
hass,
52+
HOMEASSISTANT_DOMAIN,
53+
f"deprecated_yaml_{DOMAIN}",
54+
breaks_in_ha_version="2024.12.0",
55+
is_fixable=False,
56+
issue_domain=DOMAIN,
57+
learn_more_url="https://www.home-assistant.io/integrations/file/",
58+
severity=ir.IssueSeverity.WARNING,
59+
translation_key="deprecated_yaml",
60+
translation_placeholders={
61+
"domain": DOMAIN,
62+
"integration_title": "File (Large)",
63+
},
64+
)
65+
66+
# Import the YAML config into separate config entries
67+
platforms_config: dict[Platform, list[ConfigType]] = {
68+
domain: config[domain] for domain in PLATFORMS if domain in config
69+
}
70+
for domain, items in platforms_config.items():
71+
for item in items:
72+
if item[CONF_PLATFORM] == DOMAIN:
73+
file_config_item = IMPORT_SCHEMA[domain](item)
74+
file_config_item[CONF_PLATFORM] = domain
75+
if CONF_SCAN_INTERVAL in file_config_item:
76+
del file_config_item[CONF_SCAN_INTERVAL]
77+
hass.async_create_task(
78+
hass.config_entries.flow.async_init(
79+
DOMAIN,
80+
context={"source": SOURCE_IMPORT},
81+
data=file_config_item,
82+
)
83+
)
84+
85+
return True
86+
87+
88+
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
89+
"""Set up a file component entry."""
90+
config = {**entry.data, **entry.options}
91+
filepath: str = config[CONF_FILE_PATH]
92+
if filepath and not await hass.async_add_executor_job(
93+
hass.config.is_allowed_path, filepath
94+
):
95+
raise ConfigEntryNotReady(
96+
translation_domain=DOMAIN,
97+
translation_key="dir_not_allowed",
98+
translation_placeholders={"filename": filepath},
99+
)
100+
101+
await hass.config_entries.async_forward_entry_setups(
102+
entry, [Platform(entry.data[CONF_PLATFORM])]
103+
)
104+
entry.async_on_unload(entry.add_update_listener(update_listener))
105+
if entry.data[CONF_PLATFORM] == Platform.NOTIFY and CONF_NAME in entry.data:
106+
# New notify entities are being setup through the config entry,
107+
# but during the deprecation period we want to keep the legacy notify platform,
108+
# so we forward the setup config through discovery.
109+
# Only the entities from yaml will still be available as legacy service.
110+
hass.async_create_task(
111+
discovery.async_load_platform(
112+
hass,
113+
Platform.NOTIFY,
114+
DOMAIN,
115+
config,
116+
hass.data[DOMAIN],
117+
)
118+
)
119+
120+
return True
121+
122+
123+
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
124+
"""Unload a config entry."""
125+
return await hass.config_entries.async_unload_platforms(
126+
entry, [entry.data[CONF_PLATFORM]]
127+
)
128+
129+
130+
async def update_listener(hass: HomeAssistant, entry: ConfigEntry) -> None:
131+
"""Handle options update."""
132+
await hass.config_entries.async_reload(entry.entry_id)
133+
134+
135+
async def async_migrate_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> bool:
136+
"""Migrate config entry."""
137+
if config_entry.version > 2:
138+
# Downgraded from future
139+
return False
140+
141+
if config_entry.version < 2:
142+
# Move optional fields from data to options in config entry
143+
data: dict[str, Any] = deepcopy(dict(config_entry.data))
144+
options = {}
145+
for key, value in config_entry.data.items():
146+
if key not in (CONF_FILE_PATH, CONF_PLATFORM, CONF_NAME):
147+
data.pop(key)
148+
options[key] = value
149+
150+
hass.config_entries.async_update_entry(
151+
config_entry, version=2, data=data, options=options
152+
)
153+
return True

0 commit comments

Comments
 (0)