Ejemplo n.º 1
0
        private void OnNext(StateChange <ClimateEntity, EntityState <ClimateAttributes> > e)
        {
            // event has 3 properties
            ClimateEntity entity = e.Entity;
            EntityState <ClimateAttributes>?newSate  = e.New;
            EntityState <ClimateAttributes>?oldState = e.Old;

            var currentState = e.Entity.EntityState; // should be the same as e.New (unless the was another change in the meantime)

            string area = e.Entity.Area;

            string?state = e.Entity.State;

            // attribute properties are strong typed
            double temperature      = e.New?.Attributes?.Temperature ?? 0;
            double?temperatureDelta = e.New?.Attributes?.Temperature - e.Old?.Attributes?.Temperature;


            // dump as json to view the structure
            var asJson = JsonSerializer.Serialize(e, new JsonSerializerOptions()
            {
                WriteIndented = true
            });

            Console.WriteLine(asJson);

            Console.WriteLine($"{e}");


            Console.WriteLine($"{e.New?.Attributes}");
        }
Ejemplo n.º 2
0
        public M3App(IHaContext ha)
        {
            Ha = ha;

            // Ha.CallService("notify", "persistent_notification", new { message = "Hello", title = "Yay it works in HassModel via HaContext" }, true);;

            _climateEntity = new ClimateEntity(ha, "climate.dummy_thermostat");

            _climateEntity.StateAllChanges().Where(e => e.New?.Attributes?.Temperature > 20).Subscribe();
            _climateEntity.StateAllChanges().Subscribe(OnNext);

            string?  state       = _climateEntity.State;
            string?  state2      = _climateEntity.EntityState?.State; // is the same
            DateTime?lastChanged = _climateEntity.EntityState?.LastChanged;

            Ha.StateChanges().Subscribe(e =>
            {
            });

            // Entity that has not changed yet is retrieved on demand
            var zone = new ZoneEntity(Ha, "zone.home");

            var lat = zone.Attributes?.latitude;

            var netEnergySensor = new NumericSensorEntity(Ha, "sensor.netto_energy");
            // NumericSensor has double? as state
            double?netEnergy  = netEnergySensor.State;
            double?netEnergy2 = netEnergySensor.EntityState?.State;

            netEnergySensor.StateChanges().Subscribe(e =>
                                                     Console.WriteLine($"{e.New?.Attributes?.FriendlyName} {e.New?.State} {e.New?.Attributes?.UnitOfMeasurement}"));

            // Prints: 'Netto energy 8908.81 kWh'
        }
Ejemplo n.º 3
0
        public CallServiceApp(IHaContext ha)
        {
            var climate = new ClimateEntity(ha, "climate.dummy_thermostat");

            climate.CallService("set_temperature",
                                new SetTemperatureData
            {
                HvacMode    = "heat",
                Temperature = 20,
            }
                                );
        }
Ejemplo n.º 4
0
        public async Task <Climate> Add([FromBody] Climate climate)
        {
            ClimateEntity newclimate = new ClimateEntity(climate.Id, climate.Name);
            await _context.Climate.AddAsync(newclimate);

            await _context.SaveChangesAsync();

            if (await _context.Climate.ContainsAsync(newclimate))
            {
                return(climate);
            }
            return(null);
        }
Ejemplo n.º 5
0
 public static IObservable <StateChange <ClimateEntity, EntityState <ClimateAttributes> > > StateAllChangesWithCurrent(this ClimateEntity entity)
 {
     return(entity.StateAllChangesWithCurrent <ClimateEntity, EntityState <ClimateAttributes>, ClimateAttributes>());
 }