Skip to content

Commit

Permalink
poc map
Browse files Browse the repository at this point in the history
  • Loading branch information
timon37 Tomasz Borowik committed Dec 23, 2024
1 parent acb6f6b commit 85325a8
Show file tree
Hide file tree
Showing 15 changed files with 1,865 additions and 27 deletions.
43 changes: 37 additions & 6 deletions custom_components/bermuda/bermuda_device.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@

from __future__ import annotations

from typing import TYPE_CHECKING, cast

import re

from homeassistant.components.bluetooth import MONOTONIC_TIME, BluetoothScannerDevice
Expand All @@ -35,6 +37,8 @@
DEFAULT_DEVTRACK_TIMEOUT,
)

if TYPE_CHECKING:
from .coordinator import BermudaDataUpdateCoordinator

class BermudaDevice(dict):
"""
Expand All @@ -48,15 +52,15 @@ class BermudaDevice(dict):
become entities in homeassistant, since there might be a _lot_ of them.
"""

def __init__(self, address, options) -> None:
def __init__(self, address, coordinator: BermudaDataUpdateCoordinator) -> None:
"""Initial (empty) data."""
self.coordinator = coordinator
self.name: str | None = None
self.local_name: str | None = None
self.prefname: str | None = None # "preferred" name - ideally local_name
self.address: str = address
self.ref_power: float = 0 # If non-zero, use in place of global ref_power.
self.ref_power_changed: float = 0 # Stamp for last change to ref_power, for cache zapping.
self.options = options
self.unique_id: str | None = None # mac address formatted.
self.address_type = BDADDR_TYPE_UNKNOWN
self.area_id: str | None = None
Expand Down Expand Up @@ -152,7 +156,7 @@ def set_ref_power(self, new_ref_power: float):
# changed due to ref_power), we still call apply so that the new area_distance
# gets applied.
# if nearest_scanner is not None:
self.apply_scanner_selection(nearest_scanner)
# self.apply_scanner_selection(nearest_scanner)
# Update the stamp so that the BermudaEntity can clear the cache and show the
# new measurement(s) immediately.
self.ref_power_changed = MONOTONIC_TIME()
Expand Down Expand Up @@ -190,6 +194,33 @@ def apply_scanner_selection(self, closest_scanner: BermudaDeviceScanner | None):
self.area_name,
)

def get_point(self):
from .common.point import BermudaPoint # ehhh
return BermudaPoint(self)

def get_point_fresh(self):
return BermudaPoint.get_fresh()

def apply_area(self, dist, area, maybe = None):
# _LOGGER.debug("apply_area: %s, %s, %s", dist, area, maybe)
self.maybe_area = maybe
# self.area_id = closest_scanner.area_id
self.area_distance = dist
if self.area_name != area:
self.area_prev = self.area_name
self.area_name = area
# _LOGGER.debug(": %s", got[0][1])

# old_area = self.area_name
# if (old_area != self.area_name) and self.create_sensor:
# # Our area has changed!
# _LOGGER.debug(
# "Device %s was in '%s', now '%s'",
# self.name,
# old_area,
# self.area_name,
# )

def calculate_data(self):
"""
Call after doing update_scanner() calls so that distances
Expand All @@ -211,13 +242,13 @@ def calculate_data(self):
# Update whether this 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.coordinator.options.get(CONF_DEVTRACK_TIMEOUT, DEFAULT_DEVTRACK_TIMEOUT) < self.last_seen
):
self.zone = STATE_HOME
else:
self.zone = STATE_NOT_HOME

if self.address.upper() in self.options.get(CONF_DEVICES, []):
if self.address.upper() in self.coordinator.options.get(CONF_DEVICES, []):
# We are a device we track. Flag for set-up:
self.create_sensor = True

Expand All @@ -241,7 +272,7 @@ def update_scanner(self, scanner_device: BermudaDevice, discoveryinfo: Bluetooth
self.scanners[format_mac(scanner_device.address)] = BermudaDeviceScanner(
self,
discoveryinfo, # the entire BluetoothScannerDevice struct
self.options,
self.coordinator,
scanner_device,
)
device_scanner = self.scanners[format_mac(scanner_device.address)]
Expand Down
5 changes: 3 additions & 2 deletions custom_components/bermuda/bermuda_device_scanner.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,10 @@ def __init__(
self,
parent_device: BermudaDevice, # The device being tracked
scandata: BluetoothScannerDevice, # The advertisement info from the device, received by the scanner
options,
coordinator,
scanner_device: BermudaDevice, # The scanner device that "saw" it.
) -> None:
self.coordinator = coordinator
# I am declaring these just to control their order in the dump,
# which is a bit silly, I suspect.
self.name: str = scanner_device.name or scandata.scanner.name
Expand All @@ -72,7 +73,7 @@ def __init__(
self.area_name: str | None = scanner_device.area_name
self.parent_device = parent_device
self.parent_device_address = parent_device.address
self.options = options
self.options = coordinator.options
self.stamp: float | None = 0
# Only remote scanners log timestamps, local usb adaptors do not.
self.scanner_sends_stamps = isinstance(scanner_device, BaseHaRemoteScanner)
Expand Down
3 changes: 3 additions & 0 deletions custom_components/bermuda/common/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
"""Dummy init so that pytest works."""

from __future__ import annotations
Loading

0 comments on commit 85325a8

Please sign in to comment.