Skip to content

Commit 787f68f

Browse files
author
sfstar
authored
Merge pull request #265 from julesxxl/main
from PR #264 (@remcom): ruff format and ruff check --fix and some mypy/pylint e…
2 parents 1ec1567 + 4b8f946 commit 787f68f

File tree

14 files changed

+2601
-1063
lines changed

14 files changed

+2601
-1063
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
custom_components/victron/__pycache__
2+
.DS_Store

custom_components/victron/__init__.py

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,22 @@
11
"""The victron integration."""
2+
23
from __future__ import annotations
34

45
from homeassistant.config_entries import ConfigEntry
56
from homeassistant.const import Platform
67
from homeassistant.core import HomeAssistant
78

8-
from .const import DOMAIN, CONF_HOST, CONF_PORT, SCAN_REGISTERS, CONF_INTERVAL
9+
from .const import CONF_HOST, CONF_INTERVAL, CONF_PORT, DOMAIN, SCAN_REGISTERS
910
from .coordinator import victronEnergyDeviceUpdateCoordinator as Coordinator
1011

11-
PLATFORMS: list[Platform] = [Platform.SENSOR, Platform.SWITCH, Platform.NUMBER, Platform.SELECT, Platform.BINARY_SENSOR, Platform.BUTTON]
12+
PLATFORMS: list[Platform] = [
13+
Platform.SENSOR,
14+
Platform.SWITCH,
15+
Platform.NUMBER,
16+
Platform.SELECT,
17+
Platform.BINARY_SENSOR,
18+
Platform.BUTTON,
19+
]
1220

1321

1422
async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> bool:
@@ -20,8 +28,13 @@ async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> b
2028
# TODO 3. Store an API object for your platforms to access
2129
# hass.data[DOMAIN][entry.entry_id] = MyApi(...)
2230

23-
coordinator = Coordinator(hass, config_entry.options[CONF_HOST], config_entry.options[CONF_PORT],
24-
config_entry.data[SCAN_REGISTERS], config_entry.options[CONF_INTERVAL])
31+
coordinator = Coordinator(
32+
hass,
33+
config_entry.options[CONF_HOST],
34+
config_entry.options[CONF_PORT],
35+
config_entry.data[SCAN_REGISTERS],
36+
config_entry.options[CONF_INTERVAL],
37+
)
2538
# try:
2639
# await coordinator.async_config_entry_first_refresh()
2740
# except ConfigEntryNotReady:
@@ -41,11 +54,14 @@ async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> b
4154

4255
async def async_unload_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> bool:
4356
"""Unload a config entry."""
44-
if unload_ok := await hass.config_entries.async_unload_platforms(config_entry, PLATFORMS):
57+
if unload_ok := await hass.config_entries.async_unload_platforms(
58+
config_entry, PLATFORMS
59+
):
4560
hass.data[DOMAIN].pop(config_entry.entry_id)
4661

4762
return unload_ok
4863

64+
4965
async def update_listener(hass: HomeAssistant, config_entry: ConfigEntry) -> None:
5066
"""Update listener."""
51-
await hass.config_entries.async_reload(config_entry.entry_id)
67+
await hass.config_entries.async_reload(config_entry.entry_id)

custom_components/victron/base.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,20 @@
11
from collections.abc import Callable
2-
from homeassistant.helpers.typing import StateType
3-
42
from dataclasses import dataclass
3+
54
from homeassistant.helpers.entity import EntityDescription
5+
from homeassistant.helpers.typing import StateType
6+
67

78
@dataclass
89
class VictronBaseEntityDescription(EntityDescription):
9-
slave: int = None
10-
value_fn: Callable[[dict], StateType] = lambda data, slave, key: data["data"][str(slave) + "." + str(key)]
10+
@staticmethod
11+
def lambda_func():
12+
return lambda data, slave, key: data["data"][str(slave) + "." + str(key)]
13+
14+
slave: int = None
15+
value_fn: Callable[[dict], StateType] = lambda_func()
1116

12-
@dataclass
17+
18+
@dataclass
1319
class VictronWriteBaseEntityDescription(VictronBaseEntityDescription):
14-
address: int = None
20+
address: int = None
Lines changed: 47 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,59 @@
11
"""Support for Victron Energy binary sensors."""
2-
from __future__ import annotations
3-
4-
from contextlib import suppress
5-
from typing import cast
6-
7-
from homeassistant.core import HomeAssistant, HassJob
82

9-
from collections.abc import Callable
10-
from homeassistant.helpers.typing import StateType
3+
from __future__ import annotations
114

125
from dataclasses import dataclass
6+
import logging
7+
from typing import cast
138

14-
from homeassistant.helpers.update_coordinator import CoordinatorEntity
15-
9+
from homeassistant.components.binary_sensor import (
10+
DOMAIN as BINARY_SENSOR_DOMAIN,
11+
BinarySensorEntity,
12+
BinarySensorEntityDescription,
13+
)
1614
from homeassistant.config_entries import ConfigEntry
17-
from homeassistant.core import HomeAssistant
15+
from homeassistant.core import HassJob, HomeAssistant
16+
from homeassistant.helpers import entity
1817
from homeassistant.helpers.entity_platform import AddEntitiesCallback
19-
from homeassistant.helpers import event, entity
20-
21-
from homeassistant.components.binary_sensor import BinarySensorEntityDescription, BinarySensorEntity, DOMAIN as BINARY_SENSOR_DOMAIN
18+
from homeassistant.helpers.update_coordinator import CoordinatorEntity
2219

23-
from .coordinator import victronEnergyDeviceUpdateCoordinator
2420
from .base import VictronBaseEntityDescription
25-
from .const import DOMAIN, CONF_ADVANCED_OPTIONS, register_info_dict, BoolReadEntityType
21+
from .const import DOMAIN, BoolReadEntityType, register_info_dict
22+
from .coordinator import victronEnergyDeviceUpdateCoordinator
2623

27-
import logging
2824
_LOGGER = logging.getLogger(__name__)
2925

26+
3027
async def async_setup_entry(
3128
hass: HomeAssistant,
3229
config_entry: ConfigEntry,
3330
async_add_entities: AddEntitiesCallback,
3431
) -> None:
3532
"""Set up Victron energy binary sensor entries."""
3633
_LOGGER.debug("attempting to setup binary sensor entities")
37-
victron_coordinator: victronEnergyDeviceUpdateCoordinator = hass.data[DOMAIN][config_entry.entry_id]
34+
victron_coordinator: victronEnergyDeviceUpdateCoordinator = hass.data[DOMAIN][
35+
config_entry.entry_id
36+
]
3837
_LOGGER.debug(victron_coordinator.processed_data()["register_set"])
3938
_LOGGER.debug(victron_coordinator.processed_data()["data"])
4039
descriptions = []
41-
#TODO cleanup
40+
# TODO cleanup
4241
register_set = victron_coordinator.processed_data()["register_set"]
4342
for slave, registerLedger in register_set.items():
4443
for name in registerLedger:
4544
for register_name, registerInfo in register_info_dict[name].items():
46-
_LOGGER.debug("unit == " + str(slave) + " registerLedger == " + str(registerLedger) + " registerInfo ")
45+
_LOGGER.debug(
46+
"unit == "
47+
+ str(slave)
48+
+ " registerLedger == "
49+
+ str(registerLedger)
50+
+ " registerInfo "
51+
)
4752

4853
if isinstance(registerInfo.entityType, BoolReadEntityType):
4954
description = VictronEntityDescription(
5055
key=register_name,
51-
name=register_name.replace('_', ' '),
56+
name=register_name.replace("_", " "),
5257
slave=slave,
5358
)
5459
_LOGGER.debug("composed description == " + str(description))
@@ -58,28 +63,29 @@ async def async_setup_entry(
5863
entity = {}
5964
for description in descriptions:
6065
entity = description
61-
entities.append(
62-
VictronBinarySensor(
63-
victron_coordinator,
64-
entity
65-
))
66+
entities.append(VictronBinarySensor(victron_coordinator, entity))
6667

67-
async_add_entities(
68-
entities, True
69-
)
68+
async_add_entities(entities, True)
7069

7170

7271
@dataclass
73-
class VictronEntityDescription(BinarySensorEntityDescription, VictronBaseEntityDescription):
72+
class VictronEntityDescription(
73+
BinarySensorEntityDescription, VictronBaseEntityDescription
74+
):
7475
"""Describes victron sensor entity."""
7576

77+
7678
class VictronBinarySensor(CoordinatorEntity, BinarySensorEntity):
7779
"""A binary sensor implementation for Victron energy device."""
7880

79-
def __init__(self, coordinator: victronEnergyDeviceUpdateCoordinator, description: VictronEntityDescription) -> None:
81+
def __init__(
82+
self,
83+
coordinator: victronEnergyDeviceUpdateCoordinator,
84+
description: VictronEntityDescription,
85+
) -> None:
8086
"""Initialize the binary sensor."""
8187
self.description: VictronEntityDescription = description
82-
#this needs to be changed to allow multiple of the same type
88+
# this needs to be changed to allow multiple of the same type
8389
self._attr_device_class = description.device_class
8490
self._attr_name = f"{description.name}"
8591

@@ -97,7 +103,11 @@ def __init__(self, coordinator: victronEnergyDeviceUpdateCoordinator, descriptio
97103
@property
98104
def is_on(self) -> bool:
99105
"""Return True if the binary sensor is on."""
100-
data = self.description.value_fn(self.coordinator.processed_data(), self.description.slave, self.description.key)
106+
data = self.description.value_fn(
107+
self.coordinator.processed_data(),
108+
self.description.slave,
109+
self.description.key,
110+
)
101111
return cast(bool, data)
102112

103113
@property
@@ -109,10 +119,8 @@ def available(self) -> bool:
109119
def device_info(self) -> entity.DeviceInfo:
110120
"""Return the device info."""
111121
return entity.DeviceInfo(
112-
identifiers={
113-
(DOMAIN, self.unique_id.split('_')[0])
114-
},
115-
name=self.unique_id.split('_')[1],
116-
model=self.unique_id.split('_')[0],
122+
identifiers={(DOMAIN, self.unique_id.split("_")[0])},
123+
name=self.unique_id.split("_")[1],
124+
model=self.unique_id.split("_")[0],
117125
manufacturer="victron",
118-
)
126+
)
Lines changed: 45 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,59 @@
11
"""Support for Victron energy button sensors."""
2-
from __future__ import annotations
32

4-
from homeassistant.core import HomeAssistant, HassJob
3+
from __future__ import annotations
54

65
from dataclasses import dataclass
6+
import logging
77

8-
from homeassistant.helpers.update_coordinator import CoordinatorEntity
9-
8+
from homeassistant.components.button import (
9+
DOMAIN as BUTTON_DOMAIN,
10+
ButtonDeviceClass,
11+
ButtonEntity,
12+
ButtonEntityDescription,
13+
)
1014
from homeassistant.config_entries import ConfigEntry
11-
from homeassistant.core import HomeAssistant
12-
from homeassistant.helpers.entity_platform import AddEntitiesCallback
15+
from homeassistant.core import HassJob, HomeAssistant
1316
from homeassistant.helpers import entity
14-
15-
from homeassistant.components.button import ButtonEntityDescription, ButtonDeviceClass, ButtonEntity, DOMAIN as BUTTON_DOMAIN
17+
from homeassistant.helpers.entity_platform import AddEntitiesCallback
18+
from homeassistant.helpers.update_coordinator import CoordinatorEntity
1619

1720
from .base import VictronWriteBaseEntityDescription
21+
from .const import CONF_ADVANCED_OPTIONS, DOMAIN, ButtonWriteType, register_info_dict
1822
from .coordinator import victronEnergyDeviceUpdateCoordinator
19-
from .const import DOMAIN, CONF_ADVANCED_OPTIONS, register_info_dict, ButtonWriteType
2023

21-
import logging
2224
_LOGGER = logging.getLogger(__name__)
2325

26+
2427
async def async_setup_entry(
2528
hass: HomeAssistant,
2629
config_entry: ConfigEntry,
2730
async_add_entities: AddEntitiesCallback,
2831
) -> None:
2932
"""Set up Victron energy binary sensor entries."""
3033
_LOGGER.debug("attempting to setup button entities")
31-
victron_coordinator: victronEnergyDeviceUpdateCoordinator = hass.data[DOMAIN][config_entry.entry_id]
34+
victron_coordinator: victronEnergyDeviceUpdateCoordinator = hass.data[DOMAIN][
35+
config_entry.entry_id
36+
]
3237
descriptions = []
33-
#TODO cleanup
38+
# TODO cleanup
3439
register_set = victron_coordinator.processed_data()["register_set"]
3540
for slave, registerLedger in register_set.items():
3641
for name in registerLedger:
3742
for register_name, registerInfo in register_info_dict[name].items():
38-
_LOGGER.debug("unit == " + str(slave) + " registerLedger == " + str(registerLedger) + " registerInfo ")
43+
_LOGGER.debug(
44+
"unit == "
45+
+ str(slave)
46+
+ " registerLedger == "
47+
+ str(registerLedger)
48+
+ " registerInfo "
49+
)
3950
if not config_entry.options[CONF_ADVANCED_OPTIONS]:
40-
continue
51+
continue
4152

4253
if isinstance(registerInfo.entityType, ButtonWriteType):
4354
description = VictronEntityDescription(
4455
key=register_name,
45-
name=register_name.replace('_', ' '),
56+
name=register_name.replace("_", " "),
4657
slave=slave,
4758
device_class=ButtonDeviceClass.RESTART,
4859
address=registerInfo.register,
@@ -54,25 +65,26 @@ async def async_setup_entry(
5465
entity = {}
5566
for description in descriptions:
5667
entity = description
57-
entities.append(
58-
VictronBinarySensor(
59-
victron_coordinator,
60-
entity
61-
))
68+
entities.append(VictronBinarySensor(victron_coordinator, entity))
69+
70+
async_add_entities(entities, True)
6271

63-
async_add_entities(
64-
entities, True
65-
)
6672

6773
@dataclass
68-
class VictronEntityDescription(ButtonEntityDescription, VictronWriteBaseEntityDescription):
74+
class VictronEntityDescription(
75+
ButtonEntityDescription, VictronWriteBaseEntityDescription
76+
):
6977
"""Describes victron sensor entity."""
7078

7179

7280
class VictronBinarySensor(CoordinatorEntity, ButtonEntity):
7381
"""A button implementation for Victron energy device."""
7482

75-
def __init__(self, coordinator: victronEnergyDeviceUpdateCoordinator, description: VictronEntityDescription) -> None:
83+
def __init__(
84+
self,
85+
coordinator: victronEnergyDeviceUpdateCoordinator,
86+
description: VictronEntityDescription,
87+
) -> None:
7688
"""Initialize the sensor."""
7789
self.description: VictronEntityDescription = description
7890
self._attr_device_class = description.device_class
@@ -91,7 +103,9 @@ def __init__(self, coordinator: victronEnergyDeviceUpdateCoordinator, descriptio
91103

92104
async def async_press(self) -> None:
93105
"""Handle the button press."""
94-
self.coordinator.write_register(unit=self.description.slave, address=self.description.address, value=1)
106+
self.coordinator.write_register(
107+
unit=self.description.slave, address=self.description.address, value=1
108+
)
95109

96110
@property
97111
def available(self) -> bool:
@@ -102,10 +116,8 @@ def available(self) -> bool:
102116
def device_info(self) -> entity.DeviceInfo:
103117
"""Return the device info."""
104118
return entity.DeviceInfo(
105-
identifiers={
106-
(DOMAIN, self.unique_id.split('_')[0])
107-
},
108-
name=self.unique_id.split('_')[1],
109-
model=self.unique_id.split('_')[0],
110-
manufacturer="victron",
111-
)
119+
identifiers={(DOMAIN, self.unique_id.split("_")[0])},
120+
name=self.unique_id.split("_")[1],
121+
model=self.unique_id.split("_")[0],
122+
manufacturer="victron",
123+
)

0 commit comments

Comments
 (0)