Skip to content

Commit

Permalink
Use typed config entry
Browse files Browse the repository at this point in the history
Add diagnostics
  • Loading branch information
pantherale0 authored Nov 22, 2024
1 parent 0108b9a commit ffd9536
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 19 deletions.
5 changes: 3 additions & 2 deletions custom_components/family_safety/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,13 @@

from .const import DOMAIN, AGG_ERROR, CONF_EXPR_DEFAULT, CONF_KEY_EXPR
from .coordinator import FamilySafetyCoordinator
from .config_entry import FamilySafetyConfigEntry

_LOGGER = logging.getLogger(__name__)
PLATFORMS = [Platform.SENSOR, Platform.SWITCH]


async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
async def async_setup_entry(hass: HomeAssistant, entry: FamilySafetyConfigEntry) -> bool:
"""Create ConfigEntry."""
hass.data.setdefault(DOMAIN, {})
_LOGGER.debug("Got request to setup entry.")
Expand All @@ -32,7 +33,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
experimental=entry.options.get(CONF_KEY_EXPR, CONF_EXPR_DEFAULT)
)
_LOGGER.debug("Login successful, setting up coordinator.")
hass.data[DOMAIN][entry.entry_id] = FamilySafetyCoordinator(
entry.runtime_data = FamilySafetyCoordinator(
hass,
familysafety,
entry.options.get("update_interval", entry.data["update_interval"]))
Expand Down
7 changes: 7 additions & 0 deletions custom_components/family_safety/config_entry.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
"""Define FamilySafety config entry"""

from homeassistant import config_entries

from .coordinator import FamilySafetyCoordinator

type FamilySafetyConfigEntry = config_entries.ConfigEntry[FamilySafetyCoordinator]
7 changes: 3 additions & 4 deletions custom_components/family_safety/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
OverrideTarget.WINDOWS,
OverrideTarget.XBOX
]
AGG_TITLE = "Aggregator error occured. "
AGG_HELP = "This is an upstream issue with Microsoft and is usually temporary. "
AGG_FUTURE = "Try reloading the integration in 15 minutes."
AGG_ERROR = f"{AGG_TITLE}{AGG_HELP}{AGG_FUTURE}"
AGG_ERROR = ("Aggregator error occured. "
"This is an upstream issue with Microsoft and is usually temporary. "
"Try reloading the integration in 15 minutes.")
60 changes: 60 additions & 0 deletions custom_components/family_safety/diagnostics.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
"""Diagnostics support for Reolink."""

from __future__ import annotations

from typing import Any

from homeassistant.core import HomeAssistant

from .config_entry import FamilySafetyConfigEntry


async def async_get_config_entry_diagnostics(
hass: HomeAssistant, config_entry: FamilySafetyConfigEntry
) -> dict[str, Any]:
"""Return diagnostics for a config entry."""
data = config_entry.runtime_data
diagnostic_data = {
"accounts": [],
"pending_requests": data.api.pending_requests
}
accounts = data.api.accounts
for acc in accounts:
acc_data = {
"id": acc.user_id,
"role": acc.role,
"devices": [],
"applications": [],
"today_screentime_usage": acc.today_screentime_usage,
"average_screentime_usage": acc.average_screentime_usage,
"screentime_usage": acc.screentime_usage,
"blocked_platforms": [str(x) for x in acc.blocked_platforms],
"experimental": acc.experimental,
"acc_balance": acc.account_balance,
"acc_currency": acc.account_currency
}
for dev in acc.devices:
acc_data["devices"].append({
"id": dev.device_id,
"class": dev.device_class,
"make": dev.device_make,
"model": dev.device_model,
"form_factor": dev.form_factor,
"os_name": dev.os_name,
"today_screentime_used": dev.today_time_used,
"issues": dev.issues,
"states": dev.states,
"last_seen": dev.last_seen,
"blocked": dev.blocked
})
for app in acc.applications:
acc_data["applications"].append({
"id": app.app_id,
"name": app.name,
"icon": app.icon,
"policy": app.policy,
"blocked": app.blocked
})
diagnostic_data["accounts"].append(acc_data)

return diagnostic_data
14 changes: 7 additions & 7 deletions custom_components/family_safety/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
from pyfamilysafety.application import Application

from homeassistant.components.sensor import SensorEntity, SensorEntityDescription, SensorDeviceClass
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import (
AddEntitiesCallback,
Expand All @@ -22,6 +21,7 @@
from .coordinator import FamilySafetyCoordinator

from .const import DOMAIN, CONF_KEY_EXPR, CONF_EXPR_DEFAULT
from .config_entry import FamilySafetyConfigEntry

from .entity_base import ManagedAccountEntity

Expand Down Expand Up @@ -71,19 +71,19 @@ class FamilySafetySensorEntityDescription(SensorEntityDescription):

async def async_setup_entry(
hass: HomeAssistant,
config_entry: ConfigEntry,
config_entry: FamilySafetyConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
"""Set up Family Safety sensors."""
accounts: list[Account] = hass.data[DOMAIN][config_entry.entry_id].api.accounts
accounts: list[Account] = config_entry.runtime_data.api.accounts
entities = []
for account in accounts:
if (account.user_id in config_entry.options.get("accounts", [])) or (
len(config_entry.options.get("accounts", [])) == 0
):
for app in config_entry.options.get("tracked_applications", []):
entities.append(ScreentimeSensor(
coordinator=hass.data[DOMAIN][config_entry.entry_id],
coordinator=config_entry.runtime_data,
description=FamilySafetySensorEntityDescription(
key=app,
device_class=SensorDeviceClass.DURATION,
Expand All @@ -96,15 +96,15 @@ async def async_setup_entry(
))
entities.extend(
[ScreentimeSensor(
coordinator=hass.data[DOMAIN][config_entry.entry_id],
coordinator=config_entry.runtime_data,
idx=None,
account_id=account.user_id,
description=desc
) for desc in TIME_SENSORS.values()]
)
entities.extend(
[GenericSensor(
coordinator=hass.data[DOMAIN][config_entry.entry_id],
coordinator=config_entry.runtime_data,
idx=None,
account_id=account.user_id,
description=desc
Expand All @@ -113,7 +113,7 @@ async def async_setup_entry(
if config_entry.options.get(CONF_KEY_EXPR, CONF_EXPR_DEFAULT):
entities.extend(
[GenericSensor(
coordinator=hass.data[DOMAIN][config_entry.entry_id],
coordinator=config_entry.runtime_data,
idx=None,
account_id=account.user_id,
description=desc
Expand Down
11 changes: 5 additions & 6 deletions custom_components/family_safety/switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,25 @@
from pyfamilysafety import Account

from homeassistant.components.switch import SwitchEntity, SwitchDeviceClass
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback

from .coordinator import FamilySafetyCoordinator

from .const import DOMAIN, DEFAULT_OVERRIDE_ENTITIES

from .config_entry import FamilySafetyConfigEntry
from .entity_base import PlatformOverrideEntity, ApplicationEntity

_LOGGER = logging.getLogger(__name__)


async def async_setup_entry(
hass: HomeAssistant,
config_entry: ConfigEntry,
config_entry: FamilySafetyConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
"""Set up Family Safety switches."""
accounts: list[Account] = hass.data[DOMAIN][config_entry.entry_id].api.accounts
accounts: list[Account] = config_entry.runtime_data.api.accounts
entities = []
for account in accounts:
if (
Expand All @@ -36,7 +35,7 @@ async def async_setup_entry(
for platform in DEFAULT_OVERRIDE_ENTITIES:
entities.append(
PlatformOverrideSwitch(
coordinator=hass.data[DOMAIN][config_entry.entry_id],
coordinator=config_entry.runtime_data,
idx=None,
account_id=account.user_id,
platform=platform,
Expand All @@ -45,7 +44,7 @@ async def async_setup_entry(
for app in config_entry.options.get("tracked_applications", []):
entities.append(
ApplicationBlockSwitch(
coordinator=hass.data[DOMAIN][config_entry.entry_id],
coordinator=config_entry.runtime_data,
idx=None,
account_id=account.user_id,
app_id=app,
Expand Down

0 comments on commit ffd9536

Please sign in to comment.