Skip to content

Commit

Permalink
Change to HA config of ruff
Browse files Browse the repository at this point in the history
  • Loading branch information
dependabot[bot] authored and agittins committed Jul 27, 2024
1 parent e87856a commit f5f051d
Show file tree
Hide file tree
Showing 19 changed files with 574 additions and 589 deletions.
31 changes: 7 additions & 24 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,11 @@ repos:
- id: check-yaml
- id: end-of-file-fixer
- id: trailing-whitespace
- repo: https://github.com/psf/black
rev: 24.2.0
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.5.1
hooks:
- id: black
name: black
entry: black
language: system
types: [python]
require_serial: true
- repo: local
hooks:
- id: flake8
name: flake8
entry: flake8
language: system
types: [python]
require_serial: true
- repo: https://github.com/pycqa/isort
rev: 5.13.2
hooks:
- id: isort
- repo: https://github.com/pre-commit/mirrors-prettier
rev: v4.0.0-alpha.8
hooks:
- id: prettier
- id: ruff
args:
- --fix
- --unsafe-fixes
- id: ruff-format
29 changes: 11 additions & 18 deletions custom_components/bermuda/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,21 @@

from __future__ import annotations

from homeassistant.config_entries import ConfigEntry
from homeassistant.core import Config
from homeassistant.core import HomeAssistant
from typing import TYPE_CHECKING

from homeassistant.exceptions import ConfigEntryNotReady
from homeassistant.helpers import config_validation as cv

# from homeassistant.helpers.device_registry import EventDeviceRegistryUpdatedData
from homeassistant.helpers.device_registry import DeviceEntry
from homeassistant.helpers.device_registry import format_mac
from homeassistant.helpers.device_registry import DeviceEntry, format_mac

from .const import _LOGGER
from .const import DOMAIN
from .const import PLATFORMS
from .const import STARTUP_MESSAGE
from .const import _LOGGER, DOMAIN, PLATFORMS, STARTUP_MESSAGE
from .coordinator import BermudaDataUpdateCoordinator

if TYPE_CHECKING:
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import Config, HomeAssistant

# from .const import _LOGGER_SPAM_LESS

# from typing import TYPE_CHECKING
Expand All @@ -35,9 +34,7 @@
CONFIG_SCHEMA = cv.config_entry_only_config_schema(DOMAIN)


async def async_setup(
hass: HomeAssistant, config: Config
): # pylint: disable=unused-argument;
async def async_setup(hass: HomeAssistant, config: Config): # pylint: disable=unused-argument;
"""Setting up this integration using YAML is not supported."""
return True

Expand All @@ -47,9 +44,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry):
if hass.data.get(DOMAIN) is None:
_LOGGER.info(STARTUP_MESSAGE)

coordinator = hass.data.setdefault(DOMAIN, {})[entry.entry_id] = (
BermudaDataUpdateCoordinator(hass, entry)
)
coordinator = hass.data.setdefault(DOMAIN, {})[entry.entry_id] = BermudaDataUpdateCoordinator(hass, entry)

await coordinator.async_refresh()

Expand Down Expand Up @@ -95,9 +90,7 @@ async def async_remove_config_entry_device(

async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""Handle removal of an entry."""
if unload_result := await hass.config_entries.async_unload_platforms(
entry, PLATFORMS
):
if unload_result := await hass.config_entries.async_unload_platforms(entry, PLATFORMS):
_LOGGER.debug("Unloaded platforms.")
hass.data[DOMAIN].pop(entry.entry_id)
return unload_result
Expand Down
56 changes: 23 additions & 33 deletions custom_components/bermuda/bermuda_device.py
Original file line number Diff line number Diff line change
@@ -1,31 +1,28 @@
"""Bermuda's internal representation of a bluetooth device.
"""
Bermuda's internal representation of a bluetooth device.
Each discovered bluetooth device (ie, every found transmitter) will
have one of these entries created for it. These are not HA 'devices' but
our own internal thing. They directly correspond to the entries you will
see when calling the dump_devices service call.
Even devices which are not configured/tracked will get entries created
for them, so we can use them to contribute towards measurements."""
for them, so we can use them to contribute towards measurements.
"""

from __future__ import annotations

from homeassistant.components.bluetooth import MONOTONIC_TIME
from homeassistant.components.bluetooth import BluetoothScannerDevice
from homeassistant.const import STATE_HOME
from homeassistant.const import STATE_NOT_HOME
from homeassistant.const import STATE_UNAVAILABLE
from homeassistant.components.bluetooth import MONOTONIC_TIME, BluetoothScannerDevice
from homeassistant.const import STATE_HOME, STATE_NOT_HOME, STATE_UNAVAILABLE
from homeassistant.helpers.device_registry import format_mac

from .bermuda_device_scanner import BermudaDeviceScanner
from .const import BDADDR_TYPE_UNKNOWN
from .const import CONF_DEVICES
from .const import CONF_DEVTRACK_TIMEOUT
from .const import DEFAULT_DEVTRACK_TIMEOUT
from .const import BDADDR_TYPE_UNKNOWN, CONF_DEVICES, CONF_DEVTRACK_TIMEOUT, DEFAULT_DEVTRACK_TIMEOUT


class BermudaDevice(dict):
"""This class is to represent a single bluetooth "device" tracked by Bermuda.
"""
This class is to represent a single bluetooth "device" tracked by Bermuda.
"device" in this context means a bluetooth receiver like an ESPHome
running bluetooth_proxy or a bluetooth transmitter such as a beacon,
Expand All @@ -35,8 +32,8 @@ class BermudaDevice(dict):
become entities in homeassistant, since there might be a _lot_ of them.
"""

def __init__(self, address, options):
"""Initial (empty) data"""
def __init__(self, address, options) -> None:
"""Initial (empty) data."""
self.name: str | None = None
self.local_name: str | None = None
self.prefname: str | None = None # "preferred" name - ideally local_name
Expand All @@ -54,12 +51,8 @@ def __init__(self, address, options):
self.connectable: bool = False
self.is_scanner: bool = False
self.beacon_type: set = set()
self.beacon_sources = (
[]
) # list of MAC addresses that have advertised this beacon
self.beacon_unique_id: str | None = (
None # combined uuid_major_minor for *really* unique id
)
self.beacon_sources = [] # list of MAC addresses that have advertised this beacon
self.beacon_unique_id: str | None = None # combined uuid_major_minor for *really* unique id
self.beacon_uuid: str | None = None
self.beacon_major: str | None = None
self.beacon_minor: str | None = None
Expand All @@ -69,13 +62,12 @@ def __init__(self, address, options):
self.create_sensor: bool = False # Create/update a sensor for this device
self.create_sensor_done: bool = False # Sensor should now exist
self.create_tracker_done: bool = False # device_tracker should now exist
self.last_seen: float = (
0 # stamp from most recent scanner spotting. MONOTONIC_TIME
)
self.last_seen: float = 0 # stamp from most recent scanner spotting. MONOTONIC_TIME
self.scanners: dict[str, BermudaDeviceScanner] = {}

def calculate_data(self):
"""Call after doing update_scanner() calls so that distances
"""
Call after doing update_scanner() calls so that distances
etc can be freshly smoothed and filtered.
"""
Expand All @@ -85,9 +77,7 @@ def calculate_data(self):
# Update whether the device has been seen recently, for device_tracker:
if (
self.last_seen is not None
and MONOTONIC_TIME()
- self.options.get(CONF_DEVTRACK_TIMEOUT, DEFAULT_DEVTRACK_TIMEOUT)
< self.last_seen
and MONOTONIC_TIME() - self.options.get(CONF_DEVTRACK_TIMEOUT, DEFAULT_DEVTRACK_TIMEOUT) < self.last_seen
):
self.zone = STATE_HOME
else:
Expand All @@ -97,10 +87,9 @@ def calculate_data(self):
# We are a device we track. Flag for set-up:
self.create_sensor = True

def update_scanner(
self, scanner_device: BermudaDevice, discoveryinfo: BluetoothScannerDevice
):
"""Add/Update a scanner entry on this device, indicating a received advertisement
def update_scanner(self, scanner_device: BermudaDevice, discoveryinfo: BluetoothScannerDevice):
"""
Add/Update a scanner entry on this device, indicating a received advertisement.
This gets called every time a scanner is deemed to have received an advert for
this device. It only loads data into the structure, all calculations are done
Expand Down Expand Up @@ -128,13 +117,14 @@ def update_scanner(
self.last_seen = device_scanner.stamp

def to_dict(self):
"""Convert class to serialisable dict for dump_devices"""
"""Convert class to serialisable dict for dump_devices."""
out = {}
for var, val in vars(self).items():
if var == "scanners":
scanout = {}
for address, scanner in self.scanners.items():
scanout[address] = scanner.to_dict()
val = scanout
# FIXME: val is overwritten
val = scanout # noqa
out[var] = val
return out
Loading

0 comments on commit f5f051d

Please sign in to comment.