2020-12-02 12:50:48 +00:00
|
|
|
"""Demo platform that offers a fake Number entity."""
|
2021-06-14 16:05:01 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2021-11-30 11:54:06 +00:00
|
|
|
from homeassistant.components.number import NumberEntity, NumberMode
|
2022-01-03 15:24:09 +00:00
|
|
|
from homeassistant.config_entries import ConfigEntry
|
2020-12-02 12:50:48 +00:00
|
|
|
from homeassistant.const import DEVICE_DEFAULT_NAME
|
2022-01-03 15:24:09 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
2021-10-22 15:00:00 +00:00
|
|
|
from homeassistant.helpers.entity import DeviceInfo
|
2022-01-03 15:24:09 +00:00
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
|
|
|
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
|
2020-12-02 12:50:48 +00:00
|
|
|
|
|
|
|
from . import DOMAIN
|
|
|
|
|
|
|
|
|
2022-01-03 15:24:09 +00:00
|
|
|
async def async_setup_platform(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
config: ConfigType,
|
|
|
|
async_add_entities: AddEntitiesCallback,
|
|
|
|
discovery_info: DiscoveryInfoType | None = None,
|
|
|
|
) -> None:
|
2020-12-02 12:50:48 +00:00
|
|
|
"""Set up the demo Number entity."""
|
|
|
|
async_add_entities(
|
|
|
|
[
|
|
|
|
DemoNumber(
|
|
|
|
"volume1",
|
|
|
|
"volume",
|
|
|
|
42.0,
|
|
|
|
"mdi:volume-high",
|
|
|
|
False,
|
2021-11-30 11:54:06 +00:00
|
|
|
mode=NumberMode.SLIDER,
|
2020-12-02 12:50:48 +00:00
|
|
|
),
|
|
|
|
DemoNumber(
|
|
|
|
"pwm1",
|
|
|
|
"PWM 1",
|
2021-02-05 14:08:28 +00:00
|
|
|
0.42,
|
2020-12-02 12:50:48 +00:00
|
|
|
"mdi:square-wave",
|
|
|
|
False,
|
|
|
|
0.0,
|
|
|
|
1.0,
|
|
|
|
0.01,
|
2021-11-30 11:54:06 +00:00
|
|
|
NumberMode.BOX,
|
2021-10-21 05:20:39 +00:00
|
|
|
),
|
|
|
|
DemoNumber(
|
|
|
|
"large_range",
|
|
|
|
"Large Range",
|
|
|
|
500,
|
|
|
|
"mdi:square-wave",
|
|
|
|
False,
|
|
|
|
1,
|
|
|
|
1000,
|
|
|
|
1,
|
|
|
|
),
|
|
|
|
DemoNumber(
|
|
|
|
"small_range",
|
|
|
|
"Small Range",
|
|
|
|
128,
|
|
|
|
"mdi:square-wave",
|
|
|
|
False,
|
|
|
|
1,
|
|
|
|
255,
|
|
|
|
1,
|
2020-12-02 12:50:48 +00:00
|
|
|
),
|
|
|
|
]
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2022-01-03 15:24:09 +00:00
|
|
|
async def async_setup_entry(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
config_entry: ConfigEntry,
|
|
|
|
async_add_entities: AddEntitiesCallback,
|
|
|
|
) -> None:
|
2020-12-02 12:50:48 +00:00
|
|
|
"""Set up the Demo config entry."""
|
|
|
|
await async_setup_platform(hass, {}, async_add_entities)
|
|
|
|
|
|
|
|
|
|
|
|
class DemoNumber(NumberEntity):
|
|
|
|
"""Representation of a demo Number entity."""
|
|
|
|
|
2021-06-14 16:05:01 +00:00
|
|
|
_attr_should_poll = False
|
|
|
|
|
2020-12-02 12:50:48 +00:00
|
|
|
def __init__(
|
|
|
|
self,
|
2021-06-14 16:05:01 +00:00
|
|
|
unique_id: str,
|
|
|
|
name: str,
|
|
|
|
state: float,
|
|
|
|
icon: str,
|
|
|
|
assumed: bool,
|
2022-06-14 18:16:36 +00:00
|
|
|
native_min_value: float | None = None,
|
|
|
|
native_max_value: float | None = None,
|
|
|
|
native_step: float | None = None,
|
2021-11-30 11:54:06 +00:00
|
|
|
mode: NumberMode = NumberMode.AUTO,
|
2021-06-14 16:05:01 +00:00
|
|
|
) -> None:
|
2020-12-02 12:50:48 +00:00
|
|
|
"""Initialize the Demo Number entity."""
|
2021-06-14 16:05:01 +00:00
|
|
|
self._attr_assumed_state = assumed
|
|
|
|
self._attr_icon = icon
|
|
|
|
self._attr_name = name or DEVICE_DEFAULT_NAME
|
|
|
|
self._attr_unique_id = unique_id
|
2022-06-14 18:16:36 +00:00
|
|
|
self._attr_native_value = state
|
2021-10-21 05:20:39 +00:00
|
|
|
self._attr_mode = mode
|
2021-06-14 16:05:01 +00:00
|
|
|
|
2022-06-14 18:16:36 +00:00
|
|
|
if native_min_value is not None:
|
2022-06-17 10:14:11 +00:00
|
|
|
self._attr_native_min_value = native_min_value
|
2022-06-14 18:16:36 +00:00
|
|
|
if native_max_value is not None:
|
2022-06-17 10:14:11 +00:00
|
|
|
self._attr_native_max_value = native_max_value
|
2022-06-14 18:16:36 +00:00
|
|
|
if native_step is not None:
|
2022-06-17 10:14:11 +00:00
|
|
|
self._attr_native_step = native_step
|
2020-12-02 12:50:48 +00:00
|
|
|
|
2022-02-10 09:05:58 +00:00
|
|
|
self._attr_device_info = DeviceInfo(
|
2021-10-22 15:00:00 +00:00
|
|
|
identifiers={
|
2020-12-02 12:50:48 +00:00
|
|
|
# Serial numbers are unique identifiers within a specific domain
|
2022-02-10 09:05:58 +00:00
|
|
|
(DOMAIN, unique_id)
|
2020-12-02 12:50:48 +00:00
|
|
|
},
|
2021-10-22 15:00:00 +00:00
|
|
|
name=self.name,
|
|
|
|
)
|
2020-12-02 12:50:48 +00:00
|
|
|
|
2022-06-14 18:16:36 +00:00
|
|
|
async def async_set_native_value(self, value):
|
2020-12-02 12:50:48 +00:00
|
|
|
"""Update the current value."""
|
2022-06-14 18:16:36 +00:00
|
|
|
self._attr_native_value = value
|
2020-12-02 12:50:48 +00:00
|
|
|
self.async_write_ha_state()
|