public HomeAssistantClient(HomeControlClient hcClient) { _hcClient = hcClient; // Create a new MQTT client. var factory = new MqttFactory(); _mqttClient = factory.CreateMqttClient(); _mqttClient.UseConnectedHandler(e => { Console.WriteLine("Connected with HA server"); }); _mqttClient.UseApplicationMessageReceivedHandler(async e => { string payload = Encoding.UTF8.GetString(e.ApplicationMessage.Payload); var topic = e.ApplicationMessage.Topic.Split("/"); string device_uuid = topic[1]; string cmd_name = topic[2]; var device = this._hcClient.GetDeviceByUuid(uuid: device_uuid); if (device.Type == "action" && device.Model == "light") { if (cmd_name == "set") { if (payload == "{\"state\": \"ON\"}") { await _hcClient.TurnLightOn(device_uuid); } else { await _hcClient.TurnLightOff(device_uuid); } } } else if (device.Type == "virtual") { if (payload == "ON") { await _hcClient.TurnVirtualOn(device_uuid); } else { await _hcClient.TurnVirtualOff(device_uuid); } } }); }
public hc2ha() { _hcClient = new HomeControlClient(); _haClient = new HomeAssistantClient(_hcClient); }