Skip to content

Commit c82d159

Browse files
authored
Add enum options for Octoprint status sensor (home-assistant#157213)
1 parent d890387 commit c82d159

File tree

4 files changed

+99
-6
lines changed

4 files changed

+99
-6
lines changed

homeassistant/components/octoprint/icons.json

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,39 @@
11
{
2+
"entity": {
3+
"sensor": {
4+
"file_name": {
5+
"default": "mdi:printer-3d-nozzle",
6+
"state": {
7+
"unavailable": "mdi:printer-3d-nozzle-off"
8+
}
9+
},
10+
"status": {
11+
"default": "mdi:printer-3d",
12+
"state": {
13+
"cancelling": "mdi:file-cancel",
14+
"connecting": "mdi:lan-connect",
15+
"detect_serial": "mdi:lan-connect",
16+
"error": "mdi:printer-3d-nozzle-alert",
17+
"finishing": "mdi:printer-3d-nozzle",
18+
"offline": "mdi:printer-3d-off",
19+
"offline_after_error": "mdi:printer-3d-off",
20+
"open_serial": "mdi:lan-connect",
21+
"operational": "mdi:printer-3d",
22+
"paused": "mdi:printer-3d-nozzle-off",
23+
"pausing": "mdi:printer-3d-nozzle",
24+
"printing": "mdi:printer-3d-nozzle",
25+
"printing_sd": "mdi:printer-3d-nozzle",
26+
"printing_streaming": "mdi:file-upload",
27+
"resuming": "mdi:printer-3d-nozzle",
28+
"starting": "mdi:printer-3d-nozzle",
29+
"starting_sd": "mdi:printer-3d-nozzle",
30+
"starting_streaming": "mdi:file-upload",
31+
"transferring_file": "mdi:file-upload",
32+
"unavailable": "mdi:printer-3d-off"
33+
}
34+
}
35+
}
36+
},
237
"services": {
338
"printer_connect": {
439
"service": "mdi:lan-connect"

homeassistant/components/octoprint/sensor.py

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@
2323

2424
_LOGGER = logging.getLogger(__name__)
2525

26-
JOB_PRINTING_STATES = ["Printing from SD", "Printing"]
27-
2826

2927
def _is_printer_printing(printer: OctoprintPrinterInfo) -> bool:
3028
return (
@@ -110,10 +108,38 @@ def __init__(
110108
self._attr_device_info = coordinator.device_info
111109

112110

111+
# Map the strings returned by the OctoPrint API back into values based on the underlying OctoPrint constants.
112+
# See octoprint.util.comm.MahcineCom.getStateString():
113+
# https://github.com/OctoPrint/OctoPrint/blob/7e7d418dac467e308b24c669a03e8b4256f04b45/src/octoprint/util/comm.py#L965
114+
_API_STATE_VALUE = {
115+
"Opening serial connection": "open_serial",
116+
"Detecting serial connection": "detect_serial",
117+
"Connecting": "connecting",
118+
"Operational": "operational",
119+
"Starting print from SD": "starting_sd",
120+
"Starting to send file to SD": "starting_streaming",
121+
"Starting": "starting",
122+
"Printing from SD": "printing_sd",
123+
"Sending file to SD": "printing_streaming",
124+
"Printing": "printing",
125+
"Cancelling": "cancelling",
126+
"Pausing": "pausing",
127+
"Paused": "paused",
128+
"Resuming": "resuming",
129+
"Finishing": "finishing",
130+
"Offline": "offline",
131+
"Error": "error",
132+
"Offline after error": "offline_after_error",
133+
"Transferring file to SD": "transferring_file",
134+
}
135+
136+
113137
class OctoPrintStatusSensor(OctoPrintSensorBase):
114138
"""Representation of an OctoPrint status sensor."""
115139

116-
_attr_icon = "mdi:printer-3d"
140+
_attr_device_class = SensorDeviceClass.ENUM
141+
_attr_options = list(_API_STATE_VALUE.values())
142+
_attr_translation_key = "status"
117143

118144
def __init__(
119145
self, coordinator: OctoprintDataUpdateCoordinator, device_id: str
@@ -124,11 +150,14 @@ def __init__(
124150
@property
125151
def native_value(self):
126152
"""Return sensor state."""
153+
154+
# Get printer data from the coordinator
127155
printer: OctoprintPrinterInfo = self.coordinator.data["printer"]
128156
if not printer:
129157
return None
130158

131-
return printer.state.text
159+
# Translate the string from the API into an internal state value, or return None (Unknown) if no match
160+
return _API_STATE_VALUE.get(printer.state.text)
132161

133162
@property
134163
def available(self) -> bool:
@@ -272,7 +301,7 @@ def available(self) -> bool:
272301
class OctoPrintFileNameSensor(OctoPrintSensorBase):
273302
"""Representation of an OctoPrint file name sensor."""
274303

275-
_attr_icon = "mdi:printer-3d-nozzle"
304+
_attr_translation_key = "file_name"
276305

277306
def __init__(
278307
self,

homeassistant/components/octoprint/strings.json

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,35 @@
4747
"extruder_temperature": {
4848
"name": "Extruder temperature"
4949
}
50+
},
51+
"sensor": {
52+
"file_name": {
53+
"name": "Current file"
54+
},
55+
"status": {
56+
"name": "Current state",
57+
"state": {
58+
"cancelling": "Cancelling",
59+
"connecting": "Connecting",
60+
"detect_serial": "Detecting serial connection",
61+
"error": "[%key:common::state::error%]",
62+
"finishing": "Finishing",
63+
"offline": "Offline",
64+
"offline_after_error": "Offline after error",
65+
"open_serial": "Opening serial connection",
66+
"operational": "Operational",
67+
"paused": "[%key:common::state::paused%]",
68+
"pausing": "Pausing",
69+
"printing": "Printing",
70+
"printing_sd": "Printing from SD",
71+
"printing_streaming": "Sending file to SD",
72+
"resuming": "Resuming",
73+
"starting": "Starting",
74+
"starting_sd": "Starting print from SD",
75+
"starting_streaming": "Starting to send file to SD",
76+
"transferring_file": "Transferring file to SD"
77+
}
78+
}
5079
}
5180
},
5281
"exceptions": {

tests/components/octoprint/test_sensor.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ async def test_sensors(
5757

5858
state = hass.states.get("sensor.octoprint_current_state")
5959
assert state is not None
60-
assert state.state == "Operational"
60+
assert state.state == "operational"
6161
assert state.name == "OctoPrint Current State"
6262
entry = entity_registry.async_get("sensor.octoprint_current_state")
6363
assert entry.unique_id == "Current State-uuid"

0 commit comments

Comments
 (0)