Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use ruff for linting #58

Merged
merged 2 commits into from
Apr 10, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .github/workflows/ruff.yml
koenhendriks marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
name: Ruff
on: [push, pull_request]
jobs:
ruff:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: chartboost/ruff-action@v1
7 changes: 5 additions & 2 deletions custom_components/button_plus/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""The Button+ integration."""

from __future__ import annotations

import logging
Expand All @@ -23,7 +24,9 @@
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""Set up Button+ from a config entry."""
_LOGGER.debug(f"Button+ init got new device entry! {entry.entry_id.title}")
device_configuration: DeviceConfiguration = DeviceConfiguration.from_json(entry.data.get("config"))
device_configuration: DeviceConfiguration = DeviceConfiguration.from_json(
entry.data.get("config")
)

hub = ButtonPlusHub(hass, device_configuration, entry)
hass.data.setdefault(DOMAIN, {})[entry.entry_id] = hub
Expand All @@ -35,10 +38,10 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
# This creates each HA object for each platform your device requires.
# It's done by calling the `async_setup_entry` function in each platform module.


await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
return True


async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""Unload a config entry."""
# This is called when an entry/configured device is to be removed. The class
Expand Down
35 changes: 22 additions & 13 deletions custom_components/button_plus/button.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
""" Platform for button integration. """
"""Platform for button integration."""

from __future__ import annotations

import logging
Expand All @@ -16,15 +17,14 @@
_LOGGER = logging.getLogger(__name__)



async def async_setup_entry(
hass: HomeAssistant,
config_entry: ConfigEntry,
async_add_entities: AddEntitiesCallback,
hass: HomeAssistant,
config_entry: ConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
"""Add button_entities for passed config_entry in HA."""

button_entities :list[ButtonPlusButton] = []
button_entities: list[ButtonPlusButton] = []
hub: ButtonPlusHub = hass.data[DOMAIN][config_entry.entry_id]

active_connectors = active_connectors = [
Expand All @@ -33,10 +33,14 @@ async def async_setup_entry(
if connector.connector_type_enum() in [ConnectorEnum.DISPLAY, ConnectorEnum.BAR]
]

buttons = filter(lambda b: b.button_id // 2 in active_connectors, hub.config.mqtt_buttons)
buttons = filter(
lambda b: b.button_id // 2 in active_connectors, hub.config.mqtt_buttons
)

for button in buttons:
_LOGGER.debug(f"Creating button with parameters: {button.button_id} {button.label} {hub.hub_id}")
_LOGGER.debug(
f"Creating button with parameters: {button.button_id} {button.label} {hub.hub_id}"
)
entity = ButtonPlusButton(button.button_id, hub)
button_entities.append(entity)
hub.add_button(button.button_id, entity)
Expand All @@ -51,8 +55,8 @@ def __init__(self, btn_id: int, hub: ButtonPlusHub):
self._hub = hub
self._btn_id = btn_id
self.entity_id = f"button.{self._hub_id}_{btn_id}"
self._attr_name = f'button-{btn_id}'
self._name = f'Button {btn_id}'
self._attr_name = f"button-{btn_id}"
self._name = f"Button {btn_id}"
self._device_class = ButtonDeviceClass.IDENTIFY
self._connector: Connector = hub.config.info.connectors[btn_id // 2]
self.unique_id = self.unique_id_gen()
Expand All @@ -65,10 +69,10 @@ def unique_id_gen(self):
return self.unique_id_gen_display()

def unique_id_gen_bar(self):
return f'button_{self._hub_id}_{self._btn_id}_bar_module_{self._connector.connector_id}'
return f"button_{self._hub_id}_{self._btn_id}_bar_module_{self._connector.connector_id}"

def unique_id_gen_display(self):
return f'button_{self._hub_id}_{self._btn_id}_display_module'
return f"button_{self._hub_id}_{self._btn_id}_display_module"

@property
def name(self) -> str:
Expand All @@ -87,7 +91,12 @@ def device_info(self) -> DeviceInfo:

match self._connector.connector_type_enum():
case ConnectorEnum.BAR:
identifiers = {(DOMAIN, f"{self._hub.hub_id} BAR Module {self._connector.connector_id}")}
identifiers = {
(
DOMAIN,
f"{self._hub.hub_id} BAR Module {self._connector.connector_id}",
)
}
case ConnectorEnum.DISPLAY:
identifiers = {(DOMAIN, f"{self._hub.hub_id} Display Module")}

Expand Down
2 changes: 1 addition & 1 deletion custom_components/button_plus/button_plus_api/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
""" Initialize Button+ Api module """
"""Initialize Button+ Api module"""
30 changes: 16 additions & 14 deletions custom_components/button_plus/button_plus_api/api_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@


class ApiClient:
""" Client to talk to Button+ website """
"""Client to talk to Button+ website"""

def __init__(self, session, cookie=None) -> None:
_LOGGER.debug(f"DEBUG CONFIG {cookie}")
Expand All @@ -20,11 +20,11 @@ def __init__(self, session, cookie=None) -> None:

self._cookie = cookie
self._headers = {
'authority': 'api.button.plus',
'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7',
'accept-language': 'en-NL,en-US;q=0.9,en;q=0.8,nl-NL;q=0.7,nl;q=0.6,en-GB;q=0.5',
'cache-control': 'no-cache',
'cookie': self._cookie,
"authority": "api.button.plus",
"accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7",
"accept-language": "en-NL,en-US;q=0.9,en;q=0.8,nl-NL;q=0.7,nl;q=0.6,en-GB;q=0.5",
"cache-control": "no-cache",
"cookie": self._cookie,
}

_LOGGER.debug(f"Initialize Button+ API client")
Expand Down Expand Up @@ -54,22 +54,24 @@ async def get_cookie_from_login(self, email=str, password=str):
json_data = json.dumps(data)
_LOGGER.debug(f"json dump: {json_data}")
headers = {
'accept': '*/*',
'accept-language': 'en-NL,en;q=0.9',
'content-type': 'application/json',
'origin': 'https://button.plus',
'referer': 'https://button.plus/',
'user-agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36'
"accept": "*/*",
"accept-language": "en-NL,en;q=0.9",
"content-type": "application/json",
"origin": "https://button.plus",
"referer": "https://button.plus/",
"user-agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36",
}

async with self._session.post(url, data=json_data, headers=headers) as response:
response_body = await response.text()

if not response.cookies:
raise Exception(f"Login error with username and password, response: {response_body}")
raise Exception(
f"Login error with username and password, response: {response_body}"
)

cookie_string = str(response.cookies)
match = re.search(r'auth_cookie=[^;]+', cookie_string)
match = re.search(r"auth_cookie=[^;]+", cookie_string)

auth_cookie = match.group()

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from enum import Enum


class ConnectorEnum(Enum):
NOT_CONNECTED = 0
BAR = 1
DISPLAY = 2
DISPLAY = 2
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@


class LocalApiClient:
""" Client to talk to Button+ local devices """
"""Client to talk to Button+ local devices"""

def __init__(self, ip_address, session) -> None:
self._base = f"http://{ip_address}"
Expand All @@ -25,4 +25,3 @@ async def push_config(self, config):
_LOGGER.debug(f"push_config {url}")
async with self._session.post(url, data=config.to_json()) as response:
return await response.text()

Loading
Loading