Updated apps
This commit is contained in:
@@ -0,0 +1 @@
|
||||
"""Spook - Your homie."""
|
||||
Binary file not shown.
@@ -0,0 +1,19 @@
|
||||
"""Spook - Your homie."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import TYPE_CHECKING
|
||||
|
||||
from homeassistant.exceptions import HomeAssistantError
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from homeassistant.components.number import NumberEntity
|
||||
|
||||
|
||||
def native_value_as_float(entity: NumberEntity) -> float:
|
||||
"""Return the native value of a number entity as a float."""
|
||||
try:
|
||||
return float(entity.value)
|
||||
except (TypeError, ValueError) as err:
|
||||
msg = f"Native value {entity.value!r} for {entity.entity_id} is not a number"
|
||||
raise HomeAssistantError(msg) from err
|
||||
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,45 @@
|
||||
"""Spook - Your homie."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import math
|
||||
from typing import TYPE_CHECKING
|
||||
|
||||
import voluptuous as vol
|
||||
|
||||
from homeassistant.components.number import DOMAIN, NumberEntity
|
||||
|
||||
from ....services import AbstractSpookEntityComponentService
|
||||
from . import native_value_as_float
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from homeassistant.core import ServiceCall
|
||||
|
||||
|
||||
class SpookService(AbstractSpookEntityComponentService[NumberEntity]):
|
||||
"""Number entity service, decrease value by a single step."""
|
||||
|
||||
domain = DOMAIN
|
||||
service = "decrement"
|
||||
schema = {vol.Optional("amount"): vol.Coerce(float)}
|
||||
|
||||
async def async_handle_service(
|
||||
self,
|
||||
entity: NumberEntity,
|
||||
call: ServiceCall,
|
||||
) -> None:
|
||||
"""Handle the service call."""
|
||||
amount = call.data.get("amount", entity.step or 1)
|
||||
if not math.isclose(amount % entity.step, 0, abs_tol=1e-9):
|
||||
msg = (
|
||||
f"Amount {amount} not valid for {entity.entity_id}, "
|
||||
f"it needs to be a multiple of {entity.step}"
|
||||
)
|
||||
raise ValueError(msg)
|
||||
|
||||
value = native_value_as_float(entity) - amount
|
||||
|
||||
if entity.min_value is not None:
|
||||
value = max(value, entity.min_value)
|
||||
|
||||
await entity.async_set_native_value(value)
|
||||
@@ -0,0 +1,45 @@
|
||||
"""Spook - Your homie."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import math
|
||||
from typing import TYPE_CHECKING
|
||||
|
||||
import voluptuous as vol
|
||||
|
||||
from homeassistant.components.number import DOMAIN, NumberEntity
|
||||
|
||||
from ....services import AbstractSpookEntityComponentService
|
||||
from . import native_value_as_float
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from homeassistant.core import ServiceCall
|
||||
|
||||
|
||||
class SpookService(AbstractSpookEntityComponentService[NumberEntity]):
|
||||
"""Number entity service, increase value by a single step."""
|
||||
|
||||
domain = DOMAIN
|
||||
service = "increment"
|
||||
schema = {vol.Optional("amount"): vol.Coerce(float)}
|
||||
|
||||
async def async_handle_service(
|
||||
self,
|
||||
entity: NumberEntity,
|
||||
call: ServiceCall,
|
||||
) -> None:
|
||||
"""Handle the service call."""
|
||||
amount = call.data.get("amount", entity.step or 1)
|
||||
if not math.isclose(amount % entity.step, 0, abs_tol=1e-9):
|
||||
msg = (
|
||||
f"Amount {amount} not valid for {entity.entity_id}, "
|
||||
f"it needs to be a multiple of {entity.step}"
|
||||
)
|
||||
raise ValueError(msg)
|
||||
|
||||
value = native_value_as_float(entity) + amount
|
||||
|
||||
if entity.max_value is not None:
|
||||
value = min(value, entity.max_value)
|
||||
|
||||
await entity.async_set_native_value(value)
|
||||
@@ -0,0 +1,31 @@
|
||||
"""Spook - Your homie."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import TYPE_CHECKING
|
||||
|
||||
from homeassistant.components.number import DOMAIN, NumberEntity
|
||||
from homeassistant.exceptions import HomeAssistantError
|
||||
|
||||
from ....services import AbstractSpookEntityComponentService
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from homeassistant.core import ServiceCall
|
||||
|
||||
|
||||
class SpookService(AbstractSpookEntityComponentService[NumberEntity]):
|
||||
"""Number entity service, set the max value."""
|
||||
|
||||
domain = DOMAIN
|
||||
service = "max"
|
||||
|
||||
async def async_handle_service(
|
||||
self,
|
||||
entity: NumberEntity,
|
||||
call: ServiceCall, # noqa: ARG002
|
||||
) -> None:
|
||||
"""Handle the service call."""
|
||||
if entity.max_value is None:
|
||||
msg = f"Entity {entity.entity_id} has no max value"
|
||||
raise HomeAssistantError(msg)
|
||||
await entity.async_set_native_value(entity.native_max_value)
|
||||
@@ -0,0 +1,31 @@
|
||||
"""Spook - Your homie."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import TYPE_CHECKING
|
||||
|
||||
from homeassistant.components.number import DOMAIN, NumberEntity
|
||||
from homeassistant.exceptions import HomeAssistantError
|
||||
|
||||
from ....services import AbstractSpookEntityComponentService
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from homeassistant.core import ServiceCall
|
||||
|
||||
|
||||
class SpookService(AbstractSpookEntityComponentService[NumberEntity]):
|
||||
"""Number entity service, set the min value."""
|
||||
|
||||
domain = DOMAIN
|
||||
service = "min"
|
||||
|
||||
async def async_handle_service(
|
||||
self,
|
||||
entity: NumberEntity,
|
||||
call: ServiceCall, # noqa: ARG002
|
||||
) -> None:
|
||||
"""Handle the service call."""
|
||||
if entity.min_value is None:
|
||||
msg = f"Entity {entity.entity_id} has no min value"
|
||||
raise HomeAssistantError(msg)
|
||||
await entity.async_set_native_value(entity.native_min_value)
|
||||
Reference in New Issue
Block a user