1 """Support for monitoring Repetier Server Sensors."""
3 from __future__
import annotations
15 from .
import REPETIER_API, SENSOR_TYPES, UPDATE_SIGNAL, RepetierSensorEntityDescription
17 _LOGGER = logging.getLogger(__name__)
23 add_entities: AddEntitiesCallback,
24 discovery_info: DiscoveryInfoType |
None =
None,
26 """Set up the available Repetier Server sensors."""
27 if discovery_info
is None:
31 "bed_temperature": RepetierTempSensor,
32 "extruder_temperature": RepetierTempSensor,
33 "chamber_temperature": RepetierTempSensor,
34 "current_state": RepetierSensor,
35 "current_job": RepetierJobSensor,
36 "job_end": RepetierJobEndSensor,
37 "job_start": RepetierJobStartSensor,
40 sensors_info: list[dict] = discovery_info[
"sensors"]
42 for info
in sensors_info:
43 printer_name = info[
"printer_name"]
44 api = hass.data[REPETIER_API][printer_name]
45 printer_id = info[
"printer_id"]
46 sensor_type = info[
"sensor_type"]
47 temp_id = info[
"temp_id"]
48 description = SENSOR_TYPES[sensor_type]
49 name_suffix =
"" if description.name
is UNDEFINED
else description.name
50 name = f
"{info['name']}{name_suffix}"
51 if temp_id
is not None:
52 _LOGGER.debug(
"%s Temp_id: %s", sensor_type, temp_id)
53 name = f
"{name}{temp_id}"
54 sensor_class = sensor_map[sensor_type]
55 entity = sensor_class(api, temp_id, name, printer_id, description)
56 entities.append(entity)
62 """Class to create and populate a Repetier Sensor."""
64 entity_description: RepetierSensorEntityDescription
65 _attr_should_poll =
False
73 description: RepetierSensorEntityDescription,
75 """Init new sensor."""
78 self._attributes: dict = {}
88 """Return sensor attributes."""
89 return self._attributes
93 """Return sensor state."""
98 """Get new data and update state."""
102 """Connect update callbacks."""
108 """Return new data from the api cache."""
112 _LOGGER.debug(
"Data not found for %s and %s", sensor_type, self.
_temp_id_temp_id)
119 """Update the sensor."""
120 if (data := self.
_get_data_get_data())
is None:
122 state = data.pop(
"state")
123 _LOGGER.debug(
"Printer %s State %s", self.
namename, state)
124 self._attributes.
update(data)
129 """Represent a Repetier temp sensor."""
133 """Return sensor state."""
139 """Update the sensor."""
140 if (data := self.
_get_data_get_data())
is None:
142 state = data.pop(
"state")
143 temp_set = data[
"temp_set"]
144 _LOGGER.debug(
"Printer %s Setpoint: %s, Temp: %s", self.
namename, temp_set, state)
145 self._attributes.
update(data)
150 """Represent a Repetier job sensor."""
154 """Return sensor state."""
155 if self.
_state_state
is None:
157 return round(self.
_state_state, 2)
161 """Class to create and populate a Repetier Job End timestamp Sensor."""
163 _attr_device_class = SensorDeviceClass.TIMESTAMP
166 """Update the sensor."""
167 if (data := self.
_get_data_get_data())
is None:
169 job_name = data[
"job_name"]
170 start = data[
"start"]
171 print_time = data[
"print_time"]
172 from_start = data[
"from_start"]
173 time_end = start + round(print_time, 0)
175 remaining = print_time - from_start
176 remaining_secs =
int(round(remaining, 0))
178 "Job %s remaining %s",
180 time.strftime(
"%H:%M:%S", time.gmtime(remaining_secs)),
185 """Class to create and populate a Repetier Job Start timestamp Sensor."""
187 _attr_device_class = SensorDeviceClass.TIMESTAMP
190 """Update the sensor."""
191 if (data := self.
_get_data_get_data())
is None:
193 job_name = data[
"job_name"]
194 start = data[
"start"]
195 from_start = data[
"from_start"]
197 elapsed_secs =
int(round(from_start, 0))
201 time.strftime(
"%H:%M:%S", time.gmtime(elapsed_secs)),
None __init__(self, api, temp_id, name, printer_id, RepetierSensorEntityDescription description)
def extra_state_attributes(self)
def update_callback(self)
def async_added_to_hass(self)
None async_schedule_update_ha_state(self, bool force_refresh=False)
None async_on_remove(self, CALLBACK_TYPE func)
str|UndefinedType|None name(self)
def add_entities(account, async_add_entities, tracked)
None setup_platform(HomeAssistant hass, ConfigType config, AddEntitiesCallback add_entities, DiscoveryInfoType|None discovery_info=None)
Callable[[], None] async_dispatcher_connect(HomeAssistant hass, str signal, Callable[..., Any] target)