Skip to content

Commit

Permalink
binary sensor initial
Browse files Browse the repository at this point in the history
  • Loading branch information
PTST committed Feb 23, 2024
1 parent f29d7c5 commit 68b99b2
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 1 deletion.
13 changes: 13 additions & 0 deletions .github/workflows/linting.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,17 @@ jobs:
- name: Run pylint
run: pylint custom_components/libreview/

imports-sorted:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Set up Python 3.10
uses: actions/setup-python@v4
with:
python-version: "3.10"
- name: Install isort
run: pip3 install isort
- name: Run isor
run: isort . --profile black


75 changes: 75 additions & 0 deletions custom_components/libreview/binary_sensor.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
from datetime import datetime, timedelta, timezone
from typing import Dict
from uuid import UUID

from homeassistant.components.binary_sensor import BinarySensorEntity
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity import Entity
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.update_coordinator import CoordinatorEntity
from LibreView.models import Connection, GlucoseMeasurement, Sensor
from .coordinator import LibreViewCoordinator

from .const import (
CONF_SENSOR_DURATION,
CONF_SHOW_TREND_ARROW,
CONF_UOM,
DEFAULT_ICON,
DOMAIN,
SENSOR_ICON,
TREND_ICONS,
GlucoseUnitOfMeasurement,
)


async def async_setup_entry(
hass: HomeAssistant,
entry: ConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
"""Set up G4S binary sensors based on a config entry."""
coordinator: LibreViewCoordinator = hass.data[DOMAIN][entry.entry_id]
uom = GlucoseUnitOfMeasurement(entry.data[CONF_UOM])

sensors = [
GlucoseHighSensor(coordinator, connection_id, uom)
for connection_id, _ in coordinator.data["glucose_readings"].items()
] + [
GlucoseLowSensor(coordinator, connection_id, uom)
for connection_id, _ in coordinator.data["glucose_readings"].items()
]

async_add_entities(sensors)


class GlucoseHighSensor(CoordinatorEntity, BinarySensorEntity):
coordinator: LibreViewCoordinator
uom: GlucoseUnitOfMeasurement

def __init__(
self,
coordinator: LibreViewCoordinator,
connection_id: UUID,
uom: GlucoseUnitOfMeasurement,
):
super().__init__(coordinator)
self._attr_unique_id = f"{connection_id}_glucose_reading"
self.connection_id = connection_id
self.uom = uom


class GlucoseLowSensor(CoordinatorEntity, BinarySensorEntity):
coordinator: LibreViewCoordinator
uom: GlucoseUnitOfMeasurement

def __init__(
self,
coordinator: LibreViewCoordinator,
connection_id: UUID,
uom: GlucoseUnitOfMeasurement,
):
super().__init__(coordinator)
self._attr_unique_id = f"{connection_id}_glucose_reading"
self.connection_id = connection_id
self.uom = uom
2 changes: 1 addition & 1 deletion custom_components/libreview/sensor.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from datetime import datetime, timezone, timedelta
from datetime import datetime, timedelta, timezone
from typing import Dict
from uuid import UUID

Expand Down

0 comments on commit 68b99b2

Please sign in to comment.