Skip to content

Commit

Permalink
📝 Add the ability to skip either groups or individual bulbs to keep t…
Browse files Browse the repository at this point in the history
…he number of exported devices down
  • Loading branch information
bonan committed Dec 16, 2017
1 parent 5003ada commit 723d28c
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 4 deletions.
16 changes: 16 additions & 0 deletions cmd/sladdlos/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import (
var (
cleanUpHemtjanst = flag.Bool("hemtjanst.cleanup", false, "Clean up Hemtjänst MQTT Topics")
cleanUpTradfri = flag.Bool("tradfri.cleanup", false, "Clean up Trådfri MQTT Topics")
skipGroup = flag.Bool("skip-group", false, "Skip announcing Trådfri groups as lights")
skipBulb = flag.Bool("skip-bulb", false, "Skip announcing Trådfri bulbs individually")
)

func main() {
Expand All @@ -28,6 +30,11 @@ func main() {
return
}

if *skipGroup && *skipBulb {
log.Print("-skip-group and -skip-bulb are mutually exclusive, pick one")
return
}

id := flagmqtt.NewUniqueIdentifier()

tr := transport.NewTransport(id)
Expand All @@ -36,6 +43,15 @@ func main() {

ht := sladdlos.NewHemtjanstClient(tree, id)

ht.SkipGroup = *skipGroup
if ht.SkipGroup {
log.Print("Skipping groups")
}
ht.SkipBulb = *skipBulb
if ht.SkipBulb {
log.Print("Skipping bulbs")
}

var messenger messaging.PublishSubscriber

mqClient, err := flagmqtt.NewPersistentMqtt(flagmqtt.ClientConfig{
Expand Down
15 changes: 11 additions & 4 deletions device.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,18 +60,25 @@ func NewHemtjanstGroup(client *HemtjanstClient, topic string, group *tradfri.Gro
return h
}

func (h *HemtjanstDevice) shouldSkip() bool {
return h.isGroup && h.client.SkipGroup ||
!h.isGroup && h.client.SkipBulb
}

func (h *HemtjanstDevice) OnConnect() {
h.subscribeFeatures()
if !h.shouldSkip() {
h.subscribeFeatures()
}
}

func (h *HemtjanstDevice) OnDiscover() {
if h.device != nil {
if h.device != nil && !h.shouldSkip() {
h.device.PublishMeta()
}
}

func (h *HemtjanstDevice) subscribeFeatures() {
if h.device != nil {
if h.device != nil && !h.shouldSkip() {
h.device.RLock()
defer h.device.RUnlock()
for k, v := range h.device.Features {
Expand Down Expand Up @@ -175,7 +182,7 @@ func (h *HemtjanstDevice) init() {
h.isRunning = true
h.device = dev
h.subscribeFeatures()
if h.client.Announce {
if h.client.Announce && !h.shouldSkip() {
h.device.PublishMeta()
}
if h.isGroup && h.group != nil {
Expand Down
4 changes: 4 additions & 0 deletions hemtjanst.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ type HemtjanstClient struct {
MQTT messaging.PublishSubscriber
Id string
Announce bool
SkipGroup bool
SkipBulb bool
tree *tradfri.Tree
devices map[string]*HemtjanstDevice
groups map[int]*tradfri.Group
Expand All @@ -32,6 +34,8 @@ func NewHemtjanstClient(tree *tradfri.Tree, id string) *HemtjanstClient {
h := &HemtjanstClient{
tree: tree,
Id: id,
SkipBulb: false,
SkipGroup: false,
devices: map[string]*HemtjanstDevice{},
newDevChan: make(chan *tradfri.Accessory),
newGroupChan: make(chan *tradfri.Group),
Expand Down

0 comments on commit 723d28c

Please sign in to comment.