Skip to content

Commit

Permalink
using config flow entities for weather and alarm
Browse files Browse the repository at this point in the history
  • Loading branch information
Lebe1ge committed Dec 2, 2024
1 parent 42adebf commit 433069a
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 11 deletions.
25 changes: 25 additions & 0 deletions custom_components/linus_dashboard/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import logging
from pathlib import Path
import voluptuous as vol

from homeassistant.components.frontend import (
async_remove_panel,
Expand All @@ -11,9 +12,16 @@
from homeassistant.components.lovelace.dashboard import LovelaceYAML
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
from homeassistant.components import websocket_api

from custom_components.linus_dashboard import utils
from custom_components.linus_dashboard.const import DOMAIN
from .const import (
CONF_ALARM_ENTITY,
CONF_ALARM_ENTITY_ID,
CONF_WEATHER_ENTITY,
CONF_WEATHER_ENTITY_ID,
)

_LOGGER = logging.getLogger(__name__)

Expand All @@ -23,6 +31,8 @@ async def async_setup(hass: HomeAssistant, _config: dict) -> bool:
_LOGGER.info("Setting up Linus Dashboard")
hass.data.setdefault(DOMAIN, {})

hass.components.websocket_api.async_register_command(websocket_get_entities)

return True


Expand Down Expand Up @@ -105,3 +115,18 @@ async def register_static_paths_and_resources(
# fix from https://github.com/hmmbob/WebRTC/blob/a0783df2e5426118599edc50bfd0466b1b0f0716/custom_components/webrtc/__init__.py#L83
version = getattr(hass.data["integrations"][DOMAIN], "version", 0)
await utils.init_resource(hass, js_url, str(version))


@websocket_api.websocket_command({vol.Required("type"): "linus_dashboard/get_config"})
@websocket_api.async_response
async def websocket_get_entities(
hass: HomeAssistant, connection: websocket_api.ActiveConnection, msg: dict
) -> None:
"""Handle request for getting entities."""
config_entries = hass.config_entries.async_entries(DOMAIN)
config = {
CONF_ALARM_ENTITY_ID: config_entries[0].options.get(CONF_ALARM_ENTITY),
CONF_WEATHER_ENTITY_ID: config_entries[0].options.get(CONF_WEATHER_ENTITY),
}

connection.send_message(websocket_api.result_message(msg["id"], config))
2 changes: 2 additions & 0 deletions custom_components/linus_dashboard/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,6 @@
URL_PANEL = "linus_dashboard_panel"

CONF_ALARM_ENTITY = "alarm_entity"
CONF_ALARM_ENTITY_ID = "alarm_entity_id"
CONF_WEATHER_ENTITY = "weather_entity"
CONF_WEATHER_ENTITY_ID = "weather_entity_id"
27 changes: 22 additions & 5 deletions custom_components/linus_dashboard/www/linus-strategy.js

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

Large diffs are not rendered by default.

25 changes: 22 additions & 3 deletions src/Helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { DEVICE_CLASSES, MAGIC_AREAS_DOMAIN, MAGIC_AREAS_NAME, UNDISCLOSED } fro
import { getMAEntity, getMagicAreaSlug, groupEntitiesByDomain, slugify } from "./utils";
import { EntityRegistryEntry } from "./types/homeassistant/data/entity_registry";
import { FrontendEntityComponentIconResources, IconResources } from "./types/homeassistant/data/frontend";
import { LinusDashboardConfig } from "./types/homeassistant/data/linus_dashboard";

/**
* Helper Class
Expand Down Expand Up @@ -117,6 +118,14 @@ class Helper {
*/
static #icons: IconResources;

/**
* Set to true for more verbose information in the console.
*
* @type {LinusDashboardConfig}
* @private
*/
static #linus_dashboard_config: LinusDashboardConfig;

/**
* Class constructor.
*
Expand Down Expand Up @@ -243,6 +252,16 @@ class Helper {
return this.#icons;
}

/**
* Get the linus_dashboard_config from Home Assistant's frontend.
*
* @returns {LinusDashboardConfig}
* @static
*/
static get linus_dashboard_config(): LinusDashboardConfig {
return this.#linus_dashboard_config;
}

/**
* Get the current debug mode of the mushroom strategy.
*
Expand Down Expand Up @@ -279,18 +298,18 @@ class Helper {
info.hass.callWS({ type: "config/floor_registry/list" }) as Promise<FloorRegistryEntry[]>,
info.hass.callWS({ type: "frontend/get_icons", category: "entity_component" }) as Promise<FrontendEntityComponentIconResources>,
info.hass.callWS({ type: "frontend/get_icons", category: "services" }) as Promise<FrontendEntityComponentIconResources>,
info.hass.callWS({ type: "linus_dashboard/get_config" }) as Promise<LinusDashboardConfig>,
]);

} catch (e) {
Helper.logError("An error occurred while querying Home assistant's registries!", e);
throw 'Check the console for details';
}

const [entities, devices, areas, floors, entity_component_icons, services_icons] = homeAssistantRegistries;
const [entities, devices, areas, floors, entity_component_icons, services_icons, linus_dashboard_config] = homeAssistantRegistries;

this.#icons = deepmerge(entity_component_icons.resources, services_icons.resources);

console.log('this.#icons :>> ', this.#icons);
this.#linus_dashboard_config = linus_dashboard_config;

// Dictionnaires pour un accès rapide
const areasById = Object.fromEntries(areas.map(a => [a.area_id, a]));
Expand Down
4 changes: 4 additions & 0 deletions src/types/homeassistant/data/linus_dashboard.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export interface LinusDashboardConfig {
alarm_entity_id: string | null;
weather_entity_id: string | null;
}
4 changes: 2 additions & 2 deletions src/views/HomeView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ class HomeView {
let chipModule;

// Weather chip.
const weatherEntityId = chipOptions?.weather_entity ?? Helper.domains.weather[0]?.entity_id;
const weatherEntityId = Helper.linus_dashboard_config?.weather_entity_id;

if (weatherEntityId) {
try {
Expand All @@ -84,7 +84,7 @@ class HomeView {
}

// Alarm chip.
const alarmEntityId = chipOptions?.alarm_entity ?? Helper.domains.alarm_control_panel[0]?.entity_id;
const alarmEntityId = Helper.linus_dashboard_config?.alarm_entity_id

if (alarmEntityId) {
try {
Expand Down

0 comments on commit 433069a

Please sign in to comment.