Skip to content

Commit

Permalink
Fix code style and some typo of last PR (#119)
Browse files Browse the repository at this point in the history
* Fix code style

* Transfer content in template

* Code style update with black

* Change domain name for tests

To avoid complex code style by black.
  • Loading branch information
oncleben31 committed Jan 13, 2021
1 parent daa9cae commit f045706
Show file tree
Hide file tree
Showing 15 changed files with 78 additions and 83 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/reset-instance.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ jobs:
--overwrite-if-exists
--no-input
friendly_name="Cookiecutter Home Assistant Custom Component Instance"
domain_name=cookiecutter_homeassistant_custom_component_instance
domain_name=cc_ha_cci
project_name=cookiecutter-homeassistant-custom-component-instance
- name: Create commit
Expand Down
3 changes: 0 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,3 @@
__pycache__/
/.python-version
/.vscode/
.coverage
venv
.venv
3 changes: 3 additions & 0 deletions {{cookiecutter.project_name}}/.gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
__pycache__
pythonenv*
.python-version
.coverage
venv
.venv
5 changes: 4 additions & 1 deletion {{cookiecutter.project_name}}/.vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
{
"python.linting.pylintEnabled": true,
"python.linting.enabled": true,
"python.pythonPath": "/usr/local/bin/python"
"python.pythonPath": "venv/bin/python",
"files.associations": {
"*.yaml": "home-assistant"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,9 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry):
password = entry.data.get(CONF_PASSWORD)

session = async_get_clientsession(hass)
client = {{cookiecutter.class_name_prefix}}ApiClient(
username, password, session
)
client = {{cookiecutter.class_name_prefix}}ApiClient(username, password, session)

coordinator = {{cookiecutter.class_name_prefix}}DataUpdateCoordinator(
hass, client=client
)
coordinator = {{cookiecutter.class_name_prefix}}DataUpdateCoordinator(hass, client=client)
await coordinator.async_refresh()

if not coordinator.last_update_success:
Expand All @@ -68,9 +64,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry):
return True


class {{cookiecutter.class_name_prefix}}DataUpdateCoordinator(
DataUpdateCoordinator
):
class {{cookiecutter.class_name_prefix}}DataUpdateCoordinator(DataUpdateCoordinator):
"""Class to manage fetching data from the API."""

def __init__(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,10 @@
async def async_setup_entry(hass, entry, async_add_devices):
"""Setup binary_sensor platform."""
coordinator = hass.data[DOMAIN][entry.entry_id]
async_add_devices(
[
{{cookiecutter.class_name_prefix}}BinarySensor(
coordinator, entry
)
]
)


class {{cookiecutter.class_name_prefix}}BinarySensor(
{{cookiecutter.class_name_prefix}}Entity, BinarySensorEntity
):
async_add_devices([{{cookiecutter.class_name_prefix}}BinarySensor(coordinator, entry)])


class {{cookiecutter.class_name_prefix}}BinarySensor({{cookiecutter.class_name_prefix}}Entity, BinarySensorEntity):
"""{{cookiecutter.domain_name}} binary_sensor class."""

@property
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@
from .const import PLATFORMS


class {{cookiecutter.class_name_prefix}}FlowHandler(
config_entries.ConfigFlow, domain=DOMAIN
):
class {{cookiecutter.class_name_prefix}}FlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
"""Config flow for {{cookiecutter.domain_name}}."""

VERSION = 1
Expand Down Expand Up @@ -49,9 +47,7 @@ async def async_step_user(self, user_input=None):
@staticmethod
@callback
def async_get_options_flow(config_entry):
return {{cookiecutter.class_name_prefix}}OptionsFlowHandler(
config_entry
)
return {{cookiecutter.class_name_prefix}}OptionsFlowHandler(config_entry)

async def _show_config_form(self, user_input): # pylint: disable=unused-argument
"""Show the configuration form to edit location data."""
Expand All @@ -67,19 +63,15 @@ async def _test_credentials(self, username, password):
"""Return true if credentials is valid."""
try:
session = async_create_clientsession(self.hass)
client = {{cookiecutter.class_name_prefix}}ApiClient(
username, password, session
)
client = {{cookiecutter.class_name_prefix}}ApiClient(username, password, session)
await client.async_get_data()
return True
except Exception: # pylint: disable=broad-except
pass
return False


class {{cookiecutter.class_name_prefix}}OptionsFlowHandler(
config_entries.OptionsFlow
):
class {{cookiecutter.class_name_prefix}}OptionsFlowHandler(config_entries.OptionsFlow):
"""Config flow options handler for {{cookiecutter.domain_name}}."""

def __init__(self, config_entry):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,10 @@
async def async_setup_entry(hass, entry, async_add_devices):
"""Setup sensor platform."""
coordinator = hass.data[DOMAIN][entry.entry_id]
async_add_devices(
[{{cookiecutter.class_name_prefix}}Sensor(coordinator, entry)]
)
async_add_devices([{{cookiecutter.class_name_prefix}}Sensor(coordinator, entry)])


class {{cookiecutter.class_name_prefix}}Sensor(
{{cookiecutter.class_name_prefix}}Entity
):
class {{cookiecutter.class_name_prefix}}Sensor({{cookiecutter.class_name_prefix}}Entity):
"""{{cookiecutter.domain_name}} Sensor class."""

@property
Expand All @@ -37,6 +33,4 @@ def icon(self):
@property
def device_class(self):
"""Return de device class of the sensor."""
return (
"{{cookiecutter.domain_name}}__custom_device_class"
)
return "{{cookiecutter.domain_name}}__custom_device_class"
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,10 @@
async def async_setup_entry(hass, entry, async_add_devices):
"""Setup sensor platform."""
coordinator = hass.data[DOMAIN][entry.entry_id]
async_add_devices(
[
{{cookiecutter.class_name_prefix}}BinarySwitch(
coordinator, entry
)
]
)


class {{cookiecutter.class_name_prefix}}BinarySwitch(
{{cookiecutter.class_name_prefix}}Entity, SwitchEntity
):
async_add_devices([{{cookiecutter.class_name_prefix}}BinarySwitch(coordinator, entry)])


class {{cookiecutter.class_name_prefix}}BinarySwitch({{cookiecutter.class_name_prefix}}Entity, SwitchEntity):
"""{{cookiecutter.domain_name}} switch class."""

async def async_turn_on(self, **kwargs): # pylint: disable=unused-argument
Expand Down
5 changes: 2 additions & 3 deletions {{cookiecutter.project_name}}/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

pytest_plugins = "pytest_homeassistant_custom_component"


# This fixture is used to prevent HomeAssistant from attempting to create and dismiss persistent
# notifications. These calls would fail without this fixture since the persistent_notification
# integration is never loaded during a test.
Expand All @@ -22,9 +23,7 @@ def skip_notifications_fixture():
@pytest.fixture(name="bypass_get_data")
def bypass_get_data_fixture():
"""Skip calls to get data from API."""
with patch(
"custom_components.{{cookiecutter.domain_name}}.{{cookiecutter.class_name_prefix}}ApiClient.async_get_data"
):
with patch("custom_components.{{cookiecutter.domain_name}}.{{cookiecutter.class_name_prefix}}ApiClient.async_get_data"):
yield


Expand Down
7 changes: 6 additions & 1 deletion {{cookiecutter.project_name}}/tests/const.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
"""Constants for {{cookiecutter.friendly_name}} tests."""
from custom_components.{{cookiecutter.domain_name}}.const import CONF_PASSWORD, CONF_USERNAME
from custom_components.{{cookiecutter.domain_name}}.const import (
CONF_PASSWORD,
)
from custom_components.{{cookiecutter.domain_name}}.const import (
CONF_USERNAME,
)

MOCK_CONFIG = {CONF_USERNAME: "test_username", CONF_PASSWORD: "test_password"}
5 changes: 3 additions & 2 deletions {{cookiecutter.project_name}}/tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@
import asyncio

import aiohttp
from custom_components.{{cookiecutter.domain_name}}.api import (
{{cookiecutter.class_name_prefix}}ApiClient,
)
from homeassistant.helpers.aiohttp_client import async_get_clientsession

from custom_components.{{cookiecutter.domain_name}}.api import {{cookiecutter.class_name_prefix}}ApiClient


async def test_api(hass, aioclient_mock, caplog):
"""Test API calls."""
Expand Down
19 changes: 12 additions & 7 deletions {{cookiecutter.project_name}}/tests/test_config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,24 @@
from unittest.mock import patch

import pytest
from homeassistant import config_entries, data_entry_flow
from pytest_homeassistant_custom_component.common import MockConfigEntry

from custom_components.{{cookiecutter.domain_name}}.const import (
BINARY_SENSOR,
)
from custom_components.{{cookiecutter.domain_name}}.const import (
DOMAIN,
)
from custom_components.{{cookiecutter.domain_name}}.const import (
PLATFORMS,
)
from custom_components.{{cookiecutter.domain_name}}.const import (
SENSOR,
)
from custom_components.{{cookiecutter.domain_name}}.const import (
SWITCH,
)
from homeassistant import config_entries
from homeassistant import data_entry_flow
from pytest_homeassistant_custom_component.common import MockConfigEntry

from .const import MOCK_CONFIG

Expand All @@ -22,10 +30,7 @@
@pytest.fixture(autouse=True)
def bypass_setup_fixture():
"""Prevent setup."""
with patch(
"custom_components.{{cookiecutter.domain_name}}.async_setup",
return_value=True,
), patch(
with patch("custom_components.{{cookiecutter.domain_name}}.async_setup", return_value=True,), patch(
"custom_components.{{cookiecutter.domain_name}}.async_setup_entry",
return_value=True,
):
Expand Down
17 changes: 12 additions & 5 deletions {{cookiecutter.project_name}}/tests/test_init.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
"""Test {{cookiecutter.friendly_name}} setup process."""
from homeassistant.exceptions import ConfigEntryNotReady
import pytest
from pytest_homeassistant_custom_component.common import MockConfigEntry

from custom_components.{{cookiecutter.domain_name}} import (
{{cookiecutter.class_name_prefix}}DataUpdateCoordinator,
async_reload_entry,
)
from custom_components.{{cookiecutter.domain_name}} import (
async_setup_entry,
)
from custom_components.{{cookiecutter.domain_name}} import (
async_unload_entry,
)
from custom_components.{{cookiecutter.domain_name}}.const import DOMAIN
from custom_components.{{cookiecutter.domain_name}} import (
{{cookiecutter.class_name_prefix}}DataUpdateCoordinator,
)
from custom_components.{{cookiecutter.domain_name}}.const import (
DOMAIN,
)
from homeassistant.exceptions import ConfigEntryNotReady
from pytest_homeassistant_custom_component.common import MockConfigEntry

from .const import MOCK_CONFIG

Expand Down
23 changes: 17 additions & 6 deletions {{cookiecutter.project_name}}/tests/test_switch.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,24 @@
"""Test {{cookiecutter.friendly_name}} switch."""
from unittest.mock import call, patch

from homeassistant.components.switch import SERVICE_TURN_OFF, SERVICE_TURN_ON
from unittest.mock import call
from unittest.mock import patch

from custom_components.{{cookiecutter.domain_name}} import (
async_setup_entry,
)
from custom_components.{{cookiecutter.domain_name}}.const import (
DEFAULT_NAME,
)
from custom_components.{{cookiecutter.domain_name}}.const import (
DOMAIN,
)
from custom_components.{{cookiecutter.domain_name}}.const import (
SWITCH,
)
from homeassistant.components.switch import SERVICE_TURN_OFF
from homeassistant.components.switch import SERVICE_TURN_ON
from homeassistant.const import ATTR_ENTITY_ID
from pytest_homeassistant_custom_component.common import MockConfigEntry

from custom_components.{{cookiecutter.domain_name}} import async_setup_entry
from custom_components.{{cookiecutter.domain_name}}.const import DEFAULT_NAME, DOMAIN, SWITCH

from .const import MOCK_CONFIG


Expand Down

0 comments on commit f045706

Please sign in to comment.