From f63e0a6dfeab1a05ba09ef3e0087cb404ba0dac4 Mon Sep 17 00:00:00 2001 From: RyanClark123 Date: Wed, 9 Oct 2024 20:51:05 +0100 Subject: [PATCH] Added base station as a switch ### Feature * Base station has now been removed from binary sensors and added as a switch --- custom_components/owlet/__init__.py | 2 +- custom_components/owlet/binary_sensor.py | 12 ++-- custom_components/owlet/manifest.json | 2 +- custom_components/owlet/strings.json | 8 ++- custom_components/owlet/switch.py | 58 ++++++++++++++++++++ custom_components/owlet/translations/en.json | 8 ++- custom_components/owlet/translations/fr.json | 8 ++- custom_components/owlet/translations/uk.json | 8 ++- 8 files changed, 85 insertions(+), 21 deletions(-) create mode 100644 custom_components/owlet/switch.py diff --git a/custom_components/owlet/__init__.py b/custom_components/owlet/__init__.py index 79e1de9..ca05453 100644 --- a/custom_components/owlet/__init__.py +++ b/custom_components/owlet/__init__.py @@ -30,7 +30,7 @@ from homeassistant.helpers.aiohttp_client import async_get_clientsession from .const import CONF_OWLET_EXPIRY, CONF_OWLET_REFRESH, DOMAIN, SUPPORTED_VERSIONS from .coordinator import OwletCoordinator -PLATFORMS: list[Platform] = [Platform.BINARY_SENSOR, Platform.SENSOR] +PLATFORMS: list[Platform] = [Platform.BINARY_SENSOR, Platform.SENSOR, Platform.SWITCH] _LOGGER = logging.getLogger(__name__) diff --git a/custom_components/owlet/binary_sensor.py b/custom_components/owlet/binary_sensor.py index 81e7bf9..9674335 100644 --- a/custom_components/owlet/binary_sensor.py +++ b/custom_components/owlet/binary_sensor.py @@ -92,12 +92,6 @@ SENSORS: tuple[OwletBinarySensorEntityDescription, ...] = ( device_class=BinarySensorDeviceClass.POWER, available_during_charging=True, ), - OwletBinarySensorEntityDescription( - key="base_station_on", - translation_key="base_on", - device_class=BinarySensorDeviceClass.POWER, - available_during_charging=True, - ), ) @@ -177,4 +171,8 @@ class OwletAwakeSensor(OwletBinarySensor): @property def is_on(self) -> bool: """Return true if the binary sensor is on.""" - return False if self.sock.properties[self.entity_description.key] in [8, 15] else True + return ( + False + if self.sock.properties[self.entity_description.key] in [8, 15] + else True + ) diff --git a/custom_components/owlet/manifest.json b/custom_components/owlet/manifest.json index a554df8..9f44c45 100644 --- a/custom_components/owlet/manifest.json +++ b/custom_components/owlet/manifest.json @@ -11,5 +11,5 @@ "requirements": [ "pyowletapi==2024.10.1" ], - "version": "2024.9.1" + "version": "2024.10.1" } \ No newline at end of file diff --git a/custom_components/owlet/strings.json b/custom_components/owlet/strings.json index 3bf9f63..6f9ec72 100644 --- a/custom_components/owlet/strings.json +++ b/custom_components/owlet/strings.json @@ -74,9 +74,6 @@ }, "awake": { "name": "Awake" - }, - "base_on": { - "name": "Base station on" } }, "sensor": { @@ -116,6 +113,11 @@ "movementbucket": { "name": "Movement bucket" } + }, + "switch": { + "base_on": { + "name": "Base station on" + } } } } \ No newline at end of file diff --git a/custom_components/owlet/switch.py b/custom_components/owlet/switch.py new file mode 100644 index 0000000..32b83f4 --- /dev/null +++ b/custom_components/owlet/switch.py @@ -0,0 +1,58 @@ +"""Support for Owlet switches.""" + +from __future__ import annotations + +from dataclasses import dataclass +from datetime import timedelta +from typing import Any + +from homeassistant.config_entries import ConfigEntry +from homeassistant.components.switch import SwitchEntity, SwitchEntityDescription +from homeassistant.core import HomeAssistant +from homeassistant.helpers.entity_platform import AddEntitiesCallback + +from .const import DOMAIN +from .coordinator import OwletCoordinator +from .entity import OwletBaseEntity + +SCAN_INTERVAL = timedelta(seconds=5) +PARALLEL_UPDATES = 0 + + +async def async_setup_entry( + hass: HomeAssistant, + config_entry: ConfigEntry, + async_add_entities: AddEntitiesCallback, +) -> None: + """Set up Owlet switch based on a config entry.""" + coordinators: OwletCoordinator = hass.data[DOMAIN][config_entry.entry_id].values() + + switches = [] + for coordinator in coordinators: + switches.append(OwletBaseSwitch(coordinator)) + async_add_entities(switches) + + +class OwletBaseSwitch(OwletBaseEntity, SwitchEntity): + """Defines a Owlet switch.""" + + _attr_has_entity_name = True + _attr_translation_key = "base_on" + + def __init__(self, coordinator: OwletCoordinator) -> None: + """Initialize ecobee ventilator platform.""" + super().__init__(coordinator) + self._attr_unique_id = f"{self.sock.serial}-base_station_on" + self._attr_is_on = False + + @property + def is_on(self): + return self.sock.properties["base_station_on"] + + async def async_turn_on(self, **kwargs: Any) -> None: + """Turn on the switch.""" + await self.sock.control_base_station(True) + + async def async_turn_off(self, **kwargs: Any) -> None: + """Turn off the switch.""" + await self.sock.control_base_station(False) diff --git a/custom_components/owlet/translations/en.json b/custom_components/owlet/translations/en.json index 2d0cec1..1572952 100644 --- a/custom_components/owlet/translations/en.json +++ b/custom_components/owlet/translations/en.json @@ -74,9 +74,6 @@ }, "awake": { "name": "Awake" - }, - "base_on": { - "name": "Base station on" } }, "sensor": { @@ -116,6 +113,11 @@ "movementbucket": { "name": "Movement bucket" } + }, + "switch": { + "base_on": { + "name": "Base station on" + } } } } \ No newline at end of file diff --git a/custom_components/owlet/translations/fr.json b/custom_components/owlet/translations/fr.json index cabb7cc..7a1d32e 100644 --- a/custom_components/owlet/translations/fr.json +++ b/custom_components/owlet/translations/fr.json @@ -74,9 +74,6 @@ }, "awake": { "name": "Réveillé" - }, - "base_on": { - "name": "Station de base allumée" } }, "sensor": { @@ -116,6 +113,11 @@ "movementbucket": { "name": "Seuil de mouvement" } + }, + "switch": { + "base_on": { + "name": "Station de base allumée" + } } } } \ No newline at end of file diff --git a/custom_components/owlet/translations/uk.json b/custom_components/owlet/translations/uk.json index eb3ae03..f3a0e95 100644 --- a/custom_components/owlet/translations/uk.json +++ b/custom_components/owlet/translations/uk.json @@ -74,9 +74,6 @@ }, "awake": { "name": "Awake" - }, - "base_on": { - "name": "Base station on" } }, "sensor": { @@ -116,6 +113,11 @@ "movementbucket": { "name": "Movement bucket" } + }, + "switch": { + "base_on": { + "name": "Base station on" + } } } } \ No newline at end of file