Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make the heater_bed printer object optional #328

Merged
merged 1 commit into from
Apr 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 13 additions & 39 deletions custom_components/moonraker/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,28 +84,6 @@ class MoonrakerSensorDescription(SensorEntityDescription):
else "",
subscriptions=[("display_status", "message")],
),
MoonrakerSensorDescription(
key="bed_target",
name="Bed Target",
value_fn=lambda sensor: float(
sensor.coordinator.data["status"]["heater_bed"]["target"] or 0.0
),
subscriptions=[("heater_bed", "target")],
icon="mdi:radiator",
unit=UnitOfTemperature.CELSIUS,
state_class=SensorStateClass.MEASUREMENT,
),
MoonrakerSensorDescription(
key="bed_temp",
name="Bed Temperature",
value_fn=lambda sensor: float(
sensor.coordinator.data["status"]["heater_bed"]["temperature"] or 0.0
),
subscriptions=[("heater_bed", "temperature")],
icon="mdi:radiator",
unit=UnitOfTemperature.CELSIUS,
state_class=SensorStateClass.MEASUREMENT,
),
MoonrakerSensorDescription(
key="filename",
name="Filename",
Expand Down Expand Up @@ -230,17 +208,6 @@ class MoonrakerSensorDescription(SensorEntityDescription):
icon="mdi:percent",
unit=PERCENTAGE,
),
MoonrakerSensorDescription(
key="bed_power",
name="Bed Power",
value_fn=lambda sensor: int(
sensor.coordinator.data["status"]["heater_bed"]["power"] * 100
),
subscriptions=[("heater_bed", "power")],
icon="mdi:flash",
unit=PERCENTAGE,
state_class=SensorStateClass.MEASUREMENT,
),
MoonrakerSensorDescription(
key="total_layer",
name="Total Layer",
Expand Down Expand Up @@ -492,17 +459,24 @@ async def async_setup_optional_sensors(coordinator, entry, async_add_entities):
state_class=SensorStateClass.MEASUREMENT,
)
sensors.append(desc)
elif obj.startswith("extruder"):
elif obj.startswith("extruder") or obj.startswith("heater_bed"):
if obj.startswith("extruder"):
icon = "mdi:printer-3d-nozzle-heat"
base_name = obj
else:
icon = "mdi:radiator"
base_name = "Bed"

desc = MoonrakerSensorDescription(
key=f"{obj}_temp",
status_key=obj,
name=f"{obj} Temperature".title(),
name=f"{base_name} 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",
icon=icon,
unit=UnitOfTemperature.CELSIUS,
state_class=SensorStateClass.MEASUREMENT,
)
Expand All @@ -511,13 +485,13 @@ async def async_setup_optional_sensors(coordinator, entry, async_add_entities):
desc = MoonrakerSensorDescription(
key=f"{obj}_target",
status_key=obj,
name=f"{obj} Target".title(),
name=f"{base_name} 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",
icon=icon,
unit=UnitOfTemperature.CELSIUS,
state_class=SensorStateClass.MEASUREMENT,
)
Expand All @@ -526,7 +500,7 @@ async def async_setup_optional_sensors(coordinator, entry, async_add_entities):
desc = MoonrakerSensorDescription(
key=f"{obj}_power",
status_key=obj,
name=f"{obj} Power".title(),
name=f"{base_name} Power".title(),
value_fn=lambda sensor: int(
sensor.coordinator.data["status"][sensor.status_key]["power"] * 100
),
Expand Down
12 changes: 0 additions & 12 deletions tests/test_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,18 +216,6 @@ async def test_opt_sensor_missing(hass, get_data, get_printer_objects_list):
assert state is None


async def test_missing_heater_bed(hass, get_data):
"""Test."""
get_data["status"]["heater_bed"]["target"] = None

config_entry = MockConfigEntry(domain=DOMAIN, data=MOCK_CONFIG, entry_id="test")
config_entry.add_to_hass(hass)
assert await async_setup_entry(hass, config_entry)
await hass.async_block_till_done()

assert hass.states.get("sensor.mainsail_bed_target").state == "0.0"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would probably test that this works and there is no sensors, but other sensors works



async def test_eta(hass):
"""Test."""
config_entry = MockConfigEntry(domain=DOMAIN, data=MOCK_CONFIG, entry_id="test")
Expand Down
Loading