// Copyright (C) 2020-2022 Fredrik Öhrström (gpl-3.0-or-later)
driver {
    name           = aventieswm
    meter_type     = WaterMeter
    default_fields = name,id,total_m3,error_flags,timestamp
    detect {
        mvt = AAA,25,07
    }
    fields {
        field {
            name       = status
            quantity   = Text
            info       = 'Meter status from error flags and tpl status field.'
            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
                map {
                    name  = MEASUREMENT
                    value = 0x0001
                    test  = Set
                }
                map {
                    name  = SABOTAGE
                    value = 0x0002
                    test  = Set
                }
                map {
                    name  = BATTERY
                    value = 0x0004
                    test  = Set
                }
                map {
                    name  = CS
                    value = 0x0008
                    test  = Set
                }
                map {
                    name  = HF
                    value = 0x0010
                    test  = Set
                }
                map {
                    name  = RESET
                    value = 0x0020
                    test  = Set
                }
            }
        }
        field {
            name           = total
            quantity       = Volume
            info           = 'The total water consumption recorded by this meter.'
            vif_scaling    = Auto
            dif_signedness = Signed
            match {
                measurement_type = Instantaneous
                vif_range        = Volume
            }
        }
        field {
            name           = 'consumption_at_set_date_{storage_counter}'
            quantity       = Volume
            info           = 'Water consumption at the # billing period date.'
            vif_scaling    = Auto
            dif_signedness = Signed
            match {
                measurement_type = Instantaneous
                vif_range        = Volume
                storage_nr       = 1,14
            }
        }
        field {
            name       = error_flags
            quantity   = Text
            info       = Deprecated.
            attributes = DEPRECATED
            match {
                measurement_type = Instantaneous
                vif_range        = ErrorFlags
            }
            lookup {
                name            = ERROR_FLAGS
                map_type        = BitToString
                mask_bits       = 0xffff
                default_message = ''
                map {
                    name  = MEASUREMENT
                    value = 0x0001
                    test  = Set
                }
                map {
                    name  = SABOTAGE
                    value = 0x0002
                    test  = Set
                }
                map {
                    name  = BATTERY
                    value = 0x0004
                    test  = Set
                }
                map {
                    name  = CS
                    value = 0x0008
                    test  = Set
                }
                map {
                    name  = HF
                    value = 0x0010
                    test  = Set
                }
                map {
                    name  = RESET
                    value = 0x0020
                    test  = Set
                }
            }
        }
    }
    tests {
        test {
            args     = 'Votten aventieswm 61070071 A004EB23329A477F1DD2D7820B56EB3D'
            telegram = 76442104710007612507727100076121042507B5006005E2E95A3C2A1279A5415E6732679B43369FD5FDDDD783EEEBB48236D34E7C94AF0A18A5FDA5F7D64111EB42D4D891622139F2952F9D12A20088DFA4CF8123871123EE1F6C1DCEA414879DDB4E05E508F1826D7EFBA6964DF804C9261EA23BBF03
            json     = '{"_":"telegram","media":"water","meter":"aventieswm","name":"Votten","id":"61070071","total_m3":466.472,"consumption_at_set_date_1_m3":465.96,"consumption_at_set_date_2_m3":458.88,"consumption_at_set_date_3_m3":449.65,"consumption_at_set_date_4_m3":442.35,"consumption_at_set_date_5_m3":431.07,"consumption_at_set_date_6_m3":423.98,"consumption_at_set_date_7_m3":415.23,"consumption_at_set_date_8_m3":409.03,"consumption_at_set_date_9_m3":400.79,"consumption_at_set_date_10_m3":393.2,"consumption_at_set_date_11_m3":388.63,"consumption_at_set_date_12_m3":379.26,"consumption_at_set_date_13_m3":371.26,"consumption_at_set_date_14_m3":357.84,"status":"OK","error_flags":"","timestamp":"1111-11-11T11:11:11Z"}'
            fields   = 'Votten;61070071;466.472;;1111-11-11 11:11.11'
        }
        test {
            args     = 'Vatten aventieswm 61070072 NOKEY'
            telegram = 76442104720007612507727200076121042507B50060052F2F0413281E0700431404B60083011440B300C30114A5AF00830214CBAC00C3021463A8008303149EA500C3031433A200830414C79F00C304148F9C00830514989900C30514CF9700830614269400C30614069100830714C88B0002FD171111
            json     = '{"_":"telegram","media":"water","meter":"aventieswm","name":"Vatten","id":"61070072","total_m3":466.472,"consumption_at_set_date_1_m3":465.96,"consumption_at_set_date_2_m3":458.88,"consumption_at_set_date_3_m3":449.65,"consumption_at_set_date_4_m3":442.35,"consumption_at_set_date_5_m3":431.07,"consumption_at_set_date_6_m3":423.98,"consumption_at_set_date_7_m3":415.23,"consumption_at_set_date_8_m3":409.03,"consumption_at_set_date_9_m3":400.79,"consumption_at_set_date_10_m3":393.2,"consumption_at_set_date_11_m3":388.63,"consumption_at_set_date_12_m3":379.26,"consumption_at_set_date_13_m3":371.26,"consumption_at_set_date_14_m3":357.84,"status":"ERROR_FLAGS_1100 HF MEASUREMENT","error_flags":"ERROR_FLAGS_1100 HF MEASUREMENT","timestamp":"1111-11-11T11:11:11Z"}'
            fields   = 'Vatten;61070072;466.472;ERROR_FLAGS_1100 HF MEASUREMENT;1111-11-11 11:11.11'
        }
    }
}