mirror of
https://github.com/home-assistant/core
synced 2024-09-03 08:14:07 +02:00
Small refactoring for the alexa component (#19782)
* small refactoring * fix tests
This commit is contained in:
parent
2be0d1b096
commit
ca460ace5d
@ -72,6 +72,6 @@ async def async_setup(hass, config):
|
|||||||
pass
|
pass
|
||||||
else:
|
else:
|
||||||
smart_home_config = smart_home_config or SMART_HOME_SCHEMA({})
|
smart_home_config = smart_home_config or SMART_HOME_SCHEMA({})
|
||||||
smart_home.async_setup(hass, smart_home_config)
|
await smart_home.async_setup(hass, smart_home_config)
|
||||||
|
|
||||||
return True
|
return True
|
||||||
|
@ -1036,8 +1036,7 @@ class Config:
|
|||||||
self.entity_config = entity_config or {}
|
self.entity_config = entity_config or {}
|
||||||
|
|
||||||
|
|
||||||
@ha.callback
|
async def async_setup(hass, config):
|
||||||
def async_setup(hass, config):
|
|
||||||
"""Activate Smart Home functionality of Alexa component.
|
"""Activate Smart Home functionality of Alexa component.
|
||||||
|
|
||||||
This is optional, triggered by having a `smart_home:` sub-section in the
|
This is optional, triggered by having a `smart_home:` sub-section in the
|
||||||
@ -1063,8 +1062,7 @@ def async_setup(hass, config):
|
|||||||
hass.http.register_view(SmartHomeView(smart_home_config))
|
hass.http.register_view(SmartHomeView(smart_home_config))
|
||||||
|
|
||||||
if AUTH_KEY in hass.data:
|
if AUTH_KEY in hass.data:
|
||||||
hass.loop.create_task(
|
await async_enable_proactive_mode(hass, smart_home_config)
|
||||||
async_enable_proactive_mode(hass, smart_home_config))
|
|
||||||
|
|
||||||
|
|
||||||
async def async_enable_proactive_mode(hass, smart_home_config):
|
async def async_enable_proactive_mode(hass, smart_home_config):
|
||||||
@ -1380,8 +1378,7 @@ async def async_send_changereport_message(hass, config, alexa_entity):
|
|||||||
return
|
return
|
||||||
|
|
||||||
headers = {
|
headers = {
|
||||||
"Authorization": "Bearer {}".format(token),
|
"Authorization": "Bearer {}".format(token)
|
||||||
"Content-Type": "application/json;charset=UTF-8"
|
|
||||||
}
|
}
|
||||||
|
|
||||||
endpoint = alexa_entity.entity_id()
|
endpoint = alexa_entity.entity_id()
|
||||||
@ -1402,14 +1399,14 @@ async def async_send_changereport_message(hass, config, alexa_entity):
|
|||||||
payload=payload)
|
payload=payload)
|
||||||
message.set_endpoint_full(token, endpoint)
|
message.set_endpoint_full(token, endpoint)
|
||||||
|
|
||||||
message_str = json.dumps(message.serialize())
|
message_serialized = message.serialize()
|
||||||
|
|
||||||
try:
|
try:
|
||||||
session = aiohttp_client.async_get_clientsession(hass)
|
session = aiohttp_client.async_get_clientsession(hass)
|
||||||
with async_timeout.timeout(DEFAULT_TIMEOUT, loop=hass.loop):
|
with async_timeout.timeout(DEFAULT_TIMEOUT, loop=hass.loop):
|
||||||
response = await session.post(config.endpoint,
|
response = await session.post(config.endpoint,
|
||||||
headers=headers,
|
headers=headers,
|
||||||
data=message_str,
|
json=message_serialized,
|
||||||
allow_redirects=True)
|
allow_redirects=True)
|
||||||
|
|
||||||
except (asyncio.TimeoutError, aiohttp.ClientError):
|
except (asyncio.TimeoutError, aiohttp.ClientError):
|
||||||
@ -1418,7 +1415,7 @@ async def async_send_changereport_message(hass, config, alexa_entity):
|
|||||||
|
|
||||||
response_text = await response.text()
|
response_text = await response.text()
|
||||||
|
|
||||||
_LOGGER.debug("Sent: %s", message_str)
|
_LOGGER.debug("Sent: %s", json.dumps(message_serialized))
|
||||||
_LOGGER.debug("Received (%s): %s", response.status, response_text)
|
_LOGGER.debug("Received (%s): %s", response.status, response_text)
|
||||||
|
|
||||||
if response.status != 202:
|
if response.status != 202:
|
||||||
|
@ -1783,7 +1783,7 @@ async def test_report_state(hass, aioclient_mock):
|
|||||||
assert len(aioclient_mock.mock_calls) == 1
|
assert len(aioclient_mock.mock_calls) == 1
|
||||||
call = aioclient_mock.mock_calls
|
call = aioclient_mock.mock_calls
|
||||||
|
|
||||||
call_json = json.loads(call[0][2])
|
call_json = call[0][2]
|
||||||
assert call_json["event"]["payload"]["change"]["properties"][0][
|
assert call_json["event"]["payload"]["change"]["properties"][0][
|
||||||
"value"] == "NOT_DETECTED"
|
"value"] == "NOT_DETECTED"
|
||||||
assert call_json["event"]["endpoint"][
|
assert call_json["event"]["endpoint"][
|
||||||
|
Loading…
Reference in New Issue
Block a user