code.pfad.fr/gohmekit@v0.2.1/hapip/characteristic/static.go (about) 1 package characteristic 2 3 import ( 4 "context" 5 6 "code.pfad.fr/gohmekit/hapip" 7 ) 8 9 var _ hapip.CharacteristicReader = Static[string]{} 10 11 // Static is a static characteristic. Its value can't be changed after instanciation. 12 // 13 // To make it dynamic use Static.WithRemoteRead. 14 type Static[Format format] struct { 15 Metadata[Format] 16 17 value Format 18 } 19 20 // Read fullfils the hapip.CharacteristicReader interface (for use by hapip.Handler). 21 func (s Static[Format]) Read(_ context.Context) (interface{}, error) { 22 return s.value, nil 23 } 24 25 // WithRemoteRead returns a ReadCharacterisitc which will gets its (dynamic) value from the given read function. 26 func (s Static[Format]) WithRemoteRead(read func() (Format, error)) StaticRemoteRead[Format] { 27 return StaticRemoteRead[Format]{ 28 Metadata: s.Metadata, 29 read: read, 30 } 31 } 32 33 var _ hapip.CharacteristicReader = StaticRemoteRead[string]{} 34 35 // StaticRemoteRead calls an underlying read function to get the actual value. 36 type StaticRemoteRead[Format format] struct { 37 Metadata[Format] 38 39 read func() (Format, error) 40 } 41 42 // Read fullfils the hapip.CharacteristicReader interface (for use by hapip.Handler). 43 func (s StaticRemoteRead[Format]) Read(_ context.Context) (interface{}, error) { 44 return s.read() 45 }