Skip to content

Commit

Permalink
solved #14 added time_format to configuration options
Browse files Browse the repository at this point in the history
  • Loading branch information
TekniskSupport committed Jan 9, 2022
1 parent a95ca96 commit 9be67e2
Showing 1 changed file with 15 additions and 9 deletions.
24 changes: 15 additions & 9 deletions custom_components/resrobot/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
CONF_UNIT = 'unit'
CONF_TIME_OFFSET = 'time_offset'
CONF_MEANS_OF_TRANSPORT = 'means_of_transport'
CONF_TIME_FORMAT = 'time_format'

PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
vol.Required(CONF_KEY, default=0): cv.string,
Expand All @@ -59,6 +60,7 @@
vol.Optional(CONF_FILTER_TYPE, default="must"): cv.string,
vol.Optional(CONF_FILTER_DIRECTION): cv.string,
}],
vol.Optional(CONF_TIME_FORMAT, default="%H:%M:%S"): cv.string,
}],
})
SCAN_INTERVAL = timedelta(minutes=DEFAULT_INTERVAL)
Expand All @@ -84,12 +86,13 @@ async def async_setup_platform(hass, config, async_add_devices, discovery_info=N
departure.get(CONF_MAX_JOURNEYS),
departure.get(CONF_TIME_OFFSET),
departure.get(CONF_FILTER),
departure.get(CONF_TIME_FORMAT),
discovery_info
)

async def add_sensors(hass, config, async_add_devices, api_key, fetch_interval,
number_of_sensors, unit_of_measurement, name, update_name,
location, max_journeys, time_offset, filter,
location, max_journeys, time_offset, filter, time_format,
discovery_info=None):
method = 'GET'
payload = ''
Expand Down Expand Up @@ -117,7 +120,8 @@ async def add_sensors(hass, config, async_add_devices, api_key, fetch_interval,
number_of_sensors,
unit_of_measurement,
update_name,
time_offset))
time_offset,
time_format))
async_add_devices(sensors, True)

class helperEntity(Entity):
Expand Down Expand Up @@ -237,7 +241,7 @@ class entityRepresentation(Entity):

def __init__(self, hass, helper, name, k,
number_of_sensors, unit_of_measurement,
update_name, time_offset):
update_name, time_offset, time_format):

"""Initialize a sensor."""
self._hass = hass
Expand All @@ -250,6 +254,7 @@ def __init__(self, hass, helper, name, k,
self._update_name = update_name
self._state = "Unavailable"
self._attributes = {}
self._time_format = time_format

def nameToEntityId(self, text: str, *, separator: str = "_") -> str:
text = text.lower()
Expand Down Expand Up @@ -344,14 +349,15 @@ async def async_update(self):
if (k == self._k):
if self._update_name:
self._name = data["name"]
self._attributes.update({"name": data["name"]})
self._attributes.update({"line": data["transportNumber"]})
if "rTime" in data:
self._attributes.update({"timeDate": data["date"] +" "+ data["rTime"]})
self._state = data['rTime']
date_time = data["date"] +" "+ data["rTime"]
else:
self._attributes.update({"timeDate": data["date"] +" "+ data["time"]})
self._state = data['time']
date_time = data["date"] +" "+ data["time"]
self._attributes.update({"name": data["name"]})
self._attributes.update({"line": data["transportNumber"]})
self._attributes.update({"timeDate": date_time})
date_time = datetime.strptime(date_time, '%Y-%m-%d %H:%M:%S')
self._state = date_time.strftime(self._time_format)
for attribute in data:
if attribute in getAttributes and data[attribute]:
self._attributes.update({attribute: data[attribute]})
Expand Down

0 comments on commit 9be67e2

Please sign in to comment.