diff --git a/CHANGELOG.md b/CHANGELOG.md index 562291c..3ab7fa9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,10 @@ # Changelog +## 2.0.0b8 + +- Fix async dispatcher send +- Change all sensors with date device class to timestamp [#127](https://github.com/elad-bar/ha-hpprinter/issues/127) + ## 2.0.0b7 - Safe code blocks (try / catch / log) for generating entities diff --git a/custom_components/hpprinter/managers/rest_api.py b/custom_components/hpprinter/managers/rest_api.py index 976f442..eb63468 100644 --- a/custom_components/hpprinter/managers/rest_api.py +++ b/custom_components/hpprinter/managers/rest_api.py @@ -13,7 +13,7 @@ MAXIMUM_CONNECTIONS, MAXIMUM_CONNECTIONS_PER_HOST, ) -from homeassistant.helpers.dispatcher import async_dispatcher_send +from homeassistant.helpers.dispatcher import dispatcher_send from homeassistant.util import slugify, ssl from homeassistant.util.ssl import SSLCipherList @@ -403,7 +403,7 @@ def device_data_changed(self, device_key: str): if device_key not in self._device_dispatched: self._device_dispatched.append(device_key) - async_dispatcher_send( + dispatcher_send( self._hass, SIGNAL_HA_DEVICE_DISCOVERED, self._config_manager.entry_id, diff --git a/custom_components/hpprinter/manifest.json b/custom_components/hpprinter/manifest.json index 8b71443..ba1a6ab 100644 --- a/custom_components/hpprinter/manifest.json +++ b/custom_components/hpprinter/manifest.json @@ -9,5 +9,5 @@ "iot_class": "local_polling", "issue_tracker": "https://github.com/elad-bar/ha-hpprinter/issues", "requirements": ["xmltodict~=0.13.0", "flatten_json", "defusedxml"], - "version": "2.0.0b7" + "version": "2.0.0b8" } diff --git a/custom_components/hpprinter/parameters/data_points.json b/custom_components/hpprinter/parameters/data_points.json index 2500664..e87da34 100644 --- a/custom_components/hpprinter/parameters/data_points.json +++ b/custom_components/hpprinter/parameters/data_points.json @@ -26,7 +26,7 @@ "manufacture_at": { "path": "Manufacturer.Date", "platform": "sensor", - "device_class": "date" + "device_class": "timestamp" } } }, @@ -62,7 +62,7 @@ "installation_date": { "path": "Installation.Date", "platform": "sensor", - "device_class": "date" + "device_class": "timestamp" }, "capacity_max_capacity": { "path": "Capacity.MaxCapacity" @@ -84,7 +84,7 @@ "manufacture_at": { "path": "Manufacturer.Date", "platform": "sensor", - "device_class": "date", + "device_class": "timestamp", "exclude": { "consumable_type_enum": "printhead" } @@ -98,7 +98,7 @@ "warranty_expiration_date": { "path": "Warranty.ExpirationDate", "platform": "sensor", - "device_class": "date", + "device_class": "timestamp", "exclude": { "consumable_type_enum": "printhead" } diff --git a/custom_components/hpprinter/sensor.py b/custom_components/hpprinter/sensor.py index 3445830..6be494d 100644 --- a/custom_components/hpprinter/sensor.py +++ b/custom_components/hpprinter/sensor.py @@ -47,14 +47,20 @@ def __init__( def _set_value(self): state = self.get_value() - if self.native_unit_of_measurement in [PERCENTAGE]: - state = float(state) + if state is not None: + if self.native_unit_of_measurement in [PERCENTAGE]: + state = float(state) - elif self.native_unit_of_measurement in NUMERIC_UNITS_OF_MEASUREMENT: - state = int(state) + elif self.native_unit_of_measurement in NUMERIC_UNITS_OF_MEASUREMENT: + state = int(state) - if self.device_class == SensorDeviceClass.DATE: - state = datetime.fromisoformat(state) + if self.device_class == SensorDeviceClass.DATE: + state = datetime.fromisoformat(state) + + elif self.device_class == SensorDeviceClass.TIMESTAMP: + tz = datetime.now().astimezone().tzinfo + ts = datetime.fromisoformat(state).timestamp() + state = datetime.fromtimestamp(ts, tz=tz) self._attr_native_value = state