private void UpdateSensorVariable(PhilipsHuePresenceSensor sensor, string variableName, bool?value)
 {
     if (value.HasValue)
     {
         UpdateVariable($"{Name}.{sensor.Id}.{variableName}", value.Value);
     }
 }
        private void HandleZllPresenceSensor(Sensor sensor, PhilipsHueBridge bridge)
        {
            var id           = GetUniqueId(sensor);
            var sensorDevice = new PhilipsHuePresenceSensor(sensor.Id, id, sensor.Name, bridge)
            {
                Model   = sensor.ModelId,
                Type    = sensor.Type,
                Icon    = "PhilipsHueIcon PhilipsHueIcon_PresenceSensor",
                Battery = sensor.Config.Battery ?? 100
            };

            OnPresenceSensorFound(sensorDevice);
        }
        private void UpdateSensorVariable(PhilipsHuePresenceSensor sensor, string variableName, double?value, string unit = null, Func <double, double> convert = null)
        {
            if (value.HasValue)
            {
                var v = value.Value;

                if (convert != null)
                {
                    v = convert(v);
                }

                UpdateVariable($"{Name}.{sensor.Id}.{variableName}", v, unit);
            }
        }
        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);
                }
            }
        }
 protected virtual void OnPresenceSensorFound(PhilipsHuePresenceSensor e)
 {
     PresenceSensorFound?.Invoke(this, e);
 }
 protected virtual void OnPresenceSensorFound(PhilipsHuePresenceSensor e)
 {
     PresenceSensorFound?.Invoke(this, e);
 }