Skip to content

Commit

Permalink
Code refactoring.
Browse files Browse the repository at this point in the history
  • Loading branch information
dvejsada committed Feb 23, 2024
1 parent c5f8da1 commit 5578cb1
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 152 deletions.
47 changes: 18 additions & 29 deletions pid_integration/binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,47 +21,34 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
class InfotextBinarySensor(BinarySensorEntity):
"""Sensor for departure."""
_attr_has_entity_name = True
_attr_device_class = BinarySensorDeviceClass.PROBLEM

def __init__(self, departure_board):

self._departure_board = departure_board
self._attr_unique_id = f"{self._departure_board.board_id}_{self._departure_board.conn_num+7}"
# The name of the entity
self._attr_name = "infotext"
self._attr_state = self._departure_board.infotext_state
self._extra_attr = self._departure_board.infotext_attr

self._attr_is_on, self._attr_extra_state_attributes = self._departure_board.info_text

@property
def device_info(self):
"""Return information to link this entity with the correct device."""
return {"identifiers": {(DOMAIN, self._departure_board.board_id)}, "name": self._departure_board.name}
return {"identifiers": {(DOMAIN, self._departure_board.board_id)}}

@property
def extra_state_attributes(self):
return self._extra_attr

def name(self) -> str:
"""Entity name"""
return "infotext"

@property
def icon(self):
def icon(self) -> str:
if self._attr_state:
icon = ICON_INFO_ON
else:
icon = ICON_INFO_OFF
return icon


@property
def is_on(self):
return self._attr_state

@property
def device_class(self):
return BinarySensorDeviceClass.PROBLEM

async def async_update(self):
self._attr_state = self._departure_board.infotext_state
self._extra_attr = self._departure_board.infotext_attr
self._attr_is_on, self._attr_extra_state_attributes = self._departure_board.info_text


class WheelchairSensor(BinarySensorEntity):
Expand All @@ -73,23 +60,25 @@ class WheelchairSensor(BinarySensorEntity):
def __init__(self, departure_board):

self._departure_board = departure_board
self._attr_unique_id = f"{self._departure_board.board_id}_{self._departure_board.conn_num+2}"

# The name of the entity
self._attr_name = f"wheelchair"
self._attr_unique_id = f"{self._departure_board.board_id}_{self._departure_board.conn_num+8}"

@property
def device_info(self):
"""Return information to link this entity with the correct device."""
return {"identifiers": {(DOMAIN, self._departure_board.board_id)}, "name": self._departure_board.name}
return {"identifiers": {(DOMAIN, self._departure_board.board_id)}}

@property
def name(self):
"""Entity name"""
return "wheelchair"

@property
def state(self):
if int(self._departure_board.wheelchair_accessible) == 0:
if self._departure_board.wheelchair_accessible == 0:
state = STATE_UNKNOWN
elif int(self._departure_board.wheelchair_accessible) == 1:
elif self._departure_board.wheelchair_accessible == 1:
state = STATE_ON
elif int(self._departure_board.wheelchair_accessible) == 2:
elif self._departure_board.wheelchair_accessible == 2:
state = STATE_OFF
else:
state = STATE_UNAVAILABLE
Expand Down
85 changes: 49 additions & 36 deletions pid_integration/hub.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,71 +4,84 @@
from .api_call import ApiCall


def check_not_null(response):
if response is not None:
value = response
else:
value = ""
return value


class DepartureBoard:
"""setting Departure board device"""
_attr_manufacturer = "Prague Integrated Transport"

def __init__(self, hass: HomeAssistant, api_key: str, stop_id: str, conn_num: int, response) -> None:
"""Init departure board."""
self._hass = hass
self._api_key = api_key
self._stop_id = stop_id
self._id = stop_id
self.conn_num = conn_num
self.departures = []
self.infotext_state = ""
self.infotext_attr = {}
self._stop_name = response["stops"][0]["stop_name"]
self._name = response["stops"][0]["stop_name"] + " " + check_not_null(response["stops"][0]["platform_code"])
self._wheel = response["stops"][0]["wheelchair_boarding"]
self.latitude = response["stops"][0]["stop_lat"]
self.longitude = response["stops"][0]["stop_lon"]
self.platform = check_not_null(response["stops"][0]["platform_code"])
self.zone = response["stops"][0]["zone_id"]
self.extra_attr = response["departures"]
self.check_infotext(response["infotexts"])
for i in range(self.conn_num):
self.departures.append(i)
self.conn_num = int(conn_num)
self.response = response

@property
def board_id(self) -> str:
"""ID for departure board."""
return self._id
return self._stop_id

@property
def name(self) -> str:
"""ID for departure board."""
return self._name
return self.stop_name + " " + self.platform

@property
def stop_name(self) -> str:
"""Stop name."""
return self._stop_name
return self.response["stops"][0]["stop_name"]

@property
def platform(self) -> str:
"""Platform."""
if self.response["stops"][0]["platform_code"] is not None:
value = self.response["stops"][0]["platform_code"]
else:
value = ""
return value

@property
def extra_attr(self) -> str:
"""Extra state attributes (departures)."""
return self.response["departures"]

@property
def latitude(self) -> str:
"""Latitude of the stop."""
return self.response["stops"][0]["stop_lat"]

@property
def longitude(self) -> str:
"""Longitude of the stop."""
return self.response["stops"][0]["stop_lon"]

@property
def api_key(self) -> str:
"""Provides API key."""
return self._api_key

async def async_update(self) -> None:
data = await self._hass.async_add_executor_job(ApiCall.update_info, self.api_key, self._stop_id, self.conn_num)
self.extra_attr = data["departures"]
self.check_infotext(data["infotexts"])
self.response = data

@property
def wheelchair_accessible(self):
return self._wheel
"""Wheelchair accessibility of the stop."""
return int(self.response["stops"][0]["wheelchair_boarding"])

def check_infotext(self, data):
if len(data) != 0:
self.infotext_state = True
self.infotext_attr = data[0]
@property
def zone(self) -> str:
"""Zone of the stop"""
return self.response["stops"][0]["zone_id"]

@property
def info_text(self) -> tuple:
""" State and content of info text"""
if len(self.response["infotexts"]) != 0:
state = True
text = self.response["infotexts"][0]
else:
self.infotext_state = False
self.infotext_attr = {}
state = False
text = {}

return state, text
Loading

0 comments on commit 5578cb1

Please sign in to comment.