// Copyright (C) 2022-2023 Fredrik Öhrström (gpl-3.0-or-later)
// Lumon Ultraheat heat meter.
driver {
    name           = ultraheat
    meter_type     = HeatMeter
    default_fields = name,id,heat_kwh,timestamp
    detect {
        mvt = LUG,04,04
    }
    library {
        use = meter_datetime
        use = fabrication_no
    }
    fields {
        field {
            name           = heat
            quantity       = Energy
            info           = 'The total heat energy consumption recorded by this meter.'
            vif_scaling    = Auto
            dif_signedness = Signed
            match {
                measurement_type = Instantaneous
                vif_range        = AnyEnergyVIF
            }
        }
        field {
            name           = volume
            quantity       = Volume
            info           = 'The total heating media volume recorded by this meter.'
            vif_scaling    = Auto
            dif_signedness = Signed
            match {
                measurement_type = Instantaneous
                vif_range        = Volume
            }
        }
        field {
            name           = power
            quantity       = Power
            info           = 'The current power consumption.'
            vif_scaling    = Auto
            dif_signedness = Signed
            match {
                measurement_type = Instantaneous
                vif_range        = PowerW
            }
        }
        field {
            name           = flow
            quantity       = Flow
            info           = 'The current heat media volume flow.'
            vif_scaling    = Auto
            dif_signedness = Signed
            match {
                measurement_type = Instantaneous
                vif_range        = VolumeFlow
            }
        }
        field {
            name           = flow
            quantity       = Temperature
            info           = 'The current forward heat media temperature.'
            vif_scaling    = Auto
            dif_signedness = Signed
            match {
                measurement_type = Instantaneous
                vif_range        = FlowTemperature
            }
        }
        field {
            name           = return
            quantity       = Temperature
            info           = 'The current return heat media temperature.'
            vif_scaling    = Auto
            dif_signedness = Signed
            match {
                measurement_type = Instantaneous
                vif_range        = ReturnTemperature
            }
        }
        field {
            name       = status
            quantity   = Text
            info       = 'Meter status.'
            attributes = STATUS,INCLUDE_TPL_STATUS
            match {
                measurement_type = Instantaneous
                vif_range        = ErrorFlags
            }
            lookup {
                name            = ERROR_FLAGS
                map_type        = BitToString
                mask_bits       = 0xffff
                default_message = OK
            }
        }
    }
    tests {
        test {
            args     = 'MyUltra ultraheat 70444600 NOKEY'
            telegram = 68F8F86808007200464470A7320404270000000974040970040C0E082303000C14079519000B2D0500000B3B0808000A5B52000A5F51000A6206004C14061818004C0E490603000C7800464470891071609B102D020100DB102D0201009B103B6009009A105B78009A105F74000C22726701003C22000000007C2200000000426C01018C2006000000008C3006000000008C80100600000000CC200600000000CC300600000000CC801006000000009A115B64009A115F63009B113B5208009B112D020100BC0122000000008C010E490603008C2106000000008C3106000000008C811006000000008C011406181800046D310ACA210F21040010A0C116
            json     = '{"_":"telegram","media":"heat","status":"OK","meter":"ultraheat","meter_datetime": "2022-01-10 10:49", "name":"MyUltra","id":"70444600","heat_kwh":8974.444444,"volume_m3":1995.07,"power_kw":0.5,"flow_m3h":0.808,"flow_c":52,"return_c":51,"fabrication_no": "70444600","timestamp":"1111-11-11T11:11:11Z"}'
            fields   = 'MyUltra;70444600;8974.444444;1111-11-11 11:11.11'
        }
        test {
            args     = 'MyUltra2 ultraheat 71635605 NOKEY'
            telegram = 3b44a7320556637104047afa2000202f2f0c06774202000c14399956000b2d0200f00b3b3018000a5a51030a5e520302fd170000066d0c080af42500
            json     = '{"_":"telegram","flow_c": 35.1,"flow_m3h": 1.83,"heat_kwh": 24277,"id": "71635605","media": "heat","meter": "ultraheat","meter_datetime": "2023-05-20 10:08:12","name": "MyUltra2","power_kw": -0.2,"return_c": 35.2,"status": "UNKNOWN_20","timestamp": "1111-11-11T11:11:11Z","volume_m3": 5699.39}'
            fields   = 'MyUltra2;71635605;24277;1111-11-11 11:11.11'
        }
    }
}