"""ED-Stromschätzung.""" import unittest from datetime import datetime, timezone from grotti.cost import energy_estimate def sample_at(offset_s, np=None, bp=None): base = datetime(2026, 10, 17, 15, 0, tzinfo=timezone.utc).timestamp() at = datetime.fromtimestamp(base + offset_s, tz=timezone.utc).isoformat() return {"sampled_at": at, "nozzle_power": np, "bed_power": bp} class EnergyEdTests(unittest.TestCase): def test_flat_without_heater_watts(self): est = energy_estimate(3600, {"watts": 100, "euro_per_kwh": 0.5}, [ sample_at(0, np=1.0, bp=1.0), sample_at(1800, np=1.0, bp=1.0), ]) self.assertEqual(est["source"], "flat") self.assertAlmostEqual(est["kwh"], 0.1) self.assertAlmostEqual(est["cost"], 0.05) def test_ed_caps_long_gaps(self): # 60 min Lücke → nur 90 s werden gezählt samples = [sample_at(0, np=0.5, bp=0.0), sample_at(3600, np=0.5, bp=0.0)] est = energy_estimate(3600, { "watts": 0, "nozzle_watts": 100, "bed_watts": 0, "euro_per_kwh": 1, }, samples) self.assertEqual(est["source"], "ed") self.assertAlmostEqual(est["print_kwh"], (100 * 0.5 / 1000) * (90 / 3600), places=6) def test_ed_dense_samples_full_hour(self): samples = [sample_at(i * 30, np=1.0, bp=0.0) for i in range(0, 121)] est = energy_estimate(3600, { "watts": 0, "nozzle_watts": 200, "bed_watts": 0, "euro_per_kwh": 2, }, samples) self.assertEqual(est["source"], "ed") self.assertAlmostEqual(est["print_kwh"], 0.2, places=3) self.assertAlmostEqual(est["cost"], 0.4, places=3) def test_ed_parts_split_base_nozzle_bed(self): samples = [sample_at(i * 30, np=1.0, bp=0.5) for i in range(0, 121)] est = energy_estimate(3600, { "watts": 40, "nozzle_watts": 60, "bed_watts": 220, "euro_per_kwh": 0.35, }, samples) self.assertEqual(est["source"], "ed") self.assertAlmostEqual(est["base_kwh"], 0.04, places=3) self.assertAlmostEqual(est["nozzle_kwh"], 0.06, places=3) self.assertAlmostEqual(est["bed_kwh"], 0.11, places=3) keys = {p["key"] for p in est["parts"]} self.assertEqual(keys, {"base", "nozzle", "bed"}) self.assertAlmostEqual(sum(p["kwh"] for p in est["parts"]), est["kwh"], places=6) if __name__ == "__main__": unittest.main()