Skip to content

Commit

Permalink
Small code improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
FaserF committed Oct 18, 2024
1 parent 9e37c1c commit e36eb55
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 13 deletions.
4 changes: 2 additions & 2 deletions custom_components/deutschebahn/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ async def async_setup_entry(
async def async_update_data():
"""Fetch data from Schiene."""
config = hass.data[DOMAIN][entry.entry_id]
update_interval = config.get("scan_interval", 2) # Default scan interval
async with async_timeout.timeout(update_interval - 1):
update_interval = timedelta(minutes=config.get(CONF_UPDATE_INTERVAL, 2))
async with async_timeout.timeout(update_interval - 0.1):
# Assuming data.update() is an async function, no need for async_add_executor_job
await data.update()

Expand Down
2 changes: 1 addition & 1 deletion custom_components/deutschebahn/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@
"requirements": [
"schiene==0.26"
],
"version": "3.0.3"
"version": "3.0.4"
}
18 changes: 8 additions & 10 deletions custom_components/deutschebahn/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,14 @@ async def async_setup_entry(
"""Setup sensors from a config entry created in the integrations UI."""
config = hass.data[DOMAIN][entry.entry_id]
scan_interval = timedelta(minutes=config.get(CONF_UPDATE_INTERVAL, 2))
_LOGGER.debug("Sensor async_setup_entry")
_LOGGER.debug(f"Sensor async_setup_entry Using scan interval: {scan_interval}")

if entry.options:
config.update(entry.options)

sensors = DeutscheBahnSensor(config, hass, scan_interval)
async_add_entities(
[
DeutscheBahnSensor(config, hass, scan_interval)
],
update_before_add=True
)
async_add_entities([sensors], update_before_add=True)


class DeutscheBahnSensor(SensorEntity):
"""Implementation of a Deutsche Bahn sensor."""
Expand Down Expand Up @@ -151,13 +149,13 @@ async def async_update(self):

if connections_count > 0:
if connections_count < self.max_connections:
verb = "is" if connections_count == 1 else "are"
_LOGGER.warning(
f"Requested {self.max_connections} connections, but only {connections_count} are available."
f"{self._name} Requested {self.max_connections} connections, but only {connections_count} {verb} available."
)


for con in self.connections:
# Detail info is not useful. Having a more consistent interface
# simplifies usage of template sensors.
if "details" in con:
#_LOGGER.debug(f"Processing connection: {con}")
con.pop("details")
Expand Down

0 comments on commit e36eb55

Please sign in to comment.