private async Task ExecuteCommand(PhilipsHueBulb bulb, LightCommand command)
        {
            try
            {
                await _executeCommandPolicy.ExecuteAsync(async() =>
                {
                    var client = GetClient(bulb.Bridge);
                    await client.SendCommandAsync(command, new[] { bulb.Index });
                });

                if (command.On.HasValue)
                {
                    bulb.IsOn = command.On.Value;
                    UpdateVariable($"{Name}.{bulb.Id}.IsOn", command.On.Value);
                }

                if (command.Brightness.HasValue)
                {
                    var db = command.Brightness.Value / 255d;
                    UpdateVariable($"{Name}.{bulb.Id}.Brightness", Math.Round(db, 2));
                }
            }
            catch (Exception e)
            {
                _log.Error(e.Message, e);
            }
        }
        private async void OnBridgeFound(object sender, PhilipsHueBridge bridge)
        {
            var variableName = $"PhilipsHue.{bridge.Id}.ApiKey";
            var apiKey = _variableRepository.Get<StringVariable>(variableName).Value;

            var client = new LocalHueClient(bridge.IpAddress, apiKey);
            var bulbs = await client.GetLightsAsync();
            var sensors = await client.GetSensorsAsync();

            if (bulbs != null)
            {
                foreach (var light in bulbs)
                {
                    var id = light.UniqueId.Replace(":", string.Empty).Replace("-", string.Empty);
                    var bulb = new PhilipsHueBulb(light.Id, id, light.Name, bridge)
                    {
                        Icon = "PhilipsHueIcon PhilipsHueIcon_" + light.ModelId,
                        Model = light.ModelId
                    };

                    OnBulbFound(bulb);
                }
            }

            if (sensors != null)
            {
                var presenceSensors = sensors
                    .Where(s => s.ModelId == "SML001" && s.Type == "ZLLPresence")
                    .ToList();

                foreach (var presenceSensor in presenceSensors)
                {
                    var id = presenceSensor.UniqueId.Replace(":", string.Empty).Replace("-", string.Empty);
                    var sensor = new PhilipsHuePresenceSensor(presenceSensor.Id, id, presenceSensor.Name, bridge)
                    {
                        Model = presenceSensor.ModelId,
                        Icon = "PhilipsHueIcon PhilipsHueIcon_PresenceSensor",
                        Battery = presenceSensor.Config.Battery ?? 100
                    };

                    OnPresenceSensorFound(sensor);
                }
            }
        }
        private async Task FindBulbsAsync(LocalHueClient client, PhilipsHueBridge bridge)
        {
            var bulbs = await client.GetLightsAsync();

            if (bulbs != null)
            {
                foreach (var light in bulbs)
                {
                    var id   = light.UniqueId.RemoveMacAddressDelimiters();
                    var bulb = new PhilipsHueBulb(light.Id, id, light.Name, bridge)
                    {
                        Icon  = "PhilipsHueIcon PhilipsHueIcon_" + light.ModelId,
                        Model = light.ModelId
                    };

                    OnBulbFound(bulb);
                }
            }
        }
 protected virtual void OnBulbFound(PhilipsHueBulb e)
 {
     BulbFound?.Invoke(this, e);
 }
 protected virtual void OnBulbFound(PhilipsHueBulb e)
 {
     BulbFound?.Invoke(this, e);
 }
        private bool IsEqual(PhilipsHueBulb device, Light light)
        {
            var lightId = light.UniqueId.RemoveMacAddressDelimiters();

            return(device.Id.Equals(lightId, StringComparison.OrdinalIgnoreCase));
        }