This repository has been archived by the owner on Sep 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnumber.py
100 lines (77 loc) · 3.01 KB
/
number.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
"""Number entities for Enyaq."""
from asyncio import sleep
import logging
from homeassistant.components.number import (
NumberDeviceClass,
NumberEntity,
NumberEntityDescription,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import PERCENTAGE
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import DiscoveryInfoType
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator
from .const import DATA_COODINATOR, DOMAIN
from .entity import MySkodaDataEntity
from .myskoda import Vehicle
_LOGGER = logging.getLogger(__name__)
async def async_setup_entry(
hass: HomeAssistant,
config: ConfigEntry,
async_add_entities: AddEntitiesCallback,
discovery_info: DiscoveryInfoType | None = None,
) -> None:
"""Set up the sensor platform."""
coordinator = hass.data[DOMAIN][config.entry_id][DATA_COODINATOR]
vehicles = coordinator.data.get("vehicles")
entities = [ChargeLimit(coordinator, vehicle) for vehicle in vehicles]
async_add_entities(entities, update_before_add=True)
class MySkodaNumber(MySkodaDataEntity, NumberEntity):
"""Number Entity.
Base class for all number entities in the MySkoda integration.
"""
def __init__(
self,
coordinator: DataUpdateCoordinator,
vehicle: Vehicle,
entity_description: NumberEntityDescription,
) -> None:
"""Create new Number."""
super().__init__(coordinator, vehicle, entity_description)
NumberEntity.__init__(self)
class ChargeLimit(MySkodaNumber):
"""Charge limit.
Represents the maximum value in percent that the car can be charged to.
"""
def __init__(self, coordinator: DataUpdateCoordinator, vehicle: Vehicle) -> None:
"""Create new ChargeLimit."""
super().__init__(
coordinator,
vehicle,
NumberEntityDescription(
key="charge_limit",
name=f"{vehicle.info.title} Charge Limit",
icon="mdi:battery-lock",
native_max_value=100,
native_min_value=50,
native_unit_of_measurement=PERCENTAGE,
native_step=10,
),
)
self._attr_unique_id = f"{vehicle.info.vin}_charge_limit"
self._attr_device_class = NumberDeviceClass.BATTERY
@property
def native_value(self) -> float | None: # noqa: D102
if not self.coordinator.data:
return None
self._update_device_from_coordinator()
return self.vehicle.charging.target_percent
async def async_set_native_value(self, value: float): # noqa: D102
await self.coordinator.hub.set_charge_limit(self.vehicle.info.vin, value)
for _ in range(10):
await sleep(15)
if self.native_value == value:
break
await self.coordinator.async_refresh()
_LOGGER.debug("Changed charge limit to %s.", value)