generated from ludeeus/integration_blueprint
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add entity to override platform state add options flow bump pynintendoparental to 0.0.2 update translations update readme middleware now uses bootstrap add more info to middleware
- Loading branch information
1 parent
55398a5
commit 873b926
Showing
9 changed files
with
199 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
"""Nintendo Switch Parental Controls switch platform.""" | ||
|
||
import logging | ||
from typing import Any | ||
|
||
from pynintendoparental.device import Device | ||
|
||
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 NintendoUpdateCoordinator | ||
|
||
from .const import ( | ||
DOMAIN | ||
) | ||
|
||
from .entity import NintendoDevice | ||
|
||
_LOGGER = logging.getLogger(__name__) | ||
|
||
async def async_setup_entry( | ||
hass: HomeAssistant, | ||
entry: ConfigEntry, | ||
async_add_entities: AddEntitiesCallback | ||
) -> None: | ||
"""Set up Nintendo Switch Parental Control switches.""" | ||
coordinator: NintendoUpdateCoordinator = hass.data[DOMAIN][entry.entry_id] | ||
entities = [] | ||
for device in coordinator.api.devices: | ||
entities.append( | ||
DeviceOverrideSwitch(coordinator, device.device_id) | ||
) | ||
async_add_entities(entities, True) | ||
|
||
class DeviceOverrideSwitch(NintendoDevice, SwitchEntity): | ||
"""Defines a switch that can enable or disable a device.""" | ||
|
||
def __init__( | ||
self, | ||
coordinator, | ||
device_id | ||
) -> None: | ||
"""Initialize the sensor class.""" | ||
super().__init__(coordinator, device_id, "override") | ||
self._old_state = self._device.limit_time | ||
|
||
@property | ||
def name(self) -> str: | ||
return "Block Device Access" | ||
|
||
@property | ||
def is_on(self) -> bool: | ||
return self._device.limit_time == 0 | ||
|
||
@property | ||
def device_class(self) -> SwitchDeviceClass | None: | ||
return SwitchDeviceClass.SWITCH | ||
|
||
async def async_turn_on(self, **kwargs: Any) -> None: | ||
self._old_state = self._device.limit_time | ||
return await self._device.update_max_daily_playtime(0) | ||
|
||
async def async_turn_off(self, **kwargs: Any) -> None: | ||
if self._old_state == 0: | ||
_LOGGER.warning("Attempted to disable device block when screentime limit was already 0, defaulting to 180 minutes.") | ||
# defaulting to 180 minutes | ||
await self._device.update_max_daily_playtime(180) | ||
else: | ||
await self._device.update_max_daily_playtime(self._old_state) | ||
await self.coordinator.async_request_refresh() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters