Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions homeassistant/components/mobile_app/binary_sensor.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
"""Binary sensor platform for mobile_app."""
from functools import partial

from homeassistant.components.binary_sensor import BinarySensorEntity
from homeassistant.const import CONF_NAME, CONF_UNIQUE_ID, CONF_WEBHOOK_ID, STATE_ON
from homeassistant.core import callback
Expand Down Expand Up @@ -48,7 +46,7 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
async_add_entities(entities)

@callback
def handle_sensor_registration(webhook_id, data):
def handle_sensor_registration(data):
if data[CONF_WEBHOOK_ID] != webhook_id:
return

Expand All @@ -66,7 +64,7 @@ def handle_sensor_registration(webhook_id, data):
async_dispatcher_connect(
hass,
f"{DOMAIN}_{ENTITY_TYPE}_register",
partial(handle_sensor_registration, webhook_id),
handle_sensor_registration,
)


Expand Down
12 changes: 4 additions & 8 deletions homeassistant/components/mobile_app/entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,14 @@ def __init__(self, config: dict, device: DeviceEntry, entry: ConfigEntry):
self._registration = entry.data
self._unique_id = config[CONF_UNIQUE_ID]
self._entity_type = config[ATTR_SENSOR_TYPE]
self.unsub_dispatcher = None
self._name = config[CONF_NAME]

async def async_added_to_hass(self):
"""Register callbacks."""
self.unsub_dispatcher = async_dispatcher_connect(
self.hass, SIGNAL_SENSOR_UPDATE, self._handle_update
self.async_on_remove(
async_dispatcher_connect(
self.hass, SIGNAL_SENSOR_UPDATE, self._handle_update
)
)
state = await self.async_get_last_state()

Expand All @@ -49,11 +50,6 @@ async def async_added_to_hass(self):

self.async_restore_last_state(state)

async def async_will_remove_from_hass(self):
"""Disconnect dispatcher listener when removed."""
if self.unsub_dispatcher is not None:
self.unsub_dispatcher()

@callback
def async_restore_last_state(self, last_state):
"""Restore previous state."""
Expand Down
11 changes: 6 additions & 5 deletions homeassistant/components/mobile_app/notify.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,17 +84,16 @@ def log_rate_limits(hass, device_name, resp, level=logging.INFO):

async def async_get_service(hass, config, discovery_info=None):
"""Get the mobile_app notification service."""
session = async_get_clientsession(hass)
service = hass.data[DOMAIN][DATA_NOTIFY] = MobileAppNotificationService(session)
service = hass.data[DOMAIN][DATA_NOTIFY] = MobileAppNotificationService(hass)
return service


class MobileAppNotificationService(BaseNotificationService):
"""Implement the notification service for mobile_app."""

def __init__(self, session):
def __init__(self, hass):
"""Initialize the service."""
self._session = session
self._hass = hass

@property
def targets(self):
Expand Down Expand Up @@ -141,7 +140,9 @@ async def async_send_message(self, message="", **kwargs):

try:
with async_timeout.timeout(10):
response = await self._session.post(push_url, json=data)
response = await async_get_clientsession(self._hass).post(
push_url, json=data
)
result = await response.json()

if response.status in [HTTP_OK, HTTP_CREATED, HTTP_ACCEPTED]:
Expand Down
6 changes: 2 additions & 4 deletions homeassistant/components/mobile_app/sensor.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
"""Sensor platform for mobile_app."""
from functools import partial

from homeassistant.components.sensor import SensorEntity
from homeassistant.const import CONF_NAME, CONF_UNIQUE_ID, CONF_WEBHOOK_ID
from homeassistant.core import callback
Expand Down Expand Up @@ -50,7 +48,7 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
async_add_entities(entities)

@callback
def handle_sensor_registration(webhook_id, data):
def handle_sensor_registration(data):
if data[CONF_WEBHOOK_ID] != webhook_id:
return

Expand All @@ -68,7 +66,7 @@ def handle_sensor_registration(webhook_id, data):
async_dispatcher_connect(
hass,
f"{DOMAIN}_{ENTITY_TYPE}_register",
partial(handle_sensor_registration, webhook_id),
handle_sensor_registration,
)


Expand Down
8 changes: 3 additions & 5 deletions homeassistant/components/mobile_app/webhook.py
Original file line number Diff line number Diff line change
Expand Up @@ -472,6 +472,7 @@ async def webhook_update_sensor_states(hass, config_entry, data):

device_name = config_entry.data[ATTR_DEVICE_NAME]
resp = {}

for sensor in data:
entity_type = sensor[ATTR_SENSOR_TYPE]

Expand All @@ -495,8 +496,6 @@ async def webhook_update_sensor_states(hass, config_entry, data):
}
continue

entry = {CONF_WEBHOOK_ID: config_entry.data[CONF_WEBHOOK_ID]}

try:
sensor = sensor_schema_full(sensor)
except vol.Invalid as err:
Expand All @@ -513,9 +512,8 @@ async def webhook_update_sensor_states(hass, config_entry, data):
}
continue

new_state = {**entry, **sensor}

async_dispatcher_send(hass, SIGNAL_SENSOR_UPDATE, new_state)
sensor[CONF_WEBHOOK_ID] = config_entry.data[CONF_WEBHOOK_ID]
async_dispatcher_send(hass, SIGNAL_SENSOR_UPDATE, sensor)

resp[unique_id] = {"success": True}

Expand Down
6 changes: 5 additions & 1 deletion homeassistant/util/logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from typing import Any, Awaitable, Callable, Coroutine, cast, overload

from homeassistant.const import EVENT_HOMEASSISTANT_CLOSE
from homeassistant.core import HomeAssistant, callback
from homeassistant.core import HomeAssistant, callback, is_callback


class HideSensitiveDataFilter(logging.Filter):
Expand Down Expand Up @@ -138,6 +138,7 @@ async def async_wrapper(*args: Any) -> None:
log_exception(format_err, *args)

wrapper_func = async_wrapper

else:

@wraps(func)
Expand All @@ -148,6 +149,9 @@ def wrapper(*args: Any) -> None:
except Exception: # pylint: disable=broad-except
log_exception(format_err, *args)

if is_callback(check_func):
wrapper = callback(wrapper)

wrapper_func = wrapper
return wrapper_func

Expand Down
29 changes: 29 additions & 0 deletions tests/util/test_logging.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
"""Test Home Assistant logging util methods."""
import asyncio
from functools import partial
import logging
import queue
from unittest.mock import patch

import pytest

from homeassistant.core import callback, is_callback
import homeassistant.util.logging as logging_util


Expand Down Expand Up @@ -80,3 +82,30 @@ async def job():
await hass.async_block_till_done()
assert "This is a bad coroutine" in caplog.text
assert "in test_async_create_catching_coro" in caplog.text


def test_catch_log_exception():
"""Test it is still a callback after wrapping including partial."""

async def async_meth():
pass

assert asyncio.iscoroutinefunction(
logging_util.catch_log_exception(partial(async_meth), lambda: None)
)

@callback
def callback_meth():
pass

assert is_callback(
logging_util.catch_log_exception(partial(callback_meth), lambda: None)
)

def sync_meth():
pass

wrapped = logging_util.catch_log_exception(partial(sync_meth), lambda: None)

assert not is_callback(wrapped)
assert not asyncio.iscoroutinefunction(wrapped)