Focus on switch platform

This commit is contained in:
Robin Lintermann
2025-04-17 09:56:02 +00:00
parent 2a2cef349d
commit b6d51504c4
5 changed files with 24 additions and 279 deletions

View File

@@ -4,4 +4,4 @@ DOMAIN = "smarla"
HOST = "https://devices.swing2sleep.de"
PLATFORMS = ["number", "sensor", "switch"]
PLATFORMS = ["switch"]

View File

@@ -4,25 +4,6 @@
"smartmode": {
"default": "mdi:refresh-auto"
}
},
"number": {
"intensity": {
"default": "mdi:sine-wave"
}
},
"sensor": {
"amplitude": {
"default": "mdi:sine-wave"
},
"period": {
"default": "mdi:sine-wave"
},
"activity": {
"default": "mdi:baby-face"
},
"swing_count": {
"default": "mdi:counter"
}
}
}
}

View File

@@ -1,95 +0,0 @@
"""Support for the Swing2Sleep Smarla number entities."""
from dataclasses import dataclass
from typing import Any
from pysmarlaapi import Federwiege
from homeassistant.components.number import (
NumberEntity,
NumberEntityDescription,
NumberMode,
)
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
from . import FederwiegeConfigEntry
from .entity import SmarlaBaseEntity
@dataclass(frozen=True, kw_only=True)
class SmarlaNumberEntityDescription(NumberEntityDescription):
"""Class describing Swing2Sleep Smarla number entities."""
service: str
property: str
NUMBER_TYPES: list[SmarlaNumberEntityDescription] = [
SmarlaNumberEntityDescription(
key="intensity",
translation_key="intensity",
service="babywiege",
property="intensity",
native_max_value=100,
native_min_value=0,
native_step=1,
mode=NumberMode.SLIDER,
),
]
async def async_setup_entry(
hass: HomeAssistant,
config_entry: FederwiegeConfigEntry,
async_add_entities: AddConfigEntryEntitiesCallback,
) -> None:
"""Set up the Smarla numbers from config entry."""
federwiege = config_entry.runtime_data
entities: list[NumberEntity] = []
for desc in NUMBER_TYPES:
entity = SmarlaNumber(federwiege, desc)
entities.append(entity)
async_add_entities(entities)
class SmarlaNumber(SmarlaBaseEntity, NumberEntity):
"""Representation of Smarla number."""
async def on_change(self, value: Any):
"""Notify ha when state changes."""
self.async_write_ha_state()
def __init__(
self,
federwiege: Federwiege,
description: SmarlaNumberEntityDescription,
) -> None:
"""Initialize a Smarla number."""
super().__init__(federwiege)
self.property = federwiege.get_service(description.service).get_property(
description.property
)
self.entity_description = description
self._attr_should_poll = False
self._attr_unique_id = f"{federwiege.serial_number}-{description.key}"
async def async_added_to_hass(self) -> None:
"""Run when this Entity has been added to HA."""
await self.property.add_listener(self.on_change)
async def async_will_remove_from_hass(self) -> None:
"""Entity being removed from hass."""
await self.property.remove_listener(self.on_change)
@property
def native_value(self) -> float:
"""Return the entity value to represent the entity state."""
return self.property.get()
def set_native_value(self, value: float) -> None:
"""Update to the smarla device."""
self.property.set(int(value))

View File

@@ -1,122 +0,0 @@
"""Support for the Swing2Sleep Smarla sensor entities."""
from dataclasses import dataclass
from typing import Any
from pysmarlaapi import Federwiege
from homeassistant.components.sensor import (
SensorEntity,
SensorEntityDescription,
SensorStateClass,
)
from homeassistant.const import UnitOfLength, UnitOfTime
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
from . import FederwiegeConfigEntry
from .entity import SmarlaBaseEntity
@dataclass(frozen=True, kw_only=True)
class SmarlaSensorEntityDescription(SensorEntityDescription):
"""Class describing Swing2Sleep Smarla sensor entities."""
service: str
property: str
multiple: bool = False
value_pos: int = 0
NUMBER_TYPES: list[SmarlaSensorEntityDescription] = [
SmarlaSensorEntityDescription(
key="amplitude",
translation_key="amplitude",
service="analyser",
property="oscillation",
multiple=True,
value_pos=0,
native_unit_of_measurement=UnitOfLength.MILLIMETERS,
state_class=SensorStateClass.MEASUREMENT,
),
SmarlaSensorEntityDescription(
key="period",
translation_key="period",
service="analyser",
property="oscillation",
multiple=True,
value_pos=1,
native_unit_of_measurement=UnitOfTime.MILLISECONDS,
state_class=SensorStateClass.MEASUREMENT,
),
SmarlaSensorEntityDescription(
key="activity",
translation_key="activity",
service="analyser",
property="activity",
state_class=SensorStateClass.MEASUREMENT,
),
SmarlaSensorEntityDescription(
key="swing_count",
translation_key="swing_count",
service="analyser",
property="swing_count",
state_class=SensorStateClass.MEASUREMENT,
),
]
async def async_setup_entry(
hass: HomeAssistant,
config_entry: FederwiegeConfigEntry,
async_add_entities: AddConfigEntryEntitiesCallback,
) -> None:
"""Set up the Smarla sensors from config entry."""
federwiege = config_entry.runtime_data
entities: list[SensorEntity] = []
for desc in NUMBER_TYPES:
entity = SmarlaSensor(federwiege, desc)
entities.append(entity)
async_add_entities(entities)
class SmarlaSensor(SmarlaBaseEntity, SensorEntity):
"""Representation of Smarla sensor."""
async def on_change(self, value: Any):
"""Notify ha when state changes."""
self.async_write_ha_state()
def __init__(
self,
federwiege: Federwiege,
description: SmarlaSensorEntityDescription,
) -> None:
"""Initialize a Smarla sensor."""
super().__init__(federwiege)
self.property = federwiege.get_service(description.service).get_property(
description.property
)
self.entity_description = description
self.multiple = description.multiple
self.pos = description.value_pos
self._attr_should_poll = False
self._attr_unique_id = f"{federwiege.serial_number}-{description.key}"
async def async_added_to_hass(self) -> None:
"""Run when this Entity has been added to HA."""
await self.property.add_listener(self.on_change)
async def async_will_remove_from_hass(self) -> None:
"""Entity being removed from hass."""
await self.property.remove_listener(self.on_change)
@property
def native_value(self) -> int:
"""Return the entity value to represent the entity state."""
return (
self.property.get() if not self.multiple else self.property.get()[self.pos]
)

View File

@@ -1,47 +1,28 @@
{
"config": {
"error": {
"invalid_auth": "[%key:common::config_flow::error::invalid_auth%]",
"invalid_token": "Invalid access token"
},
"step": {
"user": {
"data": {
"access_token": "[%key:common::config_flow::data::access_token%]"
},
"data_description": {
"access_token": "The access token generated from the App."
}
}
},
"abort": {
"already_configured": "[%key:common::config_flow::abort::already_configured_device%]"
}
"config": {
"error": {
"invalid_auth": "[%key:common::config_flow::error::invalid_auth%]",
"invalid_token": "Invalid access token"
},
"entity": {
"sensor": {
"amplitude": {
"name": "Amplitude"
},
"period": {
"name": "Period"
},
"activity": {
"name": "Activity"
},
"swing_count": {
"name": "Swing count"
}
"step": {
"user": {
"data": {
"access_token": "[%key:common::config_flow::data::access_token%]"
},
"number": {
"intensity": {
"name": "Intensity"
}
},
"switch": {
"smartmode": {
"name": "Smart mode"
}
"data_description": {
"access_token": "The access token generated from the App."
}
}
},
"abort": {
"already_configured": "[%key:common::config_flow::abort::already_configured_device%]"
}
}
},
"entity": {
"switch": {
"smartmode": {
"name": "Smart mode"
}
}
}
}