Skip to content

Commit cd70202

Browse files
committed
handle new line in regexp
1 parent fe65800 commit cd70202

File tree

3 files changed

+58
-11
lines changed

3 files changed

+58
-11
lines changed

src/helpers.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,5 +34,6 @@ export const wildcardToRegExp = (s: string | undefined): RegExp | undefined => {
3434
.map(x => regExpEscape(x))
3535
.join('.*') +
3636
'$',
37+
's',
3738
);
3839
};

test/config_helper.test.ts

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,3 @@
1-
// state_map:
2-
// - value: home
3-
// icon: mdi:home
4-
// attributes:
5-
// - name: gps_accuracy
6-
// value: 13
7-
81
import { expect, test } from 'vitest';
92
import { toStateMapRegex } from '../src/config-helpers';
103
import { StateMap, StateMapRegexp } from '../src/types';
@@ -35,19 +28,19 @@ test('should return convert array if state map not ', () => {
3528

3629
const expectedRegexpMap: Array<StateMapRegexp> = [
3730
{
38-
value: new RegExp('^home$'),
31+
value: new RegExp('^home$', 's'),
3932
icon: 'mdi:home',
4033
icon_color: 'green',
4134
attributes: [],
4235
},
4336
{
44-
value: new RegExp('^away$'),
37+
value: new RegExp('^away$', 's'),
4538
icon: 'mdi:home',
4639
icon_color: 'red',
4740
attributes: [
4841
{
4942
name: 'gps_accuracy',
50-
value: new RegExp('^13$'),
43+
value: new RegExp('^13$', 's'),
5144
},
5245
],
5346
},

test/history.test.ts

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { wildcardToRegExp } from '../src/helpers';
44
import { History } from '../src/types';
55
import { expect, test, describe } from 'vitest';
66
import { Context } from 'home-assistant-js-websocket';
7-
import { toHiddenRegex } from '../src/config-helpers';
7+
import { toHiddenRegex, toStateMapRegex } from '../src/config-helpers';
88

99
const hass = {
1010
locale: {
@@ -249,6 +249,59 @@ describe('attributes', () => {
249249
expect(h.attributes).toHaveLength(0);
250250
});
251251
});
252+
253+
test('should return only attributes present', () => {
254+
const configuration = buildConfig({
255+
attributes: [
256+
{
257+
value: 'unknown',
258+
label: 'Inconnu',
259+
},
260+
{
261+
value: 'battery_level',
262+
},
263+
],
264+
});
265+
266+
const history = toHistory(rawHistory, hass, configuration);
267+
history.forEach(h => {
268+
expect(h.attributes).toHaveLength(1);
269+
expect(h.attributes).toEqual([{ name: 'battery_level', value: 80 }]);
270+
});
271+
});
272+
273+
test('state with line breaks', () => {
274+
const raw = [
275+
{
276+
entity_id: 'sensor.notify_last_redmi_all_attr',
277+
state: 'state with multiple line\nline2\nline3',
278+
attributes: {
279+
Apps: 'com.google.android.apps.messaging',
280+
icon: 'mdi:message',
281+
friendly_name: 'Notify last redmi all attr',
282+
},
283+
last_changed: '2023-05-29T17:27:30.199538+00:00',
284+
last_updated: '2023-05-29T17:27:30.199538+00:00',
285+
context: context,
286+
},
287+
];
288+
289+
const configuration = buildConfig({
290+
state_map: [
291+
{
292+
value: wildcardToRegExp('*'),
293+
icon: 'mid: home',
294+
icon_color: '#094689',
295+
attributes: [{ name: 'Apps', value: wildcardToRegExp('com.google.android.apps.messaging') }],
296+
},
297+
],
298+
});
299+
300+
const history = toHistory(raw, hass, configuration);
301+
history.forEach(h => {
302+
expect(h.icon.color).toBe('#094689');
303+
});
304+
});
252305
});
253306

254307
describe('hide entry', () => {

0 commit comments

Comments
 (0)