From 04247cb1efbf963a08066e5277a5f4c1b04487f9 Mon Sep 17 00:00:00 2001 From: Elad Bar Date: Sun, 12 Dec 2021 18:08:39 +0200 Subject: [PATCH] Added support for long term statistics --- CHANGELOG.md | 4 ++++ custom_components/hpprinter/helpers/const.py | 7 ++++++- custom_components/hpprinter/managers/entity_manager.py | 3 +++ custom_components/hpprinter/manifest.json | 2 +- custom_components/hpprinter/models/entity_data.py | 5 ++++- custom_components/hpprinter/sensor.py | 5 +++++ 6 files changed, 23 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4f01dab..03445c5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Changelog +## 1.0.5 + +- Added support for long term statistics + ## 1.0.4 - Upgraded code to support breaking changes of HA v2012.12.0 diff --git a/custom_components/hpprinter/helpers/const.py b/custom_components/hpprinter/helpers/const.py index 47bf263..c82f36d 100644 --- a/custom_components/hpprinter/helpers/const.py +++ b/custom_components/hpprinter/helpers/const.py @@ -2,7 +2,11 @@ DOMAIN as DOMAIN_BINARY_SENSOR, BinarySensorDeviceClass, ) -from homeassistant.components.sensor import DOMAIN as DOMAIN_SENSOR, SensorDeviceClass +from homeassistant.components.sensor import ( + DOMAIN as DOMAIN_SENSOR, + SensorDeviceClass, + SensorStateClass, +) MANUFACTURER = "HP" DEFAULT_NAME = "HP Printer" @@ -43,6 +47,7 @@ ENTITY_UNIQUE_ID = "unique-id" ENTITY_BINARY_SENSOR_DEVICE_CLASS = "binary-sensor-device-class" ENTITY_SENSOR_DEVICE_CLASS = "sensor-device-class" +ENTITY_SENSOR_STATE_CLASS = "sensor-state-class" ENTITY_STATUS = "entity-status" ENTITY_STATUS_EMPTY = None diff --git a/custom_components/hpprinter/managers/entity_manager.py b/custom_components/hpprinter/managers/entity_manager.py index fef73b2..f945e93 100644 --- a/custom_components/hpprinter/managers/entity_manager.py +++ b/custom_components/hpprinter/managers/entity_manager.py @@ -311,6 +311,7 @@ def create_printer_sensor(self): entity.icon = PAGES_ICON entity.device_name = device_name entity.state = state + entity.sensor_state_class = SensorStateClass.TOTAL_INCREASING self.set_entity(DOMAIN_SENSOR, entity_name, entity) @@ -339,6 +340,7 @@ def create_scanner_sensor(self): entity.icon = SCANNER_ICON entity.device_name = device_name entity.state = state + entity.sensor_state_class = SensorStateClass.TOTAL_INCREASING self.set_entity(DOMAIN_SENSOR, entity_name, entity) @@ -364,6 +366,7 @@ def create_cartridge_sensor(self, cartridge, key): entity.icon = INK_ICON entity.device_name = device_name entity.state = state + entity.sensor_state_class = SensorStateClass.MEASUREMENT self.set_entity(DOMAIN_SENSOR, entity_name, entity) diff --git a/custom_components/hpprinter/manifest.json b/custom_components/hpprinter/manifest.json index 68fdbba..bf2c462 100644 --- a/custom_components/hpprinter/manifest.json +++ b/custom_components/hpprinter/manifest.json @@ -7,6 +7,6 @@ "after_dependencies": [ "logger" ], "requirements": ["xmltodict==0.12.0"], "config_flow": true, - "version": "1.0.3", + "version": "1.0.5", "iot_class": "local_polling" } diff --git a/custom_components/hpprinter/models/entity_data.py b/custom_components/hpprinter/models/entity_data.py index db2b398..242edab 100644 --- a/custom_components/hpprinter/models/entity_data.py +++ b/custom_components/hpprinter/models/entity_data.py @@ -14,6 +14,7 @@ class EntityData: disabled: bool binary_sensor_device_class: Optional[BinarySensorDeviceClass] sensor_device_class: Optional[SensorDeviceClass] + sensor_state_class: Optional[SensorStateClass] def __init__(self): self.unique_id = "" @@ -26,6 +27,7 @@ def __init__(self): self.disabled = False self.binary_sensor_device_class = None self.sensor_device_class = None + self.sensor_state_class = None def __repr__(self): obj = { @@ -38,7 +40,8 @@ def __repr__(self): ENTITY_UNIQUE_ID: self.unique_id, ENTITY_DISABLED: self.disabled, ENTITY_BINARY_SENSOR_DEVICE_CLASS: self.binary_sensor_device_class, - ENTITY_SENSOR_DEVICE_CLASS: self.sensor_device_class + ENTITY_SENSOR_DEVICE_CLASS: self.sensor_device_class, + ENTITY_SENSOR_STATE_CLASS: self.sensor_state_class } to_string = f"{obj}" diff --git a/custom_components/hpprinter/sensor.py b/custom_components/hpprinter/sensor.py index 487e593..c11569b 100644 --- a/custom_components/hpprinter/sensor.py +++ b/custom_components/hpprinter/sensor.py @@ -52,6 +52,11 @@ def device_class(self) -> SensorDeviceClass | str | None: """Return the class of this sensor.""" return self.entity.sensor_device_class + @property + def state_class(self) -> SensorStateClass | str | None: + """Return the class of this sensor.""" + return self.entity.sensor_state_class + async def async_added_to_hass_local(self): _LOGGER.info(f"Added new {self.name}")