mirror of
https://github.com/home-assistant/core.git
synced 2025-07-20 11:47:06 +00:00
Fix Yandex transport Integration, add signature to requests (#37365)
This commit is contained in:
parent
cee136ec55
commit
cecdce07cc
@ -467,7 +467,7 @@ homeassistant/components/xiaomi_miio/* @rytilahti @syssi
|
|||||||
homeassistant/components/xiaomi_tv/* @simse
|
homeassistant/components/xiaomi_tv/* @simse
|
||||||
homeassistant/components/xmpp/* @fabaff @flowolf
|
homeassistant/components/xmpp/* @fabaff @flowolf
|
||||||
homeassistant/components/yamaha_musiccast/* @jalmeroth
|
homeassistant/components/yamaha_musiccast/* @jalmeroth
|
||||||
homeassistant/components/yandex_transport/* @rishatik92
|
homeassistant/components/yandex_transport/* @rishatik92 @devbis
|
||||||
homeassistant/components/yeelight/* @rytilahti @zewelor
|
homeassistant/components/yeelight/* @rytilahti @zewelor
|
||||||
homeassistant/components/yeelightsunflower/* @lindsaymarkward
|
homeassistant/components/yeelightsunflower/* @lindsaymarkward
|
||||||
homeassistant/components/yessssms/* @flowolf
|
homeassistant/components/yessssms/* @flowolf
|
||||||
|
@ -2,6 +2,6 @@
|
|||||||
"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": ["ya_ma==0.3.8"],
|
"requirements": ["aioymaps==1.0.0"],
|
||||||
"codeowners": ["@rishatik92"]
|
"codeowners": ["@rishatik92", "@devbis"]
|
||||||
}
|
}
|
||||||
|
@ -3,11 +3,12 @@
|
|||||||
from datetime import timedelta
|
from datetime import timedelta
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
|
from aioymaps import YandexMapsRequester
|
||||||
import voluptuous as vol
|
import voluptuous as vol
|
||||||
from ya_ma import YandexMapsRequester
|
|
||||||
|
|
||||||
from homeassistant.components.sensor import PLATFORM_SCHEMA
|
from homeassistant.components.sensor import PLATFORM_SCHEMA
|
||||||
from homeassistant.const import ATTR_ATTRIBUTION, CONF_NAME, DEVICE_CLASS_TIMESTAMP
|
from homeassistant.const import ATTR_ATTRIBUTION, CONF_NAME, DEVICE_CLASS_TIMESTAMP
|
||||||
|
from homeassistant.helpers.aiohttp_client import async_create_clientsession
|
||||||
import homeassistant.helpers.config_validation as cv
|
import homeassistant.helpers.config_validation as cv
|
||||||
from homeassistant.helpers.entity import Entity
|
from homeassistant.helpers.entity import Entity
|
||||||
import homeassistant.util.dt as dt_util
|
import homeassistant.util.dt as dt_util
|
||||||
@ -35,20 +36,21 @@ PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
def setup_platform(hass, config, add_entities, discovery_info=None):
|
async def async_setup_platform(hass, config, async_add_entities, discovery_info=None):
|
||||||
"""Set up the Yandex transport sensor."""
|
"""Set up the Yandex transport sensor."""
|
||||||
stop_id = config[CONF_STOP_ID]
|
stop_id = config[CONF_STOP_ID]
|
||||||
name = config[CONF_NAME]
|
name = config[CONF_NAME]
|
||||||
routes = config[CONF_ROUTE]
|
routes = config[CONF_ROUTE]
|
||||||
|
|
||||||
data = YandexMapsRequester(user_agent=USER_AGENT)
|
client_session = async_create_clientsession(hass, requote_redirect_url=False)
|
||||||
add_entities([DiscoverMoscowYandexTransport(data, stop_id, routes, name)], True)
|
data = YandexMapsRequester(user_agent=USER_AGENT, client_session=client_session)
|
||||||
|
async_add_entities([DiscoverYandexTransport(data, stop_id, routes, name)], True)
|
||||||
|
|
||||||
|
|
||||||
class DiscoverMoscowYandexTransport(Entity):
|
class DiscoverYandexTransport(Entity):
|
||||||
"""Implementation of yandex_transport sensor."""
|
"""Implementation of yandex_transport sensor."""
|
||||||
|
|
||||||
def __init__(self, requester, stop_id, routes, name):
|
def __init__(self, requester: YandexMapsRequester, stop_id, routes, name):
|
||||||
"""Initialize sensor."""
|
"""Initialize sensor."""
|
||||||
self.requester = requester
|
self.requester = requester
|
||||||
self._stop_id = stop_id
|
self._stop_id = stop_id
|
||||||
@ -58,12 +60,12 @@ class DiscoverMoscowYandexTransport(Entity):
|
|||||||
self._name = name
|
self._name = name
|
||||||
self._attrs = None
|
self._attrs = None
|
||||||
|
|
||||||
def update(self):
|
async def async_update(self):
|
||||||
"""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
|
||||||
|
yandex_reply = await self.requester.get_stop_info(self._stop_id)
|
||||||
try:
|
try:
|
||||||
yandex_reply = self.requester.get_stop_info(self._stop_id)
|
|
||||||
data = yandex_reply["data"]
|
data = yandex_reply["data"]
|
||||||
except KeyError as key_error:
|
except KeyError as key_error:
|
||||||
_LOGGER.warning(
|
_LOGGER.warning(
|
||||||
@ -71,8 +73,8 @@ class DiscoverMoscowYandexTransport(Entity):
|
|||||||
key_error,
|
key_error,
|
||||||
yandex_reply,
|
yandex_reply,
|
||||||
)
|
)
|
||||||
self.requester.set_new_session()
|
await self.requester.set_new_session()
|
||||||
data = self.requester.get_stop_info(self._stop_id)["data"]
|
data = (await self.requester.get_stop_info(self._stop_id))["data"]
|
||||||
stop_name = data["name"]
|
stop_name = data["name"]
|
||||||
transport_list = data["transports"]
|
transport_list = data["transports"]
|
||||||
for transport in transport_list:
|
for transport in transport_list:
|
||||||
|
@ -215,6 +215,9 @@ aioswitcher==1.2.0
|
|||||||
# homeassistant.components.unifi
|
# homeassistant.components.unifi
|
||||||
aiounifi==23
|
aiounifi==23
|
||||||
|
|
||||||
|
# homeassistant.components.yandex_transport
|
||||||
|
aioymaps==1.0.0
|
||||||
|
|
||||||
# homeassistant.components.airly
|
# homeassistant.components.airly
|
||||||
airly==0.0.2
|
airly==0.0.2
|
||||||
|
|
||||||
@ -2229,9 +2232,6 @@ xmltodict==0.12.0
|
|||||||
# homeassistant.components.xs1
|
# homeassistant.components.xs1
|
||||||
xs1-api-client==3.0.0
|
xs1-api-client==3.0.0
|
||||||
|
|
||||||
# homeassistant.components.yandex_transport
|
|
||||||
ya_ma==0.3.8
|
|
||||||
|
|
||||||
# homeassistant.components.yale_smart_alarm
|
# homeassistant.components.yale_smart_alarm
|
||||||
yalesmartalarmclient==0.1.6
|
yalesmartalarmclient==0.1.6
|
||||||
|
|
||||||
|
@ -125,6 +125,9 @@ aioswitcher==1.2.0
|
|||||||
# homeassistant.components.unifi
|
# homeassistant.components.unifi
|
||||||
aiounifi==23
|
aiounifi==23
|
||||||
|
|
||||||
|
# homeassistant.components.yandex_transport
|
||||||
|
aioymaps==1.0.0
|
||||||
|
|
||||||
# homeassistant.components.airly
|
# homeassistant.components.airly
|
||||||
airly==0.0.2
|
airly==0.0.2
|
||||||
|
|
||||||
@ -980,9 +983,6 @@ wled==0.4.3
|
|||||||
# homeassistant.components.zestimate
|
# homeassistant.components.zestimate
|
||||||
xmltodict==0.12.0
|
xmltodict==0.12.0
|
||||||
|
|
||||||
# homeassistant.components.yandex_transport
|
|
||||||
ya_ma==0.3.8
|
|
||||||
|
|
||||||
# homeassistant.components.zeroconf
|
# homeassistant.components.zeroconf
|
||||||
zeroconf==0.27.1
|
zeroconf==0.27.1
|
||||||
|
|
||||||
|
@ -9,7 +9,7 @@ from homeassistant.const import CONF_NAME
|
|||||||
from homeassistant.setup import async_setup_component
|
from homeassistant.setup import async_setup_component
|
||||||
import homeassistant.util.dt as dt_util
|
import homeassistant.util.dt as dt_util
|
||||||
|
|
||||||
from tests.async_mock import patch
|
from tests.async_mock import AsyncMock, patch
|
||||||
from tests.common import assert_setup_component, load_fixture
|
from tests.common import assert_setup_component, load_fixture
|
||||||
|
|
||||||
REPLY = json.loads(load_fixture("yandex_transport_reply.json"))
|
REPLY = json.loads(load_fixture("yandex_transport_reply.json"))
|
||||||
@ -17,10 +17,10 @@ REPLY = json.loads(load_fixture("yandex_transport_reply.json"))
|
|||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
def mock_requester():
|
def mock_requester():
|
||||||
"""Create a mock ya_ma module and YandexMapsRequester."""
|
"""Create a mock for YandexMapsRequester."""
|
||||||
with patch("ya_ma.YandexMapsRequester") as requester:
|
with patch("aioymaps.YandexMapsRequester") as requester:
|
||||||
instance = requester.return_value
|
instance = requester.return_value
|
||||||
instance.get_stop_info.return_value = REPLY
|
instance.get_stop_info = AsyncMock(return_value=REPLY)
|
||||||
yield instance
|
yield instance
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user