From 823ca7ee402f71f227d61eb559e6eef62dfda71c Mon Sep 17 00:00:00 2001 From: Marc Mueller <30130371+cdce8p@users.noreply.github.com> Date: Fri, 22 Oct 2021 21:41:06 +0200 Subject: [PATCH] Use attributes octoprint (#58241) --- .../components/octoprint/binary_sensor.py | 14 +---- homeassistant/components/octoprint/sensor.py | 63 +++++-------------- 2 files changed, 17 insertions(+), 60 deletions(-) diff --git a/homeassistant/components/octoprint/binary_sensor.py b/homeassistant/components/octoprint/binary_sensor.py index e7806999698..fe18af4f808 100644 --- a/homeassistant/components/octoprint/binary_sensor.py +++ b/homeassistant/components/octoprint/binary_sensor.py @@ -52,9 +52,9 @@ class OctoPrintBinarySensorBase(CoordinatorEntity, BinarySensorEntity): ) -> None: """Initialize a new OctoPrint sensor.""" super().__init__(coordinator) - self._name = f"Octoprint {sensor_type}" - self.sensor_type = sensor_type self._device_id = device_id + self._attr_name = f"Octoprint {sensor_type}" + self._attr_unique_id = f"{sensor_type}-{device_id}" @property def device_info(self): @@ -65,16 +65,6 @@ class OctoPrintBinarySensorBase(CoordinatorEntity, BinarySensorEntity): "name": "Octoprint", } - @property - def unique_id(self): - """Return a unique id.""" - return f"{self.sensor_type}-{self._device_id}" - - @property - def name(self): - """Return the name of the sensor.""" - return self._name - @property def is_on(self): """Return true if binary sensor is on.""" diff --git a/homeassistant/components/octoprint/sensor.py b/homeassistant/components/octoprint/sensor.py index 3eef0654870..3feff099297 100644 --- a/homeassistant/components/octoprint/sensor.py +++ b/homeassistant/components/octoprint/sensor.py @@ -75,9 +75,9 @@ class OctoPrintSensorBase(CoordinatorEntity, SensorEntity): ) -> None: """Initialize a new OctoPrint sensor.""" super().__init__(coordinator) - self._sensor_type = sensor_type - self._name = f"Octoprint {sensor_type}" self._device_id = device_id + self._attr_name = f"Octoprint {sensor_type}" + self._attr_unique_id = f"{sensor_type}-{device_id}" @property def device_info(self): @@ -88,20 +88,12 @@ class OctoPrintSensorBase(CoordinatorEntity, SensorEntity): "name": "Octoprint", } - @property - def unique_id(self): - """Return a unique id.""" - return f"{self._sensor_type}-{self._device_id}" - - @property - def name(self): - """Return the name of the sensor.""" - return self._name - class OctoPrintStatusSensor(OctoPrintSensorBase): """Representation of an OctoPrint sensor.""" + _attr_icon = "mdi:printer-3d" + def __init__(self, coordinator: DataUpdateCoordinator, device_id: str) -> None: """Initialize a new OctoPrint sensor.""" super().__init__(coordinator, "Current State", device_id) @@ -115,11 +107,6 @@ class OctoPrintStatusSensor(OctoPrintSensorBase): return printer.state.text - @property - def icon(self): - """Icon to use in the frontend.""" - return "mdi:printer-3d" - @property def available(self) -> bool: """Return if entity is available.""" @@ -129,6 +116,9 @@ class OctoPrintStatusSensor(OctoPrintSensorBase): class OctoPrintJobPercentageSensor(OctoPrintSensorBase): """Representation of an OctoPrint sensor.""" + _attr_native_unit_of_measurement = PERCENTAGE + _attr_icon = "mdi:file-percent" + def __init__(self, coordinator: DataUpdateCoordinator, device_id: str) -> None: """Initialize a new OctoPrint sensor.""" super().__init__(coordinator, "Job Percentage", device_id) @@ -146,20 +136,12 @@ class OctoPrintJobPercentageSensor(OctoPrintSensorBase): return round(state, 2) - @property - def native_unit_of_measurement(self): - """Return the unit of measurement of this entity, if any.""" - return PERCENTAGE - - @property - def icon(self): - """Icon to use in the frontend.""" - return "mdi:file-percent" - class OctoPrintEstimatedFinishTimeSensor(OctoPrintSensorBase): """Representation of an OctoPrint sensor.""" + _attr_device_class = DEVICE_CLASS_TIMESTAMP + def __init__(self, coordinator: DataUpdateCoordinator, device_id: str) -> None: """Initialize a new OctoPrint sensor.""" super().__init__(coordinator, "Estimated Finish Time", device_id) @@ -175,15 +157,12 @@ class OctoPrintEstimatedFinishTimeSensor(OctoPrintSensorBase): return (read_time + timedelta(seconds=job.progress.print_time_left)).isoformat() - @property - def device_class(self): - """Return the device class of the sensor.""" - return DEVICE_CLASS_TIMESTAMP - class OctoPrintStartTimeSensor(OctoPrintSensorBase): """Representation of an OctoPrint sensor.""" + _attr_device_class = DEVICE_CLASS_TIMESTAMP + def __init__(self, coordinator: DataUpdateCoordinator, device_id: str) -> None: """Initialize a new OctoPrint sensor.""" super().__init__(coordinator, "Start Time", device_id) @@ -200,15 +179,14 @@ class OctoPrintStartTimeSensor(OctoPrintSensorBase): return (read_time - timedelta(seconds=job.progress.print_time)).isoformat() - @property - def device_class(self): - """Return the device class of the sensor.""" - return DEVICE_CLASS_TIMESTAMP - class OctoPrintTemperatureSensor(OctoPrintSensorBase): """Representation of an OctoPrint sensor.""" + _attr_native_unit_of_measurement = TEMP_CELSIUS + _attr_device_class = DEVICE_CLASS_TEMPERATURE + _attr_state_class = STATE_CLASS_MEASUREMENT + def __init__( self, coordinator: DataUpdateCoordinator, @@ -220,17 +198,6 @@ class OctoPrintTemperatureSensor(OctoPrintSensorBase): super().__init__(coordinator, f"{temp_type} {tool} temp", device_id) self._temp_type = temp_type self._api_tool = tool - self._attr_state_class = STATE_CLASS_MEASUREMENT - - @property - def unit_of_measurement(self): - """Return the unit of measurement of this entity, if any.""" - return TEMP_CELSIUS - - @property - def device_class(self): - """Return the device class of this entity.""" - return DEVICE_CLASS_TEMPERATURE @property def native_value(self):