Skip to content

Commit

Permalink
Add support for additional sensors from BME680
Browse files Browse the repository at this point in the history
  • Loading branch information
BenBergman committed Mar 1, 2024
1 parent 7732b2b commit 17435f4
Showing 1 changed file with 57 additions and 4 deletions.
61 changes: 57 additions & 4 deletions custom_components/moonraker/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@
SensorEntityDescription,
SensorStateClass,
)
from homeassistant.const import PERCENTAGE, UnitOfLength, UnitOfTemperature, UnitOfTime
from homeassistant.const import PERCENTAGE, UnitOfLength, UnitOfPressure, UnitOfTemperature, UnitOfTime
from homeassistant.core import callback

from .const import DOMAIN, METHODS, PRINTERSTATES, PRINTSTATES
from .const import OBJ, DOMAIN, METHODS, PRINTERSTATES, PRINTSTATES
from .entity import BaseMoonrakerEntity

_LOGGER = logging.getLogger(__name__)
Expand Down Expand Up @@ -131,7 +131,7 @@ class MoonrakerSensorDescription(SensorEntityDescription):
),
MoonrakerSensorDescription(
key="print_projected_total_duration",
name="print Projected Total Duration",
name="Print Projected Total Duration",
value_fn=lambda sensor: sensor.empty_result_when_not_printing(
round(
sensor.coordinator.data["status"]["print_stats"]["print_duration"]
Expand Down Expand Up @@ -370,7 +370,7 @@ async def async_setup_optional_sensors(coordinator, entry, async_add_entities):
desc = MoonrakerSensorDescription(
key=f"{split_obj[0]}_{split_obj[1]}",
status_key=obj,
name=split_obj[1].replace("_", " ").title(),
name=split_obj[1].replace("_", " ").title() + " Temperature",
value_fn=lambda sensor: sensor.coordinator.data["status"][
sensor.status_key
]["temperature"],
Expand All @@ -380,6 +380,59 @@ async def async_setup_optional_sensors(coordinator, entry, async_add_entities):
state_class=SensorStateClass.MEASUREMENT,
)
sensors.append(desc)

if split_obj[0] == "bme280":
query_obj = {OBJ: {obj: None}}
result = await coordinator.async_fetch_data(
METHODS.PRINTER_OBJECTS_QUERY, query_obj, quiet=True
)

if "pressure" in result["status"][obj].keys():
desc = MoonrakerSensorDescription(
key=f"{split_obj[0]}_{split_obj[1]}_pressure",
status_key=obj,
name=split_obj[1].replace("_", " ").title() + " Pressure",
value_fn=lambda sensor: sensor.coordinator.data["status"][
sensor.status_key
]["pressure"],
subscriptions=[(obj, "pressure")],
icon="mdi:gauge",
unit=UnitOfPressure.HPA,
state_class=SensorStateClass.MEASUREMENT,
)
sensors.append(desc)

if "humidity" in result["status"][obj].keys():
desc = MoonrakerSensorDescription(
key=f"{split_obj[0]}_{split_obj[1]}_humidity",
status_key=obj,
name=split_obj[1].replace("_", " ").title() + " Humidity",
value_fn=lambda sensor: sensor.coordinator.data["status"][
sensor.status_key
]["humidity"],
subscriptions=[(obj, "humidity")],
icon="mdi:water-percent",
unit=PERCENTAGE,
state_class=SensorStateClass.MEASUREMENT,
)
sensors.append(desc)

if "gas" in result["status"][obj].keys():
desc = MoonrakerSensorDescription(
key=f"{split_obj[0]}_{split_obj[1]}_gas",
status_key=obj,
name=split_obj[1].replace("_", " ").title() + " Gas",
value_fn=lambda sensor: sensor.coordinator.data["status"][
sensor.status_key
]["gas"],
subscriptions=[(obj, "gas")],
icon="mdi:eye",
unit=None,
state_class=SensorStateClass.MEASUREMENT,
)
sensors.append(desc)


elif split_obj[0] in fan_keys:
desc = MoonrakerSensorDescription(
key=f"{split_obj[0]}_{split_obj[1]}",
Expand Down

0 comments on commit 17435f4

Please sign in to comment.