コード例 #1
0
    public static bool AnyoneHome(this NetDaemonApp app)
    {
        try
        {
            var isa_location = app.GetState("person.isa")?.State?.ToString()?.ToLower();

            var stefan_location = app.GetState("person.stefan")?.State?.ToString()?.ToLower();
            if (isa_location != null || stefan_location != null)
            {
                if (stefan_location == "home" || stefan_location == "just arrived" ||
                    isa_location == "home" || isa_location == "just arrived")
                {
                    return(true);
                }
                else
                {
                    return(false);
                }
            }
            // return true if null, assume someone could be home
            app.Log($"A person returned null as location state, assuming someone is home.");
            return(true);
        }
        catch (System.Exception e)
        {
            // return true if something goes wrong, assume someone could be home
            app.Log($"Error: {e}");
            return(true);
        }
    }
コード例 #2
0
 public static bool AnyoneHome(this NetDaemonApp app)
 {
     try
     {
         var isa_location    = app.GetState(Isa.PersonEntity)?.State?.ToString()?.ToLower();
         var stefan_location = app.GetState(Stefan.PersonEntity)?.State?.ToString()?.ToLower();
         if (isa_location != null || stefan_location != null)
         {
             if (stefan_location == PresenceStatus.Home || stefan_location == PresenceStatus.JustArrived ||
                 isa_location == PresenceStatus.Home || isa_location == PresenceStatus.JustArrived)
             {
                 return(true);
             }
             else
             {
                 return(false);
             }
         }
         // return true if null, assume someone could be home
         app.Log($"A person returned null as location state, assuming someone is home.");
         return(true);
     }
     catch (System.Exception e)
     {
         // return true if something goes wrong, assume someone could be home
         app.Log($"Error: {e}");
         return(true);
     }
 }
コード例 #3
0
 public async static Task Notify(this NetDaemonApp app, string message)
 {
     await app.CallService("notify", "hass_discord", new
     {
         message = message,
         target  = "511278310584746008"
     });
 }
コード例 #4
0
 /// <summary>
 ///     Takes a snapshot of given entity id of camera and sends to private discord server
 /// </summary>
 /// <param name="app">NetDaemonApp to extend</param>
 /// <param name="camera">Unique id of the camera</param>
 public async static Task CameraSnapshot(this NetDaemonApp app, string camera, string snapshotPath)
 {
     await app.CallService("camera", "snapshot", new
     {
         entity_id = camera,
         filename  = snapshotPath
     });
 }
コード例 #5
0
    public static async Task SetTTSVolume(this NetDaemonApp app)
    {
        var deviceName = GetAudioNotificationDeviceName(AudioNotificationDevice.Home);

        if (app.GetState(deviceName) !.State != "playing")
        {
            await app.SetVolume(GetVolume(app), deviceName);
        }
    }
コード例 #6
0
 public static async Task Notify(
     this NetDaemonApp app,
     string category,
     string message,
     NotificationCriteria textNotificationCriteria = NotificationCriteria.Always,
     NotificationCriteria ttsNotificationCriteria  = NotificationCriteria.None,
     params TextNotificationDevice[] devices)
 {
     await Notify(app, category, message, textNotificationCriteria, ttsNotificationCriteria, null, null, devices);
 }
コード例 #7
0
    public static bool IsEveryoneInBed(this NetDaemonApp app)
    {
        var people = app.State.Where(e => e.EntityId.StartsWith("person."));

        var peopleHome = people.Count(p => p.State == "home");

        var bedOccupancyCount = app.State.Single(e => e.EntityId == "sensor.bed_occupancy_count").State;

        return(peopleHome == bedOccupancyCount);
    }
コード例 #8
0
 public static async Task SetVolume(this NetDaemonApp app, decimal volume, string entityId)
 {
     if (((decimal?)app.GetState(entityId) !.Attribute !.volume_level).GetValueOrDefault(0) != volume)
     {
         await app.CallService("media_player", "volume_set", new
         {
             entity_id    = entityId,
             volume_level = volume
         }, true);
     }
 }
コード例 #9
0
        /// <summary>
        ///     Takes a snapshot of given entity id of camera and sends to private discord server
        /// </summary>
        /// <param name="app">NetDaemonApp to extend</param>
        /// <param name="camera">Unique id of the camera</param>
        /// <returns>The path to the snapshot</returns>
        public async static Task <string> CameraSnapshot(this NetDaemonApp app, string camera)
        {
            var resultingFilename = $"/config/www/motion/{camera}_latest.jpg";
            await app.CallService("camera", "snapshot", new
            {
                entity_id = camera,
                filename  = resultingFilename
            });

            return(resultingFilename);
        }
コード例 #10
0
    public static async Task <bool> TrueNowAndAfter(this NetDaemonApp app, Func <bool> condition, TimeSpan waitDuration)
    {
        if (condition())
        {
            await Task.Delay(waitDuration);

            return(condition());
        }

        return(false);
    }
コード例 #11
0
 public async static void NotifyDiscord(
     this NetDaemonApp app,
     string channel,
     string message,
     bool mention = false)
 {
     await app.CallService("notify", "hass_discord", new
     {
         message = message,
         target  = channel
     });
 }
コード例 #12
0
 public static async Task Notify(
     this NetDaemonApp app,
     string category,
     string message,
     NotificationCriteria textNotificationCriteria,
     NotificationCriteria ttsNotificationCriteria,
     NotificationAction[]?notificationActions,
     string?imageUrl,
     params TextNotificationDevice[] devices)
 {
     await SendNotificationIfCriteriaMet(app, ttsNotificationCriteria, async() => await SendTTSNotifications(app, message));
     await SendNotificationIfCriteriaMet(app, textNotificationCriteria, async() => await SendTextNotifications(app, category, message, textNotificationCriteria, devices, notificationActions, imageUrl));
 }
コード例 #13
0
 public async static void NotifyDiscord(
     this NetDaemonApp app,
     string channel,
     string message,
     Dictionary <string, IEnumerable <string> > data,
     bool mention = false)
 {
     await app.CallService("notify", "hass_discord", new
     {
         data    = data,
         message = message,
         target  = channel
     });
 }
コード例 #14
0
        /// <summary>
        ///     Prints the contents from a IDictionary to a string
        /// </summary>
        /// <param name="app">NetDaemonApp to extend</param>
        /// <param name="dict">The dict to print from, typically from dynamic result</param>
        /// <returns></returns>
        public static string PrettyPrintDictData(this NetDaemonApp app, IDictionary <string, object>?dict)
        {
            if (dict == null)
            {
                return(string.Empty);
            }

            var builder = new StringBuilder(100);

            foreach (var key in dict.Keys)
            {
                builder.AppendLine($"{key}:{dict[key]}");
            }
            return(builder.ToString());
        }
コード例 #15
0
    public async static Task NotifyImage(this NetDaemonApp app, string message, string imagePath)
    {
        var dict = new Dictionary <string, IEnumerable <string> >
        {
            ["images"] = new List <string> {
                imagePath
            }
        };

        await app.CallService("notify", "hass_discord", new
        {
            data    = dict,
            message = message,
            target  = "511278310584746008"
        });
    }
コード例 #16
0
    private static async Task SendNotificationIfCriteriaMet(NetDaemonApp app, NotificationCriteria notificationCriteria, Func <Task> notificationAction)
    {
        switch (notificationCriteria)
        {
        case NotificationCriteria.None:
        case NotificationCriteria.Home when !app.IsAnyoneHome():
        case NotificationCriteria.NotSleeping when app.IsAnyoneSleeping():
            await Task.CompletedTask;

            break;

        default:
            await notificationAction();

            break;
        }
    }
コード例 #17
0
    private static decimal GetVolume(NetDaemonApp app)
    {
        if (app.IsAnyoneSleeping())
        {
            return(0.3M);
        }

        if (DateTime.Now.Hour > 0 && DateTime.Now.Hour <= 8)
        {
            return(0.3M);
        }
        if (DateTime.Now.Hour > 8 && DateTime.Now.Hour <= 20)
        {
            return(1M);
        }
        if (DateTime.Now.Hour > 20 && DateTime.Now.Hour <= 23)
        {
            return(0.3M);
        }

        return(0.5M);
    }
コード例 #18
0
    private static async Task SendTextNotifications(NetDaemonApp app, string category, string message,
                                                    NotificationCriteria textNotificationCriteria, TextNotificationDevice[] devices, NotificationAction[]?notificationActions = null, string?imageUrl = null)
    {
        var effectiveDevices = devices.ToList();

        if (devices.Contains(TextNotificationDevice.All))
        {
            effectiveDevices = Enums.GetValues <TextNotificationDevice>().Where(d => d != TextNotificationDevice.All)
                               .ToList();
        }

        foreach (var device in effectiveDevices)
        {
            if (textNotificationCriteria == NotificationCriteria.Home)
            {
                var person = app.State.Single(e => e.EntityId == $"person.{device.AsString(EnumFormat.Name)}".ToLower());

                if (person.State == "not_home")
                {
                    continue;
                }
            }

            // todo: support iphone and lookup notification type
            await app.CallService("notify", device.AsString(EnumFormat.DisplayName, EnumFormat.Name) !, new
            {
                message = $"{DateTime.Now:t}:{message}",
                title   = category,
                data    = new
                {
                    ttl      = 0,
                    priority = "high",
                    actions  = notificationActions?.Select(n => new { action = n.EventId, title = n.Title }),
                    image    = imageUrl
                }
            });
        }
    }
コード例 #19
0
    public static async Task TurnEverythingOff(this NetDaemonApp app, string?roomName = null, params string[] excludeEntities)
    {
        bool EntitiesToTurnOff(IEntityProperties e) =>
        (e.EntityId.StartsWith("light.") ||
         e.EntityId.StartsWith("fan.") ||
         e.EntityId.StartsWith("climate.")) &&
        !excludeEntities.ToList().Contains(e.EntityId);

        if (roomName == null)
        {
            await app.Entities(e => e.EntityId.StartsWith("light.")).TurnOff().ExecuteAsync();
        }
        else
        {
            bool Area(IEntityProperties e) => e.Attribute !.area != null && ((string)e.Attribute !.area).Split(",").Contains(roomName !.ToLower());

            await app.Entities(e => Area(e) && EntitiesToTurnOff(e)).TurnOff().ExecuteAsync();
        }

        // TODO turn off switches not marked as always on
        // dont turn off bedroom climate if occupied
        // alert if any windows doors open
    }
コード例 #20
0
    private static async Task SendTTSNotifications(NetDaemonApp app, string message)
    {
        var ttsEnabled = app.GetState("input_boolean.tts_enabled") !.State;

        if (ttsEnabled == "on")
        {
            await app.CallService("media_player", "turn_on", new
            {
                entity_id = GetAudioNotificationDeviceName(AudioNotificationDevice.Home)
            }, true);

            await SetTTSVolume(app);

            await app.CallService("tts", "amazon_polly_say", new
            {
                entity_id = GetAudioNotificationDeviceName(AudioNotificationDevice.Home),
                message   = message
            });
        }
        else
        {
            await SendTextNotifications(app, "TTS TEST", message, NotificationCriteria.Always, new[] { TextNotificationDevice.Daniel });
        }
    }
コード例 #21
0
    public static async Task Notify(this NetDaemonApp app, Uri audio, decimal?volume = null, params AudioNotificationDevice[] devices)
    {
        foreach (var device in devices)
        {
            await app.CallService("media_player", "turn_on", new
            {
                entity_id = GetAudioNotificationDeviceName(device)
            }, true);

            await app.CallService("media_player", "volume_set", new
            {
                entity_id    = GetAudioNotificationDeviceName(device),
                volume_level = volume ?? GetVolume(app)
            }, true);

            await app.CallService("media_player", "play_media", new
            {
                entity_id          = GetAudioNotificationDeviceName(device),
                media_content_id   = audio.ToString(),
                media_content_type = "music"
            });
        }
        // todo: get volume before, raise volume, set volume back to previous
    }
コード例 #22
0
ファイル: _EntityExtensions.cs プロジェクト: TheGian82/hassio
 public SwitchEntities(NetDaemonApp app)
 {
     _app = app;
 }
コード例 #23
0
ファイル: _EntityExtensions.cs プロジェクト: TheGian82/hassio
 public static SwitchEntities SwitchEx(this NetDaemonApp app) => new SwitchEntities(app);
コード例 #24
0
ファイル: _EntityExtensions.cs プロジェクト: TheGian82/hassio
 public static MediaPlayerEntities MediaPlayerEx(this NetDaemonApp app) => new MediaPlayerEntities(app);
コード例 #25
0
ファイル: _EntityExtensions.cs プロジェクト: TheGian82/hassio
 public static LightEntities LightEx(this NetDaemonApp app) => new LightEntities(app);
コード例 #26
0
ファイル: _EntityExtensions.cs プロジェクト: TheGian82/hassio
 public MediaPlayerEntities(NetDaemonApp app)
 {
     _app = app;
 }
コード例 #27
0
ファイル: _EntityExtensions.cs プロジェクト: TheGian82/hassio
 public AutomationEntities(NetDaemonApp app)
 {
     _app = app;
 }
コード例 #28
0
ファイル: _EntityExtensions.cs プロジェクト: TheGian82/hassio
 public LightEntities(NetDaemonApp app)
 {
     _app = app;
 }
コード例 #29
0
ファイル: _EntityExtensions.cs プロジェクト: TheGian82/hassio
 public CameraEntities(NetDaemonApp app)
 {
     _app = app;
 }
コード例 #30
0
ファイル: _EntityExtensions.cs プロジェクト: TheGian82/hassio
 public ScriptEntities(NetDaemonApp app)
 {
     _app = app;
 }