Skip to content

Commit

Permalink
Add fan as sensor
Browse files Browse the repository at this point in the history
  • Loading branch information
samuolis committed May 8, 2023
1 parent 6387d4a commit e5f086a
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 42 deletions.
7 changes: 4 additions & 3 deletions custom_components/brink_home/core/brink_home_cloud.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,9 @@ def __get_values(type):

# 1 as mode value changes mode to manual every time you change ventilation value
async def set_ventilation_value(self, system_id, gateway_id, mode, ventilation, value):
"""Sets alarm to provided mode."""
ventilation_value = ventilation["values"][value]["value"]
if ventilation_value is None:
return
data = {
'GatewayId': gateway_id,
'SystemId': system_id,
Expand Down Expand Up @@ -189,10 +191,9 @@ async def set_ventilation_value(self, system_id, gateway_id, mode, ventilation,
return mapped_result

async def set_mode_value(self, system_id, gateway_id, mode, ventilation, value):
mode_value = ventilation["values"][value]["value"]
mode_value = mode["values"][value]["value"]
if mode_value is None:
return
"""Sets alarm to provided mode."""
data = {
'GatewayId': gateway_id,
'SystemId': system_id,
Expand Down
76 changes: 76 additions & 0 deletions custom_components/brink_home/fan.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
from __future__ import annotations

import logging

from homeassistant.components.fan import (
DOMAIN,
FanEntity,
SUPPORT_PRESET_MODE
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant

from custom_components.brink_home import BrinkHomeDeviceEntity

from .const import (
DATA_CLIENT,
DATA_COORDINATOR,
DOMAIN,
)


_LOGGER = logging.getLogger(__name__)


async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry, async_add_entities):
"""Set up the Brink Home sensor platform."""
client = hass.data[DOMAIN][entry.entry_id][DATA_CLIENT]
coordinator = hass.data[DOMAIN][entry.entry_id][DATA_COORDINATOR]
entities = []

_LOGGER.info(f"entity data: {coordinator.data}")
for deviceIndex, _ in enumerate(coordinator.data):
entities.append(BrinkHomeVentilationFanEntity(client, coordinator, deviceIndex, "ventilation"))

_LOGGER.info(f"entity data: {entities}")
async_add_entities(entities)


class BrinkHomeVentilationFanEntity(BrinkHomeDeviceEntity, FanEntity):

async def async_set_preset_mode(self, preset_mode: str) -> None:
mode = self.coordinator.data[self.device_index]["mode"]
await self.client.set_ventilation_value(self.system_id, self.gateway_id, mode, self.data, preset_mode)

@property
def preset_mode(self) -> str:
for value in self.data["values"]:
if value["value"] == self.data["value"]:
return value["text"]
return None

@property
def preset_modes(self) -> list[str]:
"""Return a set of selectable options."""
values = []
for value in self.data["values"]:
values.append(value["text"])

return values

@property
def name(self):
return f"{self.coordinator.data[self.device_index]['name']} {self.device_info['name']}"

@property
def id(self):
return f"{DOMAIN}_{self.name}_fan"

@property
def unique_id(self):
return self.id

@property
def supported_features(self):
"""Return supported features."""
return SUPPORT_PRESET_MODE
39 changes: 0 additions & 39 deletions custom_components/brink_home/select.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,51 +26,12 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry, async_add_e

_LOGGER.info(f"entity data: {coordinator.data}")
for deviceIndex, _ in enumerate(coordinator.data):
entities.append(BrinkHomeVentilationSelectEntity(client, coordinator, deviceIndex, "ventilation"))
entities.append(BrinkHomeModeSelectEntity(client, coordinator, deviceIndex, "mode"))

_LOGGER.info(f"entity data: {entities}")
async_add_entities(entities)


class BrinkHomeVentilationSelectEntity(BrinkHomeDeviceEntity, SelectEntity):

async def async_select_option(self, option: str):
mode = self.coordinator.data[self.device_index]["mode"]
await self.client.set_ventilation_value(self.system_id, self.gateway_id, mode, self.data, option)

@property
def current_option(self) -> str:
current_value = self.data["value"]
return current_value

@property
def options(self) -> list[str]:
"""Return a set of selectable options."""
values = []
for value in self.data["values"]:
values.append(value["value"])

return values

@property
def name(self):
return f"{self.coordinator.data[self.device_index]['name']} {self.device_info['name']}"

@property
def id(self):
return f"{DOMAIN}_{self.name}_select"

@property
def unique_id(self):
return self.id

@property
def icon(self) :
"""Return the icon to use in the frontend, if any."""
return "mdi:hvac"


class BrinkHomeModeSelectEntity(BrinkHomeDeviceEntity, SelectEntity):

async def async_select_option(self, option: str):
Expand Down

0 comments on commit e5f086a

Please sign in to comment.