Nik Afiq 6ea4e84949
All checks were successful
CI / test (push) Successful in 4s
CI / build-ha-gateway (push) Successful in 1m7s
CI / build-discord-bot (push) Successful in 51s
Enhance Discord bot and HA gateway with improved structure and documentation
- Added detailed comments to clarify the purpose of various functions and types in the Discord bot and HA gateway.
- Introduced new methods in the CommandApp for handling light and switch operations, including HandleLightOn, HandleLightOff, HandleLightToggle, and their respective autocomplete functions.
- Updated the HAClient interface to include methods for fetching states and calling services, enhancing the interaction with Home Assistant.
- Improved the structure of entity and light domain models to include additional attributes and clearer documentation.
- Implemented logging enhancements in both the Discord bot and HA gateway to ensure better traceability and context in logs.
- Refactored the configuration loading process to streamline environment variable handling and defaults.
- Stubbed out switch control methods in the gRPC adapter, indicating future implementation plans.
- Enhanced telemetry setup to ensure proper initialization and shutdown procedures for observability.
2026-04-09 06:00:59 +09:00

216 lines
6.4 KiB
Go

package app
import (
"context"
"fmt"
"slices"
"strings"
"gitea.nik4nao.com/nik/home-services/discord-bot/internal/core/ports/driven"
)
// Choice is one Discord autocomplete entry.
type Choice struct {
Label string
Value string
}
// CommandApp orchestrates Discord command use cases against ha-gateway.
type CommandApp struct {
ha driven.HAGateway
}
// NewCommandApp constructs the Discord command application service.
func NewCommandApp(ha driven.HAGateway) *CommandApp {
return &CommandApp{ha: ha}
}
// HandleLightList formats discovered lights into a monospace-friendly response.
func (a *CommandApp) HandleLightList(ctx context.Context) (string, error) {
lights, err := a.ha.ListLights(ctx)
if err != nil {
return "", fmt.Errorf("handle light list: %w", err)
}
if len(lights) == 0 {
return "No lights found.", nil
}
lines := make([]string, 0, len(lights)+2)
lines = append(lines, "```text")
for _, light := range lights {
lines = append(lines, formatLightLine(light))
}
lines = append(lines, "```")
return strings.Join(lines, "\n"), nil
}
// HandleLightOn issues a turn-on request and returns a user-facing confirmation.
func (a *CommandApp) HandleLightOn(ctx context.Context, entityID string, brightnessPct *uint32, colorTempKelvin *uint32) (string, error) {
name, err := a.lookupLightName(ctx, entityID)
if err != nil {
return "", fmt.Errorf("lookup light name: %w", err)
}
if err := a.ha.TurnOnLight(ctx, entityID, brightnessPct, colorTempKelvin); err != nil {
return "", fmt.Errorf("handle light on: %w", err)
}
details := make([]string, 0, 2)
if brightnessPct != nil {
details = append(details, fmt.Sprintf("brightness %d%%", *brightnessPct))
}
if colorTempKelvin != nil {
details = append(details, fmt.Sprintf("%dK", *colorTempKelvin))
}
if len(details) == 0 {
return fmt.Sprintf("Turned on `%s`.", name), nil
}
return fmt.Sprintf("Turned on `%s` (%s).", name, strings.Join(details, ", ")), nil
}
// HandleLightOff issues a turn-off request and returns a user-facing confirmation.
func (a *CommandApp) HandleLightOff(ctx context.Context, entityID string, transition *uint32) (string, error) {
name, err := a.lookupLightName(ctx, entityID)
if err != nil {
return "", fmt.Errorf("lookup light name: %w", err)
}
if err := a.ha.TurnOffLight(ctx, entityID, transition); err != nil {
return "", fmt.Errorf("handle light off: %w", err)
}
if transition == nil {
return fmt.Sprintf("Turned off `%s`.", name), nil
}
return fmt.Sprintf("Turned off `%s` with %ds transition.", name, *transition), nil
}
// HandleLightToggle issues a toggle request and returns a user-facing confirmation.
func (a *CommandApp) HandleLightToggle(ctx context.Context, entityID string) (string, error) {
name, err := a.lookupLightName(ctx, entityID)
if err != nil {
return "", fmt.Errorf("lookup light name: %w", err)
}
if err := a.ha.ToggleLight(ctx, entityID); err != nil {
return "", fmt.Errorf("handle light toggle: %w", err)
}
return fmt.Sprintf("Toggled `%s`.", name), nil
}
// HandleSwitchList formats discovered switches into a monospace-friendly response.
func (a *CommandApp) HandleSwitchList(ctx context.Context) (string, error) {
switches, err := a.ha.ListSwitches(ctx)
if err != nil {
return "", fmt.Errorf("handle switch list: %w", err)
}
if len(switches) == 0 {
return "No switches found.", nil
}
lines := make([]string, 0, len(switches)+2)
lines = append(lines, "```text")
for _, sw := range switches {
label := sw.FriendlyName
if label == "" {
label = sw.EntityID
}
details := sw.DeviceClass
if details == "" {
details = "switch"
}
lines = append(lines, fmt.Sprintf("%s %-15s %-12s %s", stateEmoji(sw.State), label, sw.State, details))
}
lines = append(lines, "```")
return strings.Join(lines, "\n"), nil
}
// AutocompleteLights maps discovered lights into Discord autocomplete choices.
func (a *CommandApp) AutocompleteLights(ctx context.Context) ([]Choice, error) {
lights, err := a.ha.ListLights(ctx)
if err != nil {
return nil, fmt.Errorf("autocomplete lights: %w", err)
}
choices := make([]Choice, 0, len(lights))
for _, light := range lights {
label := light.FriendlyName
if label == "" {
label = light.EntityID
}
choices = append(choices, Choice{Label: label, Value: light.EntityID})
}
return choices, nil
}
// AutocompleteSwitches maps discovered switches into Discord autocomplete choices.
func (a *CommandApp) AutocompleteSwitches(ctx context.Context) ([]Choice, error) {
switches, err := a.ha.ListSwitches(ctx)
if err != nil {
return nil, fmt.Errorf("autocomplete switches: %w", err)
}
choices := make([]Choice, 0, len(switches))
for _, sw := range switches {
label := sw.FriendlyName
if label == "" {
label = sw.EntityID
}
choices = append(choices, Choice{Label: label, Value: sw.EntityID})
}
return choices, nil
}
// lookupLightName falls back to the entity ID so confirmations remain useful
// even when Home Assistant does not expose a friendly name.
func (a *CommandApp) lookupLightName(ctx context.Context, entityID string) (string, error) {
lights, err := a.ha.ListLights(ctx)
if err != nil {
return "", fmt.Errorf("list lights: %w", err)
}
idx := slices.IndexFunc(lights, func(light driven.Light) bool {
return light.EntityID == entityID
})
if idx == -1 {
return entityID, nil
}
if lights[idx].FriendlyName == "" {
return entityID, nil
}
return lights[idx].FriendlyName, nil
}
// formatLightLine keeps list output compact because Discord code blocks are
// easier to scan than rich embeds for dense discovery data.
func formatLightLine(light driven.Light) string {
label := light.FriendlyName
if label == "" {
label = light.EntityID
}
parts := make([]string, 0, 3)
if len(light.SupportedColorModes) > 0 {
parts = append(parts, strings.Join(light.SupportedColorModes, ","))
}
if light.MinColorTempKelvin > 0 && light.MaxColorTempKelvin > 0 {
parts = append(parts, fmt.Sprintf("%d-%dK", light.MinColorTempKelvin, light.MaxColorTempKelvin))
}
if light.IsHueGroup {
parts = append(parts, "hue-group")
}
details := strings.Join(parts, " ")
if details == "" {
details = "-"
}
return fmt.Sprintf("%s %-15s %-12s %s", stateEmoji(light.State), label, light.State, details)
}
// stateEmoji compresses common Home Assistant states into visually scannable output.
func stateEmoji(state string) string {
switch state {
case "on":
return "🟢"
case "off":
return "🔴"
default:
return "⚠️"
}
}