Skip to content

Commit

Permalink
219 setting an entity alongside any type of tap action is ignored (#228)
Browse files Browse the repository at this point in the history
* Fix entity alongside any type of tap_action

* Updated info.md
  • Loading branch information
marcokreeft87 committed May 16, 2023
1 parent 8982dad commit 407fe18
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 13 deletions.
4 changes: 4 additions & 0 deletions info.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
{% if installed %}

### Features
{% if version_installed.replace("v", "").replace(".","") | int < 10721 %}
- Fixed `entity alongside any type of tap_actions`
{% endif %}

{% if version_installed.replace("v", "").replace(".","") | int < 10720 %}
- Added `seperated card_styles and main entity styles`
{% endif %}
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "room-card",
"version": "1.07.20",
"version": "1.07.21",
"description": "Show entities in Home Assistant's Lovelace UI",
"keywords": [
"home-assistant",
Expand Down
6 changes: 3 additions & 3 deletions room-card.js

Large diffs are not rendered by default.

Binary file modified room-card.js.gz
Binary file not shown.
20 changes: 14 additions & 6 deletions src/entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { secondsToDuration } from './lib/seconds_to_duration';
import { formatNumber } from './lib/format_number';
import { computeStateDisplay, computeStateDomain } from './lib/compute_state_display';
import { checkConditionalValue, evalTemplate, getValue, isObject, isUnavailable, renderClasses } from './util';
import { ActionHandlerEvent, handleAction, hasAction, HomeAssistant } from 'custom-card-helpers';
import { ActionConfig, ActionHandlerEvent, handleAction, hasAction, HomeAssistant } from 'custom-card-helpers';
import { HomeAssistantEntity, EntityCondition, RoomCardEntity, RoomCardIcon, RoomCardConfig, EntityStyles, RoomCardRow, RoomCardAttributeTemplate } from './types/room-card-types';
import { html, HTMLTemplateResult, LitElement } from 'lit';
import { LAST_CHANGED, LAST_UPDATED, TIMESTAMP_FORMATS } from './lib/constants';
Expand Down Expand Up @@ -183,17 +183,25 @@ export const clickHandler = (element: LitElement, hass: HomeAssistant, entity: R
handleAction(element, hass, entity, ev.detail.action);
}

export const generateActions = (config: RoomCardConfig, entity: RoomCardEntity) => {
return {
tap_action: generateAction(config.tap_action, entity),
double_tap_action: generateAction(config.double_tap_action, entity),
hold_action: generateAction(config.hold_action, entity),
} as RoomCardEntity
}

export const generateAction = (action: ActionConfig, entity: RoomCardEntity) => {
return entity && action?.action !== undefined && action.action !== "more-info" ? entity : action;
}

export const renderTitle = (config: RoomCardConfig, hass: HomeAssistant, element: LitElement, entity?: RoomCardEntity, ) : HTMLTemplateResult => {
if(config.hide_title === true)
return null;

const _handleAction = (ev: ActionHandlerEvent): void => {
if (hass && ev.detail.action) {
clickHandler(element, hass, entity ?? {
tap_action: config.tap_action,
double_tap_action: config.double_tap_action,
hold_action: config.hold_action
} as RoomCardEntity, ev);
clickHandler(element, hass, generateActions(config, entity), ev);
}
}

Expand Down
35 changes: 34 additions & 1 deletion tests/entity/renderTitle.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ActionHandlerEvent, HomeAssistant, MoreInfoActionConfig } from 'custom-card-helpers';
import { ActionHandlerEvent, HomeAssistant, MoreInfoActionConfig, NavigateActionConfig } from 'custom-card-helpers';
import { LitElement } from "lit";
import { createMock } from "ts-auto-mock";
import { renderTitle } from "../../src/entity";
Expand Down Expand Up @@ -168,6 +168,39 @@ describe('Testing entity file function renderValue', () => {

endFn(mouseEvent);

expect(clickHandler).toHaveBeenCalled();
}),
test('Mouseclick should trigger action', () => {

const clickHandler = jest.spyOn(entityModule, 'clickHandler');
const entity: RoomCardEntity = {
stateObj: stateObj,
show_icon: true,
icon: 'mdi:table'
};

stateObj.attributes['title'] = "Test title";

const config: RoomCardConfig = {
entityIds: ['light.test_entity'],
type: '',
tap_action: { action: 'navigate' } as NavigateActionConfig,
title: 'Test'
};

const result = renderTitle(config, hass, element, entity);

// eslint-disable-next-line @typescript-eslint/ban-types
const endFn = result.values[1] as Function;

const mouseEvent = createMock<ActionHandlerEvent>({
detail: {
action: 'test'
}
});

endFn(mouseEvent);

expect(clickHandler).toHaveBeenCalled();
})
});

0 comments on commit 407fe18

Please sign in to comment.