Skip to content
This repository has been archived by the owner on Jul 7, 2024. It is now read-only.

Commit

Permalink
Fix a few typos etc (#20)
Browse files Browse the repository at this point in the history
* Fix a few typos etc

* black
  • Loading branch information
lyricnz authored Feb 9, 2023
1 parent 27f890d commit b50926f
Show file tree
Hide file tree
Showing 10 changed files with 42 additions and 48 deletions.
6 changes: 3 additions & 3 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ Contributing to this project should be as easy and transparent as possible, whet
- Submitting a fix
- Proposing new features

## Github is used for everything
## GitHub is used for everything

Github is used to host code, to track issues and feature requests, as well as accept pull requests.
GitHub is used to host code, to track issues and feature requests, as well as accept pull requests.

Pull requests are the best way to propose changes to the codebase.

Expand All @@ -23,7 +23,7 @@ Pull requests are the best way to propose changes to the codebase.

In short, when you submit code changes, your submissions are understood to be under the same [MIT License](http://choosealicense.com/licenses/mit/) that covers the project. Feel free to contact the maintainers if that's a concern.

## Report bugs using Github's [issues](../../issues)
## Report bugs using GitHub's [issues](../../issues)

GitHub issues are used to track public bugs.
Report a bug by [opening a new issue](../../issues/new/choose); it's that easy!
Expand Down
8 changes: 3 additions & 5 deletions custom_components/tplink_ess/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry):
client = TPLinkESSClient(username, password, switch_mac)

coordinator = TPLinkESSDataUpdateCoordinator(
hass, client=client, name=switch_mac, interval=interval
hass, client=client, name=switch_mac, update_interval=interval
)
await coordinator.async_refresh()

Expand All @@ -67,14 +67,13 @@ def __init__(
hass: HomeAssistant,
client: TPLinkESSClient,
name: str,
interval: int,
update_interval: timedelta | None = None,
) -> None:
"""Initialize."""
self.api = client
self._interval = interval
self.platforms = []

super().__init__(hass, _LOGGER, name=name, update_interval=interval)
super().__init__(hass, _LOGGER, name=name, update_interval=update_interval)

async def _async_update_data(self):
"""Update data via library."""
Expand All @@ -89,7 +88,6 @@ async def _async_update_data(self):

async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""Handle removal of an entry."""
coordinator = hass.data[DOMAIN][entry.entry_id]
unloaded = all(
await asyncio.gather(
*[
Expand Down
25 changes: 12 additions & 13 deletions custom_components/tplink_ess/binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,23 +41,22 @@ async def async_setup_entry(hass, entry, async_add_entities: AddEntitiesCallback
"""Setup binary_sensor platform."""
coordinator = hass.data[DOMAIN][entry.entry_id]

binary_sensors = []
limit = coordinator.data.get("num_ports")["num_ports"]
prefix = coordinator.data.get("hostname")["hostname"]

for i in range(limit):
binary_sensors.append(
TPLinkESSBinarySensor(
TPLinkBinarySensorEntityDescription(
port=i,
key=None,
name=f"{prefix} Port {i + 1}",
device_class=BinarySensorDeviceClass.CONNECTIVITY,
),
coordinator,
entry,
)
binary_sensors = [
TPLinkESSBinarySensor(
TPLinkBinarySensorEntityDescription(
port=i,
key=None,
name=f"{prefix} Port {i + 1}",
device_class=BinarySensorDeviceClass.CONNECTIVITY,
),
coordinator,
entry,
)
for i in range(limit)
]

for binary_sensor in BINARY_SENSORS_TYPES:
binary_sensors.append(TPLinkESSBinarySensor(binary_sensor, coordinator, entry))
Expand Down
9 changes: 4 additions & 5 deletions custom_components/tplink_ess/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ async def _show_config_manual(self, user_input):
async def async_step_creds(self, user_input=None):
"""Handle credential inputs from user."""
if user_input is not None:
valid = await self._test_credentials(
valid = await self._validate_credentials(
user_input[CONF_USERNAME],
user_input[CONF_PASSWORD],
self._data[CONF_MAC],
Expand All @@ -139,10 +139,8 @@ async def async_step_creds(self, user_input=None):
self._errors["base"] = "auth"
return await self._show_config_form(user_input)

user_input = {}
# Provide defaults for form
user_input[CONF_USERNAME] = ""
user_input[CONF_PASSWORD] = ""
user_input = {CONF_USERNAME: "", CONF_PASSWORD: ""}

return await self._show_config_form(user_input)

Expand Down Expand Up @@ -176,7 +174,8 @@ async def _show_config_form(self, user_input): # pylint: disable=unused-argumen
errors=self._errors,
)

async def _test_credentials(self, username, password, mac_addr):
@staticmethod
async def _validate_credentials(username, password, mac_addr):
"""Return true if credentials is valid."""
try:
client = TPLinkESSClient(username, password, mac_addr)
Expand Down
16 changes: 10 additions & 6 deletions custom_components/tplink_ess/sensor.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
"""Sensor platform for tplink_ess."""
import logging
from typing import Any

from homeassistant.components.sensor import SensorEntity
from homeassistant.config_entries import ConfigEntry
Expand Down Expand Up @@ -152,7 +151,9 @@ def __init__(
self._key = sensor_description.key
self._attr_name = sensor_description.name
self._attr_unique_id = f"{sensor_description.name}_{config.entry_id}"
self._attr_native_unit_of_measurement = sensor_description.native_unit_of_measurement
self._attr_native_unit_of_measurement = (
sensor_description.native_unit_of_measurement
)
self._last_reading = 0.0

@property
Expand All @@ -170,10 +171,13 @@ def native_value(self):
if self._attr_native_unit_of_measurement == "packets/s":
rate = 0.0
if self._last_reading:
rate = round(float(
(value - self._last_reading)
/ self.coordinator.update_interval.total_seconds()
),2)
rate = round(
float(
(value - self._last_reading)
/ self.coordinator.update_interval.total_seconds()
),
2,
)
self._last_reading = float(value)
self._last_reading = int(value)
return float(rate)
Expand Down
2 changes: 1 addition & 1 deletion custom_components/tplink_ess/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
},
"step": {
"user": {
"description": "Select manual entry or automatic disocvery.",
"description": "Select manual entry or automatic discovery.",
"menu_options": {
"manual": "Manually enter MAC address",
"discover": "Discover switches on network"
Expand Down
2 changes: 1 addition & 1 deletion requirements_test.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
-r requirements.txt
pytest-homeassistant-custom-component==0.12.49
pytest-homeassistant-custom-component==0.12.51
10 changes: 5 additions & 5 deletions tests/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ This will install `homeassistant`, `pytest`, and `pytest-homeassistant-custom-co

# Useful commands

Command | Description
------- | -----------
`pytest tests/` | This will run all tests in `tests/` and tell you how many passed/failed
`pytest --durations=10 --cov-report term-missing --cov=custom_components.tplink_ess tests` | This tells `pytest` that your target module to test is `custom_components.tplink_ess` so that it can give you a [code coverage](https://en.wikipedia.org/wiki/Code_coverage) summary, including % of code that was executed and the line numbers of missed executions.
`pytest tests/test_init.py -k test_setup_unload_and_reload_entry` | Runs the `test_setup_unload_and_reload_entry` test function located in `tests/test_init.py`
| Command | Description |
|--------------------------------------------------------------------------------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `pytest tests/` | This will run all tests in `tests/` and tell you how many passed/failed |
| `pytest --durations=10 --cov-report term-missing --cov=custom_components.tplink_ess tests` | This tells `pytest` that your target module to test is `custom_components.tplink_ess` so that it can give you a [code coverage](https://en.wikipedia.org/wiki/Code_coverage) summary, including % of code that was executed and the line numbers of missed executions. |
| `pytest tests/test_init.py -k test_setup_unload_and_reload_entry` | Runs the `test_setup_unload_and_reload_entry` test function located in `tests/test_init.py` |
4 changes: 2 additions & 2 deletions tests/test_config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ async def test_discover(hass, mock_switch):
assert result2["step_id"] == "creds"

with patch(
"custom_components.tplink_ess.config_flow.TPLinkESSFlowHandler._test_credentials",
"custom_components.tplink_ess.config_flow.TPLinkESSFlowHandler._validate_credentials",
return_value=True,
), patch(
"custom_components.tplink_ess.async_setup_entry", return_value=True
Expand Down Expand Up @@ -121,7 +121,7 @@ async def test_manual(hass, mock_switch):
assert result2["step_id"] == "creds"

with patch(
"custom_components.tplink_ess.config_flow.TPLinkESSFlowHandler._test_credentials",
"custom_components.tplink_ess.config_flow.TPLinkESSFlowHandler._validate_credentials",
return_value=True,
), patch(
"custom_components.tplink_ess.async_setup_entry", return_value=True
Expand Down
8 changes: 1 addition & 7 deletions tests/test_init.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,19 @@
"""Test tplink_ess setup process."""

import pytest

from unittest.mock import patch

from homeassistant.components.sensor import DOMAIN as SENSOR_DOMAIN
from homeassistant.components.binary_sensor import DOMAIN as BINARY_SENSOR_DOMAIN
from pytest_homeassistant_custom_component.common import MockConfigEntry

from custom_components.tplink_ess.const import (
DOMAIN,
PLATFORMS,
)

from .const import CONFIG_DATA, SWITCH_NAME

pytestmark = pytest.mark.asyncio


async def test_setup_entry(hass, mock_switch):
"""Test settting up entities."""
"""Test setting up entities."""
entry = MockConfigEntry(
domain=DOMAIN,
title=SWITCH_NAME,
Expand Down

0 comments on commit b50926f

Please sign in to comment.