Skip to content

Commit 9f68ae7

Browse files
authored
Fix UI bug, don't hide event,` switch dashboard strategy impl (#37)
1 parent 1cf8796 commit 9f68ae7

File tree

7 files changed

+59
-87
lines changed

7 files changed

+59
-87
lines changed

custom_components/lock_code_manager/__init__.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,18 +61,18 @@ async def async_setup(hass: HomeAssistant, config: Config) -> bool:
6161
await resources.async_load()
6262
resources.loaded = True
6363

64+
# Expose strategy javascript
65+
hass.http.register_static_path(
66+
STRATEGY_PATH, Path(__file__).parent / "www" / STRATEGY_FILENAME
67+
)
68+
6469
try:
6570
res_id = next(
6671
id
6772
for id, data in resources.data.items()
6873
if data["url"] == STRATEGY_PATH
6974
)
7075
except StopIteration:
71-
# Expose strategy javascript
72-
hass.http.register_static_path(
73-
STRATEGY_PATH, Path(__file__).parent / "www" / STRATEGY_FILENAME
74-
)
75-
7676
# Register strategy module
7777
data = await resources.async_create_item(
7878
{"res_type": "module", "url": STRATEGY_PATH}

custom_components/lock_code_manager/websocket.py

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,6 @@ async def async_setup(hass: HomeAssistant) -> bool:
8484
"""Enable the websocket_commands."""
8585
websocket_api.async_register_command(hass, get_slot_calendar_data)
8686
websocket_api.async_register_command(hass, get_config_entry_entities)
87-
websocket_api.async_register_command(hass, get_config_entries_to_entities)
8887

8988
return True
9089

@@ -146,31 +145,3 @@ async def get_config_entry_entities(
146145
],
147146
],
148147
)
149-
150-
151-
@websocket_api.websocket_command(
152-
{vol.Required("type"): "lock_code_manager/get_config_entries_to_entities"}
153-
)
154-
@websocket_api.async_response
155-
async def get_config_entries_to_entities(
156-
hass: HomeAssistant,
157-
connection: websocket_api.ActiveConnection,
158-
msg: dict[str, Any],
159-
) -> None:
160-
"""Return entities for multiple lock_code_manager config entries."""
161-
connection.send_result(
162-
msg["id"],
163-
[
164-
[
165-
config_entry.entry_id,
166-
config_entry.title,
167-
[
168-
entity.as_partial_dict
169-
for entity in er.async_entries_for_config_entry(
170-
er.async_get(hass), config_entry.entry_id
171-
)
172-
],
173-
]
174-
for config_entry in hass.config_entries.async_entries(DOMAIN)
175-
],
176-
)

custom_components/lock_code_manager/www/lock-code-manager-strategy.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

js/dashboard-strategy.ts

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,17 @@ import { ReactiveElement } from 'lit';
22

33
import { DEFAULT_INCLUDE_CODE_SLOT_SENSORS } from './const';
44
import { HomeAssistant } from './ha_type_stubs';
5-
import { generateView } from './helpers';
6-
import { LockCodeManagerDashboardStrategyConfig, LockCodeManagerEntitiesResponse } from './types';
5+
import { generateView, slugify } from './helpers';
6+
import { GetConfigEntriesResponse, LockCodeManagerDashboardStrategyConfig } from './types';
77

88
export class LockCodeManagerDashboardStrategy extends ReactiveElement {
99
static async generate(config: LockCodeManagerDashboardStrategyConfig, hass: HomeAssistant) {
10-
const configEntriesAndEntities = await hass.callWS<LockCodeManagerEntitiesResponse[]>({
11-
type: 'lock_code_manager/get_config_entries_to_entities'
10+
const configEntries = await hass.callWS<GetConfigEntriesResponse>({
11+
domain: 'lock_code_manager',
12+
type: 'config_entries/get'
1213
});
1314

14-
if (configEntriesAndEntities.length === 0) {
15+
if (configEntries.length === 0) {
1516
return {
1617
title: 'Lock Code Manager',
1718
views: [
@@ -29,16 +30,19 @@ export class LockCodeManagerDashboardStrategy extends ReactiveElement {
2930
};
3031
}
3132

32-
const views = await Promise.all(
33-
configEntriesAndEntities.map(([configEntryId, configEntryTitle, entities]) =>
34-
generateView(
35-
hass,
36-
configEntryId,
37-
configEntryTitle,
38-
entities,
39-
config.include_code_slot_sensors ?? DEFAULT_INCLUDE_CODE_SLOT_SENSORS
40-
)
41-
)
33+
const views: object[] = await Promise.all(
34+
configEntries.map((configEntry) => {
35+
return {
36+
path: slugify(configEntry.title),
37+
strategy: {
38+
config_entry_id: configEntry.entry_id,
39+
include_code_slot_sensors:
40+
config.include_code_slot_sensors ?? DEFAULT_INCLUDE_CODE_SLOT_SENSORS,
41+
type: 'custom:lock-code-manager'
42+
},
43+
title: configEntry.title
44+
};
45+
})
4246
);
4347

4448
if (views.length === 1) {

js/helpers.ts

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,14 @@ export async function generateView(
7070
// cards.push({});
7171
}
7272

73+
console.log({
74+
badges,
75+
cards,
76+
panel: false,
77+
path: slugify(configEntryTitle),
78+
title: configEntryTitle
79+
});
80+
7381
return {
7482
badges,
7583
cards,
@@ -135,11 +143,10 @@ function generateSlotCard(
135143
{
136144
entity: slotMapping.pinShouldBeEnabledEntity.entity_id
137145
},
138-
...maybeGenerateFoldEntityRowCard(
139-
slotMapping.codeEventEntityIds,
140-
'Unlock Events for this Slot',
141-
useFoldEntityRow
142-
),
146+
{
147+
entity: slotMapping.codeEventEntityId,
148+
name: 'PIN Last Used'
149+
},
143150
...maybeGenerateFoldEntityRowCard(
144151
slotMapping.conditionEntityIds,
145152
'Conditions',
@@ -170,14 +177,14 @@ function getSlotMapping(
170177
const mainEntityIds: string[] = [];
171178
const conditionEntityIds: string[] = [];
172179
const codeSensorEntityIds: string[] = [];
173-
const codeEventEntityIds: string[] = [];
180+
let codeEventEntityId: string;
174181
lockCodeManagerEntities
175182
.filter((entity) => entity.slotNum === slotNum)
176183
.forEach((entity) => {
177184
if (entity.key === CODE_SENSOR_KEY) {
178185
codeSensorEntityIds.push(entity.entity_id);
179186
} else if (entity.key === CODE_EVENT_KEY) {
180-
codeEventEntityIds.push(entity.entity_id);
187+
codeEventEntityId = entity.entity_id;
181188
} else if (CONDITION_KEYS.includes(entity.key)) {
182189
conditionEntityIds.push(entity.entity_id);
183190
} else if (entity.key !== PIN_SYNCED_TO_LOCKS_KEY) {
@@ -190,7 +197,7 @@ function getSlotMapping(
190197
const calendarEntityId = configEntryData.slots[slotNum];
191198
if (calendarEntityId) conditionEntityIds.unshift(calendarEntityId);
192199
return {
193-
codeEventEntityIds,
200+
codeEventEntityId,
194201
codeSensorEntityIds,
195202
conditionEntityIds,
196203
mainEntityIds,
@@ -228,7 +235,7 @@ function maybeGenerateFoldEntityRowCard(
228235
}
229236

230237
// https://gist.github.com/hagemann/382adfc57adbd5af078dc93feef01fe1
231-
function slugify(value: string, delimiter = '-'): string {
238+
export function slugify(value: string, delimiter = '-'): string {
232239
const a = 'àáâäæãåāăąçćčđďèéêëēėęěğǵḧîïíīįìıİłḿñńǹňôöòóœøōõőṕŕřßśšşșťțûüùúūǘůűųẃẍÿýžźż·';
233240
const b = `aaaaaaaaaacccddeeeeeeeegghiiiiiiiilmnnnnoooooooooprrsssssttuuuuuuuuuwxyyzzz${delimiter}`;
234241
const p = new RegExp(a.split('').join('|'), 'g');

js/types.ts

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,8 @@ export interface LockCodeManagerDashboardStrategyConfig extends LockCodeManagerS
1414
type: 'custom:lock-code-manager';
1515
}
1616

17-
export interface ConfigEntryToEntities {
18-
configEntry: ConfigEntry;
19-
entities: LockCodeManagerEntityEntry[];
20-
}
21-
2217
export interface SlotMapping {
23-
codeEventEntityIds: string[];
18+
codeEventEntityId: string;
2419
codeSensorEntityIds: string[];
2520
conditionEntityIds: string[];
2621
mainEntityIds: string[];
@@ -40,3 +35,20 @@ export interface LockCodeManagerConfigEntryData {
4035
locks: string[];
4136
slots: { [key: number]: string | null };
4237
}
38+
39+
export interface ConfigEntryJSONFragment {
40+
disabled_by: string;
41+
domain: string;
42+
entry_id: string;
43+
pref_disable_new_entities: boolean;
44+
pref_disable_polling: boolean;
45+
reason: string | null;
46+
source: string;
47+
state: string;
48+
supports_options: boolean;
49+
supports_remove_device: boolean;
50+
supports_unload: boolean;
51+
title: string;
52+
}
53+
54+
export type GetConfigEntriesResponse = ConfigEntryJSONFragment[];

tests/test_websocket.py

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -107,25 +107,3 @@ async def test_get_config_entry_entities(
107107
assert entry_id == lock_code_manager_config_entry.entry_id
108108
assert title == "Mock Title"
109109
assert len(entities) == 15
110-
111-
112-
async def test_get_config_entries_to_entities(
113-
hass: HomeAssistant,
114-
mock_lock_config_entry,
115-
lock_code_manager_config_entry,
116-
hass_ws_client: WebSocketGenerator,
117-
) -> None:
118-
"""Test get_config_entries_to_entities WS API."""
119-
ws_client = await hass_ws_client(hass)
120-
121-
# Try API call with entry ID
122-
await ws_client.send_json(
123-
{"id": 1, "type": "lock_code_manager/get_config_entries_to_entities"}
124-
)
125-
msg = await ws_client.receive_json()
126-
assert msg["success"]
127-
assert len(msg["result"]) == 1
128-
[[entry_id, title, entities]] = msg["result"]
129-
assert entry_id == lock_code_manager_config_entry.entry_id
130-
assert title == "Mock Title"
131-
assert len(entities) == 15

0 commit comments

Comments
 (0)