10000 Move medcom_ble coordinator to separate module by epenet · Pull Request #148009 · home-assistant/core · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Move medcom_ble coordinator to separate module #148009

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

Merged
merged 3 commits into from
Jul 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
J 10000 ump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 3 additions & 33 deletions homeassistant/components/medcom_ble/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,23 @@

from __future__ import annotations

from datetime import timedelta
import logging

from bleak import BleakError
from medcom_ble import MedcomBleDeviceData

from homeassistant.components import bluetooth
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import Platform
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import ConfigEntryNotReady
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
from homeassistant.util.unit_system import METRIC_SYSTEM

from .const import DEFAULT_SCAN_INTERVAL, DOMAIN
from .const import DOMAIN
from .coordinator import MedcomBleUpdateCoordinator

# Supported platforms
PLATFORMS: list[Platform] = [Platform.SENSOR]

_LOGGER = logging.getLogger(__name__)


10000 async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""Set up Medcom BLE radiation monitor from a config entry."""

address = entry.unique_id
elevation = hass.config.elevation
is_metric = hass.config.units is METRIC_SYSTEM
assert address is not None

ble_device = bluetooth.async_ble_device_from_address(hass, address)
Expand All @@ -38,26 +27,7 @@
f"Could not find Medcom BLE device with address {address}"
)

async def _async_update_method():
"""Get data from Medcom BLE radiation monitor."""
ble_device = bluetooth.async_ble_device_from_address(hass, address)
inspector = MedcomBleDeviceData(_LOGGER, elevation, is_metric)

try:
data = await inspector.update_device(ble_device)
except BleakError as err:
raise UpdateFailed(f"Unable to fetch data: {err}") from err

return data

coordinator = DataUpdateCoordinator(
hass,
_LOGGER,
config_entry=entry,
name=DOMAIN,
update_method=_async_update_method,
update_interval=timedelta(seconds=DEFAULT_SCAN_INTERVAL),
)
coordinator = MedcomBleUpdateCoordinator(hass, entry, address)

Check warning on line 30 in homeassistant/components/medcom_ble/__init__.py

View check run for this annotation

Codecov / codecov/patch

homeassistant/components/medcom_ble/__init__.py#L30

Added line #L30 was not covered by tests

await coordinator.async_config_entry_first_refresh()

Expand Down
50 changes: 50 additions & 0 deletions homeassistant/components/medcom_ble/coordinator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
"""The Medcom BLE integration."""

from __future__ import annotations

from datetime import timedelta
import logging

from bleak import BleakError
from medcom_ble import MedcomBleDevice, MedcomBleDeviceData

from homeassistant.components import bluetooth
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
from homeassistant.util.unit_system import METRIC_SYSTEM

from .const import DEFAULT_SCAN_INTERVAL, DOMAIN

_LOGGER = logging.getLogger(__name__)


class MedcomBleUpdateCoordinator(DataUpdateCoordinator[MedcomBleDevice]):
"""Coordinator for Medcom BLE radiation monitor data."""

config_entry: ConfigEntry

def __init__(self, hass: HomeAssistant, entry: ConfigEntry, address: str) -> None:
"""Initialize the coordinator."""
super().__init__(

Check warning on line 29 in homeassistant/components/medcom_ble/coordinator.py

View check run for this annotation

Codecov / codecov/patch

homeassistant/components/medcom_ble/coordinator.py#L29

Added line #L29 was not covered by tests
hass,
_LOGGER,
config_entry=entry,
name=DOMAIN,
update_interval=timedelta(seconds=DEFAULT_SCAN_INTERVAL),
)
self._address = address
self._elevation = hass.config.elevation
self._is_metric = hass.config.units is METRIC_SYSTEM

Check warning on line 38 in homeassistant/components/medcom_ble/coordinator.py

View check run for this annotation

Codecov / codecov/patch

homeassistant/components/medcom_ble/coordinator.py#L36-L38

Added lines #L36 - L38 were not covered by tests

async def _async_update_data(self) -> MedcomBleDevice:
"""Get data from Medcom BLE radiation monitor."""
ble_device = bluetooth.async_ble_device_from_address(self.hass, self._address)
inspector = MedcomBleDeviceData(_LOGGER, self._elevation, self._is_metric)

Check warning on line 43 in homeassistant/components/medcom_ble/coordinator.py

View check run for this annotation

Codecov / codecov/patch

homeassistant/components/medcom_ble/coordinator.py#L42-L43

Added lines #L42 - L43 were not covered by tests

try:
data = await inspector.update_device(ble_device)
except BleakError as err:
raise UpdateFailed(f"Unable to fetch data: {err}") from err

Check warning on line 48 in homeassistant/components/medcom_ble/coordinator.py

View check run for this annotation

Codecov / codecov/patch

homeassistant/components/medcom_ble/coordinator.py#L45-L48

Added lines #L45 - L48 were not covered by tests

return data

Check warning on line 50 in homeassistant/components/medcom_ble/coordinator.py

View check run for this annotation

Codecov / codecov/patch

homeassistant/components/medcom_ble/coordinator.py#L50

Added line #L50 was not covered by tests
18 changes: 5 additions & 13 deletions homeassistant/components/medcom_ble/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

import logging

from medcom_ble import MedcomBleDevice

from homeassistant import config_entries
from homeassistant.components.sensor import (
SensorEntity,
Expand All @@ -15,12 +13,10 @@
from homeassistant.core import HomeAssistant
from homeassistant.helpers.device_registry import CONNECTION_BLUETOOTH, DeviceInfo
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
from homeassistant.helpers.update_coordinator import (
CoordinatorEntity,
DataUpdateCoordinator,
)
from homeassistant.helpers.update_coordinator import CoordinatorEntity

Check warning on line 16 in homeassistant/components/medcom_ble/sensor.py

View check run for this annotation

Codecov / codecov/patch

homeassistant/components/medcom_ble/sensor.py#L16

Added line #L16 was not covered by tests

from .const import DOMAIN, UNIT_CPM
from .coordinator import MedcomBleUpdateCoordinator

Check warning on line 19 in homeassistant/components/medcom_ble/sensor.py

View check run for this annotation

Codecov / codecov/patch

homeassistant/components/medcom_ble/sensor.py#L19

Added line #L19 was not covered by tests

_LOGGER = logging.getLogger(__name__)

Expand All @@ -41,9 +37,7 @@
) -> None:
"""Set up Medcom BLE radiation monitor sensors."""

coordinator: DataUpdateCoordinator[MedcomBleDevice] = hass.data[DOMAIN][
entry.entry_id
]
coordinator: MedcomBleUpdateCoordinator = hass.data[DOMAIN][entry.entry_id]

Check warning on line 40 in homeassistant/components/medcom_ble/sensor.py

View check run for this annotation

Codecov / codecov/patch

homeassistant/components/medcom_ble/sensor.py#L40

Added line #L40 was not covered by tests

entities = []
_LOGGER.debug("got sensors: %s", coordinator.data.sensors)
Expand All @@ -62,16 +56,14 @@
async_add_entities(entities)


class MedcomSensor(
CoordinatorEntity[DataUpdateCoordinator[MedcomBleDevice]], SensorEntity
):
class MedcomSensor(CoordinatorEntity[MedcomBleUpdateCoordinator], SensorEntity):

Check warning on line 59 in homeassistant/components/medcom_ble/sensor.py

View check run for this annotation

Codecov / codecov/patch

homeassistant/components/medcom_ble/sensor.py#L59

Added line #L59 was not covered by tests
"""Medcom BLE radiation monitor sensors for the device."""

_attr_has_entity_name = True

def __init__(
self,
coordinator: DataUpdateCoordinator[MedcomBleDevice],
coordinator: MedcomBleUpdateCoordinator,
entity_description: SensorEntityDescription,
) -> None:
"""Populate the medcom entity with relevant data."""
Expand Down
0