Skip to content

Commit

Permalink
Fix async dispatcher send, Change all sensors with date device class …
Browse files Browse the repository at this point in the history
…to timestamp #127
  • Loading branch information
elad-bar committed May 17, 2024
1 parent c3d63b7 commit d3c1f7b
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 13 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
4 changes: 2 additions & 2 deletions custom_components/hpprinter/managers/rest_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion custom_components/hpprinter/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
8 changes: 4 additions & 4 deletions custom_components/hpprinter/parameters/data_points.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"manufacture_at": {
"path": "Manufacturer.Date",
"platform": "sensor",
"device_class": "date"
"device_class": "timestamp"
}
}
},
Expand Down Expand Up @@ -62,7 +62,7 @@
"installation_date": {
"path": "Installation.Date",
"platform": "sensor",
"device_class": "date"
"device_class": "timestamp"
},
"capacity_max_capacity": {
"path": "Capacity.MaxCapacity"
Expand All @@ -84,7 +84,7 @@
"manufacture_at": {
"path": "Manufacturer.Date",
"platform": "sensor",
"device_class": "date",
"device_class": "timestamp",
"exclude": {
"consumable_type_enum": "printhead"
}
Expand All @@ -98,7 +98,7 @@
"warranty_expiration_date": {
"path": "Warranty.ExpirationDate",
"platform": "sensor",
"device_class": "date",
"device_class": "timestamp",
"exclude": {
"consumable_type_enum": "printhead"
}
Expand Down
18 changes: 12 additions & 6 deletions custom_components/hpprinter/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit d3c1f7b

Please sign in to comment.