Skip to content

Commit

Permalink
Helpers sensors removed in favour of cascading.
Browse files Browse the repository at this point in the history
  • Loading branch information
Campioni Lorenzo (IFL ATV SC D RAD PJM) authored and Campioni Lorenzo (IFL ATV SC D RAD PJM) committed Nov 17, 2022
1 parent ce1a1d6 commit fb47a65
Showing 1 changed file with 21 additions and 56 deletions.
77 changes: 21 additions & 56 deletions custom_components/oebb/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,6 @@ async def async_setup_platform(hass, config, add_devices_callback, discovery_inf

for idx, journey in enumerate(coordinator.data["journey"]):
devices.append(OebbSensor(coordinator, idx, params["evaId"]))
devices.append(OebbHelperSensor(coordinator, idx, params["evaId"]))
add_devices_callback(devices, True)


Expand All @@ -119,7 +118,7 @@ async def fetch_data(self):
"""Get json from API endpoint."""
value = None

_LOGGER.debug("Inside get JSON")
_LOGGER.debug("Inside Fetch_data")

response = None

Expand Down Expand Up @@ -173,6 +172,7 @@ class OebbSensor(CoordinatorEntity, SensorEntity):
def __init__(self, coordinator: OebbCoordinator, idx, evaId):
"""Pass coordinator to CoordinatorEntity."""
super().__init__(coordinator)

self.idx = idx
self.formatted_idx = f"{self.idx:02}"
self._name = "oebb_journey_" + str(idx)
Expand All @@ -185,27 +185,30 @@ def __init__(self, coordinator: OebbCoordinator, idx, evaId):
@callback
def _handle_coordinator_update(self) -> None:
"""Handle updated data from the coordinator."""

data = self.coordinator.data

self.attributes = {
"startTime": data["journey"][self.idx]["ti"],
"lastStop": data["journey"][self.idx]["lastStop"],
"line": data["journey"][self.idx]["pr"],
# "provider": "oebb",
}
if self.idx >= len(self.coordinator.data["journey"]):
_LOGGER.warning("Sensor %d out of coordinator data range", self.idx)
return
else:
self.attributes = {
"startTime": data["journey"][self.idx]["ti"],
"lastStop": data["journey"][self.idx]["lastStop"],
"line": data["journey"][self.idx]["pr"],
}
now = datetime.now()

date_string = now.strftime("%d/%m/%Y")
# _LOGGER.debug("Date_string : %s", date_string)
timestamp_string = date_string + " " + self.attributes["startTime"]
# _LOGGER.debug("Timestamp_string %s:", timestamp_string)
self._state = datetime.strptime(timestamp_string, "%d/%m/%Y %H:%M")
# _LOGGER.debug("State: %s:", self._state)
self.async_write_ha_state()

# self._name = self.attributes["startTime"]

now = datetime.now()

date_string = now.strftime("%d/%m/%Y")
_LOGGER.debug("Date_string : %s", date_string)
timestamp_string = date_string + " " + self.attributes["startTime"]
_LOGGER.debug("Timestamp_string %s:", timestamp_string)
self._state = datetime.strptime(timestamp_string, "%d/%m/%Y %H:%M")
_LOGGER.debug("State: %s:", self._state)
self.async_write_ha_state()

@property
def name(self):
"""Return name."""
Expand All @@ -230,41 +233,3 @@ def extra_state_attributes(self):
def device_class(self):
"""Return device_class."""
return "timestamp"


class OebbHelperSensor(CoordinatorEntity, SensorEntity):
"""OebbSensor."""

def __init__(self, coordinator: OebbCoordinator, idx, evaId):
"""Pass coordinator to CoordinatorEntity."""
super().__init__(coordinator)
self.idx = idx
self.formatted_idx = f"{self.idx:02}"
self._name = "oebb_journey_helper_" + str(idx)
self._state = None

self._attr_unique_id = str(evaId) + "_helper_" + str(idx)

@callback
def _handle_coordinator_update(self) -> None:
"""Handle updated data from the coordinator."""
data = self.coordinator.data

self._state = data["journey"][self.idx]["ti"]

self.async_write_ha_state()

@property
def name(self):
"""Return name."""
return self._name

@property
def state(self):
"""Return state."""
return self._state

@property
def icon(self):
"""Return icon."""
return "mdi:tram"

0 comments on commit fb47a65

Please sign in to comment.