Skip to content

Commit b116619

Browse files
kellerzajoostlek
andauthored
Bump pysma to 1.0.2 and enable type checking (home-assistant#154977)
Co-authored-by: Joost Lekkerkerker <[email protected]>
1 parent a3d7601 commit b116619

File tree

14 files changed

+83
-68
lines changed

14 files changed

+83
-68
lines changed

.strict-typing

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -477,6 +477,7 @@ homeassistant.components.skybell.*
477477
homeassistant.components.slack.*
478478
homeassistant.components.sleep_as_android.*
479479
homeassistant.components.sleepiq.*
480+
homeassistant.components.sma.*
480481
homeassistant.components.smhi.*
481482
homeassistant.components.smlight.*
482483
homeassistant.components.smtp.*

homeassistant/components/sma/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
import logging
66

7-
from pysma import SMA
7+
from pysma import SMAWebConnect
88

99
from homeassistant.config_entries import ConfigEntry
1010
from homeassistant.const import (
@@ -35,7 +35,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: SMAConfigEntry) -> bool:
3535
protocol = "https" if entry.data[CONF_SSL] else "http"
3636
url = f"{protocol}://{entry.data[CONF_HOST]}"
3737

38-
sma = SMA(
38+
sma = SMAWebConnect(
3939
session=async_get_clientsession(
4040
hass=hass, verify_ssl=entry.data[CONF_VERIFY_SSL]
4141
),

homeassistant/components/sma/config_flow.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,13 @@
66
import logging
77
from typing import Any
88

9-
import pysma
9+
import attrs
10+
from pysma import (
11+
SmaAuthenticationException,
12+
SmaConnectionException,
13+
SmaReadException,
14+
SMAWebConnect,
15+
)
1016
import voluptuous as vol
1117
from yarl import URL
1218

@@ -42,7 +48,7 @@ async def validate_input(
4248
host = data[CONF_HOST] if data is not None else user_input[CONF_HOST]
4349
url = URL.build(scheme=protocol, host=host)
4450

45-
sma = pysma.SMA(
51+
sma = SMAWebConnect(
4652
session, str(url), user_input[CONF_PASSWORD], group=user_input[CONF_GROUP]
4753
)
4854

@@ -51,7 +57,7 @@ async def validate_input(
5157
device_info = await sma.device_info()
5258
await sma.close_session()
5359

54-
return device_info
60+
return attrs.asdict(device_info)
5561

5662

5763
class SmaConfigFlow(ConfigFlow, domain=DOMAIN):
@@ -90,11 +96,11 @@ async def _handle_user_input(
9096
device_info = await validate_input(
9197
self.hass, user_input=user_input, data=self._data
9298
)
93-
except pysma.exceptions.SmaConnectionException:
99+
except SmaConnectionException:
94100
errors["base"] = "cannot_connect"
95-
except pysma.exceptions.SmaAuthenticationException:
101+
except SmaAuthenticationException:
96102
errors["base"] = "invalid_auth"
97-
except pysma.exceptions.SmaReadException:
103+
except SmaReadException:
98104
errors["base"] = "cannot_retrieve_device_info"
99105
except Exception:
100106
_LOGGER.exception("Unexpected exception")

homeassistant/components/sma/coordinator.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,14 @@
66
from datetime import timedelta
77
import logging
88

9-
from pysma import SMA
10-
from pysma.exceptions import (
9+
from pysma import (
1110
SmaAuthenticationException,
1211
SmaConnectionException,
1312
SmaReadException,
13+
SMAWebConnect,
1414
)
15-
from pysma.sensor import Sensor
15+
from pysma.helpers import DeviceInfo
16+
from pysma.sensor import Sensors
1617

1718
from homeassistant.config_entries import ConfigEntry
1819
from homeassistant.const import CONF_SCAN_INTERVAL
@@ -29,8 +30,8 @@
2930
class SMACoordinatorData:
3031
"""Data class for SMA sensors."""
3132

32-
sma_device_info: dict[str, str]
33-
sensors: list[Sensor]
33+
sma_device_info: DeviceInfo
34+
sensors: Sensors
3435

3536

3637
class SMADataUpdateCoordinator(DataUpdateCoordinator[SMACoordinatorData]):
@@ -42,7 +43,7 @@ def __init__(
4243
self,
4344
hass: HomeAssistant,
4445
config_entry: ConfigEntry,
45-
sma: SMA,
46+
sma: SMAWebConnect,
4647
) -> None:
4748
"""Initialize the SMA Data Update Coordinator."""
4849
super().__init__(
@@ -57,8 +58,8 @@ def __init__(
5758
),
5859
)
5960
self.sma = sma
60-
self._sma_device_info: dict[str, str] = {}
61-
self._sensors: list[Sensor] = []
61+
self._sma_device_info = DeviceInfo()
62+
self._sensors = Sensors()
6263

6364
async def _async_setup(self) -> None:
6465
"""Setup the SMA Data Update Coordinator."""

homeassistant/components/sma/manifest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,5 @@
1313
"documentation": "https://www.home-assistant.io/integrations/sma",
1414
"iot_class": "local_polling",
1515
"loggers": ["pysma"],
16-
"requirements": ["pysma==0.7.5"]
16+
"requirements": ["pysma==1.0.2"]
1717
}

homeassistant/components/sma/sensor.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
from __future__ import annotations
44

5-
import pysma
5+
from pysma.sensor import Sensor
66

77
from homeassistant.components.sensor import (
88
SensorDeviceClass,
@@ -859,7 +859,7 @@ def __init__(
859859
self,
860860
coordinator: SMADataUpdateCoordinator,
861861
description: SensorEntityDescription | None,
862-
pysma_sensor: pysma.sensor.Sensor,
862+
pysma_sensor: Sensor,
863863
entry: SMAConfigEntry,
864864
) -> None:
865865
"""Initialize the sensor."""
@@ -873,17 +873,17 @@ def __init__(
873873
url = f"{protocol}://{entry.data[CONF_HOST]}"
874874

875875
self._sensor = pysma_sensor
876-
self._serial = coordinator.data.sma_device_info["serial"]
876+
self._serial = coordinator.data.sma_device_info.serial
877877
assert entry.unique_id
878878

879879
self._attr_device_info = DeviceInfo(
880880
configuration_url=url,
881881
identifiers={(DOMAIN, entry.unique_id)},
882-
manufacturer=coordinator.data.sma_device_info["manufacturer"],
883-
model=coordinator.data.sma_device_info["type"],
884-
name=coordinator.data.sma_device_info["name"],
885-
sw_version=coordinator.data.sma_device_info["sw_version"],
886-
serial_number=coordinator.data.sma_device_info["serial"],
882+
manufacturer=coordinator.data.sma_device_info.manufacturer,
883+
model=coordinator.data.sma_device_info.type,
884+
name=coordinator.data.sma_device_info.name,
885+
sw_version=coordinator.data.sma_device_info.sw_version,
886+
serial_number=coordinator.data.sma_device_info.serial,
887887
)
888888
self._attr_unique_id = (
889889
f"{entry.unique_id}-{pysma_sensor.key}_{pysma_sensor.key_idx}"
@@ -908,7 +908,7 @@ def available(self) -> bool:
908908
"""Return if the device is available."""
909909
return (
910910
super().available
911-
and self._serial == self.coordinator.data.sma_device_info["serial"]
911+
and self._serial == self.coordinator.data.sma_device_info.serial
912912
)
913913

914914
@property

mypy.ini

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

requirements_all.txt

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

requirements_test_all.txt

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/components/sma/__init__.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
"""Tests for the sma integration."""
22

3+
from pysma.helpers import DeviceInfo
4+
35
from homeassistant.components.sma.const import CONF_GROUP
46
from homeassistant.const import (
57
CONF_HOST,
@@ -12,13 +14,14 @@
1214

1315
from tests.common import MockConfigEntry
1416

15-
MOCK_DEVICE = {
16-
"manufacturer": "SMA",
17-
"name": "SMA Device Name",
18-
"type": "Sunny Boy 3.6",
19-
"serial": 123456789,
20-
"sw_version": "1.0.0",
21-
}
17+
MOCK_DEVICE = DeviceInfo(
18+
manufacturer="SMA",
19+
name="SMA Device Name",
20+
type="Sunny Boy 3.6",
21+
serial=123456789,
22+
sw_version="1.0.0",
23+
)
24+
2225

2326
MOCK_USER_INPUT = {
2427
CONF_HOST: "1.1.1.1",

0 commit comments

Comments
 (0)