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 system update optional (only if available) #246

Merged
merged 1 commit into from
Dec 9, 2023
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
66 changes: 36 additions & 30 deletions custom_components/moonraker/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -495,41 +495,47 @@ async def async_setup_machine_update_sensors(coordinator, entry, async_add_entit
return

coordinator.add_data_updater(_machine_update_updater)
sensors = [
MoonrakerSensorDescription(
key="machine_update_system",
name="Machine Update System",
value_fn=lambda sensor: f"{sensor.coordinator.data['machine_update']['version_info']['system']['package_count']} packages can be upgraded",
subscriptions=[],
icon="mdi:update",
entity_registry_enabled_default=False,
)
]
sensors = []

for version_info in machine_status["version_info"]:
if version_info == "system":
continue
sensors.append(
MoonrakerSensorDescription(
key=f"machine_update_{version_info}",
name=f"Version {version_info.title()}",
status_key=version_info,
value_fn=lambda sensor: (lambda v, rv: f"{v} > {rv}" if v != rv else v)(
sensor.coordinator.data["machine_update"]["version_info"][
sensor.status_key
]["version"],
sensor.coordinator.data["machine_update"]["version_info"][
sensor.status_key
]["remote_version"],
),
subscriptions=[],
icon="mdi:update",
entity_registry_enabled_default=False,
sensors.append(
MoonrakerSensorDescription(
key="machine_update_system",
name="Machine Update System",
value_fn=lambda sensor: f"{sensor.coordinator.data['machine_update']['version_info']['system']['package_count']} packages can be upgraded",
subscriptions=[],
icon="mdi:update",
entity_registry_enabled_default=False,
)
)
else:
sensors.append(
MoonrakerSensorDescription(
key=f"machine_update_{version_info}",
name=f"Version {version_info.title()}",
status_key=version_info,
value_fn=lambda sensor: (
lambda v, rv: f"{v} > {rv}" if v != rv else v
)(
sensor.coordinator.data["machine_update"]["version_info"][
sensor.status_key
]["version"],
sensor.coordinator.data["machine_update"]["version_info"][
sensor.status_key
]["remote_version"],
),
subscriptions=[],
icon="mdi:update",
entity_registry_enabled_default=False,
)
)
if len(sensors) > 0:
coordinator.load_sensor_data(sensors)
await coordinator.async_refresh()
async_add_entities(
[MoonrakerSensor(coordinator, entry, desc) for desc in sensors]
)
coordinator.load_sensor_data(sensors)
await coordinator.async_refresh()
async_add_entities([MoonrakerSensor(coordinator, entry, desc) for desc in sensors])


class MoonrakerSensor(BaseMoonrakerEntity, SensorEntity):
Expand Down
14 changes: 14 additions & 0 deletions tests/test_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -444,3 +444,17 @@ async def test_current_layer_calculated_partial_info():
"layer_height": 0.2,
}
assert calculate_current_layer(data) == 42


async def test_update_no_system_update(hass, get_machine_update_status):
"""Test update available."""
del get_machine_update_status["version_info"]["system"]

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()

entity_registry = er.async_get(hass)
entity = entity_registry.async_get("sensor.mainsail_machine_update_system")
assert entity is None
Loading