Adding id to lovelace cards in ui-lovelace.yaml#17498
Adding id to lovelace cards in ui-lovelace.yaml#17498balloob merged 16 commits intohome-assistant:devfrom
Conversation
| # Check if all cards have an ID or else add one | ||
| updated = False | ||
| views = config.get('views') | ||
| if views: |
There was a problem hiding this comment.
for view in config.get('views', []):
balloob
left a comment
There was a problem hiding this comment.
Looks good! Ok to merge when tests added. Make sure to stub out the actual operations that do I/O.
| yaml.indent(sequence=4, offset=2) | ||
| try: | ||
| with open(fname, "w", encoding='utf-8') as conf_file: | ||
| yaml.dump(data, conf_file) |
There was a problem hiding this comment.
This is dangerous, dump the yaml first to a string, then open file and truncate. Otherwise, if the dumping fails, the lovelace config is wiped out.
There was a problem hiding this comment.
Like this?
output = StringIO()
yaml.dump(data, output)
with open("conf.yaml", 'w', encoding='utf-8') as conf_file:
output.seek(0)
copyfileobj(output, conf_file)
output.close()
There was a problem hiding this comment.
So I was googling it, and that's pretty inefficient according to the author of ruamel.yaml.
Why not do this
|
You should not just update the tests, you should also make sure tests are added for the new functionality. |
Yeah I know, going to write test tonight |
| id: example | ||
| # Optional background (overwrites the global background). | ||
| background: radial-gradient(crimson, skyblue) | ||
| # Each view can have a different theme applied. Theme should be defined in the frontend. |
There was a problem hiding this comment.
line too long (92 > 79 characters)
| - url: /local/my-webfont.css | ||
| type: css | ||
|
|
||
| # Optional background for all views. Check https://developer.mozilla.org/en-US/docs/Web/CSS/background for more examples. |
There was a problem hiding this comment.
line too long (121 > 79 characters)
| from homeassistant.setup import async_setup_component | ||
| from homeassistant.components.websocket_api.const import TYPE_RESULT | ||
| from homeassistant.components.lovelace import (WriteError, load_yaml, | ||
| save_yaml, load_config) |
There was a problem hiding this comment.
continuation line under-indented for visual indent
| from homeassistant.exceptions import HomeAssistantError | ||
| from homeassistant.setup import async_setup_component | ||
| from homeassistant.components.websocket_api.const import TYPE_RESULT | ||
| from homeassistant.components.lovelace import (WriteError, load_yaml, |
There was a problem hiding this comment.
'homeassistant.components.lovelace.WriteError' imported but unused
| from tempfile import mkdtemp | ||
| from unittest.mock import patch | ||
| from ruamel.yaml import YAML | ||
| from ruamel.yaml.compat import StringIO |
There was a problem hiding this comment.
'ruamel.yaml.compat.StringIO' imported but unused
| """Test the Lovelace initialization.""" | ||
| import os | ||
| import unittest | ||
| import sys |
man/man1/nosetests.1
Outdated
| @@ -0,0 +1,581 @@ | |||
| .\" Man page generated from reStructuredText. | |||
There was a problem hiding this comment.
This file was probably added to the commit by mistake, huh?
| """Error writing the data.""" | ||
|
|
||
|
|
||
| def save_yaml(fname: str, data: JSON_TYPE): |
There was a problem hiding this comment.
So there is a lot of copy paste going on in these methods. I think that it's fine for now.
A bunch of that will go away once we migrate the util.yaml to ruamel.yaml, as it implements YAML 1.2 and not YAML 1.1.
There was a problem hiding this comment.
btw, I can only recommend ruamel.yaml. Recently started porting esphomeyaml to use it and it's rather simple - since esphomeyaml uses almost the same loader/yaml_util code (😄), I can then also port it to home assistant
| yaml.indent(sequence=4, offset=2) | ||
| tmp_fname = fname + "__TEMP__" | ||
| try: | ||
| with open(os.open(tmp_fname, O_WRONLY | O_CREAT | O_TRUNC, 0o600), |
There was a problem hiding this comment.
Use 0o644, it doesn't have to be private
| from homeassistant.components.lovelace import (load_yaml, | ||
| save_yaml, load_config) | ||
|
|
||
| from tests.common import patch_yaml_files |
There was a problem hiding this comment.
'tests.common.patch_yaml_files' imported but unused
| import os | ||
| import unittest | ||
| from tempfile import mkdtemp | ||
| from io import StringIO |
|
So just wondering... why isn't this documented? Also the release blog post might have at least mentioned this. Any view on this @balloob @bramkragten? |
|
This seems to be causing the file permissions to get reset. I have the file shared over Samba for editing purposes, and every time an ID is added the permissions get set back to read-only for group and other. |
|
The permissions get set to 644, we create a new file and copy that to prevent your config from beining deleted in case of an error. |
|
@frenck nothing changed from a configuration point, but I will add some lines that we add ids if you don't specify them. |
|
@bramkragten I do not agree, for example, the documentation on the Lovelace UI, include the example, states the ID is optional. Apparently, it isn't. My main issue: Magic happened. Something touched my files. Fills it with UUID's (which damn sure aren't pretty), file permissions get reset and there is literally NOTHING in the docs... That can't be right... Some questions that aren't answered now:
So docs are definitely needed. |
|
File permissions should not change....that is a major inconvenience (and requires users to change behavior). Other than that, all we need to indicate in the docs is that |
|
Well, I've learned this is no longer limited to file permissions or changing For example, YAML has multiple options to list multiline strings. This change beautifully migrates it to something else for you. So that might need docs as well. cards:
- type: markdown
content: "Starting with Home Assistant 0.72, we're experimenting with a new way of defining your interface. We're calling it the **Lovelace UI**."Becomes cards:
- id: af5c07ccc4c14d978e25ec7148c20ee6 # Automatically created id
type: markdown
content: Starting with Home Assistant 0.72, we're experimenting with a new
way of defining your interface. We're calling it the **Lovelace UI**.(note the change in |
|
I am guessing it is related to the length and actually improves readability :) Sure, we should include that in the docs so that users are not surprised. |
|
@arsaboo It changes around unrelated stuff, which should not be happening. |
If this is being done, the permissions should be changed back afterwards, otherwise it is a real hassle if we want to be able to update the file over SFTP or Samba. |
|
When I add something to my configuration I Should be the only one to change it. |
|
If you add an ID to all your cards and views we will not touch your file. |
|
You clearly did not see this comment in this issue... |
|
Yes I have. If all cards and views have IDs, we will not add them and won't save the YAML thus not changing the layout. |
|
It simply is not possible to add things programmatically to YAML without ever changing the layout. The indents are hardcoded as well. So if someone would have a different indentation, what would be perfect YAML, we would still save it with 4 spaces. So if you care about your styling, and don't want to use the edit functionality, make sure everything has an ID. |
|
IMHO we should revert this PR. I don't get why we are having migration/helper logic in this case, while we are usually just marking breaking changes.
That statement should have been made before and the PR should have been declined in the first place. Just based on that.
So wow, really? That is like saying... If you forget it once... We'll just go ahead and fool around? As a punishment for forgetting things? I'm sorry, I want things to move forward. But this is just the wrong approach. |
|
I think maybe we should treat this upcoming UI editor more like the existing Automation and Script editors. Power users simply don't use these tools for the reasons stated by @bramkragten. We should only touch the lovelace file IF a user decides to SAVE from the UI editor. This way power users can just avoid it (like we do today) and users who like the editors can use it. In fact in order to use the automation editor the user needs to meet certain requirements so we should do the same requirements here. |
|
I'm only speaking for myself here, but my issue with this isn't so much the fact that the file is being updated with these randomly generated ids (I could probably live with that) but the fact that, in doing this, it is resetting the file permissions on the file which makes it a lot harder to edit the file over SFTP (and possibly Samba, I don't use hass.io so I don't know about that). |
|
@frenck I do understand that it's annoying for power users. For them, introducing a breaking change would probably be more useful. However, once a UI editor comes in we'll have to have a migration system in place. If you're a new user, you should in the future never have to touch YAML files. But every so often the config schema will need to be changed. Then, we can't simply create breaking changes anymore as that's really bad for new users. I mean we're already using If we're going to need to use a migration system in the future (for the UI editor), why not integrate the migration system now? The other option would be to have two different lovelace config systems:
The problem with that approach would be that:
I think we should focus on making the YAML migration tool better, so that it preserves more of the base format. But constantly having breaking changes is something that I hope lovelace/config entries would solve going forward. |
|
@OttoWinter That is still no justification. But thanks for your input.
Large part of the HA system is moving that logic OUT of the configuration files managed by the power user. This just re-introduces old mistakes. I'll go ahead an see if I can create a PR that reverts this (since a couple of PR has been dumped on top of this file already this became a little puzzle). |
|
PS: Another one came up... "!secrets" no longer work now. Which is kinda a security breaking change. |
|
@gadgetchnnel made a update for that f6b9e68 |
Description:
First step to configurate lovelace from the ui, adding ID's to cards in ui-lovelace.yaml
Related issue (if applicable): fixes #
Pull request in home-assistant.io with documentation (if applicable): home-assistant/home-assistant.io#<home-assistant.io PR number goes here>
Example entry for
configuration.yaml(if applicable):Checklist:
tox. Your PR cannot be merged unless tests passIf user exposed functionality or configuration variables are added/changed:
If the code communicates with devices, web services, or third-party tools:
REQUIREMENTSvariable (example).requirements_all.txtby runningscript/gen_requirements_all.py..coveragerc.If the code does not interact with devices: