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

86 lines
2.4 KiB
Go

package grpc
import (
hav1 "gitea.nik4nao.com/nik/home-services/gen/ha/v1"
"gitea.nik4nao.com/nik/home-services/ha-gateway/internal/core/domain"
)
// domainStateToProto normalizes domain state timestamps and attributes for gRPC.
func domainStateToProto(s *domain.EntityState) *hav1.EntityState {
return &hav1.EntityState{
EntityId: string(s.EntityID),
State: s.State,
Attributes: s.Attributes,
LastChanged: s.LastChanged.Format("2006-01-02T15:04:05Z07:00"),
LastUpdated: s.LastUpdated.Format("2006-01-02T15:04:05Z07:00"),
}
}
// protoTurnOnToParams preserves optional protobuf fields so the app layer can
// distinguish "unset" from explicit zero values.
func protoTurnOnToParams(r *hav1.TurnOnRequest) domain.TurnOnParams {
p := domain.TurnOnParams{
EntityID: domain.EntityID(r.EntityId),
}
if r.BrightnessPct != nil {
v := r.GetBrightnessPct()
p.BrightnessPct = &v
}
if r.ColorTempKelvin != nil {
v := r.GetColorTempKelvin()
p.ColorTempKelvin = &v
}
if r.RgbColor != nil {
p.RGBColor = &domain.RGBColor{
R: uint8(r.RgbColor.R),
G: uint8(r.RgbColor.G),
B: uint8(r.RgbColor.B),
}
}
if r.Transition != nil {
v := r.GetTransition()
p.Transition = &v
}
return p
}
// protoTurnOffToParams preserves optional protobuf fields for turn-off calls.
func protoTurnOffToParams(r *hav1.TurnOffRequest) domain.TurnOffParams {
p := domain.TurnOffParams{
EntityID: domain.EntityID(r.EntityId),
}
if r.Transition != nil {
v := r.GetTransition()
p.Transition = &v
}
return p
}
// domainLightToProto exposes discovery metadata without leaking domain enums.
func domainLightToProto(l domain.Light) *hav1.LightEntity {
modes := make([]string, len(l.SupportedColorModes))
for i, m := range l.SupportedColorModes {
modes[i] = string(m)
}
return &hav1.LightEntity{
EntityId: string(l.EntityID),
FriendlyName: l.FriendlyName,
State: l.State,
SupportedColorModes: modes,
MinColorTempKelvin: l.MinColorTempKelvin,
MaxColorTempKelvin: l.MaxColorTempKelvin,
IsHueGroup: l.IsHueGroup,
EffectList: l.EffectList,
}
}
// domainSwitchToProto exposes switch discovery metadata over gRPC.
func domainSwitchToProto(s domain.Switch) *hav1.SwitchEntity {
return &hav1.SwitchEntity{
EntityId: string(s.EntityID),
FriendlyName: s.FriendlyName,
State: s.State,
DeviceClass: s.DeviceClass,
}
}