Skip to content

Commit

Permalink
Minor update
Browse files Browse the repository at this point in the history
 - Added binary sensor for parameter heating_up
 - Code cleaning
  • Loading branch information
pwesters authored Feb 4, 2022
1 parent 2bcf784 commit 96df62e
Showing 1 changed file with 82 additions and 0 deletions.
82 changes: 82 additions & 0 deletions custom_components/watts_vision/binary_sensor.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
from datetime import timedelta
import logging
from typing import Callable, Optional

from homeassistant.components.binary_sensor import BinarySensorEntity
from homeassistant.helpers.typing import (
ConfigType,
DiscoveryInfoType,
HomeAssistantType,
)

from .const import DOMAIN
from .watts_api import WattsApi

_LOGGER = logging.getLogger(__name__)

SCAN_INTERVAL = timedelta(seconds=120)

async def async_setup_platform(
hass: HomeAssistantType,
config: ConfigType,
async_add_entities: Callable,
discovery_info: Optional[DiscoveryInfoType] = None,
) -> None:
"""Set up the binary_sensor platform."""
wattsClient = hass.data[DOMAIN]['api']

smartHomes = wattsClient.getSmartHomes()

sensors = []

for y in range(len(smartHomes)):
for x in range(len(smartHomes[str(y)]['devices'])):
sensors.append(WattsVisionHeatingBinarySensor(wattsClient, smartHomes[str(y)]['smarthome_id'], smartHomes[str(y)]['devices'][str(x)]['id']))

async_add_entities(sensors, update_before_add=True)

class WattsVisionHeatingBinarySensor(BinarySensorEntity):
"""Representation of a Watts Vision thermostat."""

def __init__(self, wattsClient: WattsApi, smartHome: str, deviceID: str):
super().__init__()
self.client = wattsClient
self.smartHome = smartHome
self.deviceID = deviceID
self._name = "watts_thermostat_heating"
self._state: bool = False
self._available = True

@property
def name(self) -> str:
"""Return the name of the entity."""
return self._name + '_' + self.deviceID

@property
def unique_id(self) -> str:
"""Return the unique ID of the sensor."""
return 'thermostat_heating_' + self.deviceID

@property
def available(self) -> bool:
"""Return True if entity is available."""
return self._available

@property
def is_on(self):
"""Return the state of the sensor."""
return self._state

async def async_update(self):
try:
smartHomeDevice = await self.client.getDevice(self.smartHome, self.deviceID)
_LOGGER.warning(self.deviceID + ': ' + smartHomeDevice["heating_up"])
if smartHomeDevice["heating_up"] == '0':
_LOGGER.warning(self.deviceID + ': not heating up')
self._state = False
else:
_LOGGER.warning(self.deviceID + ': heating up')
self._state = True
except:
self._available = False
_LOGGER.exception("Error retrieving data.")

0 comments on commit 96df62e

Please sign in to comment.