Skip to content

Commit

Permalink
Merge pull request #78 from RobertD502/add_connection_status
Browse files Browse the repository at this point in the history
Add Connection status binary sensor
  • Loading branch information
RobertD502 authored Jun 10, 2024
2 parents f7c517f + 92f5105 commit 9c8ebc7
Show file tree
Hide file tree
Showing 4 changed files with 146 additions and 1 deletion.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ Each bridge has the following entities:
| Entity | Entity Type | Additional Comments |
|----------------------|-------------| --- |
| `LED brightness` | `Number` | Brightness between 20-100. |
| `Connection status` | `Binary Sensor` | Used to show if the bridge is reported as being online by Flair. |
| `RSSI` | `Sensor` | If your bridge is connected via ethernet, this sensor will always report 0.0 |

## Puck
Expand All @@ -104,6 +105,7 @@ Each puck has the following entities:
| `Temperature calibration` | `Number` | |
| `Temperature scale` | `Select` | |
| `Associated gateway` | `Sensor` | Displays the name of the gateway (as named in the Flair app) the puck is using. If the puck is a gateway itself, this sensor will read "Self". |
| `Connection status` | `Binary Sensor` | Used to show if the puck is reported as being online by Flair. |
| `RSSI` | `Sensor` | |
| `Voltage` | `Sensor` | Displays the current voltage of the puck. If using batteries to power your puck, this can be used to monitor battery health. |

Expand Down Expand Up @@ -133,6 +135,7 @@ Each Vent has the following entities:
| `Duct Temperature` | `Sensor` | |
| `Reported state` | `Sensor` | This entity is disabled by default. Value corresponds to the percent open of the vent as last reported by the sensor on the vent itself. Can be used in automations to determine if puck failed to open/close a vent by comparing if the state of this sensor is equal to the position of the related vent cover entity (for example checking 5 minutes after the current position of the vent cover entity changed). |
| `Associated gateway` | `Sensor` | Displays the name of the bridge or puck (as named in the Flair app) the vent is using as a gateway. |
| `Connection status` | `Binary Sensor` | Used to show if the vent is reported as being online by Flair. |
| `RSSI` | `Sensor` | |
| `Voltage` | `Sensor` | Displays the current voltage of the vent. If using batteries to power your vent, this can be used to monitor battery health. |

Expand Down
141 changes: 141 additions & 0 deletions custom_components/flair/binary_sensor.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
"""Binary Sensor platform for Flair integration."""
from __future__ import annotations

from typing import Any

from datetime import datetime, timedelta
from flairaio.model import Bridge, Puck, Vent

from homeassistant.components.binary_sensor import (
BinarySensorDeviceClass,
BinarySensorEntity,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity import EntityCategory
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.update_coordinator import CoordinatorEntity

from .const import DOMAIN, LOGGER, TYPE_TO_MODEL
from .coordinator import FlairDataUpdateCoordinator


async def async_setup_entry(
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
) -> None:
"""Set Up Flair Binary Sensor Entities."""

coordinator: FlairDataUpdateCoordinator = hass.data[DOMAIN][entry.entry_id]

binary_sensors = []

for structure_id, structure_data in coordinator.data.structures.items():
# Pucks
if structure_data.pucks:
for puck_id, puck_data in structure_data.pucks.items():
binary_sensors.append(Connectivity(coordinator, structure_id, puck_id, 'pucks'))
# Vents
if structure_data.vents:
for vent_id, vent_data in structure_data.vents.items():
binary_sensors.append(Connectivity(coordinator, structure_id, vent_id, 'vents'))
# Bridges
if structure_data.bridges:
for bridge_id, bridge_data in structure_data.bridges.items():
binary_sensors.append(Connectivity(coordinator, structure_id, bridge_id, 'bridges'))

async_add_entities(binary_sensors)


class Connectivity(CoordinatorEntity, BinarySensorEntity):
"""Representation of Bridge, Puck, and Vent connection status."""

def __init__(self, coordinator, structure_id, device_id, device_type):
super().__init__(coordinator)
self.device_id = device_id
self.device_type = device_type
self.structure_id = structure_id
self.last_logged = None
self.next_log = None

@property
def structure_data(self) -> Structure:
"""Handle coordinator structure data."""

return self.coordinator.data.structures[self.structure_id]

@property
def device_data(self) -> Bridge | Puck | Vent:
"""Handle coordinator device data."""

if self.device_type == 'pucks':
return self.structure_data.pucks[self.device_id]
elif self.device_type == 'vents':
return self.structure_data.vents[self.device_id]
else:
return self.structure_data.bridges[self.device_id]

@property
def device_info(self) -> dict[str, Any]:
"""Return device registry information for this entity."""

return {
"identifiers": {(DOMAIN, self.device_data.id)},
"name": self.device_data.attributes['name'],
"manufacturer": "Flair",
"model": TYPE_TO_MODEL[self.device_type],
"configuration_url": "https://my.flair.co/",
}

@property
def unique_id(self) -> str:
"""Sets unique ID for this entity."""

return str(self.device_data.id) + '_connectivity'

@property
def name(self) -> str:
"""Return name of the entity."""

return "Connection status"

@property
def has_entity_name(self) -> bool:
"""Indicate that entity has name defined."""

return True

@property
def entity_category(self) -> EntityCategory:
"""Set category to diagnostic."""

return EntityCategory.DIAGNOSTIC

@property
def device_class(self) -> BinarySensorDeviceClass:
"""Return entity device class."""

return BinarySensorDeviceClass.CONNECTIVITY

@property
def is_on(self) -> bool:
"""Return True if device is online (connected to a gateway)."""

if not self.device_data.attributes['inactive']:
return True
else:
current_dt = datetime.now()
if not self.last_logged:
LOGGER.warning(
f'Flair {TYPE_TO_MODEL[self.device_type]}: {self.device_data.attributes["name"]} is reported to be offline.'
)
self.last_logged = current_dt
self.next_log = current_dt + timedelta(seconds=300)
else:
# If 5 minutes has elapsed since the last log, log the device being offline.
if (self.next_log - current_dt).total_seconds() <= 0:
LOGGER.warning(
f'Flair {TYPE_TO_MODEL[self.device_type]}: {self.device_data.attributes["name"]} is reported to be offline.'
)
self.last_logged = current_dt
self.next_log = current_dt + timedelta(seconds=300)
return False
1 change: 1 addition & 0 deletions custom_components/flair/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
DEFAULT_SCAN_INTERVAL = 30
DOMAIN = "flair"
PLATFORMS = [
Platform.BINARY_SENSOR,
Platform.BUTTON,
Platform.CLIMATE,
Platform.COVER,
Expand Down
2 changes: 1 addition & 1 deletion custom_components/flair/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@
"requirements": [
"flairaio==0.2.0"
],
"version": "0.2.0"
"version": "0.2.1"
}

0 comments on commit 9c8ebc7

Please sign in to comment.