Enable strict typing for the tautulli integration (#55448)

This commit is contained in:
Joakim Sørensen 2021-09-23 16:56:21 +02:00 committed by GitHub
parent 1a47fcc4e3
commit 98d0c84468
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 60 additions and 22 deletions

View File

@ -104,6 +104,7 @@ homeassistant.components.switcher_kis.*
homeassistant.components.synology_dsm.* homeassistant.components.synology_dsm.*
homeassistant.components.systemmonitor.* homeassistant.components.systemmonitor.*
homeassistant.components.tag.* homeassistant.components.tag.*
homeassistant.components.tautulli.*
homeassistant.components.tcp.* homeassistant.components.tcp.*
homeassistant.components.tile.* homeassistant.components.tile.*
homeassistant.components.tradfri.* homeassistant.components.tradfri.*

View File

@ -1,4 +1,8 @@
"""A platform which allows you to get information from Tautulli.""" """A platform which allows you to get information from Tautulli."""
from __future__ import annotations
from typing import Any
from pytautulli import PyTautulli from pytautulli import PyTautulli
import voluptuous as vol import voluptuous as vol
@ -13,8 +17,11 @@ from homeassistant.const import (
CONF_SSL, CONF_SSL,
CONF_VERIFY_SSL, CONF_VERIFY_SSL,
) )
from homeassistant.core import HomeAssistant
from homeassistant.helpers.aiohttp_client import async_get_clientsession from homeassistant.helpers.aiohttp_client import async_get_clientsession
import homeassistant.helpers.config_validation as cv import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType, StateType
from homeassistant.helpers.update_coordinator import CoordinatorEntity from homeassistant.helpers.update_coordinator import CoordinatorEntity
from .coordinator import TautulliDataUpdateCoordinator from .coordinator import TautulliDataUpdateCoordinator
@ -42,20 +49,26 @@ PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
) )
async def async_setup_platform(hass, config, async_add_entities, discovery_info=None): async def async_setup_platform(
hass: HomeAssistant,
config: ConfigType,
async_add_entities: AddEntitiesCallback,
discovery_info: DiscoveryInfoType | None = None,
) -> None:
"""Create the Tautulli sensor.""" """Create the Tautulli sensor."""
name = config.get(CONF_NAME) name = config[CONF_NAME]
host = config[CONF_HOST] host = config[CONF_HOST]
port = config.get(CONF_PORT) port = config[CONF_PORT]
path = config.get(CONF_PATH) path = config[CONF_PATH]
api_key = config[CONF_API_KEY] api_key = config[CONF_API_KEY]
monitored_conditions = config.get(CONF_MONITORED_CONDITIONS) monitored_conditions = config.get(CONF_MONITORED_CONDITIONS, [])
user = config.get(CONF_MONITORED_USERS) users = config.get(CONF_MONITORED_USERS, [])
use_ssl = config[CONF_SSL] use_ssl = config[CONF_SSL]
verify_ssl = config.get(CONF_VERIFY_SSL) verify_ssl = config[CONF_VERIFY_SSL]
session = async_get_clientsession(hass=hass, verify_ssl=verify_ssl)
session = async_get_clientsession(hass, verify_ssl)
api_client = PyTautulli( api_client = PyTautulli(
api_token=api_key, api_token=api_key,
hostname=host, hostname=host,
@ -68,9 +81,17 @@ async def async_setup_platform(hass, config, async_add_entities, discovery_info=
coordinator = TautulliDataUpdateCoordinator(hass=hass, api_client=api_client) coordinator = TautulliDataUpdateCoordinator(hass=hass, api_client=api_client)
entities = [TautulliSensor(coordinator, name, monitored_conditions, user)] async_add_entities(
new_entities=[
async_add_entities(entities, True) TautulliSensor(
coordinator=coordinator,
name=name,
monitored_conditions=monitored_conditions,
usernames=users,
)
],
update_before_add=True,
)
class TautulliSensor(CoordinatorEntity, SensorEntity): class TautulliSensor(CoordinatorEntity, SensorEntity):
@ -78,37 +99,43 @@ class TautulliSensor(CoordinatorEntity, SensorEntity):
coordinator: TautulliDataUpdateCoordinator coordinator: TautulliDataUpdateCoordinator
def __init__(self, coordinator, name, monitored_conditions, users): def __init__(
self,
coordinator: TautulliDataUpdateCoordinator,
name: str,
monitored_conditions: list[str],
usernames: list[str],
) -> None:
"""Initialize the Tautulli sensor.""" """Initialize the Tautulli sensor."""
super().__init__(coordinator) super().__init__(coordinator)
self.monitored_conditions = monitored_conditions self.monitored_conditions = monitored_conditions
self.usernames = users self.usernames = usernames
self._name = name self._name = name
@property @property
def name(self): def name(self) -> str:
"""Return the name of the sensor.""" """Return the name of the sensor."""
return self._name return self._name
@property @property
def native_value(self): def native_value(self) -> StateType:
"""Return the state of the sensor.""" """Return the state of the sensor."""
if not self.coordinator.activity: if not self.coordinator.activity:
return 0 return 0
return self.coordinator.activity.stream_count return self.coordinator.activity.stream_count or 0
@property @property
def icon(self): def icon(self) -> str:
"""Return the icon of the sensor.""" """Return the icon of the sensor."""
return "mdi:plex" return "mdi:plex"
@property @property
def native_unit_of_measurement(self): def native_unit_of_measurement(self) -> str:
"""Return the unit this state is expressed in.""" """Return the unit this state is expressed in."""
return "Watching" return "Watching"
@property @property
def extra_state_attributes(self): def extra_state_attributes(self) -> dict[str, Any] | None:
"""Return attributes for the sensor.""" """Return attributes for the sensor."""
if ( if (
not self.coordinator.activity not self.coordinator.activity
@ -149,8 +176,7 @@ class TautulliSensor(CoordinatorEntity, SensorEntity):
continue continue
_attributes[session.username]["Activity"] = session.state _attributes[session.username]["Activity"] = session.state
if self.monitored_conditions: for key in self.monitored_conditions:
for key in self.monitored_conditions: _attributes[session.username][key] = getattr(session, key)
_attributes[session.username][key] = getattr(session, key)
return _attributes return _attributes

View File

@ -1155,6 +1155,17 @@ no_implicit_optional = true
warn_return_any = true warn_return_any = true
warn_unreachable = true warn_unreachable = true
[mypy-homeassistant.components.tautulli.*]
check_untyped_defs = true
disallow_incomplete_defs = true
disallow_subclassing_any = true
disallow_untyped_calls = true
disallow_untyped_decorators = true
disallow_untyped_defs = true
no_implicit_optional = true
warn_return_any = true
warn_unreachable = true
[mypy-homeassistant.components.tcp.*] [mypy-homeassistant.components.tcp.*]
check_untyped_defs = true check_untyped_defs = true
disallow_incomplete_defs = true disallow_incomplete_defs = true