1 """Service for obtaining information about closer bus from Transport Yandex Service."""
3 from __future__
import annotations
5 from datetime
import timedelta
8 from aioymaps
import CaptchaError, NoSessionError, YandexMapsRequester
9 import voluptuous
as vol
12 PLATFORM_SCHEMA
as SENSOR_PLATFORM_SCHEMA,
24 _LOGGER = logging.getLogger(__name__)
26 STOP_NAME =
"stop_name"
27 USER_AGENT =
"Home Assistant"
29 CONF_STOP_ID =
"stop_id"
32 DEFAULT_NAME =
"Yandex Transport"
37 PLATFORM_SCHEMA = SENSOR_PLATFORM_SCHEMA.extend(
39 vol.Required(CONF_STOP_ID): cv.string,
40 vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
41 vol.Optional(CONF_ROUTE, default=[]): vol.All(cv.ensure_list, [cv.string]),
49 async_add_entities: AddEntitiesCallback,
50 discovery_info: DiscoveryInfoType |
None =
None,
52 """Set up the Yandex transport sensor."""
53 stop_id = config[CONF_STOP_ID]
54 name = config[CONF_NAME]
55 routes = config[CONF_ROUTE]
58 ymaps = YandexMapsRequester(user_agent=USER_AGENT, client_session=client_session)
60 await ymaps.set_new_session()
61 except CaptchaError
as ex:
63 "%s. You may need to disable the integration for some time",
71 """Implementation of yandex_transport sensor."""
73 _attr_attribution =
"Data provided by maps.yandex.ru"
74 _attr_icon =
"mdi:bus"
76 def __init__(self, requester: YandexMapsRequester, stop_id, routes, name) ->
None:
77 """Initialize sensor."""
86 """Get the latest data from maps.yandex.ru and update the states."""
91 except (CaptchaError, NoSessionError)
as ex:
93 "%s. You may need to disable the integration for some time",
98 data = yandex_reply[
"data"]
99 except KeyError
as key_error:
102 "Exception KeyError was captured, missing key is %s. Yandex"
110 await self.
requesterrequester.set_new_session()
114 stop_name = data[
"name"]
115 transport_list = data[
"transports"]
116 for transport
in transport_list:
117 for thread
in transport[
"threads"]:
118 if "Events" not in thread[
"BriefSchedule"]:
120 if thread.get(
"noBoarding")
is True:
122 for event
in thread[
"BriefSchedule"][
"Events"]:
126 if "railway" in transport[
"Types"]:
128 [x[
"name"]
for x
in thread[
"EssentialStops"]]
131 route = transport[
"name"]
136 if "Estimated" not in event
and "Scheduled" not in event:
139 departure = event.get(
"Estimated")
or event[
"Scheduled"]
140 posix_time_next =
int(departure[
"value"])
141 if closer_time
is None or closer_time > posix_time_next:
142 closer_time = posix_time_next
143 if route
not in attrs:
145 attrs[route].append(departure[
"text"])
146 attrs[STOP_NAME] = stop_name
148 if closer_time
is None:
151 self.
_state_state = dt_util.utc_from_timestamp(closer_time).replace(microsecond=0)
156 """Return the state of the sensor."""
161 """Return the device class."""
162 return SensorDeviceClass.TIMESTAMP
166 """Return the name of the sensor."""
167 return self.
_name_name
171 """Return the state attributes."""
def async_update(self, *tries=0)
None __init__(self, YandexMapsRequester requester, stop_id, routes, name)
def extra_state_attributes(self)
aiohttp.ClientSession async_create_clientsession()
None async_setup_platform(HomeAssistant hass, ConfigType config, AddEntitiesCallback async_add_entities, DiscoveryInfoType|None discovery_info=None)