mirror of
https://github.com/home-assistant/core.git
synced 2025-07-09 22:37:11 +00:00
Use EntityDescription - repetier (#55926)
This commit is contained in:
parent
69d6d5ffce
commit
32b8be5a6e
@ -1,10 +1,14 @@
|
||||
"""Support for Repetier-Server sensors."""
|
||||
from __future__ import annotations
|
||||
|
||||
from dataclasses import dataclass
|
||||
from datetime import timedelta
|
||||
import logging
|
||||
|
||||
import pyrepetier
|
||||
import voluptuous as vol
|
||||
|
||||
from homeassistant.components.sensor import SensorEntityDescription
|
||||
from homeassistant.const import (
|
||||
CONF_API_KEY,
|
||||
CONF_HOST,
|
||||
@ -109,33 +113,66 @@ def has_all_unique_names(value):
|
||||
return value
|
||||
|
||||
|
||||
SENSOR_TYPES = {
|
||||
# Type, Unit, Icon, post
|
||||
"bed_temperature": [
|
||||
"temperature",
|
||||
TEMP_CELSIUS,
|
||||
None,
|
||||
"_bed_",
|
||||
DEVICE_CLASS_TEMPERATURE,
|
||||
],
|
||||
"extruder_temperature": [
|
||||
"temperature",
|
||||
TEMP_CELSIUS,
|
||||
None,
|
||||
"_extruder_",
|
||||
DEVICE_CLASS_TEMPERATURE,
|
||||
],
|
||||
"chamber_temperature": [
|
||||
"temperature",
|
||||
TEMP_CELSIUS,
|
||||
None,
|
||||
"_chamber_",
|
||||
DEVICE_CLASS_TEMPERATURE,
|
||||
],
|
||||
"current_state": ["state", None, "mdi:printer-3d", "", None],
|
||||
"current_job": ["progress", PERCENTAGE, "mdi:file-percent", "_current_job", None],
|
||||
"job_end": ["progress", None, "mdi:clock-end", "_job_end", None],
|
||||
"job_start": ["progress", None, "mdi:clock-start", "_job_start", None],
|
||||
@dataclass
|
||||
class RepetierRequiredKeysMixin:
|
||||
"""Mixin for required keys."""
|
||||
|
||||
type: str
|
||||
|
||||
|
||||
@dataclass
|
||||
class RepetierSensorEntityDescription(
|
||||
SensorEntityDescription, RepetierRequiredKeysMixin
|
||||
):
|
||||
"""Describes Repetier sensor entity."""
|
||||
|
||||
|
||||
SENSOR_TYPES: dict[str, RepetierSensorEntityDescription] = {
|
||||
"bed_temperature": RepetierSensorEntityDescription(
|
||||
key="bed_temperature",
|
||||
type="temperature",
|
||||
native_unit_of_measurement=TEMP_CELSIUS,
|
||||
name="_bed_",
|
||||
device_class=DEVICE_CLASS_TEMPERATURE,
|
||||
),
|
||||
"extruder_temperature": RepetierSensorEntityDescription(
|
||||
key="extruder_temperature",
|
||||
type="temperature",
|
||||
native_unit_of_measurement=TEMP_CELSIUS,
|
||||
name="_extruder_",
|
||||
device_class=DEVICE_CLASS_TEMPERATURE,
|
||||
),
|
||||
"chamber_temperature": RepetierSensorEntityDescription(
|
||||
key="chamber_temperature",
|
||||
type="temperature",
|
||||
native_unit_of_measurement=TEMP_CELSIUS,
|
||||
name="_chamber_",
|
||||
device_class=DEVICE_CLASS_TEMPERATURE,
|
||||
),
|
||||
"current_state": RepetierSensorEntityDescription(
|
||||
key="current_state",
|
||||
type="state",
|
||||
icon="mdi:printer-3d",
|
||||
),
|
||||
"current_job": RepetierSensorEntityDescription(
|
||||
key="current_job",
|
||||
type="progress",
|
||||
native_unit_of_measurement=PERCENTAGE,
|
||||
icon="mdi:file-percent",
|
||||
name="_current_job",
|
||||
),
|
||||
"job_end": RepetierSensorEntityDescription(
|
||||
key="job_end",
|
||||
type="progress",
|
||||
icon="mdi:clock-end",
|
||||
name="_job_end",
|
||||
),
|
||||
"job_start": RepetierSensorEntityDescription(
|
||||
key="job_start",
|
||||
type="progress",
|
||||
icon="mdi:clock-start",
|
||||
name="_job_start",
|
||||
),
|
||||
}
|
||||
|
||||
SENSOR_SCHEMA = vol.Schema(
|
||||
|
@ -8,7 +8,7 @@ from homeassistant.const import DEVICE_CLASS_TIMESTAMP
|
||||
from homeassistant.core import callback
|
||||
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
||||
|
||||
from . import REPETIER_API, SENSOR_TYPES, UPDATE_SIGNAL
|
||||
from . import REPETIER_API, SENSOR_TYPES, UPDATE_SIGNAL, RepetierSensorEntityDescription
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
@ -35,12 +35,13 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
|
||||
printer_id = info["printer_id"]
|
||||
sensor_type = info["sensor_type"]
|
||||
temp_id = info["temp_id"]
|
||||
name = f"{info['name']}{SENSOR_TYPES[sensor_type][3]}"
|
||||
description = SENSOR_TYPES[sensor_type]
|
||||
name = f"{info['name']}{description.name or ''}"
|
||||
if temp_id is not None:
|
||||
_LOGGER.debug("%s Temp_id: %s", sensor_type, temp_id)
|
||||
name = f"{name}{temp_id}"
|
||||
sensor_class = sensor_map[sensor_type]
|
||||
entity = sensor_class(api, temp_id, name, printer_id, sensor_type)
|
||||
entity = sensor_class(api, temp_id, name, printer_id, description)
|
||||
entities.append(entity)
|
||||
|
||||
add_entities(entities, True)
|
||||
@ -49,48 +50,33 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
|
||||
class RepetierSensor(SensorEntity):
|
||||
"""Class to create and populate a Repetier Sensor."""
|
||||
|
||||
def __init__(self, api, temp_id, name, printer_id, sensor_type):
|
||||
"""Init new sensor."""
|
||||
self._api = api
|
||||
self._attributes = {}
|
||||
self._available = False
|
||||
self._temp_id = temp_id
|
||||
self._name = name
|
||||
self._printer_id = printer_id
|
||||
self._sensor_type = sensor_type
|
||||
self._state = None
|
||||
self._attr_device_class = SENSOR_TYPES[self._sensor_type][4]
|
||||
entity_description: RepetierSensorEntityDescription
|
||||
_attr_should_poll = False
|
||||
|
||||
@property
|
||||
def available(self) -> bool:
|
||||
"""Return True if entity is available."""
|
||||
return self._available
|
||||
def __init__(
|
||||
self,
|
||||
api,
|
||||
temp_id,
|
||||
name,
|
||||
printer_id,
|
||||
description: RepetierSensorEntityDescription,
|
||||
):
|
||||
"""Init new sensor."""
|
||||
self.entity_description = description
|
||||
self._api = api
|
||||
self._attributes: dict = {}
|
||||
self._temp_id = temp_id
|
||||
self._printer_id = printer_id
|
||||
self._state = None
|
||||
|
||||
self._attr_name = name
|
||||
self._attr_available = False
|
||||
|
||||
@property
|
||||
def extra_state_attributes(self):
|
||||
"""Return sensor attributes."""
|
||||
return self._attributes
|
||||
|
||||
@property
|
||||
def name(self):
|
||||
"""Return the name of the sensor."""
|
||||
return self._name
|
||||
|
||||
@property
|
||||
def native_unit_of_measurement(self):
|
||||
"""Return the unit of measurement of this entity, if any."""
|
||||
return SENSOR_TYPES[self._sensor_type][1]
|
||||
|
||||
@property
|
||||
def icon(self):
|
||||
"""Icon to use in the frontend."""
|
||||
return SENSOR_TYPES[self._sensor_type][2]
|
||||
|
||||
@property
|
||||
def should_poll(self):
|
||||
"""Return False as entity is updated from the component."""
|
||||
return False
|
||||
|
||||
@property
|
||||
def native_value(self):
|
||||
"""Return sensor state."""
|
||||
@ -109,14 +95,13 @@ class RepetierSensor(SensorEntity):
|
||||
|
||||
def _get_data(self):
|
||||
"""Return new data from the api cache."""
|
||||
data = self._api.get_data(self._printer_id, self._sensor_type, self._temp_id)
|
||||
sensor_type = self.entity_description.key
|
||||
data = self._api.get_data(self._printer_id, sensor_type, self._temp_id)
|
||||
if data is None:
|
||||
_LOGGER.debug(
|
||||
"Data not found for %s and %s", self._sensor_type, self._temp_id
|
||||
)
|
||||
self._available = False
|
||||
_LOGGER.debug("Data not found for %s and %s", sensor_type, self._temp_id)
|
||||
self._attr_available = False
|
||||
return None
|
||||
self._available = True
|
||||
self._attr_available = True
|
||||
return data
|
||||
|
||||
def update(self):
|
||||
@ -125,7 +110,7 @@ class RepetierSensor(SensorEntity):
|
||||
if data is None:
|
||||
return
|
||||
state = data.pop("state")
|
||||
_LOGGER.debug("Printer %s State %s", self._name, state)
|
||||
_LOGGER.debug("Printer %s State %s", self.name, state)
|
||||
self._attributes.update(data)
|
||||
self._state = state
|
||||
|
||||
@ -147,7 +132,7 @@ class RepetierTempSensor(RepetierSensor):
|
||||
return
|
||||
state = data.pop("state")
|
||||
temp_set = data["temp_set"]
|
||||
_LOGGER.debug("Printer %s Setpoint: %s, Temp: %s", self._name, temp_set, state)
|
||||
_LOGGER.debug("Printer %s Setpoint: %s, Temp: %s", self.name, temp_set, state)
|
||||
self._attributes.update(data)
|
||||
self._state = state
|
||||
|
||||
@ -166,10 +151,7 @@ class RepetierJobSensor(RepetierSensor):
|
||||
class RepetierJobEndSensor(RepetierSensor):
|
||||
"""Class to create and populate a Repetier Job End timestamp Sensor."""
|
||||
|
||||
@property
|
||||
def device_class(self):
|
||||
"""Return the device class."""
|
||||
return DEVICE_CLASS_TIMESTAMP
|
||||
_attr_device_class = DEVICE_CLASS_TIMESTAMP
|
||||
|
||||
def update(self):
|
||||
"""Update the sensor."""
|
||||
@ -194,10 +176,7 @@ class RepetierJobEndSensor(RepetierSensor):
|
||||
class RepetierJobStartSensor(RepetierSensor):
|
||||
"""Class to create and populate a Repetier Job Start timestamp Sensor."""
|
||||
|
||||
@property
|
||||
def device_class(self):
|
||||
"""Return the device class."""
|
||||
return DEVICE_CLASS_TIMESTAMP
|
||||
_attr_device_class = DEVICE_CLASS_TIMESTAMP
|
||||
|
||||
def update(self):
|
||||
"""Update the sensor."""
|
||||
|
Loading…
x
Reference in New Issue
Block a user