diff --git a/pid_integration/binary_sensor.py b/pid_integration/binary_sensor.py index da3b1d6..a9a07da 100644 --- a/pid_integration/binary_sensor.py +++ b/pid_integration/binary_sensor.py @@ -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): @@ -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 diff --git a/pid_integration/hub.py b/pid_integration/hub.py index 1890ebe..ea14177 100644 --- a/pid_integration/hub.py +++ b/pid_integration/hub.py @@ -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 diff --git a/pid_integration/sensor.py b/pid_integration/sensor.py index 9634ed0..2d1878c 100644 --- a/pid_integration/sensor.py +++ b/pid_integration/sensor.py @@ -4,6 +4,7 @@ from homeassistant.components.sensor import SensorEntity, SensorDeviceClass from datetime import timedelta, datetime +from zoneinfo import ZoneInfo from .const import ICON_BUS, ICON_TRAM, ICON_METRO, ICON_TRAIN, DOMAIN, ICON_STOP, ICON_LAT, ICON_LON, ICON_ZONE, ICON_PLATFORM, ICON_UPDATE from homeassistant.const import EntityCategory @@ -16,16 +17,17 @@ async def async_setup_entry(hass, config_entry, async_add_entities): """Add sensors for passed config_entry in HA.""" departure_board = hass.data[DOMAIN][config_entry.entry_id] new_entities = [] - for i in departure_board.departures: - new_entities.append(DepartureSensor(departure_board.departures[i], departure_board)) - new_entities.append(StopSensor(int(departure_board.conn_num)+1, departure_board)) - new_entities.append(LatSensor(int(departure_board.conn_num)+3, departure_board)) - new_entities.append(LonSensor(int(departure_board.conn_num)+4, departure_board)) - new_entities.append(ZoneSensor(int(departure_board.conn_num)+5, departure_board)) + for i in range(departure_board.conn_num): + new_entities.append(DepartureSensor(i, departure_board)) + new_entities.append(StopSensor(departure_board.conn_num+1, departure_board)) + new_entities.append(LatSensor(departure_board.conn_num+2, departure_board)) + new_entities.append(LonSensor(departure_board.conn_num+3, departure_board)) + new_entities.append(ZoneSensor(departure_board.conn_num+4, departure_board)) if departure_board.platform != "": - new_entities.append(PlatformSensor(int(departure_board.conn_num)+6, departure_board)) + new_entities.append(PlatformSensor(departure_board.conn_num+5, departure_board)) + new_entities.append(UpdateSensor(departure_board.conn_num+6, departure_board)) + # Add all entities to HA - new_entities.append(UpdateSensor(int(departure_board.conn_num) + 8, departure_board)) async_add_entities(new_entities) @@ -38,29 +40,23 @@ def __init__(self, departure: int, departure_board): self._departure = departure self._departure_board = departure_board self._attr_unique_id = f"{self._departure_board.board_id}_{self._departure}" - - # The name of the entity - self._attr_name = f"departure {self._departure+1}" - self._state = self._departure_board.extra_attr[self._departure]["route"]["short_name"] - self._extra_attr = self._departure_board.extra_attr[self._departure] + self._attr_native_value = self._departure_board.extra_attr[self._departure]["route"]["short_name"] + self._attr_extra_state_attributes = self._departure_board.extra_attr[self._departure] self._route_type = self._departure_board.extra_attr[self._departure]["route"]["type"] @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} - - - @property - def extra_state_attributes(self): - return self._extra_attr + return {"identifiers": {(DOMAIN, self._departure_board.board_id)}} @property - def state(self): - return self._state + def name(self) -> str: + """Return entity name""" + return f"departure {self._departure+1}" @property def icon(self): + """Return entity icon based on the type of vehicle""" if int(self._route_type) == 0: icon = ICON_TRAM elif int(self._route_type) == 1: @@ -72,8 +68,8 @@ def icon(self): return icon async def async_update(self): - self._state = self._departure_board.extra_attr[self._departure]["route"]["short_name"] - self._extra_attr = self._departure_board.extra_attr[self._departure] + self._attr_native_value = self._departure_board.extra_attr[self._departure]["route"]["short_name"] + self._attr_extra_state_attributes = self._departure_board.extra_attr[self._departure] class StopSensor(SensorEntity): @@ -87,25 +83,17 @@ def __init__(self, departure: int, departure_board): self._departure = departure self._departure_board = departure_board self._attr_unique_id = f"{self._departure_board.board_id}_{self._departure}" - - # The name of the entity - self._attr_name = f"stop name" - self._state = self._departure_board.stop_name - + self._attr_native_value = self._departure_board.stop_name @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 available(self) -> bool: - """To be implemented.""" - return True - - @property - def state(self): - return self._state + def name(self) -> str: + """Return entity name""" + return "stop name" class LatSensor(SensorEntity): @@ -119,24 +107,17 @@ def __init__(self, departure: int, departure_board): self._departure = departure self._departure_board = departure_board self._attr_unique_id = f"{self._departure_board.board_id}_{self._departure}" - - # The name of the entity - self._attr_name = f"latitude" - self._state = self._departure_board.latitude + self._attr_native_value = self._departure_board.latitude @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 available(self) -> bool: - """To be implemented.""" - return True - - @property - def state(self): - return self._state + def name(self) -> str: + """Return entity name""" + return "latitude" class LonSensor(SensorEntity): @@ -150,24 +131,17 @@ def __init__(self, departure: int, departure_board): self._departure = departure self._departure_board = departure_board self._attr_unique_id = f"{self._departure_board.board_id}_{self._departure}" - - # The name of the entity - self._attr_name = f"longitude" - self._state = self._departure_board.longitude + self._attr_native_value = self._departure_board.longitude @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} - - @property - def available(self) -> bool: - """To be implemented.""" - return True + return {"identifiers": {(DOMAIN, self._departure_board.board_id)}} @property - def state(self): - return self._state + def name(self) -> str: + """Return entity name""" + return "longitude" class ZoneSensor(SensorEntity): @@ -181,24 +155,17 @@ def __init__(self, departure: int, departure_board): self._departure = departure self._departure_board = departure_board self._attr_unique_id = f"{self._departure_board.board_id}_{self._departure}" - - # The name of the entity - self._attr_name = f"zone" - self._state = self._departure_board.zone + self._attr_native_value = self._departure_board.zone @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} - - @property - def available(self) -> bool: - """To be implemented.""" - return True + return {"identifiers": {(DOMAIN, self._departure_board.board_id)}} @property - def state(self): - return self._state + def name(self) -> str: + """Return entity name""" + return "zone" class PlatformSensor(SensorEntity): @@ -212,19 +179,17 @@ def __init__(self, departure: int, departure_board): self._departure = departure self._departure_board = departure_board self._attr_unique_id = f"{self._departure_board.board_id}_{self._departure}" - - # The name of the entity - self._attr_name = f"platform" - self._state = self._departure_board.platform + self._attr_native_value = self._departure_board.platform @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 state(self): - return self._state + def name(self) -> str: + """Return entity name""" + return "platform" class UpdateSensor(SensorEntity): @@ -239,21 +204,19 @@ def __init__(self, departure: int, departure_board): self._departure = departure self._departure_board = departure_board self._attr_unique_id = f"{self._departure_board.board_id}_{self._departure}" - - # The name of the entity - self._attr_name = f"updated" - self._state = datetime.now() + self._attr_native_value = datetime.now(tz=ZoneInfo("Europe/Prague")) @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 state(self): - return self._state + def name(self) -> str: + """Return entity name""" + return "updated" async def async_update(self): await self._departure_board.async_update() - self._state = datetime.now() + self._attr_native_value = datetime.now(tz=ZoneInfo("Europe/Prague"))