Skip to content

Commit

Permalink
Fix time format error
Browse files Browse the repository at this point in the history
  • Loading branch information
Ernst79 committed Mar 17, 2022
1 parent 59bea69 commit 7a9ed1c
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 22 deletions.
6 changes: 3 additions & 3 deletions custom_components/ble_monitor/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
from homeassistant.helpers.entity_registry import (
async_entries_for_device,
)
import homeassistant.util.dt as dt_util
from homeassistant.util import dt

from .ble_parser import BleParser
from .const import (
Expand Down Expand Up @@ -571,7 +571,7 @@ def __init__(self, config, dataqueue):
self.tracker_whitelist = []
self.report_unknown = False
self.report_unknown_whitelist = []
self.last_bt_reset = dt_util.now()
self.last_bt_reset = dt.now()
if self.config[CONF_REPORT_UNKNOWN]:
if self.config[CONF_REPORT_UNKNOWN] != "Off":
self.report_unknown = self.config[CONF_REPORT_UNKNOWN]
Expand Down Expand Up @@ -752,7 +752,7 @@ def run(self):
if (interface_is_ok[hci] is False) and (self.config[CONF_BT_AUTO_RESTART] is True):
interfaces_to_reset.append(hci)
if interfaces_to_reset:
ts_now = dt_util.now()
ts_now = dt.now()
if (ts_now - self.last_bt_reset).seconds > 60:
for iface in interfaces_to_reset:
_LOGGER.error(
Expand Down
16 changes: 8 additions & 8 deletions custom_components/ble_monitor/binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
)
from homeassistant.helpers.event import async_call_later
from homeassistant.helpers.restore_state import RestoreEntity
import homeassistant.util.dt as dt_util
from homeassistant.util import dt

from .helper import (
identifier_normalize,
Expand Down Expand Up @@ -142,7 +142,7 @@ async def async_add_binary_sensor(key, sensortype, firmware, manufacturer=None,
batt = {} # batteries
mibeacon_cnt = 0
hpriority = []
ts_last = dt_util.now()
ts_last = dt.now()
ts_now = ts_last
data = {}
await asyncio.sleep(0)
Expand Down Expand Up @@ -235,7 +235,7 @@ async def async_add_binary_sensor(key, sensortype, firmware, manufacturer=None,
):
hpriority.append(entity)
data = None
ts_now = dt_util.now()
ts_now = dt.now()
if ts_now - ts_last < timedelta(seconds=self.period):
continue
ts_last = ts_now
Expand Down Expand Up @@ -422,7 +422,7 @@ def collect(self, data, batt_attr=None):
self._extra_state_attributes[ATTR_BATTERY_LEVEL] = batt_attr
if "motion timer" in data:
if data["motion timer"] == 1:
self._extra_state_attributes["last_motion"] = dt_util.now()
self._extra_state_attributes["last_motion"] = dt.now()
# dirty hack for kettle status
if self._device_type in KETTLES:
if self._newstate == 0:
Expand Down Expand Up @@ -483,7 +483,7 @@ def __init__(self, config, key, devtype, firmware, description, manufacturer=Non
def reset_state(self, event=None):
"""Reset state of the sensor."""
# check if the latest update of the timer is longer than the set timer value
if dt_util.now() - self._start_timer >= timedelta(seconds=self._reset_timer):
if dt.now() - self._start_timer >= timedelta(seconds=self._reset_timer):
self._state = False
self.schedule_update_ha_state(False)

Expand All @@ -493,15 +493,15 @@ async def async_update(self):
if self._reset_timer > 0:
try:
# if there is a last_motion attribute, check the timer
now = dt_util.now()
self._start_timer = self._extra_state_attributes["last_motion"]
now = dt.now()
self._start_timer = dt.parse_datetime(self._extra_state_attributes["last_motion"])

if now - self._start_timer >= timedelta(seconds=self._reset_timer):
self._state = False
else:
self._state = True
async_call_later(self.hass, self._reset_timer, self.reset_state)
except KeyError:
except (KeyError, ValueError):
self._state = self._newstate
else:
self._state = self._newstate
14 changes: 7 additions & 7 deletions custom_components/ble_monitor/device_tracker.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
from homeassistant.helpers.event import async_call_later
from homeassistant.helpers import device_registry
from homeassistant.helpers.restore_state import RestoreEntity
import homeassistant.util.dt as dt_util
from homeassistant.util import dt

from .helper import (
identifier_normalize,
Expand Down Expand Up @@ -107,7 +107,7 @@ async def async_add_device_tracker(key):
trackers_by_key = {}
trackers = []
adv_cnt = 0
ts_last = dt_util.now()
ts_last = dt.now()
ts_now = ts_last
data = None
await asyncio.sleep(0)
Expand Down Expand Up @@ -163,7 +163,7 @@ async def async_add_device_tracker(key):
except AttributeError:
continue
data = None
ts_now = dt_util.now()
ts_now = dt.now()
if ts_now - ts_last < timedelta(seconds=self.period):
continue
ts_last = ts_now
Expand Down Expand Up @@ -212,8 +212,8 @@ async def async_added_to_hass(self):
self.ready_for_update = True
return
if "last_seen" in old_state.attributes:
self._last_seen = dt_util.parse_datetime(old_state.attributes["last_seen"])
self._extra_state_attributes["last_seen"] = dt_util.parse_datetime(
self._last_seen = dt.parse_datetime(old_state.attributes["last_seen"])
self._extra_state_attributes["last_seen"] = dt.parse_datetime(
old_state.attributes["last_seen"]
)

Expand All @@ -238,7 +238,7 @@ def is_beacon(self):
@property
def is_connected(self):
"""Return the connection state of the device."""
return self._last_seen and (dt_util.now() - self._last_seen) < timedelta(
return self._last_seen and (dt.now() - self._last_seen) < timedelta(
seconds=self._consider_home
)

Expand Down Expand Up @@ -358,7 +358,7 @@ def data_update(self, data):
if self.enabled is False:
return

now = dt_util.now()
now = dt.now()
# Do not update within scan interval to save resources
if self._last_seen:
if now - self._last_seen <= timedelta(seconds=self._scan_interval):
Expand Down
2 changes: 1 addition & 1 deletion custom_components/ble_monitor/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@
],
"dependencies": [],
"codeowners": ["@Ernst79", "@Magalex2x14", "@Thrilleratplay"],
"version": "8.0.4-beta",
"version": "8.0.5-beta",
"iot_class": "local_polling"
}
8 changes: 5 additions & 3 deletions custom_components/ble_monitor/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from homeassistant.helpers.event import async_call_later
from homeassistant.helpers.restore_state import RestoreEntity
from homeassistant.components.sensor import SensorEntity
import homeassistant.util.dt as dt_util
from homeassistant.util import dt
from homeassistant.util.temperature import convert as convert_temp

from .helper import (
Expand Down Expand Up @@ -165,7 +165,7 @@ async def async_add_sensor(key, sensortype, firmware, manufacturer=None, data={}
rssi = {} # rssi
ble_adv_cnt = 0

ts_now = dt_util.now()
ts_now = dt.now()
ts_restart = ts_now
ts_last_update = ts_now
period_cnt = 0
Expand Down Expand Up @@ -280,7 +280,7 @@ async def async_add_sensor(key, sensortype, firmware, manufacturer=None, data={}
entity.async_schedule_update_ha_state(True)
entity.pending_update = False
data = None
ts_now = dt_util.now()
ts_now = dt.now()
if ts_now - ts_last_update < timedelta(seconds=self.period):
continue
ts_last_update = ts_now
Expand Down Expand Up @@ -906,6 +906,8 @@ def collect(self, data, period_cnt, batt_attr=None):
self._extra_state_attributes["weight removed"] = bool(
data["weight removed"]
)
if "impedance" not in data and data["type"] == "Mi Scale V2":
self._extra_state_attributes["impedance"] = "unavailable"
if "impedance" in data:
self._extra_state_attributes["impedance"] = data["impedance"]
if "weight unit" in data:
Expand Down

0 comments on commit 7a9ed1c

Please sign in to comment.