hass-core/homeassistant/components/motionmount/number.py
RJPoelstra 2c2e6171e2
Add integration for Vogel's MotionMount (#103498)
* Skeleton for Vogel's MotionMount support.

* Generated updates.

* Add validation of the discovered information.

* Add manual configuration

* Use a mac address as a unique id

* Add tests for config_flow

* Add a 'turn' sensor entity.

* Add all needed sensors.

* Add number and select entity for control of MotionMount

* Update based on development checklist

* Preset selector now updates when a preset is chosen

* Fix adding presets selector to device

* Remove irrelevant TODO

* Bump python-MotionMount requirement

* Invert direction of turn slider

* Prepare for PR

* Make sure entities have correct values when created

* Use device's mac address as unique id for entities.

* Fix missing files in .coveragerc

* Remove typing ignore from device library.

Improved typing also gave rise to the need to improve the callback mechanism

* Improve typing

* Convert property to shorthand form

* Remove unneeded CONF_NAME in ConfigEntry

* Add small comment

* Refresh coordinator on notification from MotionMount

* Use translation for entity

* Bump python-MotionMount

* Raise `ConfigEntryNotReady` when connect fails

* Use local variable

* Improve exception handling

* Reduce duplicate code

* Make better use of constants

* Remove unneeded callback

* Remove other occurrence of unneeded callback

* Improve removal of suffix

* Catch 'getaddrinfo' exception

* Add config flow tests for invalid hostname

* Abort if device with same hostname is already configured

* Make sure we connect to a device with the same unique id as configured

* Convert function names to snake_case

* Remove unneeded commented-out code

* Use tuple

* Make us of config_entry id when mac is missing

* Prevent update of entities when nothing changed

* Don't store data in `hass.data` until we know we will proceed

* Remove coordinator

* Handle situation where mac is EMPTY_MAC

* Disable polling

* Fix failing hassfest

* Avoid calling unique-id-less discovery handler for situations where we've an unique id
2023-12-22 12:04:58 +01:00

71 lines
2.3 KiB
Python

"""Support for MotionMount numeric control."""
import motionmount
from homeassistant.components.number import NumberEntity
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import PERCENTAGE
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from .const import DOMAIN
from .entity import MotionMountEntity
async def async_setup_entry(
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
) -> None:
"""Set up Vogel's MotionMount from a config entry."""
mm: motionmount.MotionMount = hass.data[DOMAIN][entry.entry_id]
async_add_entities(
(
MotionMountExtension(mm, entry),
MotionMountTurn(mm, entry),
)
)
class MotionMountExtension(MotionMountEntity, NumberEntity):
"""The target extension position of a MotionMount."""
_attr_native_max_value = 100
_attr_native_min_value = 0
_attr_native_unit_of_measurement = PERCENTAGE
_attr_translation_key = "motionmount_extension"
def __init__(self, mm: motionmount.MotionMount, config_entry: ConfigEntry) -> None:
"""Initialize Extension number."""
super().__init__(mm, config_entry)
self._attr_unique_id = f"{self._base_unique_id}-extension"
@property
def native_value(self) -> float:
"""Get native value."""
return float(self.mm.extension or 0)
async def async_set_native_value(self, value: float) -> None:
"""Set the new value for extension."""
await self.mm.set_extension(int(value))
class MotionMountTurn(MotionMountEntity, NumberEntity):
"""The target turn position of a MotionMount."""
_attr_native_max_value = 100
_attr_native_min_value = -100
_attr_native_unit_of_measurement = PERCENTAGE
_attr_translation_key = "motionmount_turn"
def __init__(self, mm: motionmount.MotionMount, config_entry: ConfigEntry) -> None:
"""Initialize Turn number."""
super().__init__(mm, config_entry)
self._attr_unique_id = f"{self._base_unique_id}-turn"
@property
def native_value(self) -> float:
"""Get native value."""
return float(self.mm.turn or 0) * -1
async def async_set_native_value(self, value: float) -> None:
"""Set the new value for turn."""
await self.mm.set_turn(int(value * -1))