printer_ha_sensor.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. from datetime import datetime
  2. from sqlalchemy import Boolean, DateTime, Float, ForeignKey, Integer, String, func
  3. from sqlalchemy.orm import Mapped, mapped_column, relationship
  4. from backend.app.core.database import Base
  5. class PrinterHASensor(Base):
  6. """A read-only Home Assistant entity bound to a printer (#1148, #448).
  7. Deliberately *not* a ``SmartPlug`` row with a wider entity pattern. A plug
  8. carries auto-on/auto-off, schedules, power alerts, energy snapshots and
  9. ``controls_printer_power``; none of that means anything for a door contact,
  10. and ``get_smart_plug_by_printer`` would hand the card's power button a
  11. sensor to switch. Sensors get their own table and their own read-only
  12. routes instead.
  13. Not to be confused with ``PrinterSensorHistory``, which stores the
  14. printer's *own* heater readings.
  15. """
  16. __tablename__ = "printer_ha_sensors"
  17. id: Mapped[int] = mapped_column(primary_key=True)
  18. printer_id: Mapped[int] = mapped_column(ForeignKey("printers.id", ondelete="CASCADE"), index=True)
  19. name: Mapped[str] = mapped_column(String(100))
  20. entity_id: Mapped[str] = mapped_column(String(255))
  21. # "binary" for binary_sensor.*, "numeric" for sensor.*. Decides how the
  22. # state is rendered and which alert fields apply.
  23. kind: Mapped[str] = mapped_column(String(16), default="binary")
  24. # HA's own device_class, snapshotted when the entity is bound. Drives the
  25. # on/off wording (door -> Open/Closed, motion -> Detected/Clear) and the
  26. # icon, so the card doesn't have to say "On" for an open door.
  27. device_class: Mapped[str | None] = mapped_column(String(32), nullable=True)
  28. # Numeric only: "°C", "%", "ppm", ... shown next to the value.
  29. unit: Mapped[str | None] = mapped_column(String(16), nullable=True)
  30. # What counts as needing attention. One notion, three consumers: the pill
  31. # colour on the card, the notification, and the print interlock.
  32. # Binary sensors use alert_state ("on"/"off"/None), numeric ones the
  33. # thresholds. All None means "just show the value".
  34. alert_state: Mapped[str | None] = mapped_column(String(8), nullable=True)
  35. alert_above: Mapped[float | None] = mapped_column(Float, nullable=True)
  36. alert_below: Mapped[float | None] = mapped_column(Float, nullable=True)
  37. # Hold queued prints for this printer while the sensor is in its alert
  38. # state — the enclosure-door case this feature was asked for. Opt-in, and
  39. # only ever a *hold*: the item stays pending with a waiting_reason and
  40. # dispatches by itself once the door closes.
  41. block_print: Mapped[bool] = mapped_column(Boolean, default=False, server_default="0")
  42. notify_on_alert: Mapped[bool] = mapped_column(Boolean, default=False, server_default="0")
  43. show_on_printer_card: Mapped[bool] = mapped_column(Boolean, default=True, server_default="1")
  44. sort_order: Mapped[int] = mapped_column(Integer, default=0, server_default="0")
  45. # Last poll result. Persisted so a restart doesn't blank the card until the
  46. # first poll lands, and so notifications only fire on a real transition.
  47. last_state: Mapped[str | None] = mapped_column(String(64), nullable=True)
  48. last_changed: Mapped[datetime | None] = mapped_column(DateTime, nullable=True)
  49. last_checked: Mapped[datetime | None] = mapped_column(DateTime, nullable=True)
  50. created_at: Mapped[datetime] = mapped_column(DateTime, server_default=func.now())
  51. updated_at: Mapped[datetime] = mapped_column(DateTime, server_default=func.now(), onupdate=func.now())
  52. printer: Mapped["Printer"] = relationship(back_populates="ha_sensors")
  53. from backend.app.models.printer import Printer # noqa: E402