Skip to content

Commit

Permalink
1.2.0
Browse files Browse the repository at this point in the history
fix Authentication doesn't work #35
resolve timezone issues preventing correct screen time values (#34)
update translations
bump pyfamilysafety to 0.3.0
add account name to entity names
  • Loading branch information
pantherale0 committed Feb 24, 2024
1 parent 76d50b1 commit 7b9c172
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 54 deletions.
4 changes: 2 additions & 2 deletions custom_components/family_safety/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
"iot_class": "cloud_polling",
"issue_tracker": "https://github.com/pantherale0/ha-familysafety/issues",
"requirements": [
"pyfamilysafety==0.2.1"
"pyfamilysafety==0.3.0"
],
"ssdp": [],
"version": "1.1.1",
"version": "1.2.0",
"zeroconf": []
}
58 changes: 27 additions & 31 deletions custom_components/family_safety/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,42 +11,44 @@
from homeassistant.components.sensor import SensorEntity, SensorDeviceClass
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback, async_get_current_platform
from homeassistant.helpers.entity_platform import (
AddEntitiesCallback,
async_get_current_platform,
)

from .coordinator import FamilySafetyCoordinator

from .const import (
DOMAIN
)
from .const import DOMAIN

from .entity_base import ManagedAccountEntity, ApplicationEntity

_LOGGER = logging.getLogger(__name__)


async def async_setup_entry(
hass: HomeAssistant,
config_entry: ConfigEntry,
async_add_entities: AddEntitiesCallback
hass: HomeAssistant,
config_entry: ConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
"""Set up Family Safety sensors."""
accounts: list[Account] = hass.data[DOMAIN][config_entry.entry_id].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
len(config_entry.options.get("accounts", [])) == 0
):
entities.append(
AccountScreentimeSensor(
coordinator=hass.data[DOMAIN][config_entry.entry_id],
idx=None,
account_id=account.user_id
account_id=account.user_id,
)
)
entities.append(
AccountBalanceSensor(
coordinator=hass.data[DOMAIN][config_entry.entry_id],
idx=None,
account_id=account.user_id
account_id=account.user_id,
)
)
for app in config_entry.options.get("tracked_applications", []):
Expand All @@ -55,7 +57,7 @@ async def async_setup_entry(
coordinator=hass.data[DOMAIN][config_entry.entry_id],
idx=None,
account_id=account.user_id,
app_id=app
app_id=app,
)
)

Expand All @@ -64,19 +66,16 @@ async def async_setup_entry(
platform = async_get_current_platform()
platform.async_register_entity_service(
name="block_app",
schema={
vol.Required("name"): str
},
func="async_block_application"
schema={vol.Required("name"): str},
func="async_block_application",
)
platform.async_register_entity_service(
name="unblock_app",
schema={
vol.Required("name"): str
},
func="async_unblock_application"
schema={vol.Required("name"): str},
func="async_unblock_application",
)


class AccountBalanceSensor(ManagedAccountEntity, SensorEntity):
"""A balance sensor for the account."""

Expand All @@ -87,7 +86,7 @@ def __init__(self, coordinator: FamilySafetyCoordinator, idx, account_id) -> Non
@property
def name(self) -> str:
"""Return name of entity."""
return "Available Balance"
return f"{self._account.first_name} Available Balance"

@property
def native_value(self) -> float:
Expand All @@ -104,6 +103,7 @@ def device_class(self) -> SensorDeviceClass | None:
"""Return device class."""
return SensorDeviceClass.MONETARY


class AccountScreentimeSensor(ManagedAccountEntity, SensorEntity):
"""Aggregate screentime sensor."""

Expand All @@ -114,12 +114,12 @@ def __init__(self, coordinator: FamilySafetyCoordinator, idx, account_id) -> Non
@property
def name(self) -> str:
"""Return entity name."""
return "Used Screen Time"
return f"{self._account.first_name} Used Screen Time"

@property
def native_value(self) -> float:
"""Return duration (minutes)."""
return (self._account.today_screentime_usage/1000)/60
return (self._account.today_screentime_usage / 1000) / 60

@property
def native_unit_of_measurement(self) -> str | None:
Expand All @@ -137,24 +137,22 @@ def extra_state_attributes(self) -> Mapping[str, Any] | None:
devices = {}
for device in self._account.devices:
if device.today_time_used:
devices[device.device_name] = (device.today_time_used/1000)/60
devices[device.device_name] = (device.today_time_used / 1000) / 60
else:
devices[device.device_name] = 0
applications = {}
for app in self._account.applications:
applications[app.name] = app.usage
return {
"application_usage": applications,
"device_usage": devices
}
return {"application_usage": applications, "device_usage": devices}


class ApplicationScreentimeSensor(ApplicationEntity, SensorEntity):
"""Application specific screentime sensor."""

@property
def name(self) -> str:
"""Return entity name."""
return f"{self._application.name} Used Screen Time"
return f"{self._account.first_name} {self._application.name} Used Screen Time"

@property
def native_value(self) -> float:
Expand All @@ -174,6 +172,4 @@ def device_class(self) -> SensorDeviceClass | None:
@property
def extra_state_attributes(self) -> Mapping[str, Any] | None:
"""Return additional state attributes."""
return {
"blocked": self._application.blocked
}
return {"blocked": self._application.blocked}
37 changes: 18 additions & 19 deletions custom_components/family_safety/switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,35 +12,34 @@

from .coordinator import FamilySafetyCoordinator

from .const import (
DOMAIN,
DEFAULT_OVERRIDE_ENTITIES
)
from .const import DOMAIN, DEFAULT_OVERRIDE_ENTITIES

from .entity_base import PlatformOverrideEntity, ApplicationEntity

_LOGGER = logging.getLogger(__name__)


async def async_setup_entry(
hass: HomeAssistant,
config_entry: ConfigEntry,
async_add_entities: AddEntitiesCallback
hass: HomeAssistant,
config_entry: ConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
"""Set up Family Safety switches."""
accounts: list[Account] = hass.data[DOMAIN][config_entry.entry_id].api.accounts
entities = []
for account in accounts:
if (config_entry.options.get("accounts", None) is None) or (
account.user_id in config_entry.options.get("accounts", [])) or (
len(config_entry.options.get("accounts", []))==0
if (
(config_entry.options.get("accounts", None) is None)
or (account.user_id in config_entry.options.get("accounts", []))
or (len(config_entry.options.get("accounts", [])) == 0)
):
for platform in DEFAULT_OVERRIDE_ENTITIES:
entities.append(
PlatformOverrideSwitch(
coordinator=hass.data[DOMAIN][config_entry.entry_id],
idx=None,
account_id=account.user_id,
platform=platform
platform=platform,
)
)
for app in config_entry.options.get("tracked_applications", []):
Expand All @@ -49,19 +48,20 @@ async def async_setup_entry(
coordinator=hass.data[DOMAIN][config_entry.entry_id],
idx=None,
account_id=account.user_id,
app_id=app
app_id=app,
)
)

async_add_entities(entities, True)


class ApplicationBlockSwitch(ApplicationEntity, SwitchEntity):
"""Define application switch."""

@property
def name(self) -> str:
"""Return entity name."""
return f"Block {self._application.name}"
return f"{self._account.first_name} Block {self._application.name}"

@property
def is_on(self) -> bool:
Expand All @@ -83,21 +83,20 @@ async def async_turn_on(self, **kwargs: Any) -> None:
await self._application.block_app()
await self.coordinator.async_request_refresh()


class PlatformOverrideSwitch(PlatformOverrideEntity, SwitchEntity):
"""Platform override switch."""

def __init__(self,
coordinator: FamilySafetyCoordinator,
idx,
account_id,
platform) -> None:
def __init__(
self, coordinator: FamilySafetyCoordinator, idx, account_id, platform
) -> None:
"""Create PlatformOverrideSwitch."""
super().__init__(coordinator, idx, account_id, platform)

@property
def name(self) -> str:
"""Return entity name."""
return f"Block {str(self._platform)}"
return f"{self._account.first_name} Block {str(self._platform)}"

@property
def is_on(self) -> bool:
Expand Down
2 changes: 1 addition & 1 deletion custom_components/family_safety/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"config": {
"step": {
"user": {
"description": "If you need help with the configuration have a look here: https://github.com/pantherale0/ha-familysafety",
"description": "If you need help with the configuration have a look here: https://github.com/pantherale0/ha-familysafety - Click this link [Microsoft Login](https://login.live.com/oauth20_authorize.srf?cobrandid=b5d15d4b-695a-4cd5-93c6-13f551b310df&client_id=000000000004893A&response_type=code&redirect_uri=https%3A%2F%2Flogin.live.com%2Foauth20_desktop.srf&response_mode=query&scope=service%3A%3Afamilymobile.microsoft.com%3A%3AMBI_SSL&lw=1&fl=easi2&login_hint=) to retrieve the OAuth response URL.",
"data": {
"refresh_token": "Refresh token",
"response_url": "OAuth response URL",
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ colorlog==6.8.0
homeassistant==2023.2.0
pip>=21.0,<23.4
ruff==0.1.9
pyfamilysafety==0.2.1
pyfamilysafety==0.3.0

0 comments on commit 7b9c172

Please sign in to comment.