Skip to content

Commit

Permalink
refactor(dashboard): update Linus dashboard integration
Browse files Browse the repository at this point in the history
  • Loading branch information
Lebe1ge committed Oct 11, 2024
1 parent f2a1058 commit a3ce83d
Show file tree
Hide file tree
Showing 13 changed files with 8,757 additions and 61 deletions.
6 changes: 2 additions & 4 deletions .devcontainer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "Thank-you-Linus/Linus-Dashboard",
"image": "mcr.microsoft.com/vscode/devcontainers/python:3.12-bullseye",
"image": "mcr.microsoft.com/devcontainers/python:3.12",
"postCreateCommand": "scripts/setup",
"forwardPorts": [
8123
Expand Down Expand Up @@ -37,7 +37,5 @@
}
},
"remoteUser": "vscode",
"features": {
"ghcr.io/devcontainers/features/rust:1": {}
}
"features": {}
}
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ Use [black](https://github.com/ambv/black) to make sure the code follows the sty

## Test your code modification

This custom component is based on [integration_blueprint template](https://github.com/ludeeus/integration_blueprint).
This custom component is based on [linus_dashboard template](https://github.com/Thank-you-Linus/Linus-Dashboard).

It comes with development environment in a container, easy to launch
if you use Visual Studio Code. With this container you will have a stand alone
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ File | Purpose | Documentation

1. Create a new repository in GitHub, using this repository as a template by clicking the "Use this template" button in the GitHub UI.
1. Open your new repository in Visual Studio Code devcontainer (Preferably with the "`Dev Containers: Clone Repository in Named Container Volume...`" option).
1. Rename all instances of the `integration_blueprint` to `custom_components/<your_integration_domain>` (e.g. `custom_components/awesome_integration`).
1. Rename all instances of the `linus_dashboard` to `custom_components/<your_integration_domain>` (e.g. `custom_components/awesome_integration`).
1. Rename all instances of the `Integration Blueprint` to `<Your Integration Name>` (e.g. `Awesome Integration`).
1. Run the `scripts/develop` to start HA and test out your new integration.

Expand Down
8 changes: 4 additions & 4 deletions README_EXAMPLE.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Linus Dashboard
# Integration Blueprint

[![GitHub Release][releases-shield]][releases]
[![GitHub Activity][commits-shield]][commits]
Expand Down Expand Up @@ -28,7 +28,7 @@ Platform | Description
1. Download _all_ the files from the `custom_components/linus_dashboard/` directory (folder) in this repository.
1. Place the files you downloaded in the new directory (folder) you created.
1. Restart Home Assistant
1. In the HA UI go to "Configuration" -> "Integrations" click "+" and search for "Integration blueprint"
1. In the HA UI go to "Configuration" -> "Integrations" click "+" and search for "Linus dashboard"

## Configuration is done in the UI

Expand All @@ -41,11 +41,11 @@ If you want to contribute to this please read the [Contribution guidelines](CONT
***

[linus_dashboard]: https://github.com/Thank-you-Linus/Linus-Dashboard
[buymecoffee]: https://www.buymeacoffee.com/linus
[buymecoffee]: https://www.buymeacoffee.com/ludeeus
[buymecoffeebadge]: https://img.shields.io/badge/buy%20me%20a%20coffee-donate-yellow.svg?style=for-the-badge
[commits-shield]: https://img.shields.io/github/commit-activity/y/ludeeus/linus_dashboard.svg?style=for-the-badge
[commits]: https://github.com/Thank-you-Linus/Linus-Dashboard/commits/main
[discord]: https://discord.gg/ej2Xn4GTww
[discord]: https://discord.gg/Qa5fW2R
[discord-shield]: https://img.shields.io/discord/330944238910963714.svg?style=for-the-badge
[exampleimg]: example.png
[forum-shield]: https://img.shields.io/badge/community-forum-brightgreen.svg?style=for-the-badge
Expand Down
103 changes: 62 additions & 41 deletions custom_components/linus_dashboard/__init__.py
Original file line number Diff line number Diff line change
@@ -1,55 +1,76 @@
"""
Custom integration to integrate linus_dashboard with Home Assistant.
"""Linus Dashboard integration for Home Assistant."""

For more details about this integration, please refer to
https://github.com/Thank-you-Linus/Linus-Dashboard
"""
import logging
import os

from __future__ import annotations
from typing import TYPE_CHECKING
from homeassistant.components.frontend import (
add_extra_js_url,
async_register_built_in_panel,
async_remove_panel,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant

from homeassistant.components import frontend
from homeassistant.components.lovelace import _register_panel
from homeassistant.components.lovelace.dashboard import LovelaceYAML
_LOGGER = logging.getLogger(__name__)

from .load_dashboard import load_dashboard
from .load_plugins import load_plugins
DOMAIN = "linus_dashboard"

from .const import DOMAIN, VERSION

if TYPE_CHECKING:
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant


async def async_setup(hass: HomeAssistant, config: ConfigEntry) -> bool:
"""Set up the Linus Dashboard integration."""
load_plugins(hass, DOMAIN)

async def async_setup(hass: HomeAssistant, _config: dict) -> bool:
"""Set up Linus Dashboard."""
_LOGGER.info("Setting up Linus Dashboard")
hass.data.setdefault(DOMAIN, {})
return True


# https://developers.home-assistant.io/docs/config_entries_index/#setting-up-an-entry
async def async_setup_entry(
hass: HomeAssistant,
entry: ConfigEntry,
) -> bool:
"""Set up this integration using UI."""

load_dashboard(hass, DOMAIN)
entry.async_on_unload(entry.add_update_listener(async_reload_entry))

async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""Set up Linus Dashboard from a config entry."""
_LOGGER.info("Setting up Linus Dashboard entry")

# Path to the YAML file for the dashboard
dashboard_path = os.path.join(
hass.config.path(f"custom_components/{DOMAIN}/lovelace/ui-lovelace.yaml")
)

# Path to the JavaScript file for the strategy
strategy_js_url = f"/{DOMAIN}/js/linus-strategy.js"
strategy_js_path = hass.config.path(
f"custom_components/{DOMAIN}/js/linus-strategy.js"
)
hass.http.register_static_path(strategy_js_url, strategy_js_path, False)

# Add the JavaScript file as a frontend resource
add_extra_js_url(hass, strategy_js_url)

# Use a unique name for the panel to avoid conflicts
sidebar_title = "Linus Dashboard"
sidebar_icon = "mdi:bow-tie"
panel_url = DOMAIN
async_register_built_in_panel(
hass,
panel_url,
sidebar_title,
sidebar_icon,
config={
"mode": "yaml",
"icon": sidebar_icon,
"title": sidebar_title,
"filename": dashboard_path,
},
)

# Store the entry
hass.data[DOMAIN][entry.entry_id] = panel_url
return True


async def async_remove_entry(hass, config_entry):
frontend.async_remove_panel(hass, "linus-dashboard")
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""Unload a config entry."""
_LOGGER.info("Unloading Linus Dashboard entry")

# Retrieve and remove the panel name
panel_url = hass.data[DOMAIN].pop(entry.entry_id, None)
if panel_url:
async_remove_panel(hass, panel_url)

async def async_reload_entry(
hass: HomeAssistant,
entry: ConfigEntry,
) -> None:
"""Reload config entry."""
await async_remove_entry(hass, entry)
await async_setup_entry(hass, entry)
return True
15 changes: 10 additions & 5 deletions custom_components/linus_dashboard/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,38 +6,43 @@
from homeassistant import config_entries, data_entry_flow
from homeassistant.helpers import selector

from .const import DOMAIN, LOGGER
from .const import DOMAIN


class BlueprintFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
"""Config flow for Linus Dashboard."""

VERSION = 1

async def async_step_user(self, user_input: dict | None = None) -> data_entry_flow.FlowResult:
async def async_step_user(
self, user_input: dict | None = None
) -> data_entry_flow.FlowResult:
"""Handle a flow initialized by the user."""
_errors = {}

# If user input is provided, create the entry
if user_input is not None:
return self.async_create_entry(
title="Configuration",
data=user_input,
)

# Show the form to the user with the required fields
return self.async_show_form(
step_id="user",
data_schema=vol.Schema(
{
vol.Required("alarm_entity_id"): selector.EntitySelector(
vol.Optional("alarm_entity_id"): selector.EntitySelector(
selector.EntitySelectorConfig(
domain="alarm_control_panel",
),
),
vol.Required("weather_entity_id"): selector.EntitySelector(
vol.Optional("weather_entity_id"): selector.EntitySelector(
selector.EntitySelectorConfig(
domain="weather",
),
),
},
),
errors=_errors,
)
)
2 changes: 2 additions & 0 deletions custom_components/linus_dashboard/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@

DOMAIN = "linus_dashboard"
ATTRIBUTION = "Data provided by http://jsonplaceholder.typicode.com/"

VERSION = "0.0.1"
Loading

0 comments on commit a3ce83d

Please sign in to comment.