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

Logbook: Localize Message & Style updates #6978

Merged
merged 24 commits into from
Sep 30, 2020
Merged

Conversation

zsarnett
Copy link
Contributor

@zsarnett zsarnett commented Sep 13, 2020

Proposed change

image
00e8135a302167be077c6f454006fdf9
image

Type of change

  • New feature (thank you!)
  • Code quality improvements to existing code or addition of tests

Checklist

  • The code change is tested and works locally.
  • There is no commented out code in this PR.

Core PR is reliant on the frontend PR merging before it can be merged as it is a breaking change: home-assistant/core#40070

@zsarnett
Copy link
Contributor Author

zsarnett commented Sep 13, 2020

Previous Conversation

zsarnett 2 hours ago •
Couple things:

  • Currently, in the Logbook Element we are basing the icon off of the current state of the entity. Not the state of the entity at the time of the logbook entry
    To do this, we need the logbook API to return the state that the entity was set to. Right now we return a message that includes the state but in a "was set to On" form.

  • We are unable, at least to my testing and knowledge, to specify more than one entity for the logbook. Its either 1 entity or all entities. I would like this card to work with multiple entities. Example: All motion entities.

@bdraco Worked with you recently to help us with the More info logbook. Would you be able to help me with these changes in the backend to better support this card and the logbook as a whole?


@bdraco
Getting the icon is expensive because we have to decode the json. If there can be only one icon in the attributes we can usually avoid the json decode. I think that's the case, but I need to validate that.

Can you provide a bit more detail on the multi entity use case?


@zsarnett
We are already decoding the object that is sent to the frontend. (I don't know much about the backend section) but if you have the was turned on message. I don't know how that's built but if you send in the last on as a state property we get the icon in the frontend. I don't know if that's what you mean when you say expensive.

Use case would be this card. Being able to see a log book for all Motion Sensor entities and building a security dashboard of sorts with the card. All Windows. We would be allowing the users to choose which entities to query for instead of just one or all of them


bdraco (J. Nick Koston)
For the icon, can you try

home-assistant/core#40039


@bdraco
For the icon, can you try

home-assistant/core#40039

I think that will only work if there is a custom icon set. To calculate the default icon on the frontend, I think you need the 'state' as well?


@bdraco

For the icon, can you try
home-assistant/core#40039

I think that will only work if there is a custom icon set. To calculate the default icon on the frontend, I think you need the 'state' as well?

I adjusted the PR to provide state and icon


@bdraco

We have this giant block to create the message home-assistant/core@aa07171/homeassistant/components/logbook/init.py#L551. Ideally we send state from the backend and no message and the frontend will generate the message (localized) instead. This would get us a smaller response.

I'd also like to drop "domain" as it appears to be unused by the frontend and means we end up sending the string again since its already in "entity_id"


@bdraco
I dont think we need to send Icon or domain. We can get those from Hass in the frontend

I can drop icon but I think its needed to fully match what the frontend does:
https://github.com/home-assistant/frontend/blob/dev/src/common/entity/state_icon.ts#L22


@zsarnett

Actually, Icon would be helpful actually. Re reading the code


@zsarnett

We don't need domain. We currently use domain but we don't need to


@bdraco

What do you think about doing this part on the frontend?
home-assistant/core@aa07171/homeassistant/components/logbook/init.py#L551

Edit: even if the first step is just porting the code and it isn't localized, it would be an improvement as the backend wouldn't have to send those text blobs for each state.


@zsarnett

I much prefer us to do that in the Frontend as we can run translations (I actually don't know if that done in the backend or not)

@bramkragten (who is probably in bed) would be a better fit to say whether we should or not.

I personally think it should be in the frontend though

@zsarnett
Copy link
Contributor Author

I have pulled out the changes for the logbook into here. Original Discussion was here: #6976

@zsarnett
Copy link
Contributor Author

I have started the logbook message in the frontend but it still needs localized

@zsarnett zsarnett changed the title Log book style updates rework functionality Logbook: Localize Message & Style updates Sep 15, 2020
@bdraco
Copy link
Member

bdraco commented Sep 15, 2020

Screen Shot 2020-09-14 at 10 03 50 PM

It looks like we are missing icons for start/stop/restart events in the homeassistant domain

@bdraco
Copy link
Member

bdraco commented Sep 15, 2020

{
	"when": "2020-09-15T02:59:41+00:00",
	"name": "Home Assistant",
	"message": "stopped",
	"domain": "homeassistant"
}, {
	"when": "2020-09-15T03:00:05+00:00",
	"name": "Masterbed TV Cabinet 32 Restart",
	"state": "off",
	"entity_id": "switch.masterbed_tv_cabinet_32_restart",
	"icon": "mdi:restart"
}, {
	"when": "2020-09-15T03:00:05+00:00",
	"name": "Gate Trigger 32 Relay",
	"state": "off",
	"entity_id": "binary_sensor.gate_trigger_32_relay"
}

@zsarnett
Copy link
Contributor Author

@bdraco This should be fixed in the latest commit

@bdraco
Copy link
Member

bdraco commented Sep 15, 2020

Looks good.

We did loose the exact time on the more info pop up. Was that one purpose?

@zsarnett
Copy link
Contributor Author

Yes that was on purpose

.entityId=${item.entity_id}
@click=${this._entityClicked}
>
${!this.narrow
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
${!this.narrow
${!this.narrow && !this.relativeTime

It's redundant to show 2 types of time for the same element.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm I disagree. I was thinking about adding it to the narrow version as well

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Narrow being: Relative_Time - Absolute_Time

Reason being that some people want to know when but I don't want to calculate how many hours/mins ago that was

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Put the absolute time in a tooltip?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

image

Tooltip or something like this?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah that could also work, but the logbook lines can be long, and they wrap now, you don't want a secondary line then I guess...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

image

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From that statement of no secondary then you don't want the relative time in the secondary with the tooltip either? You are saying keep it to the left?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Personally the time to the left makes it look disconnected but idk. I like having both times. Tooltip would be okay. But sometime I want to know the absolute and sometimes I want to know the relative time without having to calculate it

Comment on lines 88 to 89
case "cover": {
const open = compareState !== "closed";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did you move all domain-specific icon code in here instead of importing the function?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to default after wards. If the conditions aren't met we run the last lines of the domain icon

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldnt this on top be enough? If the domain has a specific icon function let that handle it...

  if (domain in domainIcons) {
    return domainIcons[domain](state);	
  }

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some domains have a specific icon but also non specific. Those are a fallback

stateObj: HassEntity,
domain: string
): string => {
const localizePath = "ui.components.logbook.messages";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Move it out of the function.

case "opening":
case "window":
if (isOn) {
return hass.localize(`${localizePath}.was_opened`);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we make the localize names:
ui.components.logbook.messages.${domain}.${device_class || "_"}.${state}

That's what we also use for the state translations, and requires a lot less code.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The state translations are built by core for all the states. Is it even possible to do that here? like in the code there are a lot of if this state do this otherwise this. Doing it your way would require me to do this for all states? no?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You would move the logic to the translations instead of the code, not more complicated I guess?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How do I do the logic in Json?

@zsarnett
Copy link
Contributor Author

zsarnett commented Sep 22, 2020

The link styles just look weird. Especially with two on the same line.

I can put the time on the right. Just made sense to me to have them as the secondary information.

@zsarnett
Copy link
Contributor Author

zsarnett commented Sep 22, 2020

Actually removing the link was my main reason off the PR

image

This just looks dated

@bramkragten
Copy link
Member

bramkragten commented Sep 24, 2020

But in your proposal, you would have a click target (automation) in a click target (entity)? And it would not be clear what a click would do?

I think since they perform the same action, open the more info dialog, they should look the same?

@zsarnett
Copy link
Contributor Author

Well in my mind and the reasoning was the main action of the row click was the Entity that was changed. The more info for the most important entity of the row. The Auatomation/script that changed it is supporting information and is displayed as such. Clicking the link more info to the supporting and clicking the row more info to the main entity.

Is there no better solution than Anchor links 🤮 ... I guess not

@zsarnett
Copy link
Contributor Author

zsarnett commented Sep 24, 2020

4405287a30799b83a8c28b9acbbc9115

@bdraco
Copy link
Member

bdraco commented Sep 25, 2020

Once this is good to go, I can start working on making the triggers localizable.

@bramkragten
Copy link
Member

Does this need a backend change or is this a bug? Am getting an error:

2020-09-28 13:20:08 ERROR (SyncWorker_27) [homeassistant.components.recorder.util] Error executing query: unsupported operand type(s) for |: 'NoneType' and 'BinaryExpression'
2020-09-28 13:20:08 ERROR (MainThread) [aiohttp.server] Error handling request
Traceback (most recent call last):
  File "/Users/bram/Documents/projects/home-assistant/core/venv/lib/python3.7/site-packages/aiohttp/web_protocol.py", line 418, in start
    resp = await task
  File "/Users/bram/Documents/projects/home-assistant/core/venv/lib/python3.7/site-packages/aiohttp/web_app.py", line 458, in _handle
    resp = await handler(request)
  File "/Users/bram/Documents/projects/home-assistant/core/venv/lib/python3.7/site-packages/aiohttp/web_middlewares.py", line 119, in impl
    return await handler(request)
  File "/Users/bram/Documents/projects/home-assistant/core/homeassistant/components/http/request_context.py", line 18, in request_context_middleware
    return await handler(request)
  File "/Users/bram/Documents/projects/home-assistant/core/homeassistant/components/http/ban.py", line 72, in ban_middleware
    return await handler(request)
  File "/Users/bram/Documents/projects/home-assistant/core/homeassistant/components/http/auth.py", line 127, in auth_middleware
    return await handler(request)
  File "/Users/bram/Documents/projects/home-assistant/core/homeassistant/components/http/view.py", line 129, in handle
    result = await result
  File "/Users/bram/Documents/projects/home-assistant/core/homeassistant/components/logbook/__init__.py", line 254, in get
    return await hass.async_add_executor_job(json_events)
  File "/usr/local/Cellar/python/3.7.4/Frameworks/Python.framework/Versions/3.7/lib/python3.7/concurrent/futures/thread.py", line 57, in run
    result = self.fn(*self.args, **self.kwargs)
  File "/Users/bram/Documents/projects/home-assistant/core/homeassistant/components/logbook/__init__.py", line 250, in json_events
    entity_matches_only,
  File "/Users/bram/Documents/projects/home-assistant/core/homeassistant/components/logbook/__init__.py", line 477, in _get_events
    filters.entity_filter() | (Events.event_type != EVENT_STATE_CHANGED)
TypeError: unsupported operand type(s) for |: 'NoneType' and 'BinaryExpression'

@bramkragten
Copy link
Member

Still think the icons should be vertically centered

@zsarnett
Copy link
Contributor Author

Works for me on the latest core dev

@bdraco
Copy link
Member

bdraco commented Sep 28, 2020

Does this need a backend change or is this a bug? Am getting an error:

Does this still happen with home-assistant/core#40387 ?

@bdraco
Copy link
Member

bdraco commented Sep 28, 2020

Does this need a backend change or is this a bug? Am getting an error:

Does this still happen with home-assistant/core#40387 ?

@bramkragten Fixed here home-assistant/core#40565. b7859a0 (#40565)

@bramkragten bramkragten merged commit e2427c8 into dev Sep 30, 2020
@bramkragten bramkragten deleted the logbook-style-and-rework branch September 30, 2020 08:24
@bramkragten bramkragten mentioned this pull request Sep 30, 2020
@github-actions github-actions bot locked and limited conversation to collaborators Jul 5, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants