github.com/e154/smart-home@v0.17.2-0.20240311175135-e530a6e5cd45/system/supervisor/entity.go (about)

     1  // This file is part of the Smart Home
     2  // Program complex distribution https://github.com/e154/smart-home
     3  // Copyright (C) 2016-2023, Filippov Alex
     4  //
     5  // This library is free software: you can redistribute it and/or
     6  // modify it under the terms of the GNU Lesser General Public
     7  // License as published by the Free Software Foundation; either
     8  // version 3 of the License, or (at your option) any later version.
     9  //
    10  // This library is distributed in the hope that it will be useful,
    11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    13  // Library General Public License for more details.
    14  //
    15  // You should have received a copy of the GNU Lesser General Public
    16  // License along with this library.  If not, see
    17  // <https://www.gnu.org/licenses/>.
    18  
    19  package supervisor
    20  
    21  import (
    22  	m "github.com/e154/smart-home/models"
    23  )
    24  
    25  // NewEntity ...
    26  func NewEntity(a PluginActor) m.EntityShort {
    27  
    28  	info := a.Info()
    29  	actions := make([]m.EntityActionShort, len(info.Actions))
    30  	var i int
    31  	for _, a := range info.Actions {
    32  		actions[i] = m.EntityActionShort{
    33  			Name:        a.Name,
    34  			Description: a.Description,
    35  			ImageUrl:    a.ImageUrl,
    36  			Icon:        a.Icon,
    37  		}
    38  		i++
    39  	}
    40  
    41  	states := make([]m.EntityStateShort, len(info.States))
    42  	i = 0
    43  	for _, a := range info.States {
    44  		states[i] = m.EntityStateShort{
    45  			Name:        a.Name,
    46  			Description: a.Description,
    47  			ImageUrl:    a.ImageUrl,
    48  			Icon:        a.Icon,
    49  		}
    50  		i++
    51  	}
    52  
    53  	attributes := make(m.Attributes, len(a.Attributes()))
    54  	for k, a := range a.Attributes() {
    55  		attributes[k] = &m.Attribute{
    56  			Name:  a.Name,
    57  			Type:  a.Type,
    58  			Value: a.Value,
    59  		}
    60  	}
    61  
    62  	settings := make(m.Attributes, len(a.Settings()))
    63  	for k, a := range a.Settings() {
    64  		settings[k] = &m.Attribute{
    65  			Name:  a.Name,
    66  			Type:  a.Type,
    67  			Value: a.Value,
    68  		}
    69  	}
    70  
    71  	entity := m.EntityShort{
    72  		Id:          info.Id,
    73  		Description: info.Description,
    74  		Type:        info.PluginName,
    75  		Icon:        info.Icon,
    76  		ImageUrl:    info.ImageUrl,
    77  		Actions:     actions,
    78  		States:      states,
    79  		Attributes:  attributes,
    80  		Settings:    settings,
    81  		Area:        info.Area,
    82  		Metrics:     a.Metrics(),
    83  		Hidden:      info.Hidde,
    84  	}
    85  	if cs := info.State; cs != nil {
    86  		entity.State = &m.EntityStateShort{
    87  			Name:        cs.Name,
    88  			Description: cs.Description,
    89  			ImageUrl:    cs.ImageUrl,
    90  			Icon:        cs.Icon,
    91  		}
    92  	}
    93  
    94  	return entity
    95  }