Skip to content

Commit

Permalink
Add support for multiple extruder
Browse files Browse the repository at this point in the history
The patch add support for multiple extruder, the klipper convention to do so
is quite simple, the first one is called extruder and the others are named
extruder1, extruder2 and so on. By moving the extruder sensors to the optional
sensor where we use the object list it's quite easy to support multiple one
  • Loading branch information
Eric Tremblay committed Mar 25, 2024
1 parent 6a563e0 commit cce725f
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 33 deletions.
78 changes: 45 additions & 33 deletions custom_components/moonraker/sensor.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Sensor platform for Moonraker integration."""

import logging
from collections.abc import Callable
from dataclasses import dataclass
Expand Down Expand Up @@ -77,28 +78,6 @@ class MoonrakerSensorDescription(SensorEntityDescription):
else "",
subscriptions=[("display_status", "message")],
),
MoonrakerSensorDescription(
key="extruder_temp",
name="Extruder Temperature",
value_fn=lambda sensor: float(
sensor.coordinator.data["status"]["extruder"]["temperature"] or 0.0
),
subscriptions=[("extruder", "temperature")],
icon="mdi:printer-3d-nozzle-heat",
unit=UnitOfTemperature.CELSIUS,
state_class=SensorStateClass.MEASUREMENT,
),
MoonrakerSensorDescription(
key="extruder_target",
name="Extruder Target",
value_fn=lambda sensor: float(
sensor.coordinator.data["status"]["extruder"]["target"] or 0.0
),
subscriptions=[("extruder", "target")],
icon="mdi:printer-3d-nozzle-heat",
unit=UnitOfTemperature.CELSIUS,
state_class=SensorStateClass.MEASUREMENT,
),
MoonrakerSensorDescription(
key="bed_target",
name="Bed Target",
Expand Down Expand Up @@ -256,17 +235,6 @@ class MoonrakerSensorDescription(SensorEntityDescription):
unit=PERCENTAGE,
state_class=SensorStateClass.MEASUREMENT,
),
MoonrakerSensorDescription(
key="extruder_power",
name="Extruder Power",
value_fn=lambda sensor: int(
sensor.coordinator.data["status"]["extruder"]["power"] * 100
),
subscriptions=[("extruder", "power")],
icon="mdi:flash",
unit=PERCENTAGE,
state_class=SensorStateClass.MEASUREMENT,
),
MoonrakerSensorDescription(
key="total_layer",
name="Total Layer",
Expand Down Expand Up @@ -423,6 +391,50 @@ async def async_setup_optional_sensors(coordinator, entry, async_add_entities):
unit=PERCENTAGE,
)
sensors.append(desc)
elif obj.startswith("extruder"):
desc = MoonrakerSensorDescription(
key=f"{obj}_temp",
status_key=obj,
name=f"{obj} Temperature".title(),
value_fn=lambda sensor: float(
sensor.coordinator.data["status"][sensor.status_key]["temperature"]
or 0.0
),
subscriptions=[(obj, "temperature")],
icon="mdi:printer-3d-nozzle-heat",
unit=UnitOfTemperature.CELSIUS,
state_class=SensorStateClass.MEASUREMENT,
)
sensors.append(desc)

desc = MoonrakerSensorDescription(
key=f"{obj}_target",
status_key=obj,
name=f"{obj} Target".title(),
value_fn=lambda sensor: float(
sensor.coordinator.data["status"][sensor.status_key]["target"]
or 0.0
),
subscriptions=[(obj, "target")],
icon="mdi:printer-3d-nozzle-heat",
unit=UnitOfTemperature.CELSIUS,
state_class=SensorStateClass.MEASUREMENT,
)
sensors.append(desc)

desc = MoonrakerSensorDescription(
key=f"{obj}_power",
status_key=obj,
name=f"{obj} Power".title(),
value_fn=lambda sensor: int(
sensor.coordinator.data["status"][sensor.status_key]["power"] * 100
),
subscriptions=[(obj, "power")],
icon="mdi:flash",
unit=PERCENTAGE,
state_class=SensorStateClass.MEASUREMENT,
)
sensors.append(desc)

coordinator.load_sensor_data(sensors)
await coordinator.async_refresh()
Expand Down
3 changes: 3 additions & 0 deletions docs/entities/sensors.rst
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,15 @@ Sensors that are added on integration startup.
- From Moonraker API (display_status, message)
* - Extruder Temperature
- Extruder Temperature
- Multiple extruders are supported
- From Moonraker API (extruder, temperature)
* - Extruder Temperature Target
- Extruder Temperature Target
- Multiple extruders are supported
- From Moonraker API (extruder, target)
* - Extruder Power
- Extruder current power consumption (in %). 100% = Max, 0% = close
- Multiple extruders are supported
- From Moonraker API (extruder, power)
* - Bed Temperature
- Bed Temperature
Expand Down
9 changes: 9 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,14 @@ def get_data_fixture():
"pressure_advance": 0.325,
"smooth_time": 0.04,
},
"extruder1": {
"temperature": 220.01,
"target": 220.0,
"power": 0.6667108063925052,
"can_extrude": True,
"pressure_advance": 0.325,
"smooth_time": 0.04,
},
"heater_bed": {
"temperature": 60.01,
"target": 60.0,
Expand Down Expand Up @@ -269,6 +277,7 @@ def get_printer_objects_list_fixture():
"manual_probe",
"toolhead",
"extruder",
"extruder1",
"temperature_fan fan_temp",
"temperature_host host_temp",
"bme280 bme280_temp",
Expand Down
3 changes: 3 additions & 0 deletions tests/test_sensor.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Test moonraker sensor."""

import datetime as dt
from unittest.mock import patch

Expand Down Expand Up @@ -75,6 +76,8 @@ async def test_sensor_services_update(hass, get_data):
("mainsail_bed_temperature", "60.01"),
("mainsail_extruder_target", "205.0"),
("mainsail_extruder_temperature", "205.02"),
("mainsail_extruder1_target", "220.0"),
("mainsail_extruder1_temperature", "220.01"),
("mainsail_progress", "90"),
("mainsail_printer_state", "ready"),
("mainsail_filename", "CE3E3V2_picture_frame_holder.gcode"),
Expand Down

0 comments on commit cce725f

Please sign in to comment.