Skip to content

Commit

Permalink
Merge pull request #116 from mariusz-ostoja-swierczynski/pylint
Browse files Browse the repository at this point in the history
🚨 Satisfy Ruff and Pylint
  • Loading branch information
anarion80 authored Sep 17, 2024
2 parents e7f715e + 53ad64e commit 0d36ec2
Show file tree
Hide file tree
Showing 11 changed files with 176 additions and 147 deletions.
62 changes: 7 additions & 55 deletions custom_components/tech/__init__.py
Original file line number Diff line number Diff line change
@@ -1,43 +1,32 @@
"""The Tech Controllers integration."""

import asyncio
import logging

from aiohttp import ClientSession

from homeassistant.config_entries import ConfigEntry
from homeassistant.const import CONF_NAME, CONF_TOKEN
from homeassistant.const import CONF_TOKEN
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import ConfigEntryAuthFailed
from homeassistant.helpers import config_validation as cv
from homeassistant.helpers.aiohttp_client import async_get_clientsession
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
from homeassistant.helpers.typing import ConfigType

from . import assets
from .const import (
API_TIMEOUT,
CONTROLLER,
DOMAIN,
PLATFORMS,
SCAN_INTERVAL,
UDID,
USER_ID,
)
from .tech import Tech, TechError, TechLoginError
from .const import DOMAIN, PLATFORMS, USER_ID
from .coordinator import TechCoordinator
from .tech import Tech

_LOGGER = logging.getLogger(__name__)

CONFIG_SCHEMA = cv.config_entry_only_config_schema(DOMAIN)


async def async_setup(hass: HomeAssistant, config: dict): # pylint: disable=unused-argument
async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool: # pylint: disable=unused-argument
"""Set up the Tech Controllers component."""
return True


async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""Set up Tech Controllers from a config entry."""
_LOGGER.debug("Setting up component's entry.")
_LOGGER.debug("Setting up component's entry")
_LOGGER.debug("Entry id: %s", str(entry.entry_id))
_LOGGER.debug(
"Entry -> title: %s, data: %s, id: %s, domain: %s",
Expand Down Expand Up @@ -73,40 +62,3 @@ async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
hass.data[DOMAIN].pop(entry.entry_id)

return unload_ok


class TechCoordinator(DataUpdateCoordinator):
"""TECH API data update coordinator."""

config_entry: ConfigEntry

def __init__(
self, hass: HomeAssistant, session: ClientSession, user_id: str, token: str
) -> None:
"""Initialize my coordinator."""
super().__init__(
hass,
_LOGGER,
# Name of the data. For logging purposes.
name=DOMAIN,
# Polling interval. Will only be polled if there are subscribers.
update_interval=SCAN_INTERVAL,
)
self.api = Tech(session, user_id, token)

async def _async_update_data(self):
"""Fetch data from TECH API endpoint(s)."""

_LOGGER.debug(
"Updating data for: %s", str(self.config_entry.data[CONTROLLER][CONF_NAME])
)

try:
async with asyncio.timeout(API_TIMEOUT):
return await self.api.module_data(
self.config_entry.data[CONTROLLER][UDID]
)
except TechLoginError as err:
raise ConfigEntryAuthFailed from err
except TechError as err:
raise UpdateFailed(f"Error communicating with API: {err}") from err
2 changes: 1 addition & 1 deletion custom_components/tech/assets.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ async def load_subtitles(language: str, api):
None
"""
global TRANSLATIONS # noqa: PLW0603
global TRANSLATIONS # noqa: PLW0603 # pylint: disable=global-statement
TRANSLATIONS = await api.get_translations(language)


Expand Down
18 changes: 13 additions & 5 deletions custom_components/tech/binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,17 @@
import logging

from homeassistant.components import binary_sensor
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import (
CONF_PARAMS,
CONF_TYPE,
STATE_OFF,
STATE_ON,
EntityCategory,
)
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import StateType, UndefinedType

from . import TechCoordinator, assets
from .const import (
Expand All @@ -26,9 +30,13 @@
_LOGGER = logging.getLogger(__name__)


async def async_setup_entry(hass, config_entry, async_add_entities):
async def async_setup_entry(
hass: HomeAssistant,
config_entry: ConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
"""Set up entry."""
_LOGGER.debug("Setting up entry for sensors...")
_LOGGER.debug("Setting up entry for sensors")
controller = config_entry.data[CONTROLLER]
coordinator = hass.data[DOMAIN][config_entry.entry_id]

Expand Down Expand Up @@ -72,12 +80,12 @@ def unique_id(self) -> str:
return f"{self._unique_id}_tile_binary_sensor"

@property
def name(self):
def name(self) -> str | UndefinedType | None:
"""Return the name of the device."""
return self._name

@property
def state(self):
def state(self) -> str | int | float | StateType | None:
"""Get the state of the binary sensor."""
return STATE_ON if self._state else STATE_OFF

Expand All @@ -87,7 +95,7 @@ class RelaySensor(TileBinarySensor):

def __init__(
self, device, coordinator: TechCoordinator, config_entry, device_class=None
):
) -> None:
"""Initialize the tile relay sensor."""
TileBinarySensor.__init__(self, device, coordinator, config_entry)
self._attr_device_class = device_class
Expand Down
45 changes: 27 additions & 18 deletions custom_components/tech/climate.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
"""Support for Tech HVAC system."""
"""The Tech Controllers Coordinator."""

import logging
from typing import Any

from custom_components.tech import TechCoordinator
from homeassistant.components.climate import ClimateEntity
from homeassistant.components.climate.const import (
ClimateEntityFeature,
Expand All @@ -24,10 +23,13 @@
STATE_ON,
UnitOfTemperature,
)
from homeassistant.core import callback
from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers.device_registry import DeviceInfo
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.update_coordinator import CoordinatorEntity

from .const import CONTROLLER, DOMAIN, INCLUDE_HUB_IN_NAME, MANUFACTURER, UDID, VER
from .coordinator import TechCoordinator

_LOGGER = logging.getLogger(__name__)

Expand All @@ -36,7 +38,11 @@
SUPPORT_HVAC = [HVACMode.HEAT, HVACMode.OFF]


async def async_setup_entry(hass, config_entry, async_add_entities):
async def async_setup_entry(
hass: HomeAssistant,
config_entry: ConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
"""Set up entry."""
udid = config_entry.data[CONTROLLER][UDID]
coordinator = hass.data[DOMAIN][config_entry.entry_id]
Expand All @@ -55,9 +61,11 @@ class TechThermostat(ClimateEntity, CoordinatorEntity):
_attr_has_entity_name = True
_attr_name = None

def __init__(self, device, coordinator: TechCoordinator, config_entry: ConfigEntry):
def __init__(
self, device, coordinator: TechCoordinator, config_entry: ConfigEntry
) -> None:
"""Initialize the Tech device."""
_LOGGER.debug("Init TechThermostat...")
_LOGGER.debug("Init TechThermostat")
super().__init__(coordinator)
self._config_entry = config_entry
self._udid = config_entry.data[CONTROLLER][UDID]
Expand All @@ -77,12 +85,13 @@ def __init__(self, device, coordinator: TechCoordinator, config_entry: ConfigEnt
+ config_entry.data[CONTROLLER][VER]
)
self._temperature = None
self._target_temperature = None
self.update_properties(device)
# Remove the line below after HA 2025.1
self._enable_turn_on_off_backwards_compatibility = False

@property
def device_info(self):
def device_info(self) -> DeviceInfo | None:
"""Returns device information in a dictionary format."""
return {
ATTR_IDENTIFIERS: {
Expand Down Expand Up @@ -163,7 +172,7 @@ def unique_id(self) -> str:
return f"{self._unique_id}_zone_climate"

@property
def supported_features(self):
def supported_features(self) -> ClimateEntityFeature:
"""Return the list of supported features."""
return (
ClimateEntityFeature.TARGET_TEMPERATURE
Expand All @@ -172,46 +181,46 @@ def supported_features(self):
)

@property
def hvac_mode(self):
def hvac_mode(self) -> HVACMode:
"""Return hvac operation ie. heat, cool mode.
Need to be one of HVAC_MODE_*.
"""
return self._mode

@property
def hvac_modes(self):
def hvac_modes(self) -> list[HVACMode]:
"""Return the list of available hvac operation modes.
Need to be a subset of HVAC_MODES.
"""
return SUPPORT_HVAC

@property
def hvac_action(self) -> str | None:
def hvac_action(self) -> HVACAction | None:
"""Return the current running hvac operation if supported.
Need to be one of CURRENT_HVAC_*.
"""
return self._state

@property
def temperature_unit(self):
def temperature_unit(self) -> str:
"""Return the unit of measurement."""
return UnitOfTemperature.CELSIUS

@property
def target_temperature_step(self):
def target_temperature_step(self) -> float | None:
"""Return the supported step of target temperature."""
return 0.1

@property
def current_temperature(self):
def current_temperature(self) -> float | None:
"""Return the current temperature."""
return self._temperature

@property
def current_humidity(self):
def current_humidity(self) -> float | None:
"""Return current humidity."""
return self._humidity

Expand All @@ -226,11 +235,11 @@ def max_temp(self) -> float:
return DEFAULT_MAX_TEMP

@property
def target_temperature(self):
def target_temperature(self) -> float | None:
"""Return the temperature we try to reach."""
return self._target_temperature

async def async_set_temperature(self, **kwargs):
async def async_set_temperature(self, **kwargs: Any) -> None:
"""Set new target temperatures."""
temperature = kwargs.get(ATTR_TEMPERATURE)
if temperature:
Expand All @@ -244,7 +253,7 @@ async def async_set_temperature(self, **kwargs):
self._target_temperature = temperature
await self.coordinator.async_request_refresh()

async def async_set_hvac_mode(self, hvac_mode):
async def async_set_hvac_mode(self, hvac_mode: HVACMode) -> None:
"""Set new target hvac mode."""
_LOGGER.debug("%s: Setting hvac mode to %s", self.device_name, hvac_mode)
if hvac_mode == HVACMode.OFF:
Expand Down
9 changes: 6 additions & 3 deletions custom_components/tech/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import voluptuous as vol

from homeassistant import config_entries, core, exceptions
from homeassistant.config_entries import SOURCE_USER, ConfigEntry
from homeassistant.config_entries import SOURCE_USER, ConfigEntry, ConfigFlowResult
from homeassistant.const import (
ATTR_ID,
CONF_NAME,
Expand Down Expand Up @@ -166,11 +166,12 @@ async def _async_finish_controller(self, user_input: dict[str, str]) -> FlowResu
)[CONTROLLER][CONF_NAME],
data=controller,
)
return None

async def async_step_select_controllers(
self,
user_input: dict[str, str] | None = None,
) -> FlowResult:
) -> ConfigFlowResult:
"""Handle the selection of controllers."""
if not user_input:
self._controllers = self._create_controllers_array(
Expand All @@ -184,7 +185,9 @@ async def async_step_select_controllers(

return await self._async_finish_controller(user_input)

async def async_step_user(self, user_input: dict[str, str] | None = None):
async def async_step_user(
self, user_input: dict[str, str] | None = None
) -> ConfigFlowResult:
"""Handle the initial step."""
errors = {}
if user_input is not None:
Expand Down
2 changes: 1 addition & 1 deletion custom_components/tech/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@

DEFAULT_ICON = "mdi:eye"

PLATFORMS = [Platform.BINARY_SENSOR, Platform.SENSOR, Platform.CLIMATE]
PLATFORMS = [Platform.BINARY_SENSOR, Platform.CLIMATE, Platform.SENSOR]

SCAN_INTERVAL: Final = timedelta(seconds=60)
API_TIMEOUT: Final = 60
Expand Down
Loading

0 comments on commit 0d36ec2

Please sign in to comment.