Skip to content

Commit

Permalink
Add active tray sensor
Browse files Browse the repository at this point in the history
  • Loading branch information
AdrianGarside committed Sep 8, 2023
1 parent 61c69a1 commit 8eca79e
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 3 deletions.
35 changes: 34 additions & 1 deletion custom_components/bambu_lab/definitions.py
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ class BambuLabBinarySensorEntityDescription(BinarySensorEntityDescription, Bambu
),
BambuLabSensorEntityDescription(
key="tray_now",
translation_key="active_tray",
translation_key="active_tray_index",
icon="mdi:printer-3d-nozzle",
available_fn=lambda self: self.coordinator.get_model().supports_feature(
Features.AMS) and self.coordinator.get_model().ams.tray_now != 255,
Expand All @@ -302,6 +302,39 @@ class BambuLabBinarySensorEntityDescription(BinarySensorEntityDescription, Bambu
available_fn=lambda self: self.coordinator.get_model().info.subtask_name != "",
value_fn=lambda self: self.coordinator.get_model().info.subtask_name
),
BambuLabSensorEntityDescription(
key="active_tray",
translation_key="active_tray",
icon="mdi:printer-3d-nozzle",
value_fn=lambda self: self.coordinator.get_model().get_active_tray().name,
extra_attributes=lambda self:
{
"color": f"#{self.coordinator.get_model().get_active_tray().color}",
"name": self.coordinator.get_model().get_active_tray().name,
"nozzle_temp_min": self.coordinator.get_model().get_active_tray().nozzle_temp_min,
"nozzle_temp_max": self.coordinator.get_model().get_active_tray().nozzle_temp_max,
"type": self.coordinator.get_model().get_active_tray().type,
},
available_fn=lambda self: self.coordinator.get_model().get_active_tray() is not None,
exists_fn=lambda coordinator: coordinator.get_model().supports_feature(Features.AMS) and not coordinator.get_model().supports_feature(Features.K_VALUE)
),
BambuLabSensorEntityDescription(
key="active_tray",
translation_key="active_tray",
icon="mdi:printer-3d-nozzle",
value_fn=lambda self: self.coordinator.get_model().get_active_tray().name,
extra_attributes=lambda self:
{
"color": f"#{self.coordinator.get_model().get_active_tray().color}",
"k_value": self.coordinator.get_model().get_active_tray().k,
"name": self.coordinator.get_model().get_active_tray().name,
"nozzle_temp_min": self.coordinator.get_model().get_active_tray().nozzle_temp_min,
"nozzle_temp_max": self.coordinator.get_model().get_active_tray().nozzle_temp_max,
"type": self.coordinator.get_model().get_active_tray().type,
},
available_fn=lambda self: self.coordinator.get_model().get_active_tray() is not None,
exists_fn=lambda coordinator: coordinator.get_model().supports_feature(Features.AMS) and coordinator.get_model().supports_feature(Features.K_VALUE)
),
)

VIRTUAL_TRAY_SENSORS: tuple[BambuLabSensorEntityDescription, ...] = (
Expand Down
19 changes: 17 additions & 2 deletions custom_components/bambu_lab/pybambu/models.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import math

from dataclasses import dataclass

from .utils import \
Expand Down Expand Up @@ -29,6 +31,7 @@ def __init__(self, client, device_type, serial):
self.external_spool = ExternalSpool(client)
self.hms = HMSList(client)
self.camera = Camera()
self._active_tray = None

def print_update(self, data):
"""Update from dict"""
Expand Down Expand Up @@ -83,7 +86,19 @@ def supports_feature(self, feature):
case Features.CAMERA_RTSP:
return self.info.device_type == "X1" or self.info.device_type == "X1C"
return False


def get_active_tray(self):
if self.supports_feature(Features.AMS):
if self.ams.tray_now == 255:
return None
if self.ams.tray_now == 254:
return self.external_spool
for ams in self.ams.data:
active_ams = self.ams.data[math.floor(self.ams.tray_now / 4)]
active_tray = self.ams.tray_now % 4
return active_ams.tray[active_tray]
else:
return self.external_spool

@dataclass
class Lights:
Expand Down Expand Up @@ -536,7 +551,7 @@ def __init__(self):

def print_update(self, data):
if len(data) == 1:
# If the day is exactly one entry then it's just the ID and the tray is empty.
# If the data is exactly one entry then it's just the ID and the tray is empty.
self.empty = True
self.idx = ""
self.name = "Empty"
Expand Down
3 changes: 3 additions & 0 deletions custom_components/bambu_lab/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,9 @@
"active_tray": {
"name": "Active tray"
},
"active_tray_index": {
"name": "Active tray index"
},
"humidity_index": {
"name": "Humidity index"
},
Expand Down

0 comments on commit 8eca79e

Please sign in to comment.