Fix yandex captcha detecting (#56132)

Yandex recently switched to the new captcha page and the new version of aiomaps supports it.
The check for captcha is moved to platform setup.

Fixes #56035
This commit is contained in:
Ivan Belokobylskiy 2021-10-23 12:14:32 +03:00 committed by GitHub
parent a0c96f2a9a
commit 059880ebdc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 23 additions and 7 deletions

View File

@ -2,7 +2,7 @@
"domain": "yandex_transport", "domain": "yandex_transport",
"name": "Yandex Transport", "name": "Yandex Transport",
"documentation": "https://www.home-assistant.io/integrations/yandex_transport", "documentation": "https://www.home-assistant.io/integrations/yandex_transport",
"requirements": ["aioymaps==1.1.0"], "requirements": ["aioymaps==1.2.1"],
"codeowners": ["@rishatik92", "@devbis"], "codeowners": ["@rishatik92", "@devbis"],
"iot_class": "cloud_polling" "iot_class": "cloud_polling"
} }

View File

@ -3,7 +3,7 @@
from datetime import timedelta from datetime import timedelta
import logging import logging
from aioymaps import YandexMapsRequester from aioymaps import CaptchaError, YandexMapsRequester
import voluptuous as vol import voluptuous as vol
from homeassistant.components.sensor import PLATFORM_SCHEMA, SensorEntity from homeassistant.components.sensor import PLATFORM_SCHEMA, SensorEntity
@ -42,8 +42,16 @@ async def async_setup_platform(hass, config, async_add_entities, discovery_info=
routes = config[CONF_ROUTE] routes = config[CONF_ROUTE]
client_session = async_create_clientsession(hass, requote_redirect_url=False) client_session = async_create_clientsession(hass, requote_redirect_url=False)
data = YandexMapsRequester(user_agent=USER_AGENT, client_session=client_session) ymaps = YandexMapsRequester(user_agent=USER_AGENT, client_session=client_session)
async_add_entities([DiscoverYandexTransport(data, stop_id, routes, name)], True) try:
await ymaps.set_new_session()
except CaptchaError as ex:
_LOGGER.error(
"%s. You may need to disable the integration for some time",
ex,
)
return
async_add_entities([DiscoverYandexTransport(ymaps, stop_id, routes, name)], True)
class DiscoverYandexTransport(SensorEntity): class DiscoverYandexTransport(SensorEntity):
@ -63,7 +71,14 @@ class DiscoverYandexTransport(SensorEntity):
"""Get the latest data from maps.yandex.ru and update the states.""" """Get the latest data from maps.yandex.ru and update the states."""
attrs = {} attrs = {}
closer_time = None closer_time = None
try:
yandex_reply = await self.requester.get_stop_info(self._stop_id) yandex_reply = await self.requester.get_stop_info(self._stop_id)
except CaptchaError as ex:
_LOGGER.error(
"%s. You may need to disable the integration for some time",
ex,
)
return
try: try:
data = yandex_reply["data"] data = yandex_reply["data"]
except KeyError as key_error: except KeyError as key_error:

View File

@ -264,7 +264,7 @@ aiovlc==0.1.0
aiowatttime==0.1.1 aiowatttime==0.1.1
# homeassistant.components.yandex_transport # homeassistant.components.yandex_transport
aioymaps==1.1.0 aioymaps==1.2.1
# homeassistant.components.airly # homeassistant.components.airly
airly==1.1.0 airly==1.1.0

View File

@ -191,7 +191,7 @@ aiovlc==0.1.0
aiowatttime==0.1.1 aiowatttime==0.1.1
# homeassistant.components.yandex_transport # homeassistant.components.yandex_transport
aioymaps==1.1.0 aioymaps==1.2.1
# homeassistant.components.airly # homeassistant.components.airly
airly==1.1.0 airly==1.1.0

View File

@ -20,6 +20,7 @@ def mock_requester():
"""Create a mock for YandexMapsRequester.""" """Create a mock for YandexMapsRequester."""
with patch("aioymaps.YandexMapsRequester") as requester: with patch("aioymaps.YandexMapsRequester") as requester:
instance = requester.return_value instance = requester.return_value
instance.set_new_session = AsyncMock()
instance.get_stop_info = AsyncMock(return_value=REPLY) instance.get_stop_info = AsyncMock(return_value=REPLY)
yield instance yield instance