-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstore.ts
140 lines (113 loc) · 4.45 KB
/
store.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
// lovelace card imports.
import { HomeAssistant } from '../types/home-assistant-frontend/home-assistant';
import { HassEntity } from 'home-assistant-js-websocket';
// our imports.
import { HassService } from '../services/hass-service';
import { MediaControlService } from '../services/media-control-service';
import { SpotifyPlusService } from '../services/spotifyplus-service';
import { Card } from '../card';
import { BaseEditor } from '../editor/base-editor';
import { CardConfig } from '../types/card-config';
import { ConfigArea } from '../types/config-area';
import { Section } from '../types/section';
import { MediaPlayer } from './media-player';
/**
* Card storage class instance.
*
* This class is used to store references to common services and data areas
* that are used by the various card sections.
* */
export class Store {
/** Home Assistant instance. */
public hass: HomeAssistant;
/** Card configuration data. */
public config: CardConfig;
/** Custom card instance. */
public readonly card: Card | BaseEditor;
/** Home Assistant services helper instance. */
public hassService: HassService;
/** SpotifyPlus services helper instance. */
public spotifyPlusService: SpotifyPlusService;
/** MediaControlService services helper instance. */
public mediaControlService: MediaControlService;
/** SpotifyPlus MediaPlayer object that will process requests. */
public player: MediaPlayer;
/** Currently selected section. */
public section: Section;
/** Currently selected ConfigArea **/
static selectedConfigArea: ConfigArea = ConfigArea.GENERAL;
/** card editor render flags for individual sections. */
static hasCardEditLoadedMediaList: { [key: string]: boolean } = {};
/**
* Initializes a new instance of the class.
*
* @param hass Home Assistant instance.
* @param config Card configuration data.
* @param card Custom card instance.
* @param section Currently selected section of the card.
* @param playerId Entity ID of the SpotifyPlus device that will process requests.
*/
constructor(hass: HomeAssistant, config: CardConfig, card: Card | BaseEditor, section: Section, playerId: string) {
// if hass property has not been set yet, then it's a programmer problem!
if (!hass) {
throw new Error("STPC0005 hass property has not been set!");
}
// initialize storage.
this.hass = hass;
this.config = config;
this.card = card;
this.hassService = new HassService(hass);
this.mediaControlService = new MediaControlService(this.hassService);
this.spotifyPlusService = new SpotifyPlusService(hass, card);
this.player = this.getMediaPlayerObject(playerId);
this.section = section;
}
/**
* Returns a MediaPlayer object for the given entity id value.
*
* @param entityId Entity ID of the media player.
* @returns A MediaPlayer object.
* @throws Error if the specified entityId values does not exist in the hass.states data.
*/
public getMediaPlayerObject(entityId: string) {
// has an entity been configured?
if ((!this.config) || (!this.config.entity) || (this.config.entity.trim() == "")) {
// entityId will not be set in the config if coming from the card picker;
// this is ok, as we want it to render a "needs configured" card ui.
// in this case, we just create an "empty" MediaPlayer instance.
return new MediaPlayer({
entity_id: "",
state: "",
last_changed: "",
last_updated: "",
attributes: {},
context: {
id: "",
user_id: "",
parent_id: "",
}
});
}
// does entity id prefix exist in hass state data?
const hassEntitys = Object.values(this.hass.states)
.filter((ent) => ent.entity_id.match(entityId));
// if not, then it's an error!
if (!hassEntitys) {
throw new Error("Entity id '" + JSON.stringify(entityId) + "' could not be matched in the state machine");
}
// find the exact matching HA media player entity and create the media player instance.
let player: MediaPlayer | null = null;
hassEntitys.forEach(item => {
const haEntity = item as HassEntity;
if (haEntity.entity_id.toLowerCase() == entityId.toLowerCase()) {
player = new MediaPlayer(haEntity);
}
})
// did we find the player?
if (player) {
return player;
} else {
throw new Error("Entity id '" + JSON.stringify(entityId) + "' does not exist in the state machine");
}
}
}