Skip to content

Commit e5be426

Browse files
authored
Merge pull request #16 from plugwise/cleanup
Further cleanup
2 parents 55424cf + 9980265 commit e5be426

File tree

11 files changed

+44
-681
lines changed

11 files changed

+44
-681
lines changed

custom_components/plugwise_usb/__init__.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,6 @@ class PlugwiseUSBEntity(Entity):
189189

190190
entity_description: PlugwiseEntityDescription
191191

192-
# Github issue #265: the entity_description is not working accordingly
193192
def __init__(
194193
self, node: PlugwiseNode, entity_description: PlugwiseEntityDescription
195194
) -> None:
@@ -203,9 +202,7 @@ def __init__(
203202
"sw_version": f"{node.firmware_version}",
204203
}
205204
self._attr_name = f"{entity_description.name} ({node.mac[-5:]})"
206-
# Github issue #265
207-
self._attr_should_poll = entity_description.should_poll # type: ignore[attr-defined]
208-
# /Github issue #265
205+
self._attr_should_poll = entity_description.should_poll
209206
self._attr_unique_id = f"{node.mac}-{entity_description.key}"
210207
self._node = node
211208
self.entity_description = entity_description

custom_components/plugwise_usb/binary_sensor.py

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@
77
from homeassistant.core import HomeAssistant
88
from homeassistant.helpers import entity_platform
99
from homeassistant.helpers.entity_platform import AddEntitiesCallback
10-
from plugwise_usb.nodes import PlugwiseNode # pw-beta usb
10+
from plugwise_usb.nodes import PlugwiseNode
1111

12-
from . import PlugwiseUSBEntity # pw-beta
13-
from .const import ( # pw-beta usb
12+
from . import PlugwiseUSBEntity
13+
from .const import (
1414
ATTR_SCAN_DAYLIGHT_MODE,
1515
ATTR_SCAN_RESET_TIMER,
1616
ATTR_SCAN_SENSITIVITY_MODE,
@@ -53,8 +53,7 @@ async def async_add_binary_sensors(mac: str):
5353
[
5454
USBBinarySensor(api_stick.devices[mac], description)
5555
for description in PW_BINARY_SENSOR_TYPES
56-
if description.plugwise_api == STICK
57-
and description.key in api_stick.devices[mac].features
56+
if description.key in api_stick.devices[mac].features
5857
]
5958
)
6059
if entities:
@@ -88,8 +87,6 @@ def discoved_device(mac: str):
8887
api_stick.subscribe_stick_callback(discoved_device, CB_NEW_NODE)
8988

9089

91-
# pw-beta
92-
# Github issue #265
9390
class USBBinarySensor(PlugwiseUSBEntity, BinarySensorEntity): # type: ignore[misc]
9491
"""Representation of a Plugwise USB Binary Sensor."""
9592

@@ -102,8 +99,7 @@ def __init__(
10299
@property
103100
def is_on(self) -> bool:
104101
"""Return true if the binary_sensor is on."""
105-
# Github issue #265
106-
return getattr(self._node, self.entity_description.state_request_method) # type: ignore[attr-defined]
102+
return getattr(self._node, self.entity_description.state_request_method)
107103

108104
def _service_scan_config(self, **kwargs):
109105
"""Service call to configure motion sensor of Scan device."""

custom_components/plugwise_usb/config_flow.py

Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,43 +3,35 @@
33

44
from typing import Any
55

6-
import serial.tools.list_ports # pw-beta usb
6+
import serial.tools.list_ports
77
import voluptuous as vol
88

9-
from homeassistant.components import usb # pw-beta usb
9+
from homeassistant.components import usb
1010
from homeassistant.config_entries import ConfigFlow
1111
from homeassistant.const import CONF_BASE
1212
from homeassistant.core import callback
1313
from homeassistant.data_entry_flow import FlowResult
14-
from plugwise_usb import Stick # pw-beta usb
14+
from plugwise_usb import Stick
15+
from plugwise_usb.exceptions import (
16+
NetworkDown,
17+
PortError,
18+
StickInitError,
19+
TimeoutException,
20+
)
1521

16-
# pw-beta Note; the below are explicit through isort
17-
from plugwise_usb.exceptions import NetworkDown # pw-beta usb
18-
from plugwise_usb.exceptions import PortError # pw-beta usb
19-
from plugwise_usb.exceptions import StickInitError # pw-beta usb
20-
from plugwise_usb.exceptions import TimeoutException # pw-beta usb
21-
22-
# pw-beta Note; the below are explicit through isort
23-
from .const import CONF_MANUAL_PATH # pw-beta usb
24-
from .const import CONF_USB_PATH # pw-beta usb
25-
from .const import DOMAIN
22+
from .const import CONF_MANUAL_PATH, CONF_USB_PATH, DOMAIN
2623

2724

2825
@callback
29-
def plugwise_stick_entries(hass): # pw-beta usb
26+
def plugwise_stick_entries(hass):
3027
"""Return existing connections for Plugwise USB-stick domain."""
3128
sticks = []
3229
for entry in hass.config_entries.async_entries(DOMAIN):
3330
sticks.append(entry.data.get(CONF_USB_PATH))
3431
return sticks
3532

3633

37-
# Github issue: #265
38-
# Might be a `tuple[dict[str, str], Stick | None]` for typing, but that throws
39-
# Item None of Optional[Any] not having attribute mac [union-attr]
40-
async def validate_usb_connection(
41-
self, device_path=None
42-
) -> tuple[dict[str, str], Any]: # pw-beta usb
34+
async def validate_usb_connection(self, device_path=None) -> tuple[dict[str, str], Any]:
4335
"""Test if device_path is a real Plugwise USB-Stick."""
4436
errors = {}
4537

@@ -71,7 +63,7 @@ class PlugwiseUSBConfigFlow(ConfigFlow, domain=DOMAIN):
7163

7264
async def async_step_user(
7365
self, user_input: dict[str, Any] | None = None
74-
) -> FlowResult: # pw-beta usb
66+
) -> FlowResult:
7567
"""Step when user initializes a integration."""
7668
errors: dict[str, str] = {}
7769
ports = await self.hass.async_add_executor_job(serial.tools.list_ports.comports)
@@ -108,7 +100,7 @@ async def async_step_user(
108100

109101
async def async_step_manual_path(
110102
self, user_input: dict[str, Any] | None = None
111-
) -> FlowResult: # pw-beta usb
103+
) -> FlowResult:
112104
"""Step when manual path to device."""
113105
errors: dict[str, str] = {}
114106
if user_input is not None:

custom_components/plugwise_usb/const.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import logging
44
from typing import Final
55

6-
import voluptuous as vol # pw-beta usb
6+
import voluptuous as vol
77

88
from homeassistant.const import Platform
99
from homeassistant.helpers import config_validation as cv
@@ -82,7 +82,7 @@
8282
SERVICE_USB_DEVICE_REMOVE: Final = "device_remove"
8383
SERVICE_USB_DEVICE_SCHEMA: Final = vol.Schema(
8484
{vol.Required(ATTR_MAC_ADDRESS): cv.string}
85-
) # pw-beta usb
85+
)
8686

8787

8888
# USB Relay device constants

custom_components/plugwise_usb/manifest.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,6 @@
88
"integration_type": "hub",
99
"iot_class": "local_polling",
1010
"loggers": ["plugwise_usb"],
11-
"requirements": ["plugwise-usb==0.31.0a4"],
12-
"version": "0.40.0a1"
11+
"requirements": ["plugwise-usb==0.31.0"],
12+
"version": "0.40.0a3"
1313
}

custom_components/plugwise_usb/models.py

Lines changed: 5 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -22,64 +22,47 @@
2222
)
2323
from homeassistant.helpers.entity import EntityDescription
2424

25-
from .const import STICK, USB_MOTION_ID, USB_RELAY_ID
25+
from .const import USB_MOTION_ID, USB_RELAY_ID
2626

2727

2828
@dataclass
29-
class PlugwiseRequiredKeysMixin:
30-
"""Mixin for required keys."""
31-
32-
plugwise_api: str
33-
34-
35-
@dataclass
36-
class PlugwiseEntityDescription(EntityDescription, PlugwiseRequiredKeysMixin):
29+
class PlugwiseEntityDescription(EntityDescription):
3730
"""Generic Plugwise entity description."""
3831

32+
should_poll: bool = False
33+
state_request_method: str = "dummy"
34+
3935

4036
@dataclass
4137
class PlugwiseSensorEntityDescription(
4238
SensorEntityDescription, PlugwiseEntityDescription
4339
):
4440
"""Describes Plugwise sensor entity."""
4541

46-
should_poll: bool = False
47-
state_class: str | None = SensorStateClass.MEASUREMENT
48-
state_request_method: str | None = None
49-
5042

5143
@dataclass
5244
class PlugwiseSwitchEntityDescription(
5345
SwitchEntityDescription, PlugwiseEntityDescription
5446
):
5547
"""Describes Plugwise switch entity."""
5648

57-
should_poll: bool = False
58-
state_request_method: str | None = None
59-
6049

6150
@dataclass
6251
class PlugwiseBinarySensorEntityDescription(
6352
BinarySensorEntityDescription, PlugwiseEntityDescription
6453
):
6554
"""Describes Plugwise binary sensor entity."""
6655

67-
icon_off: str | None = None
68-
should_poll: bool = False
69-
state_request_method: str | None = None
70-
7156

7257
PW_SENSOR_TYPES: tuple[PlugwiseSensorEntityDescription, ...] = (
7358
PlugwiseSensorEntityDescription(
74-
plugwise_api=STICK,
7559
key="power_1s",
7660
name="Power usage",
7761
device_class=SensorDeviceClass.POWER,
7862
native_unit_of_measurement=UnitOfPower.WATT,
7963
state_request_method="current_power_usage",
8064
),
8165
PlugwiseSensorEntityDescription(
82-
plugwise_api=STICK,
8366
key="energy_consumption_today",
8467
name="Energy consumption today",
8568
device_class=SensorDeviceClass.ENERGY,
@@ -88,7 +71,6 @@ class PlugwiseBinarySensorEntityDescription(
8871
state_request_method="energy_consumption_today",
8972
),
9073
PlugwiseSensorEntityDescription(
91-
plugwise_api=STICK,
9274
key="ping",
9375
name="Ping roundtrip",
9476
icon="mdi:speedometer",
@@ -98,7 +80,6 @@ class PlugwiseBinarySensorEntityDescription(
9880
entity_category=EntityCategory.DIAGNOSTIC,
9981
),
10082
PlugwiseSensorEntityDescription(
101-
plugwise_api=STICK,
10283
key="power_8s",
10384
name="Power usage 8 seconds",
10485
device_class=SensorDeviceClass.POWER,
@@ -107,7 +88,6 @@ class PlugwiseBinarySensorEntityDescription(
10788
entity_registry_enabled_default=False,
10889
),
10990
PlugwiseSensorEntityDescription(
110-
plugwise_api=STICK,
11191
key="RSSI_in",
11292
name="Inbound RSSI",
11393
device_class=SensorDeviceClass.SIGNAL_STRENGTH,
@@ -117,7 +97,6 @@ class PlugwiseBinarySensorEntityDescription(
11797
entity_category=EntityCategory.DIAGNOSTIC,
11898
),
11999
PlugwiseSensorEntityDescription(
120-
plugwise_api=STICK,
121100
key="RSSI_out",
122101
name="Outbound RSSI",
123102
device_class=SensorDeviceClass.SIGNAL_STRENGTH,
@@ -127,7 +106,6 @@ class PlugwiseBinarySensorEntityDescription(
127106
entity_category=EntityCategory.DIAGNOSTIC,
128107
),
129108
PlugwiseSensorEntityDescription(
130-
plugwise_api=STICK,
131109
key="power_con_cur_hour",
132110
name="Power consumption current hour",
133111
icon="mdi:lightning-bolt",
@@ -136,7 +114,6 @@ class PlugwiseBinarySensorEntityDescription(
136114
entity_registry_enabled_default=False,
137115
),
138116
PlugwiseSensorEntityDescription(
139-
plugwise_api=STICK,
140117
key="power_prod_cur_hour",
141118
name="Power production current hour",
142119
icon="mdi:lightning-bolt",
@@ -145,7 +122,6 @@ class PlugwiseBinarySensorEntityDescription(
145122
entity_registry_enabled_default=False,
146123
),
147124
PlugwiseSensorEntityDescription(
148-
plugwise_api=STICK,
149125
key="power_con_today",
150126
name="Power consumption today",
151127
icon="mdi:lightning-bolt",
@@ -154,7 +130,6 @@ class PlugwiseBinarySensorEntityDescription(
154130
entity_registry_enabled_default=False,
155131
),
156132
PlugwiseSensorEntityDescription(
157-
plugwise_api=STICK,
158133
key="power_con_prev_hour",
159134
name="Power consumption previous hour",
160135
icon="mdi:lightning-bolt",
@@ -164,7 +139,6 @@ class PlugwiseBinarySensorEntityDescription(
164139
entity_registry_enabled_default=False,
165140
),
166141
PlugwiseSensorEntityDescription(
167-
plugwise_api=STICK,
168142
key="power_con_yesterday",
169143
name="Power consumption yesterday",
170144
icon="mdi:lightning-bolt",
@@ -177,7 +151,6 @@ class PlugwiseBinarySensorEntityDescription(
177151

178152
PW_SWITCH_TYPES: tuple[PlugwiseSwitchEntityDescription, ...] = (
179153
PlugwiseSwitchEntityDescription(
180-
plugwise_api=STICK,
181154
key=USB_RELAY_ID,
182155
device_class=SwitchDeviceClass.OUTLET,
183156
name="Relay state",
@@ -187,7 +160,6 @@ class PlugwiseBinarySensorEntityDescription(
187160

188161
PW_BINARY_SENSOR_TYPES: tuple[PlugwiseBinarySensorEntityDescription, ...] = (
189162
PlugwiseBinarySensorEntityDescription(
190-
plugwise_api=STICK,
191163
key=USB_MOTION_ID,
192164
name="Motion",
193165
device_class=BinarySensorDeviceClass.MOTION,

custom_components/plugwise_usb/sensor.py

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
from homeassistant.helpers.entity_platform import AddEntitiesCallback
99
from plugwise_usb.nodes import PlugwiseNode
1010

11-
from . import PlugwiseUSBEntity # pw-beta usb
12-
from .const import CB_NEW_NODE, DOMAIN, STICK # pw-beta usb
11+
from . import PlugwiseUSBEntity
12+
from .const import CB_NEW_NODE, DOMAIN, STICK
1313
from .models import PW_SENSOR_TYPES, PlugwiseSensorEntityDescription
1414

1515
PARALLEL_UPDATES = 0
@@ -30,8 +30,7 @@ async def async_add_sensors(mac: str):
3030
[
3131
USBSensor(api_stick.devices[mac], description)
3232
for description in PW_SENSOR_TYPES
33-
if description.plugwise_api == STICK
34-
and description.key in api_stick.devices[mac].features
33+
if description.key in api_stick.devices[mac].features
3534
]
3635
)
3736
if entities:
@@ -48,8 +47,7 @@ def discoved_device(mac: str):
4847
api_stick.subscribe_stick_callback(discoved_device, CB_NEW_NODE)
4948

5049

51-
# Github issue #265
52-
class USBSensor(PlugwiseUSBEntity, SensorEntity): # type: ignore[misc] # pw-beta usb
50+
class USBSensor(PlugwiseUSBEntity, SensorEntity): # type: ignore[misc]
5351
"""Representation of a Plugwise USB sensor."""
5452

5553
def __init__(
@@ -61,9 +59,7 @@ def __init__(
6159
@property
6260
def native_value(self) -> float | None:
6361
"""Return the native value of the sensor."""
64-
# Github issue #265
65-
state_value = getattr(self._node, self.entity_description.state_request_method) # type: ignore[attr-defined]
66-
# /Github issue #265
62+
state_value = getattr(self._node, self.entity_description.state_request_method)
6763
if state_value is not None:
6864
return float(round(state_value, 3))
6965
return None

0 commit comments

Comments
 (0)