package devices import ( "xiaomihome/xiaomi" ) // Create creates the appropriate device control interface for the given device. // It uses the device's URN (SPEC type) to determine which control interface to return. // // Returns: // - Switch, Light, AirConditioner, Fan, Cover, Humidifier, Vacuum, // WaterHeater, or Thermostat depending on the device type // - nil, nil if the device type is not recognized (no error, just unsupported) // - an error if there was a problem creating the control // // Usage: // // info, _ := client.GetDevice(ctx, "some-did") // ctrl, err := devices.Create(client, info) // switch d := ctrl.(type) { // case devices.Switch: // d.TurnOn(ctx) // case devices.Light: // d.SetBrightness(ctx, 80) // } func Create(client *xiaomi.Client, info *xiaomi.DeviceInfo) (interface{}, error) { return createImpl(client, info) } // NewDevice creates the appropriate device control interface, returning the // common Device interface. Use type switch to access type-specific methods: // // dev, err := devices.NewDevice(client, info) // switch d := dev.(type) { // case devices.Switch: // d.TurnOn(ctx) // case devices.Light: // d.SetBrightness(ctx, 80) // } func NewDevice(client *xiaomi.Client, info *xiaomi.DeviceInfo) (Device, error) { ctrl, err := createImpl(client, info) if err != nil { return nil, err } if ctrl == nil { return nil, nil } return ctrl.(Device), nil } // createImpl is the internal implementation shared by Create and NewDevice. func createImpl(client *xiaomi.Client, info *xiaomi.DeviceInfo) (interface{}, error) { if info == nil { return nil, xiaomi.ErrDeviceNotFound } urn := info.URN // Determine device type from URN and try constructors in order var ctrl interface{} var err error // URN-based routing // miot-spec-v2: air-conditioner → urn:miot-spec-v2:device:air-conditioner:... // miot-spec-v2: light → urn:miot-spec-v2:device:light:... // etc. switch { case matchURN(urn, "air-conditioner", "airer", "air-condition-outlet", "air-condition-eco", "ac-partner"): ctrl, err = NewAirConditioner(client, info) case matchURN(urn, "light", "lightbulb", "night-light", "ceiling-light"): ctrl, err = NewLight(client, info) case matchURN(urn, "switch", "outlet", "powerstrip", "toggle-switch", "wireless-switch", "curtain-switch"): ctrl, err = NewSwitch(client, info) case matchURN(urn, "fan", "ceiling-fan", "ventilator", "air-fresh", "air-purifier"): ctrl, err = NewFan(client, info) case matchURN(urn, "curtain", "airer-rack", "airer"): ctrl, err = NewCover(client, info) case matchURN(urn, "humidifier", "dehumidifier", "humidifier-dehumidifier"): ctrl, err = NewHumidifier(client, info) case matchURN(urn, "vacuum", "sweeper", "robot-cleaner"): ctrl, err = NewVacuum(client, info) case matchURN(urn, "water-heater", "water-cooler", "kettle"): ctrl, err = NewWaterHeater(client, info) case matchURN(urn, "heater", "electric-blanket", "thermostat"): ctrl, err = NewThermostat(client, info) case matchURN(urn, "sensor-occupy", "motion-sensor", "magnet"): ctrl, err = NewOccupancySensor(client, info) case matchURN(urn, "sensor-temp", "sensor-humidity", "sensor-ht", "sensor-smoke", "sensor-gasm", "illumination"): ctrl, err = NewTempHumiditySensor(client, info) case matchURN(urn, "speaker", "bluetooth-speaker", "alarm-clock", "media-player", "playback", "music"): ctrl, err = NewSpeaker(client, info) default: // Not a supported device type — not an error, just returns nil return nil, nil } if err != nil { return nil, err } return ctrl, nil } // matchURN checks if the device URN contains any of the given type strings. func matchURN(urn string, types ...string) bool { for _, t := range types { if len(urn) >= len(t) { // Simple substring match — URN format: urn:miot-spec-v2:device:TYPE:... for i := 0; i <= len(urn)-len(t); i++ { if urn[i:i+len(t)] == t { return true } } } } return false }