- 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.
60 lines
1.9 KiB
Go
60 lines
1.9 KiB
Go
package grpc
|
|
|
|
import (
|
|
"context"
|
|
|
|
hav1 "gitea.nik4nao.com/nik/home-services/gen/ha/v1"
|
|
"gitea.nik4nao.com/nik/home-services/ha-gateway/internal/core/domain"
|
|
"gitea.nik4nao.com/nik/home-services/ha-gateway/internal/core/ports/driving"
|
|
)
|
|
|
|
type LightGRPC struct {
|
|
hav1.UnimplementedLightServiceServer
|
|
svc driving.LightService
|
|
}
|
|
|
|
// NewLightGRPC constructs the gRPC adapter for LightService.
|
|
func NewLightGRPC(svc driving.LightService) *LightGRPC {
|
|
return &LightGRPC{svc: svc}
|
|
}
|
|
|
|
// TurnOn maps a protobuf turn-on request into domain parameters.
|
|
func (h *LightGRPC) TurnOn(ctx context.Context, req *hav1.TurnOnRequest) (*hav1.LightResponse, error) {
|
|
s, err := h.svc.TurnOn(ctx, protoTurnOnToParams(req))
|
|
if err != nil {
|
|
return nil, grpcError(err)
|
|
}
|
|
return &hav1.LightResponse{State: domainStateToProto(s)}, nil
|
|
}
|
|
|
|
// TurnOff maps a protobuf turn-off request into domain parameters.
|
|
func (h *LightGRPC) TurnOff(ctx context.Context, req *hav1.TurnOffRequest) (*hav1.LightResponse, error) {
|
|
s, err := h.svc.TurnOff(ctx, protoTurnOffToParams(req))
|
|
if err != nil {
|
|
return nil, grpcError(err)
|
|
}
|
|
return &hav1.LightResponse{State: domainStateToProto(s)}, nil
|
|
}
|
|
|
|
// Toggle forwards a light toggle request to the domain service.
|
|
func (h *LightGRPC) Toggle(ctx context.Context, req *hav1.ToggleRequest) (*hav1.LightResponse, error) {
|
|
s, err := h.svc.Toggle(ctx, domain.EntityID(req.EntityId))
|
|
if err != nil {
|
|
return nil, grpcError(err)
|
|
}
|
|
return &hav1.LightResponse{State: domainStateToProto(s)}, nil
|
|
}
|
|
|
|
// ListLights returns discovery-oriented light metadata for clients such as Discord.
|
|
func (h *LightGRPC) ListLights(ctx context.Context, req *hav1.ListLightsRequest) (*hav1.ListLightsResponse, error) {
|
|
lights, err := h.svc.ListLights(ctx)
|
|
if err != nil {
|
|
return nil, grpcError(err)
|
|
}
|
|
out := make([]*hav1.LightEntity, 0, len(lights))
|
|
for _, l := range lights {
|
|
out = append(out, domainLightToProto(l))
|
|
}
|
|
return &hav1.ListLightsResponse{Lights: out}, nil
|
|
}
|