from . import DEVICEID, init


def test_pow1_energy():
    reg, entities = init(
        {"extra": {"uiid": 5}},  # ACS 16A
        {"devices": {DEVICEID: {"reporting": {"energy": [3600, 999]}}}},
    )

    reg.dispatcher_send(
        DEVICEID,
        {
            "hundredDaysKwhData": "010209020703030300030909080106030601070302030009040506040304050003050104060901050201050205020307070300040001040300040404060304030308030203040504040700060808070302050900020408050905000808020607010804020708070509050401040609040608030802070709050001050705050801040701050000060701010800040902010601000509000704030304000302040101030709050306020805050309020806060900000006000308000004000305010309040700070009010206010200030406010302030501030804040300020400040700010808020307050209010208010803040502040604030307060905030201040704000201020603030109020003040101030902040108050006070600030007040608030500020603"
        },
    )

    energy = next(e for e in entities if e.uid == "energy")
    assert energy.state == 1.29
    assert energy.extra_state_attributes["history"] == [
        1.29,
        2.73,
        3.3,
        3.99,
        8.16,
        3.61,
        7.32,
        3.09,
        4.56,
        4.34,
        5.03,
        5.14,
        6.91,
        5.21,
        5.25,
        2.37,
        7.3,
        4.01,
        4.3,
        4.44,
        6.34,
        3.38,
        3.23,
        4.54,
        4.7,
        6.88,
        7.32,
        5.9,
        2.48,
        5.95,
        0.88,
        2.67,
        1.84,
        2.78,
        7.59,
        5.41,
        4.69,
        4.68,
        3.82,
        7.79,
        5.01,
        5.75,
        5.81,
        4.71,
        5.0,
        6.71,
        1.8,
        4.92,
        1.61,
        0.59,
        0.74,
        3.34,
        0.32,
        4.11,
        3.79,
        5.36,
        2.85,
        5.39,
        2.86,
        6.9,
        0.06,
        0.38,
        0.04,
        0.35,
        1.39,
        4.7,
        7.09,
        1.26,
        1.2,
        3.46,
        1.32,
        3.51,
        3.84,
        4.3,
        2.4,
        4.7,
        1.88,
        2.37,
        5.29,
        1.28,
        1.83,
        4.52,
        4.64,
        3.37,
        6.95,
        3.21,
        4.74,
        0.21,
        2.63,
        3.19,
        2.03,
        4.11,
        3.92,
        4.18,
        5.06,
        7.6,
        3.07,
        4.68,
        3.5,
        2.63,
    ]


def test_pow2_energy():
    reg, entities = init(
        {"extra": {"uiid": 32}},  # POWR2
        {"devices": {DEVICEID: {"reporting": {"energy": [3600, 999]}}}},
    )

    reg.dispatcher_send(
        DEVICEID,
        {
            "hundredDaysKwhData": "000100000101000100000101000100000100000101000101000101000101000101000101000101000101000101000101000101000101000101000101000101000101000101000101000101000101000101000101000101000101000101000101000101000101000101000101000101000101000101000100000101000101000101000101000101000101000101000101000101000101000101000101000101000101000101000101000101000101000101000101000101000100000101000101000101000101000101000101000101000101000101000101000101000100000100000101000101000101000101000101000101000101000101000101000100000101000101000101000101000101000100000100000101000101000101000101000101000101000101000101"
        },
    )

    energy = next(e for e in entities if e.uid == "energy")
    assert energy.state == 0.1
    assert energy.extra_state_attributes["history"] == [
        0.1,
        0.11,
        0.1,
        0.11,
        0.1,
        0.1,
        0.11,
        0.11,
        0.11,
        0.11,
        0.11,
        0.11,
        0.11,
        0.11,
        0.11,
        0.11,
        0.11,
        0.11,
        0.11,
        0.11,
        0.11,
        0.11,
        0.11,
        0.11,
        0.11,
        0.11,
        0.11,
        0.11,
        0.11,
        0.11,
        0.11,
        0.11,
        0.11,
        0.11,
        0.11,
        0.11,
        0.11,
        0.11,
        0.11,
        0.1,
        0.11,
        0.11,
        0.11,
        0.11,
        0.11,
        0.11,
        0.11,
        0.11,
        0.11,
        0.11,
        0.11,
        0.11,
        0.11,
        0.11,
        0.11,
        0.11,
        0.11,
        0.11,
        0.11,
        0.11,
        0.11,
        0.1,
        0.11,
        0.11,
        0.11,
        0.11,
        0.11,
        0.11,
        0.11,
        0.11,
        0.11,
        0.11,
        0.11,
        0.1,
        0.1,
        0.11,
        0.11,
        0.11,
        0.11,
        0.11,
        0.11,
        0.11,
        0.11,
        0.11,
        0.1,
        0.11,
        0.11,
        0.11,
        0.11,
        0.11,
        0.1,
        0.1,
        0.11,
        0.11,
        0.11,
        0.11,
        0.11,
        0.11,
        0.11,
        0.11,
    ]


def test_dualr3_energy():
    reg, entities = init(
        {"extra": {"uiid": 126}},  # Luz WC Suite
        {"devices": {DEVICEID: {"reporting": {"energy_1": [3600, 999]}}}},
    )

    reg.dispatcher_send(
        DEVICEID,
        {
            "kwhHistories_00": "000000010001000000010001000100010000000100000001000000010001000000010000000100010000000100000000000100010000000000010000000100010000000100000001000000010000000100000001000100010001000100000001000100010001000100000001000000010001000100010001000100000001000100000000000100010001000000010000000200000001000100010001000100000001000000010001000100010001000100010001000100010000000100010001000000010001000100000001000100010001000200010001000100010000000100010001000100010001000100000002000100000001000100000001000100010001000100010000000100000001000100010001000000010000000100000000000100010001000000010000000100010001000100010001000100010000000100010001000100010000000100010000000100010000000100010000000100000001000000010001"
        },
    )

    energy = next(e for e in entities if e.uid == "energy_1")
    assert energy.state == 0.0

    history = energy.extra_state_attributes["history"]
    assert len(history) == 180
    assert history[:100] == [
        0.0,
        0.01,
        0.01,
        0.0,
        0.01,
        0.01,
        0.01,
        0.01,
        0.0,
        0.01,
        0.0,
        0.01,
        0.0,
        0.01,
        0.01,
        0.0,
        0.01,
        0.0,
        0.01,
        0.01,
        0.0,
        0.01,
        0.0,
        0.0,
        0.01,
        0.01,
        0.0,
        0.0,
        0.01,
        0.0,
        0.01,
        0.01,
        0.0,
        0.01,
        0.0,
        0.01,
        0.0,
        0.01,
        0.0,
        0.01,
        0.0,
        0.01,
        0.01,
        0.01,
        0.01,
        0.01,
        0.0,
        0.01,
        0.01,
        0.01,
        0.01,
        0.01,
        0.0,
        0.01,
        0.0,
        0.01,
        0.01,
        0.01,
        0.01,
        0.01,
        0.01,
        0.0,
        0.01,
        0.01,
        0.0,
        0.0,
        0.01,
        0.01,
        0.01,
        0.0,
        0.01,
        0.0,
        0.02,
        0.0,
        0.01,
        0.01,
        0.01,
        0.01,
        0.01,
        0.0,
        0.01,
        0.0,
        0.01,
        0.01,
        0.01,
        0.01,
        0.01,
        0.01,
        0.01,
        0.01,
        0.01,
        0.01,
        0.0,
        0.01,
        0.01,
        0.01,
        0.0,
        0.01,
        0.01,
        0.01,
    ]


def test_spm_energy():
    reg, entities = init(
        {"extra": {"uiid": 130}},  # DeviceXXXXa6
        {"devices": {DEVICEID: {"reporting": {"energy_2": [3600, 999]}}}},
    )

    reg.dispatcher_send(
        DEVICEID,
        {
            "kwhHistories_01": "0388043000000000000000000002010602260e43032907410b810528060408280892074106050843052303940b72127007840c510e280d870c330b0005970a0310080a39101711580c990c590a920c030d980c060c220e09110610640e25094105080c580e17099506200251000300000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"
        },
    )

    energy = next(e for e in entities if e.uid == "energy_2")
    assert energy.state == 3.88

    history = energy.extra_state_attributes["history"]
    assert len(history) == 180
    assert history[:100] == [
        3.88,
        4.3,
        0.0,
        0.0,
        0.0,
        0.0,
        0.02,
        1.06,
        2.26,
        14.43,
        3.29,
        7.41,
        11.81,
        5.28,
        6.04,
        8.28,
        8.92,
        7.41,
        6.05,
        8.43,
        5.23,
        3.94,
        11.72,
        18.7,
        7.84,
        12.51,
        14.28,
        13.87,
        12.33,
        11.0,
        5.97,
        10.03,
        16.08,
        10.39,
        16.17,
        17.58,
        12.99,
        12.59,
        10.92,
        12.03,
        13.98,
        12.06,
        12.22,
        14.09,
        17.06,
        16.64,
        14.25,
        9.41,
        5.08,
        12.58,
        14.17,
        9.95,
        6.2,
        2.51,
        0.03,
        0.0,
        0.0,
        0.0,
        0.0,
        0.0,
        0.0,
        0.0,
        0.0,
        0.0,
        0.0,
        0.0,
        0.0,
        0.0,
        0.0,
        0.0,
        0.0,
        0.0,
        0.0,
        0.0,
        0.0,
        0.0,
        0.0,
        0.0,
        0.0,
        0.0,
        0.0,
        0.0,
        0.0,
        0.0,
        0.0,
        0.0,
        0.0,
        0.0,
        0.0,
        0.0,
        0.0,
        0.0,
        0.0,
        0.0,
        0.0,
        0.0,
        0.0,
        0.0,
        0.0,
        0.0,
    ]


def test_s40_energy():
    reg, entities = init(
        {"extra": {"uiid": 182}},  # Manta
        {"devices": {DEVICEID: {"reporting": {"energy": [3600, 999]}}}},
    )

    reg.dispatcher_send(
        DEVICEID,
        {
            "hundredDaysKwhData": "000002000002000001000002000000000001000001000001000002000002000002000001000001000001000002000001000002000001000001000001000001000002000002000002000002000000000001000000000001000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001000002000001000001000000000002000001000004000000000000000000000008000000000000000000000007000103000004000003000003000101000108000201000200000200000300000303000100000206000207000000000201000000000000000000000000000000000102000000000000000105000000"
        },
    )

    energy = next(e for e in entities if e.uid == "energy")
    assert energy.state == 0.02
    assert energy.extra_state_attributes["history"] == [
        0.02,
        0.02,
        0.01,
        0.02,
        0.0,
        0.01,
        0.01,
        0.01,
        0.02,
        0.02,
        0.02,
        0.01,
        0.01,
        0.01,
        0.02,
        0.01,
        0.02,
        0.01,
        0.01,
        0.01,
        0.01,
        0.02,
        0.02,
        0.02,
        0.02,
        0.0,
        0.01,
        0.0,
        0.01,
        0.01,
        0.0,
        0.0,
        0.0,
        0.0,
        0.0,
        0.0,
        0.0,
        0.0,
        0.0,
        0.0,
        0.0,
        0.0,
        0.0,
        0.0,
        0.0,
        0.0,
        0.0,
        0.0,
        0.0,
        0.0,
        0.0,
        0.0,
        0.0,
        0.0,
        0.0,
        0.0,
        0.0,
        0.0,
        0.01,
        0.02,
        0.01,
        0.01,
        0.0,
        0.02,
        0.01,
        0.04,
        0.0,
        0.0,
        0.0,
        0.08,
        0.0,
        0.0,
        0.0,
        0.07,
        0.13,
        0.04,
        0.03,
        0.03,
        0.11,
        0.18,
        0.21,
        0.2,
        0.2,
        0.3,
        0.33,
        0.1,
        0.26,
        0.27,
        0.0,
        0.21,
        0.0,
        0.0,
        0.0,
        0.0,
        0.0,
        0.12,
        0.0,
        0.0,
        0.15,
        0.0,
    ]


def test_powr3_energy():
    reg, entities = init(
        {"extra": {"uiid": 190}},  # Terrarium POW
        {"devices": {DEVICEID: {"reporting": {"energy": [3600, 999]}}}},
    )

    reg.dispatcher_send(
        DEVICEID,
        {
            "hoursKwhData": "006007015015015014014015015015015015009008008000000000001001000000001008009008015015015014014014014015015015008009008001000000001000000001001008008008014014015015015015015015015015008009008000000001000001001001000008009008015014015014015014015015015015008009008000000001000001000000000008008008015015015015015015015015014015009008008000001000000001001001000008008014015014015015014015015014014008008008000001001000000000001001001008009014014014015015014008008008008008008009000000000001001000001000000009008015015015014015015014015015015009008009000000000001001000000000000009008015014015010008008008008008008008008008001000000001000001000000000009008015015015015015015014015015015011008008000000000001001000000000001008009008008008008008008009008009008009008008001000001001000000000000001008008015015015015015015012015015015008008008000000001001000000001001000009008015015015015015016015015015010008009008001000000001000000001001001008008008008008009008009008008009008008008008001000000000001000001000001008008015015015015015015015015015009008008008000000001001000000001001000009008015015015015014015015015015015008008008001000001000000001000000000008008014015014015014015014015014014012008008000000001000000000001001000009008015015015015015015015015014015008008009000000001001000001000000001008008014014014014014014015015015015008008008001000001000000000001001001008009015015015015014015015015015014009009008001001000001000000000000001008008015015015015015015015014015015015009009000000001001000000001000000008008015014015014015014014015014015014010008001000000000001000000001000008008015015014015015015015014015015015009008000001001000000000001000000008008015014015015014015014015014015011008008001000000001000000001001001008009015015015015015015015015015015011008008000000001001000000001000000008008015014014015014015014014015014008008008001000000001000000001000001008008015014015014015015014015014014008008008000000001001000000001000000008008008004000000000000000000000000000008009001000000001000000001001000009008008008008008008008008008009009008008008000001000000001000001000000009008015015015014015015014000000000000000000000000000000000000000000000000"
        },
    )

    energy = next(e for e in entities if e.uid == "energy")
    assert energy.state == 0.06

    history = energy.extra_state_attributes["history"]
    assert len(history) == 720
    assert history[:100] == [
        0.06,
        0.07,
        0.15,
        0.15,
        0.15,
        0.14,
        0.14,
        0.15,
        0.15,
        0.15,
        0.15,
        0.15,
        0.09,
        0.08,
        0.08,
        0.0,
        0.0,
        0.0,
        0.01,
        0.01,
        0.0,
        0.0,
        0.01,
        0.08,
        0.09,
        0.08,
        0.15,
        0.15,
        0.15,
        0.14,
        0.14,
        0.14,
        0.14,
        0.15,
        0.15,
        0.15,
        0.08,
        0.09,
        0.08,
        0.01,
        0.0,
        0.0,
        0.01,
        0.0,
        0.0,
        0.01,
        0.01,
        0.08,
        0.08,
        0.08,
        0.14,
        0.14,
        0.15,
        0.15,
        0.15,
        0.15,
        0.15,
        0.15,
        0.15,
        0.15,
        0.08,
        0.09,
        0.08,
        0.0,
        0.0,
        0.01,
        0.0,
        0.01,
        0.01,
        0.01,
        0.0,
        0.08,
        0.09,
        0.08,
        0.15,
        0.14,
        0.15,
        0.14,
        0.15,
        0.14,
        0.15,
        0.15,
        0.15,
        0.15,
        0.08,
        0.09,
        0.08,
        0.0,
        0.0,
        0.01,
        0.0,
        0.01,
        0.0,
        0.0,
        0.0,
        0.08,
        0.08,
        0.08,
        0.15,
        0.15,
    ]
