Skip to content

Commit 22220a2

Browse files
committed
refactor: linting
1 parent 3f7c44e commit 22220a2

File tree

13 files changed

+61
-56
lines changed

13 files changed

+61
-56
lines changed

.devcontainer/devcontainer.json

+2-4
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@
33
"image": "ludeeus/container:integration-debian",
44
"name": "Blueprint integration development",
55
"context": "..",
6-
"appPort": [
7-
"9123:8123"
8-
],
6+
"appPort": ["9123:8123"],
97
"postCreateCommand": "container install",
108
"extensions": [
119
"ms-python.python",
@@ -27,4 +25,4 @@
2725
"editor.formatOnType": true,
2826
"files.trimTrailingWhitespace": true
2927
}
30-
}
28+
}

.flake8

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@ select = C,E,F,W,B,SIM,T
88
# the line lengths are enforced by black and docformatter
99
# therefore we ignore E501 and B950 here
1010
# SIM119 - are irrelevant as we still support python 3.6 series
11-
ignore = E501,B950,W503,E203,SIM119
11+
ignore = E501,B950,W503,E203,SIM119

.vscode/launch.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,4 @@
3131
]
3232
}
3333
]
34-
}
34+
}

.vscode/settings.json

+3-13
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
{
22
"editor.formatOnSave": true,
33
"python.formatting.provider": "black",
4-
"python.sortImports.args": [
5-
"--profile",
6-
"black"
7-
],
4+
"python.sortImports.args": ["--profile", "black"],
85
"editor.codeActionsOnSave": {
96
"source.organizeImports": true
107
},
@@ -13,12 +10,5 @@
1310
"files.associations": {
1411
"*.yaml": "home-assistant"
1512
},
16-
"cSpell.words": [
17-
"actions",
18-
"assistant",
19-
"hassfest",
20-
"home",
21-
"master",
22-
"uhoo"
23-
],
24-
}
13+
"cSpell.words": ["actions", "assistant", "hassfest", "home", "master", "uhoo"]
14+
}

.yamllint

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@ rules:
44
# 80 chars should be enough, but don't fail if a line is longer
55
line-length:
66
max: 80
7-
level: warning
7+
level: warning

custom_components/uhoo/__init__.py

+14-2
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,11 @@
66
"""
77

88
import asyncio
9-
from typing import Dict
9+
from typing import Dict, List
1010

1111
from pyuhoo import Client
1212
from pyuhoo.device import Device
13+
from pyuhoo.errors import UnauthorizedError
1314

1415
from homeassistant.config_entries import ConfigEntry
1516
from homeassistant.const import CONF_PASSWORD, CONF_USERNAME
@@ -41,6 +42,14 @@ async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> b
4142
session = async_get_clientsession(hass)
4243
client = Client(username, password, session, debug=True)
4344

45+
try:
46+
client = Client(username, password, session, debug=True)
47+
await client.login()
48+
except UnauthorizedError as err:
49+
LOGGER.error(
50+
f"Error: received a 401 Unauthorized error attempting to login:\n{err}"
51+
)
52+
4453
coordinator = UhooDataUpdateCoordinator(hass, client=client)
4554
await coordinator.async_refresh()
4655

@@ -66,7 +75,7 @@ class UhooDataUpdateCoordinator(DataUpdateCoordinator):
6675
def __init__(self, hass: HomeAssistant, client: Client) -> None:
6776
"""Initialize."""
6877
self.client = client
69-
self.platforms = []
78+
self.platforms: List[str] = []
7079
self.user_settings_temp = None
7180

7281
super().__init__(hass, LOGGER, name=DOMAIN, update_interval=UPDATE_INTERVAL)
@@ -77,6 +86,9 @@ async def _async_update_data(self) -> Dict[str, Device]:
7786
self.user_settings_temp = self.client.user_settings_temp
7887
return self.client.get_devices()
7988
except Exception as exception:
89+
LOGGER.error(
90+
f"Error: an exception occurred while attempting to get latest data:\n{exception}"
91+
)
8092
raise UpdateFailed() from exception
8193

8294

custom_components/uhoo/config_flow.py

+7-5
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
from homeassistant.core import callback
1515
from homeassistant.helpers.aiohttp_client import async_create_clientsession
1616

17-
from .const import DOMAIN, PLATFORMS
17+
from .const import DOMAIN, LOGGER, PLATFORMS
1818

1919

2020
class UhooFlowHandler(ConfigFlow, domain=DOMAIN): # type: ignore[call-arg]
@@ -86,10 +86,12 @@ async def _test_credentials(self, username, password):
8686
client = Client(username, password, session)
8787
await client.login()
8888
return True
89-
except UnauthorizedError:
90-
pass
91-
except Exception:
92-
pass
89+
except UnauthorizedError as err:
90+
LOGGER.error(
91+
f"Error: received a 401 Unauthorized error attempting to login:\n{err}"
92+
)
93+
except Exception as err:
94+
LOGGER.error(f"Error: exception while attempting to login:\n{err}")
9395
return False
9496

9597

custom_components/uhoo/const.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from datetime import timedelta
22
import logging
33

4-
from homeassistant.const import (
4+
from homeassistant.const import ( # noqa:F401
55
ATTR_DEVICE_CLASS,
66
CONCENTRATION_MICROGRAMS_PER_CUBIC_METER,
77
CONCENTRATION_PARTS_PER_BILLION,
@@ -10,11 +10,14 @@
1010
DEVICE_CLASS_TEMPERATURE,
1111
PERCENTAGE,
1212
PRESSURE_HPA,
13+
TEMP_CELSIUS,
1314
TEMP_FAHRENHEIT,
1415
)
1516

1617
# Base component constants
1718
NAME = "uHoo Integration"
19+
MODEL = "uHoo Indoor Air Monitor"
20+
MANUFACTURER = "uHoo"
1821
DOMAIN = "uhoo"
1922
VERSION = "0.0.3"
2023
ISSUE_URL = "https://github.com/csacca/uhoo-homeassistant/issues"

custom_components/uhoo/manifest.json

+3-7
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,6 @@
66
"issue_tracker": "https://github.com/csacca/uhoo-homeassistant/issues",
77
"version": "0.0.2",
88
"config_flow": true,
9-
"codeowners": [
10-
"@csacca"
11-
],
12-
"requirements": [
13-
"pyuhoo==0.0.3"
14-
]
15-
}
9+
"codeowners": ["@csacca"],
10+
"requirements": ["pyuhoo==0.0.3"]
11+
}

custom_components/uhoo/sensor.py

+19-15
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
"""UhooSensorEntity class"""
22

3+
from pyuhoo.device import Device
4+
35
from custom_components.uhoo import UhooDataUpdateCoordinator
46
from homeassistant.components.sensor import SensorEntity
57
from homeassistant.core import HomeAssistant
@@ -15,7 +17,8 @@
1517
ATTR_UNIQUE_ID,
1618
ATTR_UNIT,
1719
DOMAIN,
18-
NAME,
20+
MANUFACTURER,
21+
MODEL,
1922
SENSOR_TYPES,
2023
TEMP_CELSIUS,
2124
TEMP_FAHRENHEIT,
@@ -45,58 +48,59 @@ def __init__(
4548
self, kind: str, serial_number: str, coordinator: UhooDataUpdateCoordinator
4649
):
4750
super().__init__(coordinator)
51+
self._coordinator = coordinator
4852
self._kind = kind
4953
self._serial_number = serial_number
5054

5155
@property
5256
def name(self):
5357
"""Return the name of the particular component."""
54-
return (
55-
f"uHoo {self._serial_number} {SENSOR_TYPES[self.sensor_type][ATTR_LABEL]}"
56-
)
58+
device: Device = self.coordinator.data[self._serial_number]
59+
return f"uHoo {device.name} {SENSOR_TYPES[self._kind][ATTR_LABEL]}"
5760

5861
@property
5962
def unique_id(self):
6063
"""Return a unique ID to use for this entity."""
61-
return f"{self._serial_number}_{SENSOR_TYPES[self.sensor_type][ATTR_UNIQUE_ID]}"
64+
return f"{self._serial_number}_{SENSOR_TYPES[self._kind][ATTR_UNIQUE_ID]}"
6265

6366
@property
6467
def device_info(self):
6568
# we probably could pull the firmware version if we wanted
6669
# its in the api somewhere
70+
device: Device = self.coordinator.data[self._serial_number]
6771
return {
6872
"identifiers": {(DOMAIN, self._serial_number)},
69-
"name": NAME,
70-
"model": NAME,
71-
"manufacturer": NAME,
73+
"name": device.name,
74+
"model": MODEL,
75+
"manufacturer": MANUFACTURER,
7276
}
7377

7478
@property
7579
def state(self):
7680
"""State of the sensor."""
77-
device = self.coordinator.data[self.serial_number]
78-
state = getattr(device, self.sensor_type)
81+
device: Device = self._coordinator.data[self._serial_number]
82+
state = getattr(device, self._kind)
7983
if isinstance(state, list):
8084
state = state[0]
8185
return state
8286

8387
@property
8488
def device_class(self):
8589
"""Return the device class."""
86-
return SENSOR_TYPES[self.sensor_type][ATTR_DEVICE_CLASS]
90+
return SENSOR_TYPES[self._kind][ATTR_DEVICE_CLASS]
8791

8892
@property
8993
def icon(self):
9094
"""Return the icon."""
91-
return SENSOR_TYPES[self.sensor_type][ATTR_ICON]
95+
return SENSOR_TYPES[self._kind][ATTR_ICON]
9296

9397
@property
9498
def unit_of_measurement(self):
9599
"""Return unit of measurement."""
96-
if self.sensor_type == API_TEMP:
97-
if self.coordinator.user_settings_temp == "f":
100+
if self._kind == API_TEMP:
101+
if self._coordinator.user_settings_temp == "f":
98102
return TEMP_FAHRENHEIT
99103
else:
100104
return TEMP_CELSIUS
101105
else:
102-
return SENSOR_TYPES[self.sensor_type][ATTR_UNIT]
106+
return SENSOR_TYPES[self._kind][ATTR_UNIT]

custom_components/uhoo/translations/en.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,4 @@
1616
"single_instance_allowed": "Only a single instance is allowed."
1717
}
1818
}
19-
}
19+
}

poetry.lock

+4-4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ readme = "README.md"
99

1010
[tool.poetry.dependencies]
1111
python = "^3.8"
12-
pyuhoo = "^0.0.3"
12+
pyuhoo = "^0.0.4"
1313

1414
[tool.poetry.dev-dependencies]
1515
homeassistant = "^2021.8.0b0"

0 commit comments

Comments
 (0)