Ejemplo n.º 1
0
        private static void HandleServiceCallAttribute(INetDaemon _daemon, INetDaemonApp netDaemonApp, MethodInfo method)
        {
            var(signatureOk, err) = CheckIfServiceCallSignatureIsOk(method);
            if (!signatureOk)
            {
                _daemon.Logger.LogWarning(err);
                return;
            }

            dynamic serviceData = new FluentExpandoObject();

            serviceData.method = method.Name;
            serviceData.@class = netDaemonApp.GetType().Name;
            _daemon.CallService("netdaemon", "register_service", serviceData);

            _daemon.ListenServiceCall("netdaemon", $"{serviceData.@class}_{serviceData.method}",
                                      async(data) =>
            {
                try
                {
                    var expObject = data as ExpandoObject;
                    await method.InvokeAsync(netDaemonApp, expObject !).ConfigureAwait(false);
                }
                catch (Exception e)
                {
                    _daemon.Logger.LogError(e, "Failed to invoke the ServiceCall function");
                }
            });
        }
Ejemplo n.º 2
0
        public void SetPropertyFromYaml(INetDaemonApp app, PropertyInfo prop, YamlSequenceNode seq)
        {
            if (prop.PropertyType.IsGenericType && prop.PropertyType?.GetGenericTypeDefinition() == typeof(IEnumerable <>))
            {
                Type listType = prop.PropertyType?.GetGenericArguments()[0] ??
                                throw new NullReferenceException($"The property {prop.Name} of Class {app.GetType().Name} is not compatible with configuration");

                IList list = listType.CreateListOfPropertyType() ??
                             throw new NullReferenceException("Failed to create listtype, plese check {prop.Name} of Class {app.GetType().Name}");

                foreach (YamlNode item in seq.Children)
                {
                    if (item.NodeType != YamlNodeType.Scalar)
                    {
                        throw new NotSupportedException($"The property {prop.Name} of Class {app.GetType().Name} is not compatible with configuration");
                    }
                    var scalarNode = (YamlScalarNode)item;
                    ReplaceSecretIfExists(scalarNode);
                    var value = ((YamlScalarNode)item).ToObject(listType) ??
                                throw new NotSupportedException($"The class {app.GetType().Name} and property {prop.Name} has wrong type in items");

                    list.Add(value);
                }
                // Bind the list to the property
                prop.SetValue(app, list);
            }
        }
Ejemplo n.º 3
0
        public void SetPropertyFromYaml(INetDaemonApp app, PropertyInfo prop, YamlScalarNode sc)
        {
            ReplaceSecretIfExists(sc);
            var scalarValue = sc.ToObject(prop.PropertyType) ??
                              throw new NotSupportedException($"The class {app.GetType().Name} and property {prop.Name} unexpected value {sc.Value} is wrong type");

            // Bind the list to the property
            prop.SetValue(app, scalarValue);
        }
Ejemplo n.º 4
0
        internal DaemonHostTestBase()
        {
            _loggerMock                = new LoggerMock();
            _defaultHassClientMock     = HassClientMock.DefaultMock;
            _defaultDataRepositoryMock = new Mock <IDataRepository>();
            _defaultDaemonHost         = new NetDaemonHost(_defaultHassClientMock.Object, _defaultDataRepositoryMock.Object, _loggerMock.LoggerFactory);
            _defaultDaemonApp          = new BaseTestApp();
            _defaultDaemonApp.StartUpAsync(_defaultDaemonHost);

            _notConnectedDaemonHost = new NetDaemonHost(HassClientMock.MockConnectFalse.Object, _defaultDataRepositoryMock.Object, _loggerMock.LoggerFactory);
        }
Ejemplo n.º 5
0
        private static void HandleStateChangedAttribute(
            INetDaemon _daemon,
            HomeAssistantStateChangedAttribute hassStateChangedAttribute,
            INetDaemonApp netDaemonApp,
            MethodInfo method
            )
        {
            var(signatureOk, err) = CheckIfStateChangedSignatureIsOk(method);

            if (!signatureOk)
            {
                _daemon.Logger.LogWarning(err);
                return;
            }

            _daemon.ListenState(hassStateChangedAttribute.EntityId,
                                async(entityId, to, from) =>
            {
                try
                {
                    if (hassStateChangedAttribute.To != null)
                    {
                        if ((dynamic)hassStateChangedAttribute.To != to?.State)
                        {
                            return;
                        }
                    }

                    if (hassStateChangedAttribute.From != null)
                    {
                        if ((dynamic)hassStateChangedAttribute.From != from?.State)
                        {
                            return;
                        }
                    }

                    // If we don´t accept all changes in the state change
                    // and we do not have a state change so return
                    if (to?.State == from?.State && !hassStateChangedAttribute.AllChanges)
                    {
                        return;
                    }

                    await method.InvokeAsync(netDaemonApp, entityId, to !, from !).ConfigureAwait(false);
                }
                catch (Exception e)
                {
                    _daemon.Logger.LogError(e, "Failed to invoke the ServiceCall funcition");
                }
            });
        }
Ejemplo n.º 6
0
        public static void HandleAttributeInitialization(this INetDaemonApp netDaemonApp, INetDaemon _daemon)
        {
            var netDaemonAppType = netDaemonApp.GetType();

            foreach (var method in netDaemonAppType.GetMethods())
            {
                foreach (var attr in method.GetCustomAttributes(false))
                {
                    switch (attr)
                    {
                    case HomeAssistantServiceCallAttribute hasstServiceCallAttribute:
                        HandleServiceCallAttribute(_daemon, netDaemonApp, method);
                        break;

                    case HomeAssistantStateChangedAttribute hassStateChangedAttribute:
                        HandleStateChangedAttribute(_daemon, hassStateChangedAttribute, netDaemonApp, method);
                        break;
                    }
                }
            }
        }
Ejemplo n.º 7
0
 /// <summary>
 ///     Constructor
 /// </summary>
 /// <param name="entityIds">The unique ids of the entities managed</param>
 /// <param name="daemon">The Daemon that will handle API calls to Home Assistant</param>
 /// <param name="app">The Daemon App calling fluent API</param>
 public EntityBase(IEnumerable <string> entityIds, INetDaemon daemon, INetDaemonApp app)
 {
     EntityIds = entityIds;
     Daemon    = daemon;
     App       = app;
 }
Ejemplo n.º 8
0
 /// <summary>
 ///     Constructor
 /// </summary>
 /// <param name="entityIds">The unique ids of the entities managed</param>
 /// <param name="daemon">The Daemon that will handle API calls to Home Assistant</param>
 /// <param name="app">The Daemon App calling fluent API</param>
 public MediaPlayerManager(IEnumerable <string> entityIds, INetDaemon daemon, INetDaemonApp app) : base(entityIds, daemon, app)
 {
 }
Ejemplo n.º 9
0
 /// <summary>
 ///     Default constructor
 /// </summary>
 /// <param name="entityIds">The entityIds of the InputSelects</param>
 /// <param name="daemon">The net daemon that manage Home Assistant API:s</param>
 /// <param name="app">The Daemon App calling fluent API</param>
 public InputSelectManager(IEnumerable <string> entityIds, INetDaemon daemon, INetDaemonApp app)
 {
     _entityIds = entityIds;
     _daemon    = daemon;
     _app       = app;
 }
Ejemplo n.º 10
0
 /// <summary>
 ///     Constructor
 /// </summary>
 /// <param name="delayTaskCompletionSource"></param>
 /// <param name="daemonApp"></param>
 public DelayResult(TaskCompletionSource <bool> delayTaskCompletionSource, INetDaemonApp daemonApp)
 {
     _delayTaskCompletionSource = delayTaskCompletionSource;
     _daemonApp = daemonApp;
 }
Ejemplo n.º 11
0
 /// <summary>
 ///     Constructor
 /// </summary>
 /// <param name="func">The lambda to filter events</param>
 /// <param name="daemon">NetDaemon that manage API calls to Home Assistant</param>
 public FluentEventManager(Func <FluentEventProperty, bool> func, INetDaemonApp daemon)
 {
     _funcSelector = func;
     _daemon       = daemon;
 }
Ejemplo n.º 12
0
 /// <summary>
 ///     Constructor
 /// </summary>
 /// <param name="events">The events to handle</param>
 /// <param name="daemon">NetDaemon that manage API calls to Home Assistant</param>
 public FluentEventManager(IEnumerable <string> events, INetDaemonApp daemon)
 {
     _events = events;
     _daemon = daemon;
 }