Fix type issues [mobile_app] (#67091)

This commit is contained in:
Marc Mueller 2022-02-23 17:47:54 +01:00 committed by GitHub
parent 49aabcb2ac
commit 8b7639940e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 24 additions and 53 deletions

View file

@ -1,4 +1,6 @@
"""Binary sensor platform for mobile_app."""
from typing import Any
from homeassistant.components.binary_sensor import BinarySensorEntity
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import CONF_NAME, CONF_UNIQUE_ID, CONF_WEBHOOK_ID, STATE_ON
@ -18,7 +20,6 @@ from .const import (
ATTR_SENSOR_TYPE,
ATTR_SENSOR_TYPE_BINARY_SENSOR as ENTITY_TYPE,
ATTR_SENSOR_UNIQUE_ID,
DATA_DEVICES,
DOMAIN,
)
from .entity import MobileAppEntity, unique_id
@ -39,7 +40,7 @@ async def async_setup_entry(
for entry in entries:
if entry.domain != ENTITY_TYPE or entry.disabled_by:
continue
config = {
config: dict[str, Any] = {
ATTR_SENSOR_ATTRIBUTES: {},
ATTR_SENSOR_DEVICE_CLASS: entry.device_class or entry.original_device_class,
ATTR_SENSOR_ICON: entry.original_icon,
@ -49,7 +50,7 @@ async def async_setup_entry(
ATTR_SENSOR_UNIQUE_ID: entry.unique_id,
ATTR_SENSOR_ENTITY_CATEGORY: entry.entity_category,
}
entities.append(MobileAppBinarySensor(config, entry.device_id, config_entry))
entities.append(MobileAppBinarySensor(config, config_entry))
async_add_entities(entities)
@ -65,9 +66,7 @@ async def async_setup_entry(
CONF_NAME
] = f"{config_entry.data[ATTR_DEVICE_NAME]} {data[ATTR_SENSOR_NAME]}"
device = hass.data[DOMAIN][DATA_DEVICES][data[CONF_WEBHOOK_ID]]
async_add_entities([MobileAppBinarySensor(data, device, config_entry)])
async_add_entities([MobileAppBinarySensor(data, config_entry)])
async_dispatcher_connect(
hass,

View file

@ -7,6 +7,7 @@ from homeassistant.components import notify
from homeassistant.components.device_automation import InvalidDeviceAutomationConfig
from homeassistant.const import CONF_DEVICE_ID, CONF_DOMAIN, CONF_TYPE
from homeassistant.core import Context, HomeAssistant
from homeassistant.exceptions import TemplateError
from homeassistant.helpers import config_validation as cv, template
from .const import DOMAIN
@ -62,7 +63,7 @@ async def async_call_action_from_config(
try:
service_data[key] = template.render_complex(value_template, variables)
except template.TemplateError as err:
except TemplateError as err:
raise InvalidDeviceAutomationConfig(
f"Error rendering {key}: {err}"
) from err

View file

@ -37,7 +37,6 @@ async def async_setup_entry(
"""Set up OwnTracks based off an entry."""
entity = MobileAppEntity(entry)
async_add_entities([entity])
return True
class MobileAppEntity(TrackerEntity, RestoreEntity):

View file

@ -1,4 +1,6 @@
"""A entity class for mobile_app."""
from __future__ import annotations
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import (
ATTR_ICON,
@ -8,7 +10,6 @@ from homeassistant.const import (
STATE_UNAVAILABLE,
)
from homeassistant.core import callback
from homeassistant.helpers.device_registry import DeviceEntry
from homeassistant.helpers.dispatcher import async_dispatcher_connect
from homeassistant.helpers.restore_state import RestoreEntity
@ -33,10 +34,9 @@ def unique_id(webhook_id, sensor_unique_id):
class MobileAppEntity(RestoreEntity):
"""Representation of an mobile app entity."""
def __init__(self, config: dict, device: DeviceEntry, entry: ConfigEntry) -> None:
def __init__(self, config: dict, entry: ConfigEntry) -> None:
"""Initialize the entity."""
self._config = config
self._device = device
self._entry = entry
self._registration = entry.data
self._unique_id = config[CONF_UNIQUE_ID]

View file

@ -60,7 +60,7 @@ def setup_encrypt() -> tuple[int, Callable]:
return (SecretBox.KEY_SIZE, encrypt)
def _decrypt_payload(key: str, ciphertext: str) -> dict[str, str]:
def _decrypt_payload(key: str | None, ciphertext: str) -> dict[str, str] | None:
"""Decrypt encrypted payload."""
try:
keylen, decrypt = setup_decrypt()
@ -72,13 +72,13 @@ def _decrypt_payload(key: str, ciphertext: str) -> dict[str, str]:
_LOGGER.warning("Ignoring encrypted payload because no decryption key known")
return None
key = key.encode("utf-8")
key = key[:keylen]
key = key.ljust(keylen, b"\0")
key_bytes = key.encode("utf-8")
key_bytes = key_bytes[:keylen]
key_bytes = key_bytes.ljust(keylen, b"\0")
try:
message = decrypt(ciphertext, key)
message = json.loads(message.decode("utf-8"))
msg_bytes = decrypt(ciphertext, key_bytes)
message = json.loads(msg_bytes.decode("utf-8"))
_LOGGER.debug("Successfully decrypted mobile_app payload")
return message
except ValueError:

View file

@ -4,6 +4,7 @@ from __future__ import annotations
from contextlib import suppress
from http import HTTPStatus
import secrets
from typing import cast
from aiohttp.web import Request, Response
import emoji
@ -89,7 +90,7 @@ class RegistrationsView(HomeAssistantView):
# If otherwise empty string contains emoji
# use descriptive name of the first emoji
data[ATTR_DEVICE_NAME] = emoji.demojize(
emoji.emoji_lis(data[ATTR_DEVICE_NAME])[0]["emoji"]
cast(str, emoji.emoji_lis(data[ATTR_DEVICE_NAME])[0]["emoji"])
).replace(":", "")
else:
# Fallback to DEVICE_ID

View file

@ -28,7 +28,7 @@ class PushChannel:
self.support_confirm = support_confirm
self._send_message = send_message
self.on_teardown = on_teardown
self.pending_confirms = {}
self.pending_confirms: dict[str, dict] = {}
@callback
def async_send_notification(self, data, fallback_send):

View file

@ -1,6 +1,8 @@
"""Sensor platform for mobile_app."""
from __future__ import annotations
from typing import Any
from homeassistant.components.sensor import SensorDeviceClass, SensorEntity
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import (
@ -28,7 +30,6 @@ from .const import (
ATTR_SENSOR_TYPE_SENSOR as ENTITY_TYPE,
ATTR_SENSOR_UNIQUE_ID,
ATTR_SENSOR_UOM,
DATA_DEVICES,
DOMAIN,
)
from .entity import MobileAppEntity, unique_id
@ -49,7 +50,7 @@ async def async_setup_entry(
for entry in entries:
if entry.domain != ENTITY_TYPE or entry.disabled_by:
continue
config = {
config: dict[str, Any] = {
ATTR_SENSOR_ATTRIBUTES: {},
ATTR_SENSOR_DEVICE_CLASS: entry.device_class or entry.original_device_class,
ATTR_SENSOR_ICON: entry.original_icon,
@ -60,7 +61,7 @@ async def async_setup_entry(
ATTR_SENSOR_UOM: entry.unit_of_measurement,
ATTR_SENSOR_ENTITY_CATEGORY: entry.entity_category,
}
entities.append(MobileAppSensor(config, entry.device_id, config_entry))
entities.append(MobileAppSensor(config, config_entry))
async_add_entities(entities)
@ -76,9 +77,7 @@ async def async_setup_entry(
CONF_NAME
] = f"{config_entry.data[ATTR_DEVICE_NAME]} {data[ATTR_SENSOR_NAME]}"
device = hass.data[DOMAIN][DATA_DEVICES][data[CONF_WEBHOOK_ID]]
async_add_entities([MobileAppSensor(data, device, config_entry)])
async_add_entities([MobileAppSensor(data, config_entry)])
async_dispatcher_connect(
hass,

View file

@ -2477,27 +2477,6 @@ ignore_errors = true
[mypy-homeassistant.components.minecraft_server.sensor]
ignore_errors = true
[mypy-homeassistant.components.mobile_app.binary_sensor]
ignore_errors = true
[mypy-homeassistant.components.mobile_app.device_action]
ignore_errors = true
[mypy-homeassistant.components.mobile_app.device_tracker]
ignore_errors = true
[mypy-homeassistant.components.mobile_app.helpers]
ignore_errors = true
[mypy-homeassistant.components.mobile_app.http_api]
ignore_errors = true
[mypy-homeassistant.components.mobile_app.push_notification]
ignore_errors = true
[mypy-homeassistant.components.mobile_app.sensor]
ignore_errors = true
[mypy-homeassistant.components.netgear]
ignore_errors = true

View file

@ -94,13 +94,6 @@ IGNORED_MODULES: Final[list[str]] = [
"homeassistant.components.minecraft_server",
"homeassistant.components.minecraft_server.helpers",
"homeassistant.components.minecraft_server.sensor",
"homeassistant.components.mobile_app.binary_sensor",
"homeassistant.components.mobile_app.device_action",
"homeassistant.components.mobile_app.device_tracker",
"homeassistant.components.mobile_app.helpers",
"homeassistant.components.mobile_app.http_api",
"homeassistant.components.mobile_app.push_notification",
"homeassistant.components.mobile_app.sensor",
"homeassistant.components.netgear",
"homeassistant.components.netgear.config_flow",
"homeassistant.components.netgear.device_tracker",