Skip to content

Commit 1293e7e

Browse files
authored
Improve type hints in mfi (home-assistant#160985)
1 parent 3e81cea commit 1293e7e

File tree

3 files changed

+43
-33
lines changed

3 files changed

+43
-33
lines changed

homeassistant/components/mfi/sensor.py

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,15 @@
44

55
import logging
66

7-
from mficlient.client import FailedToLogin, MFiClient
7+
from mficlient.client import FailedToLogin, MFiClient, Port as MFiPort
88
import requests
99
import voluptuous as vol
1010

1111
from homeassistant.components.sensor import (
1212
PLATFORM_SCHEMA as SENSOR_PLATFORM_SCHEMA,
1313
SensorDeviceClass,
1414
SensorEntity,
15+
StateType,
1516
)
1617
from homeassistant.const import (
1718
CONF_HOST,
@@ -64,24 +65,29 @@ def setup_platform(
6465
discovery_info: DiscoveryInfoType | None = None,
6566
) -> None:
6667
"""Set up mFi sensors."""
67-
host = config.get(CONF_HOST)
68-
username = config.get(CONF_USERNAME)
69-
password = config.get(CONF_PASSWORD)
70-
use_tls = config.get(CONF_SSL)
71-
verify_tls = config.get(CONF_VERIFY_SSL)
68+
host: str = config[CONF_HOST]
69+
username: str = config[CONF_USERNAME]
70+
password: str = config[CONF_PASSWORD]
71+
use_tls: bool = config[CONF_SSL]
72+
verify_tls: bool = config[CONF_VERIFY_SSL]
7273
default_port = 6443 if use_tls else 6080
73-
port = int(config.get(CONF_PORT, default_port))
74+
network_port: int = config.get(CONF_PORT, default_port)
7475

7576
try:
7677
client = MFiClient(
77-
host, username, password, port=port, use_tls=use_tls, verify=verify_tls
78+
host,
79+
username,
80+
password,
81+
port=network_port,
82+
use_tls=use_tls,
83+
verify=verify_tls,
7884
)
7985
except (FailedToLogin, requests.exceptions.ConnectionError) as ex:
8086
_LOGGER.error("Unable to connect to mFi: %s", str(ex))
8187
return
8288

8389
add_entities(
84-
MfiSensor(port, hass)
90+
MfiSensor(port)
8591
for device in client.get_devices()
8692
for port in device.ports.values()
8793
if port.model in SENSOR_MODELS
@@ -91,18 +97,17 @@ def setup_platform(
9197
class MfiSensor(SensorEntity):
9298
"""Representation of a mFi sensor."""
9399

94-
def __init__(self, port, hass):
100+
def __init__(self, port: MFiPort) -> None:
95101
"""Initialize the sensor."""
96102
self._port = port
97-
self._hass = hass
98103

99104
@property
100-
def name(self):
105+
def name(self) -> str:
101106
"""Return the name of the sensor."""
102107
return self._port.label
103108

104109
@property
105-
def native_value(self):
110+
def native_value(self) -> StateType:
106111
"""Return the state of the sensor."""
107112
try:
108113
tag = self._port.tag
@@ -129,7 +134,7 @@ def device_class(self) -> SensorDeviceClass | None:
129134
return None
130135

131136
@property
132-
def native_unit_of_measurement(self):
137+
def native_unit_of_measurement(self) -> str | None:
133138
"""Return the unit of measurement of this entity, if any."""
134139
try:
135140
tag = self._port.tag

homeassistant/components/mfi/switch.py

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import logging
66
from typing import Any
77

8-
from mficlient.client import FailedToLogin, MFiClient
8+
from mficlient.client import FailedToLogin, MFiClient, Port as MFiPort
99
import requests
1010
import voluptuous as vol
1111

@@ -51,18 +51,23 @@ def setup_platform(
5151
add_entities: AddEntitiesCallback,
5252
discovery_info: DiscoveryInfoType | None = None,
5353
) -> None:
54-
"""Set up mFi sensors."""
55-
host = config.get(CONF_HOST)
56-
username = config.get(CONF_USERNAME)
57-
password = config.get(CONF_PASSWORD)
58-
use_tls = config[CONF_SSL]
59-
verify_tls = config.get(CONF_VERIFY_SSL)
54+
"""Set up mFi switches."""
55+
host: str = config[CONF_HOST]
56+
username: str = config[CONF_USERNAME]
57+
password: str = config[CONF_PASSWORD]
58+
use_tls: bool = config[CONF_SSL]
59+
verify_tls: bool = config[CONF_VERIFY_SSL]
6060
default_port = 6443 if use_tls else 6080
61-
port = int(config.get(CONF_PORT, default_port))
61+
network_port: int = config.get(CONF_PORT, default_port)
6262

6363
try:
6464
client = MFiClient(
65-
host, username, password, port=port, use_tls=use_tls, verify=verify_tls
65+
host,
66+
username,
67+
password,
68+
port=network_port,
69+
use_tls=use_tls,
70+
verify=verify_tls,
6671
)
6772
except (FailedToLogin, requests.exceptions.ConnectionError) as ex:
6873
_LOGGER.error("Unable to connect to mFi: %s", str(ex))
@@ -79,23 +84,23 @@ def setup_platform(
7984
class MfiSwitch(SwitchEntity):
8085
"""Representation of an mFi switch-able device."""
8186

82-
def __init__(self, port):
87+
def __init__(self, port: MFiPort) -> None:
8388
"""Initialize the mFi device."""
8489
self._port = port
85-
self._target_state = None
90+
self._target_state: bool | None = None
8691

8792
@property
88-
def unique_id(self):
93+
def unique_id(self) -> str:
8994
"""Return the unique ID of the device."""
9095
return self._port.ident
9196

9297
@property
93-
def name(self):
98+
def name(self) -> str:
9499
"""Return the name of the device."""
95100
return self._port.label
96101

97102
@property
98-
def is_on(self):
103+
def is_on(self) -> bool | None:
99104
"""Return true if the device is on."""
100105
return self._port.output
101106

tests/components/mfi/test_sensor.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,22 +34,22 @@ async def test_setup_missing_config(hass: HomeAssistant) -> None:
3434
"""Test setup with missing configuration."""
3535
with mock.patch("homeassistant.components.mfi.sensor.MFiClient") as mock_client:
3636
config = {"sensor": {"platform": "mfi"}}
37-
assert await async_setup_component(hass, "sensor", config)
37+
assert await async_setup_component(hass, COMPONENT.DOMAIN, config)
3838
assert not mock_client.called
3939

4040

4141
async def test_setup_failed_login(hass: HomeAssistant) -> None:
4242
"""Test setup with login failure."""
4343
with mock.patch("homeassistant.components.mfi.sensor.MFiClient") as mock_client:
4444
mock_client.side_effect = FailedToLogin
45-
assert not PLATFORM.setup_platform(hass, GOOD_CONFIG, None)
45+
assert not PLATFORM.setup_platform(hass, GOOD_CONFIG[COMPONENT.DOMAIN], None)
4646

4747

4848
async def test_setup_failed_connect(hass: HomeAssistant) -> None:
4949
"""Test setup with connection failure."""
5050
with mock.patch("homeassistant.components.mfi.sensor.MFiClient") as mock_client:
5151
mock_client.side_effect = requests.exceptions.ConnectionError
52-
assert not PLATFORM.setup_platform(hass, GOOD_CONFIG, None)
52+
assert not PLATFORM.setup_platform(hass, GOOD_CONFIG[COMPONENT.DOMAIN], None)
5353

5454

5555
async def test_setup_minimum(hass: HomeAssistant) -> None:
@@ -111,7 +111,7 @@ async def test_setup_adds_proper_devices(hass: HomeAssistant) -> None:
111111
await hass.async_block_till_done()
112112
for ident, port in ports.items():
113113
if ident != "bad":
114-
mock_sensor.assert_any_call(port, hass)
114+
mock_sensor.assert_any_call(port)
115115
assert mock.call(ports["bad"], hass) not in mock_sensor.mock_calls
116116

117117

@@ -124,7 +124,7 @@ def port_fixture() -> mock.MagicMock:
124124
@pytest.fixture(name="sensor")
125125
def sensor_fixture(hass: HomeAssistant, port: mock.MagicMock) -> mfi.MfiSensor:
126126
"""Sensor fixture."""
127-
sensor = mfi.MfiSensor(port, hass)
127+
sensor = mfi.MfiSensor(port)
128128
sensor.hass = hass
129129
return sensor
130130

0 commit comments

Comments
 (0)