Clean up some types (#7801)

Co-authored-by: Zack Barett <arnett.zackary@gmail.com>
This commit is contained in:
Paulus Schoutsen 2020-11-25 12:31:51 +01:00 committed by GitHub
parent 51332bc7e7
commit fc48c59eb0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
33 changed files with 217 additions and 158 deletions

View File

@ -1,5 +1,6 @@
import { Theme } from "../../data/ws-themes";
import { darkStyles, derivedStyles } from "../../resources/styles";
import { HomeAssistant, Theme } from "../../types";
import type { HomeAssistant } from "../../types";
import {
hex2rgb,
lab2hex,

View File

@ -1,5 +1,5 @@
import { HassEntities } from "home-assistant-js-websocket";
import { GroupEntity } from "../../types";
import type { GroupEntity } from "../../data/group";
import { DEFAULT_VIEW_ENTITY_ID } from "../const";
// Return an ordered array of available views

View File

@ -1,5 +1,5 @@
import { HassEntities } from "home-assistant-js-websocket";
import { GroupEntity } from "../../types";
import { GroupEntity } from "../../data/group";
export const getGroupEntities = (
entities: HassEntities,

View File

@ -1,5 +1,5 @@
import { HassEntities } from "home-assistant-js-websocket";
import { GroupEntity } from "../../types";
import { GroupEntity } from "../../data/group";
import { computeDomain } from "./compute_domain";
import { getGroupEntities } from "./get_group_entities";

View File

@ -1,5 +1,5 @@
import { HassEntities } from "home-assistant-js-websocket";
import { GroupEntity } from "../../types";
import { GroupEntity } from "../../data/group";
import { computeDomain } from "./compute_domain";
// Split a collection into a list of groups and a 'rest' list of ungrouped

View File

@ -13,11 +13,12 @@ import { fireEvent } from "../common/dom/fire_event";
import { computeStateName } from "../common/entity/compute_state_name";
import { supportsFeature } from "../common/entity/supports-feature";
import {
CameraEntity,
CAMERA_SUPPORT_STREAM,
computeMJPEGStreamUrl,
fetchStreamUrl,
} from "../data/camera";
import { CameraEntity, HomeAssistant } from "../types";
import { HomeAssistant } from "../types";
import "./ha-hls-player";
@customElement("ha-camera-stream")

View File

@ -1,8 +1,14 @@
import type { HomeAssistant, Calendar, CalendarEvent } from "../types";
import type { HomeAssistant, CalendarEvent } from "../types";
import { computeDomain } from "../common/entity/compute_domain";
import { HA_COLOR_PALETTE } from "../common/const";
import { computeStateName } from "../common/entity/compute_state_name";
export interface Calendar {
entity_id: string;
name?: string;
backgroundColor?: string;
}
export const fetchCalendarEvents = async (
hass: HomeAssistant,
start: Date,

View File

@ -1,10 +1,25 @@
import {
HassEntityAttributeBase,
HassEntityBase,
} from "home-assistant-js-websocket";
import { timeCachePromiseFunc } from "../common/util/time-cache-function-promise";
import { CameraEntity, HomeAssistant } from "../types";
import { HomeAssistant } from "../types";
import { getSignedPath } from "./auth";
export const CAMERA_SUPPORT_ON_OFF = 1;
export const CAMERA_SUPPORT_STREAM = 2;
interface CameraEntityAttributes extends HassEntityAttributeBase {
model_name: string;
access_token: string;
brand: string;
motion_detection: boolean;
}
export interface CameraEntity extends HassEntityBase {
attributes: CameraEntityAttributes;
}
export interface CameraPreferences {
preload_stream: boolean;
}

15
src/data/group.ts Normal file
View File

@ -0,0 +1,15 @@
import {
HassEntityAttributeBase,
HassEntityBase,
} from "home-assistant-js-websocket";
interface GroupEntityAttributes extends HassEntityAttributeBase {
entity_id: string[];
order: number;
auto?: boolean;
view?: boolean;
control?: "hidden";
}
export interface GroupEntity extends HassEntityBase {
attributes: GroupEntityAttributes;
}

View File

@ -1,5 +1,17 @@
import {
HassEntityAttributeBase,
HassEntityBase,
} from "home-assistant-js-websocket";
import { HomeAssistant } from "../types";
interface InputSelectEntityAttributes extends HassEntityAttributeBase {
options: string[];
}
export interface InputSelectEntity extends HassEntityBase {
attributes: InputSelectEntityAttributes;
}
export interface InputSelect {
id: string;
name: string;

View File

@ -1,3 +1,24 @@
import {
HassEntityAttributeBase,
HassEntityBase,
} from "home-assistant-js-websocket";
interface LightEntityAttributes extends HassEntityAttributeBase {
min_mireds: number;
max_mireds: number;
friendly_name: string;
brightness: number;
hs_color: number[];
color_temp: number;
white_value: number;
effect?: string;
effect_list: string[] | null;
}
export interface LightEntity extends HassEntityBase {
attributes: LightEntityAttributes;
}
export const SUPPORT_BRIGHTNESS = 1;
export const SUPPORT_COLOR_TEMP = 2;
export const SUPPORT_EFFECT = 4;

View File

@ -16,11 +16,48 @@ import {
mdiVideo,
mdiWeb,
} from "@mdi/js";
import type { HassEntity } from "home-assistant-js-websocket";
import type {
HassEntityAttributeBase,
HassEntityBase,
} from "home-assistant-js-websocket";
import type { HomeAssistant } from "../types";
import { UNAVAILABLE_STATES } from "./entity";
import { supportsFeature } from "../common/entity/supports-feature";
interface MediaPlayerEntityAttributes extends HassEntityAttributeBase {
media_content_type?: any;
media_artist?: string;
media_playlist?: string;
media_series_title?: string;
media_season?: any;
media_episode?: any;
app_name?: string;
media_position_updated_at?: string | number | Date;
media_duration?: number;
media_position?: number;
media_title?: string;
icon?: string;
entity_picture_local?: string;
is_volume_muted?: boolean;
volume_level?: number;
source?: string;
source_list?: string[];
sound_mode?: string;
sound_mode_list?: string[];
}
export interface MediaPlayerEntity extends HassEntityBase {
attributes: MediaPlayerEntityAttributes;
state:
| "playing"
| "paused"
| "idle"
| "off"
| "on"
| "unavailable"
| "unknown";
}
export const SUPPORT_PAUSE = 1;
export const SUPPORT_SEEK = 2;
export const SUPPORT_VOLUME_SET = 4;
@ -149,32 +186,34 @@ export const browseLocalMediaPlayer = (
media_content_id: mediaContentId,
});
export const getCurrentProgress = (stateObj: HassEntity): number => {
let progress = stateObj.attributes.media_position;
export const getCurrentProgress = (stateObj: MediaPlayerEntity): number => {
let progress = stateObj.attributes.media_position!;
if (stateObj.state !== "playing") {
return progress;
}
progress +=
(Date.now() -
new Date(stateObj.attributes.media_position_updated_at).getTime()) /
new Date(stateObj.attributes.media_position_updated_at!).getTime()) /
1000.0;
return progress;
};
export const computeMediaDescription = (stateObj: HassEntity): string => {
export const computeMediaDescription = (
stateObj: MediaPlayerEntity
): string => {
let secondaryTitle: string;
switch (stateObj.attributes.media_content_type) {
case "music":
case "image":
secondaryTitle = stateObj.attributes.media_artist;
secondaryTitle = stateObj.attributes.media_artist!;
break;
case "playlist":
secondaryTitle = stateObj.attributes.media_playlist;
secondaryTitle = stateObj.attributes.media_playlist!;
break;
case "tvshow":
secondaryTitle = stateObj.attributes.media_series_title;
secondaryTitle = stateObj.attributes.media_series_title!;
if (stateObj.attributes.media_season) {
secondaryTitle += " S" + stateObj.attributes.media_season;
@ -191,7 +230,7 @@ export const computeMediaDescription = (stateObj: HassEntity): string => {
};
export const computeMediaControls = (
stateObj: HassEntity
stateObj: MediaPlayerEntity
): ControlButton[] | undefined => {
if (!stateObj) {
return undefined;

View File

@ -5,12 +5,39 @@ import {
mdiWeatherRainy,
mdiWeatherWindy,
} from "@mdi/js";
import {
HassEntityAttributeBase,
HassEntityBase,
} from "home-assistant-js-websocket";
import { css, html, svg, SVGTemplateResult, TemplateResult } from "lit-element";
import { styleMap } from "lit-html/directives/style-map";
import { formatNumber } from "../common/string/format_number";
import "../components/ha-icon";
import "../components/ha-svg-icon";
import type { HomeAssistant, WeatherEntity } from "../types";
import type { HomeAssistant } from "../types";
interface ForecastAttribute {
temperature: number;
datetime: string;
templow?: number;
precipitation?: number;
precipitation_probability?: number;
humidity?: number;
condition?: string;
daytime?: boolean;
}
interface WeatherEntityAttributes extends HassEntityAttributeBase {
temperature: number;
humidity?: number;
forecast?: ForecastAttribute[];
wind_speed: string;
wind_bearing: string;
}
export interface WeatherEntity extends HassEntityBase {
attributes: WeatherEntityAttributes;
}
export const weatherSVGs = new Set<string>([
"clear-night",

View File

@ -5,6 +5,10 @@ export interface Webhook {
domain: string;
name: string;
}
export interface WebhookError {
code: number;
message: string;
}
export const fetchWebhooks = (hass: HomeAssistant): Promise<Webhook[]> =>
hass.callWS({

View File

@ -1,5 +1,19 @@
import { Connection, createCollection } from "home-assistant-js-websocket";
import { Themes } from "../types";
export interface Theme {
// Incomplete
"primary-color": string;
"text-primary-color": string;
"accent-color": string;
[key: string]: string;
}
export interface Themes {
default_theme: string;
default_dark_theme: string | null;
themes: Record<string, Theme>;
darkMode: boolean;
}
const fetchThemes = (conn) =>
conn.sendMessagePromise({

View File

@ -17,8 +17,9 @@ import {
CAMERA_SUPPORT_STREAM,
fetchCameraPrefs,
updateCameraPrefs,
CameraEntity,
} from "../../../data/camera";
import type { CameraEntity, HomeAssistant } from "../../../types";
import type { HomeAssistant } from "../../../types";
class MoreInfoCamera extends LitElement {
@property({ attribute: false }) public hass?: HomeAssistant;

View File

@ -10,8 +10,9 @@ import {
import { html, TemplateResult } from "lit-html";
import { dynamicElement } from "../../../common/dom/dynamic-element-directive";
import { computeStateDomain } from "../../../common/entity/compute_state_domain";
import { GroupEntity } from "../../../data/group";
import "../../../state-summary/state-card-content";
import { GroupEntity, HomeAssistant } from "../../../types";
import { HomeAssistant } from "../../../types";
import {
importMoreInfoControl,
domainMoreInfoType,

View File

@ -24,8 +24,9 @@ import {
SUPPORT_COLOR_TEMP,
SUPPORT_EFFECT,
SUPPORT_WHITE_VALUE,
LightEntity,
} from "../../../data/light";
import type { HomeAssistant, LightEntity } from "../../../types";
import type { HomeAssistant } from "../../../types";
interface HueSatColor {
h: number;

View File

@ -34,14 +34,15 @@ import {
SUPPORT_VOLUME_BUTTONS,
SUPPORT_VOLUME_MUTE,
SUPPORT_VOLUME_SET,
MediaPlayerEntity,
} from "../../../data/media-player";
import { HomeAssistant, MediaEntity } from "../../../types";
import { HomeAssistant } from "../../../types";
@customElement("more-info-media_player")
class MoreInfoMediaPlayer extends LitElement {
@property({ attribute: false }) public hass!: HomeAssistant;
@property({ attribute: false }) public stateObj?: MediaEntity;
@property({ attribute: false }) public stateObj?: MediaPlayerEntity;
@query("#ttsInput") private _ttsInput?: HTMLInputElement;

View File

@ -18,11 +18,14 @@ import { LocalStorage } from "../../common/decorators/local-storage";
import { HASSDomEvent } from "../../common/dom/fire_event";
import "../../components/ha-card";
import "../../components/ha-menu-button";
import { fetchCalendarEvents, getCalendars } from "../../data/calendar";
import {
Calendar,
fetchCalendarEvents,
getCalendars,
} from "../../data/calendar";
import "../../layouts/ha-app-layout";
import { haStyle } from "../../resources/styles";
import type {
Calendar,
CalendarEvent,
CalendarViewChanged,
HomeAssistant,

View File

@ -20,9 +20,9 @@ import {
createCloudhook,
deleteCloudhook,
} from "../../../../data/cloud";
import { fetchWebhooks, Webhook } from "../../../../data/webhook";
import { fetchWebhooks, Webhook, WebhookError } from "../../../../data/webhook";
import { haStyle } from "../../../../resources/styles";
import { HomeAssistant, WebhookError } from "../../../../types";
import { HomeAssistant } from "../../../../types";
import { showManageCloudhookDialog } from "../dialog-manage-cloudhook/show-dialog-manage-cloudhook";
@customElement("cloud-webhooks")

View File

@ -29,7 +29,8 @@ import { isValidEntityId } from "../../../common/entity/valid_entity_id";
import { iconColorCSS } from "../../../common/style/icon_color_css";
import "../../../components/ha-card";
import { ActionHandlerEvent } from "../../../data/lovelace";
import { HomeAssistant, LightEntity } from "../../../types";
import { LightEntity } from "../../../data/light";
import { HomeAssistant } from "../../../types";
import { actionHandler } from "../common/directives/action-handler-directive";
import { findEntities } from "../common/find-entites";
import { handleAction } from "../common/handle-action";

View File

@ -16,9 +16,8 @@ import { HASSDomEvent } from "../../../common/dom/fire_event";
import { debounce } from "../../../common/util/debounce";
import "../../../components/ha-card";
import "../../../components/ha-icon";
import { fetchCalendarEvents } from "../../../data/calendar";
import { Calendar, fetchCalendarEvents } from "../../../data/calendar";
import type {
Calendar,
CalendarEvent,
CalendarViewChanged,
FullCalendarView,

View File

@ -22,9 +22,9 @@ import { supportsFeature } from "../../../common/entity/supports-feature";
import "../../../components/ha-card";
import "../../../components/ha-icon-button";
import { UNAVAILABLE, UNAVAILABLE_STATES } from "../../../data/entity";
import { SUPPORT_BRIGHTNESS } from "../../../data/light";
import { LightEntity, SUPPORT_BRIGHTNESS } from "../../../data/light";
import { ActionHandlerEvent } from "../../../data/lovelace";
import { HomeAssistant, LightEntity } from "../../../types";
import { HomeAssistant } from "../../../types";
import { actionHandler } from "../common/directives/action-handler-directive";
import { findEntities } from "../common/find-entites";
import { handleAction } from "../common/handle-action";

View File

@ -37,8 +37,9 @@ import {
SUPPORT_BROWSE_MEDIA,
SUPPORT_SEEK,
SUPPORT_TURN_ON,
MediaPlayerEntity,
} from "../../../data/media-player";
import type { HomeAssistant, MediaEntity } from "../../../types";
import type { HomeAssistant } from "../../../types";
import { findEntities } from "../common/find-entites";
import { hasConfigOrEntityChanged } from "../common/has-changed";
import { installResizeObserver } from "../common/install-resize-observer";
@ -499,8 +500,8 @@ export class HuiMediaControlCard extends LitElement implements LovelaceCard {
}
}
private get _stateObj(): MediaEntity | undefined {
return this.hass!.states[this._config!.entity] as MediaEntity;
private get _stateObj(): MediaPlayerEntity | undefined {
return this.hass!.states[this._config!.entity] as MediaPlayerEntity;
}
private _handleSeek(e: MouseEvent): void {

View File

@ -27,8 +27,9 @@ import {
getWind,
weatherAttrIcons,
weatherSVGStyles,
WeatherEntity,
} from "../../../data/weather";
import type { HomeAssistant, WeatherEntity } from "../../../types";
import type { HomeAssistant } from "../../../types";
import { actionHandler } from "../common/directives/action-handler-directive";
import { findEntities } from "../common/find-entites";
import { hasConfigOrEntityChanged } from "../common/has-changed";

View File

@ -26,6 +26,7 @@ import {
EntityRegistryEntry,
subscribeEntityRegistry,
} from "../../../data/entity_registry";
import { GroupEntity } from "../../../data/group";
import { domainToName } from "../../../data/integration";
import {
LovelaceCardConfig,
@ -33,7 +34,7 @@ import {
LovelaceViewConfig,
} from "../../../data/lovelace";
import { SENSOR_DEVICE_CLASS_BATTERY } from "../../../data/sensor";
import { GroupEntity, HomeAssistant } from "../../../types";
import { HomeAssistant } from "../../../types";
import {
AlarmPanelCardConfig,
EntitiesCardConfig,

View File

@ -15,9 +15,9 @@ import { styleMap } from "lit-html/directives/style-map";
import { STATES_OFF } from "../../../common/const";
import parseAspectRatio from "../../../common/util/parse-aspect-ratio";
import "../../../components/ha-camera-stream";
import { fetchThumbnailUrlWithCache } from "../../../data/camera";
import { CameraEntity, fetchThumbnailUrlWithCache } from "../../../data/camera";
import { UNAVAILABLE } from "../../../data/entity";
import { CameraEntity, HomeAssistant } from "../../../types";
import { HomeAssistant } from "../../../types";
const UPDATE_INTERVAL = 10000;
const DEFAULT_FILTER = "grayscale(100%)";

View File

@ -21,9 +21,12 @@ import "../../../components/entity/state-badge";
import "../../../components/ha-paper-dropdown-menu";
import { UNAVAILABLE_STATES } from "../../../data/entity";
import { forwardHaptic } from "../../../data/haptics";
import { setInputSelectOption } from "../../../data/input_select";
import {
InputSelectEntity,
setInputSelectOption,
} from "../../../data/input_select";
import { ActionHandlerEvent } from "../../../data/lovelace";
import { HomeAssistant, InputSelectEntity } from "../../../types";
import { HomeAssistant } from "../../../types";
import { EntitiesCardEntityConfig } from "../cards/types";
import { actionHandler } from "../common/directives/action-handler-directive";
import { handleAction } from "../common/handle-action";

View File

@ -28,6 +28,7 @@ import {
SUPPORT_VOLUME_MUTE,
SUPPORT_VOLUME_SET,
computeMediaDescription,
MediaPlayerEntity,
} from "../../../data/media-player";
import type { HomeAssistant } from "../../../types";
import { hasConfigOrEntityChanged } from "../common/has-changed";
@ -80,7 +81,7 @@ class HuiMediaPlayerEntityRow extends LitElement implements LovelaceRow {
return html``;
}
const stateObj = this.hass.states[this._config.entity];
const stateObj = this.hass.states[this._config.entity] as MediaPlayerEntity;
const state = stateObj.state;
if (!stateObj) {

View File

@ -20,8 +20,9 @@ import {
getWeatherUnit,
getWeatherStateIcon,
weatherSVGStyles,
WeatherEntity,
} from "../../../data/weather";
import type { HomeAssistant, WeatherEntity } from "../../../types";
import type { HomeAssistant } from "../../../types";
import type { EntitiesCardEntityConfig } from "../cards/types";
import { hasConfigOrEntityChanged } from "../common/has-changed";
import "../components/hui-generic-entity-row";

View File

@ -15,9 +15,9 @@ import {
import { stopPropagation } from "../common/dom/stop_propagation";
import { computeStateName } from "../common/entity/compute_state_name";
import "../components/entity/state-badge";
import { setInputSelectOption } from "../data/input_select";
import { InputSelectEntity, setInputSelectOption } from "../data/input_select";
import type { PolymerIronSelectEvent } from "../polymer-types";
import type { HomeAssistant, InputSelectEntity } from "../types";
import type { HomeAssistant } from "../types";
@customElement("state-card-input_select")
class StateCardInputSelect extends LitElement {

View File

@ -3,14 +3,13 @@ import {
Connection,
HassConfig,
HassEntities,
HassEntityAttributeBase,
HassEntityBase,
HassServices,
MessageBase,
} from "home-assistant-js-websocket";
import { LocalizeFunc } from "./common/translations/localize";
import { CoreFrontendUserData } from "./data/frontend";
import { getHassTranslations } from "./data/translation";
import { Themes } from "./data/ws-themes";
import { ExternalMessaging } from "./external_app/external_messaging";
declare global {
@ -62,11 +61,6 @@ export interface ClassElement {
descriptor?: PropertyDescriptor;
}
export interface WebhookError {
code: number;
message: string;
}
export interface Credential {
auth_provider_type: string;
auth_provider_id: string;
@ -87,21 +81,6 @@ export interface CurrentUser {
mfa_modules: MFAModule[];
}
export interface Theme {
// Incomplete
"primary-color": string;
"text-primary-color": string;
"accent-color": string;
[key: string]: string;
}
export interface Themes {
default_theme: string;
default_dark_theme: string | null;
themes: Record<string, Theme>;
darkMode: boolean;
}
export interface ThemeSettings {
theme: string;
dark?: boolean;
@ -121,12 +100,6 @@ export interface Panels {
[name: string]: PanelInfo;
}
export interface Calendar {
entity_id: string;
name?: string;
backgroundColor?: string;
}
export interface CalendarEvent {
summary: string;
title: string;
@ -261,69 +234,6 @@ export interface HomeAssistant {
): Promise<LocalizeFunc>;
}
export type LightEntity = HassEntityBase & {
attributes: HassEntityAttributeBase & {
min_mireds: number;
max_mireds: number;
friendly_name: string;
brightness: number;
hs_color: number[];
color_temp: number;
white_value: number;
effect?: string;
effect_list: string[] | null;
};
};
export type GroupEntity = HassEntityBase & {
attributes: HassEntityAttributeBase & {
entity_id: string[];
order: number;
auto?: boolean;
view?: boolean;
control?: "hidden";
};
};
export type CameraEntity = HassEntityBase & {
attributes: HassEntityAttributeBase & {
model_name: string;
access_token: string;
brand: string;
motion_detection: boolean;
};
};
export type MediaEntity = HassEntityBase & {
attributes: HassEntityAttributeBase & {
media_duration: number;
media_position: number;
media_title: string;
icon?: string;
entity_picture_local?: string;
is_volume_muted?: boolean;
volume_level?: number;
source?: string;
source_list?: string[];
sound_mode?: string;
sound_mode_list?: string[];
};
state:
| "playing"
| "paused"
| "idle"
| "off"
| "on"
| "unavailable"
| "unknown";
};
export type InputSelectEntity = HassEntityBase & {
attributes: HassEntityAttributeBase & {
options: string[];
};
};
export interface Route {
prefix: string;
path: string;
@ -340,24 +250,3 @@ export interface LocalizeMixin {
hass?: HomeAssistant;
localize: LocalizeFunc;
}
interface ForecastAttribute {
temperature: number;
datetime: string;
templow?: number;
precipitation?: number;
precipitation_probability?: number;
humidity?: number;
condition?: string;
daytime?: boolean;
}
export type WeatherEntity = HassEntityBase & {
attributes: HassEntityAttributeBase & {
temperature: number;
humidity?: number;
forecast?: ForecastAttribute[];
wind_speed: string;
wind_bearing: string;
};
};