Skip to content

Commit 62ef613

Browse files
authored
Merge pull request #248 from tijsverkoyen/246-target_time-and-next_charging_time
246 target time and next charging time
2 parents 47a1f54 + 69e8372 commit 62ef613

File tree

9 files changed

+119
-15
lines changed

9 files changed

+119
-15
lines changed

README.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -532,12 +532,11 @@ This is exposed as a switch.
532532
* **Coupling Status Enum Sensor**, (only if supported), the connectivity status of the system.
533533
* **Boost Switch**, (only if supported), boost mode for capacity tariff Flanders
534534
* **TargetDistance Number Sensor**, (only if supported), distance to be charged additionally when in Smart mode
535+
* **TargetTime Time Entity**, (only if supported), charging completion time when in Smart mode
535536
* **ReachableDistance Number Sensor**, (only if supported), estimated max driving distance that could be charged during
536537
the Smart mode session
537538
* **TargetReached Binary Sensor**, (only if supported), charging target is reached during this Smart mode session
538-
539-
__Remark:__ if you have a charging station that supports setting the Target Time or Next Charging Time: please contact
540-
us
539+
* **NextChargingTime Time Sensor**, (only if supported), estimated time when charging will start in Smart Mode
541540
542541
### NHC Heating Cooling Action
543542

custom_components/nhc2/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ async def async_setup(hass, config):
7474
"select",
7575
"sensor",
7676
"switch",
77+
"time",
7778
"update",
7879
)
7980

custom_components/nhc2/entities/generic_chargingstation_charging_status.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
from homeassistant.components.sensor import SensorEntity, SensorDeviceClass
22

3-
from ..nhccoco.devices.easee_chargingstation import CocoEaseeChargingstation
3+
from ..nhccoco.devices.generic_chargingstation import CocoGenericChargingstation
44
from .nhc_entity import NHCBaseEntity
55

66

77
class Nhc2GenericChargingstationChargingStatusEntity(NHCBaseEntity, SensorEntity):
88
_attr_has_entity_name = True
99

10-
def __init__(self, device_instance: CocoEaseeChargingstation, hub, gateway):
10+
def __init__(self, device_instance: CocoGenericChargingstation, hub, gateway):
1111
"""Initialize a sensor."""
1212
super().__init__(device_instance, hub, gateway)
1313

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
from datetime import time
2+
3+
from homeassistant.components.sensor import SensorEntity, SensorDeviceClass
4+
5+
from ..nhccoco.devices.generic_chargingstation import CocoGenericChargingstation
6+
from .nhc_entity import NHCBaseEntity
7+
8+
9+
class Nhc2GenericChargingstationNextChargingTimeEntity(NHCBaseEntity, SensorEntity):
10+
_attr_has_entity_name = True
11+
12+
def __init__(self, device_instance: CocoGenericChargingstation, hub, gateway):
13+
"""Initialize a time entity."""
14+
super().__init__(device_instance, hub, gateway)
15+
16+
self._attr_unique_id = device_instance.uuid + '_next_charging_time'
17+
self._attr_device_class = SensorDeviceClass.TIMESTAMP
18+
self._attr_native_value = self._device.next_charging_time
19+
20+
@property
21+
def name(self) -> str:
22+
return 'Next Charging Time'
23+
24+
@property
25+
def state(self) -> time:
26+
return self._device.next_charging_time
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
from datetime import time
2+
3+
from homeassistant.components.time import TimeEntity
4+
5+
from ..nhccoco.devices.generic_chargingstation import CocoGenericChargingstation
6+
from .nhc_entity import NHCBaseEntity
7+
8+
9+
class Nhc2GenericChargingstationTargetTimeEntity(NHCBaseEntity, TimeEntity):
10+
_attr_has_entity_name = True
11+
12+
def __init__(self, device_instance: CocoGenericChargingstation, hub, gateway):
13+
"""Initialize a time entity."""
14+
super().__init__(device_instance, hub, gateway)
15+
16+
self._attr_unique_id = device_instance.uuid + '_target_time'
17+
18+
@property
19+
def name(self) -> str:
20+
return 'Target Time'
21+
22+
@property
23+
def state(self) -> time:
24+
return self._device.target_time
25+
26+
async def async_set_value(self, value: time) -> None:
27+
self._device.set_target_time(self._gateway, value)
28+
self.schedule_update_ha_state()
29+

custom_components/nhc2/nhccoco/devices/generic_chargingstation.py

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1+
from datetime import time
2+
13
from ..const import PROPERTY_BOOST, PROPERTY_BOOST_VALUE_FALSE, PROPERTY_BOOST_VALUE_TRUE, PROPERTY_CHARGING_MODE, \
24
PROPERTY_CHARGING_STATUS, PROPERTY_COUPLING_STATUS, PROPERTY_ELECTRICAL_POWER, PROPERTY_EV_STATUS, \
35
PROPERTY_NEXT_CHARGING_TIME, PROPERTY_REACHABLE_DISTANCE, PROPERTY_STATUS, PROPERTY_STATUS_VALUE_OFF, \
46
PROPERTY_STATUS_VALUE_ON, PROPERTY_TARGET_DISTANCE, PROPERTY_TARGET_TIME, PROPERTY_TARGET_REACHED, \
57
PROPERTY_TARGET_REACHED_VALUE_TRUE
6-
from ..helpers import to_float_or_none, to_int_or_none
8+
from ..helpers import to_float_or_none, to_int_or_none, to_time_or_none
79
from .device import CoCoDevice
810

911

@@ -101,8 +103,8 @@ def target_distance_range(self) -> tuple[float, float, float]:
101103
return self.extract_property_definition_description_range(PROPERTY_TARGET_DISTANCE)
102104

103105
@property
104-
def target_time(self) -> str | None:
105-
return self.extract_property_value(PROPERTY_TARGET_TIME)
106+
def target_time(self) -> time | None:
107+
return to_time_or_none(self.extract_property_value(PROPERTY_TARGET_TIME))
106108

107109
@property
108110
def supports_target_time(self) -> bool:
@@ -133,8 +135,8 @@ def supports_target_reached(self) -> bool:
133135
return self.has_property(PROPERTY_TARGET_REACHED)
134136

135137
@property
136-
def next_charging_time(self) -> str | None:
137-
return self.extract_property_value(PROPERTY_NEXT_CHARGING_TIME)
138+
def next_charging_time(self) -> time | None:
139+
return to_time_or_none(self.extract_property_value(PROPERTY_NEXT_CHARGING_TIME))
138140

139141
@property
140142
def supports_next_charging_time(self) -> bool:
@@ -158,11 +160,8 @@ def set_boost(self, gateway, active: bool):
158160
def set_target_distance(self, gateway, target_distance: int):
159161
gateway.add_device_control(self.uuid, PROPERTY_TARGET_DISTANCE, target_distance)
160162

161-
def set_target_time(self, gateway, target_time: str):
162-
gateway.add_device_control(self.uuid, PROPERTY_TARGET_TIME, target_time)
163+
def set_target_time(self, gateway, target_time: time):
164+
gateway.add_device_control(self.uuid, PROPERTY_TARGET_TIME, target_time.strftime('%H:%M'))
163165

164166
def set_reachable_distance(self, gateway, reachable_distance: int):
165167
gateway.add_device_control(self.uuid, PROPERTY_REACHABLE_DISTANCE, reachable_distance)
166-
167-
def set_next_charging_time(self, gateway, next_charging_time: str):
168-
gateway.add_device_control(self.uuid, PROPERTY_NEXT_CHARGING_TIME, next_charging_time)

custom_components/nhc2/nhccoco/helpers.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from datetime import time
2+
13
from .const import MQTT_DATA_PARAMS, MQTT_DATA_PARAMS_DEVICES, MQTT_DATA_PARAMS_DEVICES_PROPERTIES, \
24
MQTT_DATA_PARAMS_DEVICES_UUID, MQTT_DATA_METHOD, MQTT_DATA_METHOD_DEVICES_CONTROL, MQTT_DATA_PARAMS_SYSTEMINFO, \
35
MQTT_DATA_PARAMS_SYSTEMINFO_SWVERSIONS, MQTT_DATA_PARAMS_SYSTEMINFO_SWVERSIONS_COCO_IMAGE, \
@@ -73,3 +75,14 @@ def to_int_or_none(value) -> int | None:
7375
if value is None or value == '':
7476
return None
7577
return int(float(value))
78+
79+
80+
def to_time_or_none(value) -> time | None:
81+
if value is None or value == '':
82+
return None
83+
84+
try:
85+
hours, minutes = map(int, value.split(':'))
86+
return time(hour=hours, minute=minutes)
87+
except ValueError:
88+
return None

custom_components/nhc2/sensor.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
from .entities.generic_chargingstation_coupling_status import Nhc2GenericChargingstationCouplingStatusEntity
3030
from .entities.generic_chargingstation_electrical_power import Nhc2GenericChargingstationElectricalPowerEntity
3131
from .entities.generic_chargingstation_ev_status import Nhc2GenericChargingstationEvStatusEntity
32+
from .entities.generic_chargingstation_next_charging_time import Nhc2GenericChargingstationNextChargingTimeEntity
3233
from .entities.generic_domestichotwaterunit_coupling_status import Nhc2GenericDomestichotwaterunitCouplingStatusEntity
3334
from .entities.generic_energyhome_electrical_power_consumption import \
3435
Nhc2GenericEnergyhomeElectricalPowerConsumptionEntity
@@ -439,6 +440,8 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
439440
entities.append(Nhc2GenericChargingstationEvStatusEntity(device_instance, hub, gateway))
440441
if device_instance.supports_charging_status:
441442
entities.append(Nhc2GenericChargingstationChargingStatusEntity(device_instance, hub, gateway))
443+
if device_instance.supports_next_charging_time:
444+
entities.append(Nhc2GenericChargingstationNextChargingTimeEntity(device_instance, hub, gateway))
442445

443446
async_add_entities(entities)
444447

custom_components/nhc2/time.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
"""Support for NHC2 time."""
2+
import logging
3+
4+
from homeassistant.const import CONF_USERNAME
5+
6+
from .nhccoco.coco import CoCo
7+
8+
from .entities.generic_chargingstation_target_time import Nhc2GenericChargingstationTargetTimeEntity
9+
from .nhccoco.devices.generic_chargingstation import CocoGenericChargingstation
10+
11+
from .const import DOMAIN, KEY_GATEWAY
12+
13+
KEY_ENTITY = 'nhc2_times'
14+
15+
_LOGGER = logging.getLogger(__name__)
16+
17+
18+
async def async_setup_entry(hass, config_entry, async_add_entities):
19+
_LOGGER.info('Configuring time')
20+
21+
hass.data.setdefault(KEY_ENTITY, {})[config_entry.entry_id] = []
22+
23+
gateway: CoCo = hass.data[KEY_GATEWAY][config_entry.entry_id]
24+
hub = (DOMAIN, config_entry.data[CONF_USERNAME])
25+
26+
device_instances = gateway.get_device_instances(CocoGenericChargingstation)
27+
_LOGGER.info('→ Found %s Generic Chargingstation Implementation', len(device_instances))
28+
if len(device_instances) > 0:
29+
entities = []
30+
for device_instance in device_instances:
31+
if device_instance.supports_target_time:
32+
entities.append(Nhc2GenericChargingstationTargetTimeEntity(device_instance, hub, gateway))
33+
34+
async_add_entities(entities)

0 commit comments

Comments
 (0)