public string TurnOn(HaDevice device)
 {
     return TurnOn(device.deviceId.ToString());
 }
 public HaDevice GetDevice(HaDevice device)
 {
     return GetDevice(device.deviceId.ToString());
 }
 public string GetDeviceState(HaDevice device)
 {
     string status = string.Empty;
     switch (device.deviceType)
     {
         case DeviceType.Thermostat:
             if ((device.sr != null) && (device.sr.Length > 0))
             {
                 List<SensorReading> setpoints = device.sr.ToList<SensorReading>();
                 SensorReading temperature = setpoints.Find(s => (s.name.StartsWith("Temperature")));
                 string state = string.Empty;
                 if (device.level > 0)
                 {
                     SensorReading heating1 = setpoints.Find(s => (s.name == "Heating1"));
                     SensorReading cooling1 = setpoints.Find(s => (s.name == "Cooling1"));
                     state = "Comfort(" + heating1.value + ")";
                 }
                 else
                 {
                     SensorReading energyHeat = setpoints.Find(s => (s.name == "EnergyHeat"));
                     SensorReading energyCool = setpoints.Find(s => (s.name == "EnergyCool"));
                     state = "Econ(" + energyHeat.value + ")";
                 }
                 status = String.Format("The {0} is set to {1} and the temp. is {2}", device.deviceName, state, temperature.value);
             }
             else
             {
                 status = "Unable to get readings from " + device.name;
             }
             break;
         case DeviceType.DimmerSwitch:
             if (device.level == 0)
             {
                 status = "The " + device.name + " is off";
             }
             else
             {
                 status = "The " + device.name + " is on ";
                 if (device.level < 100)
                 {
                     status = status + " and is at " + device.level + "%";
                 }
             }
             break;
         case DeviceType.ZonePlayer:
         case DeviceType.StandardSwitch:
             status = String.Format("The {0} is {1}", device.name, (device.level == 0) ? "off" : "on");
             break;
         case DeviceType.LevelDisplayer:
             status = String.Format("The value of {0} is {1}", device.name, device.level);
             break;
         default:
             Console.WriteLine(device.name + " is " + device.deviceType.ToString());
             break;
     }
     return status;
 }
 /// <summary>
 /// 
 /// </summary>
 /// <param name="d"></param>
 /// <returns></returns>
 private string deviceToString(HaDevice d)
 {
     switch (d.deviceType) {
         case DeviceType.PowerOutlet:
         case DeviceType.StandardSwitch:
             return d.deviceName + "~" + d.deviceId + "~" + (d.level > 0 ? "255" : "0") + "~" + "BinarySwitch";
         case DeviceType.DimmerSwitch:
             return d.deviceName + "~" + d.deviceId + "~" + (int)d.level + "~" + "MultiLevelSwitch";
         case DeviceType.Thermostat:
             return d.deviceName + "~" + d.deviceId + "~" + (int)d.level + "~" + "Thermostat";
         case DeviceType.MotionSensor:
         case DeviceType.MultiLevelSensor:
         case DeviceType.LevelDisplayer:
             return d.deviceName + "~" + d.deviceId + "~" + (int)d.level + "~" + "Sensor";
         default:
             return d.deviceName + "~" + d.deviceId + "~" + (int)d.level + "~" + "Sensor";
     }
 }
        /// <summary>
        /// Called by InControl when a new device should be tracked locally.
        /// </summary>
        /// <param name="haDevice"></param>
        /// <returns></returns>
        public HaDevice trackDevice(HaDeviceDto haDevice)
        {
            // InControl will call this every time it starts up. It'll call it for each device it currently knows about.
            // We should use the data passed in to create an HaDevice and track it in our localDevices list.
            // Then return that new HaDevice back to InControl.

            var newDevice = new HaDevice();

            // Grab some of the other values and set them in our local device
            newDevice.providerDeviceId = haDevice.uniqueName;
            newDevice.deviceId = haDevice.deviceId;
            newDevice.name = haDevice.deviceName;

            // Our devices always start at level unless there's a value set in the database saving its state
            newDevice.level = 0;

            // See if the device has a value saved in the database for it's initial level
            try {
                var savedStateRaw = base.getDeviceMetadata(newDevice.deviceId, PRIOR_STATE);
                if (savedStateRaw != null) {
                    double savedValue = 0;
                    if (double.TryParse(savedStateRaw, out savedValue)) {
                        newDevice.level = savedValue;
                    }
                }
            } catch { }

            // Add the device to our local list
            localDevices.Add(newDevice);

            return newDevice;
        }
 private void IdentifyDevice(string request,Communicator client)
 {
     HaDevice d = null;
     Thermostat t = null;
     DeviceFound = true;
     //InControlCommunicator client = new InControlCommunicator();
     foreach (HaDevice dvc in client.Devices)
     {
         if (request.ToLower().Contains(dvc.name.ToLower()))
         {
             DeviceIsThermostat = false;
             device = dvc;
             return;
         }
     }
     foreach (Thermostat tstat in client.Thermostats)
     {
         if (request.ToLower().Contains(tstat.name.ToLower()))
         {
             DeviceIsThermostat = true;
             thermostat = tstat;
             return;
         }
     }
     foreach (string token in request.Split(' '))
     {
         if (!string.IsNullOrEmpty(token) && token.Length > 5)
         {
             d = client.Devices.Find(o => (o.name.ToLower().StartsWith(token.ToLower())));
             if (d != null)
             {
                 DeviceIsThermostat = false;
                 device = d;
                 return;
             }
             t = client.Thermostats.Find(o => (o.name.ToLower().StartsWith(token.ToLower())));
             if (t != null)
             {
                 DeviceIsThermostat = true;
                 thermostat = t;
                 return;
             }
         }
     }
     if (request.ToLower().Contains("all"))
     {
         device = new HaDevice();
         device.deviceName = "All";
         return;
     }
     if (request.ToLower().Contains("both"))
     {
         device = new HaDevice();
         device.deviceName = "both";
         return;
     }
     DeviceFound = false;
 }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="dataType"></param>
        /// <param name="value"></param>
        private void setReading(HaDevice device, OWMDataTypes dataType, double value, string label)
        {
            if (device.sensorReadings == null)
            {
                device.sensorReadings = new List<SensorReading>();
            }

            var reading = device.sensorReadings.Where(r => r.name == dataType.ToString()).FirstOrDefault();

            if (reading == null)
            {
                reading = new SensorReading();
                device.sensorReadings.Add(reading);
            }

            reading.value = value;
            reading.name = dataType.ToString();
            reading.label = label;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="haDevice"></param>
        /// <returns></returns>
        public HaDevice trackDevice(HaDeviceDto haDevice)
        {
            // Nofies the controller of a new device to be tracked. Adds the new device to the local list.
            var newDevice = new HaDevice();

            // Our devices always start at level 0.
            newDevice.level = 0;

            // Grab some of the other values and set them in our local device
            newDevice.providerDeviceId = haDevice.uniqueName;
            newDevice.deviceId = haDevice.deviceId;
            newDevice.name = haDevice.deviceName;

            // Add the device to our local list
            localDevices.Add(newDevice);

            return newDevice;
        }