Skip to content
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

Add binary sensor and remove parts from normal sensor #6

Merged
merged 5 commits into from
Jun 4, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions custom_components/tahoma/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
)

PLATFORMS = [
"binary_sensor",
"cover",
"light",
"lock",
Expand Down
72 changes: 72 additions & 0 deletions custom_components/tahoma/binary_sensor.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
"""Support for Tahoma binary sensors."""
from datetime import timedelta
import logging

from homeassistant.components.binary_sensor import BinarySensorEntity
from homeassistant.const import ATTR_BATTERY_LEVEL, STATE_OFF, STATE_ON

from .const import DOMAIN, TAHOMA_TYPES, TAHOMA_BINARY_SENSOR_DEVICE_CLASSES
from .tahoma_device import TahomaDevice

_LOGGER = logging.getLogger(__name__)

SCAN_INTERVAL = timedelta(seconds=120)


async def async_setup_entry(hass, entry, async_add_entities):
"""Set up the Tahoma sensors from a config entry."""

data = hass.data[DOMAIN][entry.entry_id]

entities = []
controller = data.get("controller")

for device in data.get("devices"):
if TAHOMA_TYPES[device.uiclass] == "sensor":
entities.append(TahomaBinarySensor(device, controller))

async_add_entities(entities)


class TahomaBinarySensor(TahomaDevice, BinarySensorEntity):
"""Representation of a Tahoma Binary Sensor."""

def __init__(self, tahoma_device, controller):
"""Initialize the sensor."""
super().__init__(tahoma_device, controller)

self._state = None

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

@property
def device_class(self):
"""Return the class of the device."""
return (
TAHOMA_BINARY_SENSOR_DEVICE_CLASSES.get(self.tahoma_device.widget)
or TAHOMA_BINARY_SENSOR_DEVICE_CLASSES.get(self.tahoma_device.uiclass)
or None
)

def update(self):
"""Update the state."""
self.controller.get_states([self.tahoma_device])

if "core:ContactState" in self.tahoma_device.active_states:
self.current_value = self.tahoma_device.active_states.get("core:ContactState")

if "core:OccupancyState" in self.tahoma_device.active_states:
self.current_value = self.tahoma_device.active_states.get("core:OccupancyState")

if "core:SmokeState" in self.tahoma_device.active_states:
self.current_value = self.tahoma_device.active_states.get("core:SmokeState") != "notDetected"

if self.current_value:
self._state = STATE_ON
else:
self._sate = STATE_OFF

_LOGGER.debug("Update %s, state: %s", self._name, self._state)
13 changes: 13 additions & 0 deletions custom_components/tahoma/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@
DEVICE_CLASS_WINDOW,
)

from homeassistant.components.binary_sensor import (
DEVICE_CLASS_SMOKE,
DEVICE_CLASS_MOTION,
DEVICE_CLASS_OPENING
)

"""Constants for the Tahoma integration."""

DOMAIN = "tahoma"
Expand Down Expand Up @@ -34,6 +40,13 @@
"Window": DEVICE_CLASS_WINDOW,
}

# TODO ADD io:SomfyContactIOSystemSensor & io:SomfyBasicContactIOSystemSensor & rtds:RTDSContactSensor & rtds:RTDSMotionSensor
TAHOMA_BINARY_SENSOR_DEVICE_CLASSES = {
"Smoke": DEVICE_CLASS_SMOKE,
"Motion": DEVICE_CLASS_MOTION,
"Contact": DEVICE_CLASS_OPENING
}

# Tahoma Attributes
ATTR_MEM_POS = "memorized_position"
ATTR_RSSI_LEVEL = "rssi_level"
Expand Down
18 changes: 6 additions & 12 deletions custom_components/tahoma/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,31 +60,25 @@ def unit_of_measurement(self):
def update(self):
"""Update the state."""
self.controller.get_states([self.tahoma_device])

if self.tahoma_device.type == "io:LightIOSystemSensor":
self.current_value = self.tahoma_device.active_states["core:LuminanceState"]
self._available = bool(
self.tahoma_device.active_states.get("core:StatusState") == "available"
)

if self.tahoma_device.type == "io:SomfyContactIOSystemSensor":
self.current_value = self.tahoma_device.active_states["core:ContactState"]
self._available = bool(
self.tahoma_device.active_states.get("core:StatusState") == "available"
)

if self.tahoma_device.type == "io:SomfyBasicContactIOSystemSensor":
self.current_value = self.tahoma_device.active_states["core:ContactState"]
self._available = bool(
self.tahoma_device.active_states.get("core:StatusState") == "available"
)

if self.tahoma_device.type == "rtds:RTDSContactSensor":
self.current_value = self.tahoma_device.active_states["core:ContactState"]
self._available = True

if self.tahoma_device.type == "rtds:RTDSMotionSensor":
self.current_value = self.tahoma_device.active_states["core:OccupancyState"]
self._available = True

if self.tahoma_device.type == "io:TemperatureIOSystemSensor":
self.current_value = round(
float(self.tahoma_device.active_states["core:TemperatureState"]), 1
)
self._available = True

_LOGGER.debug("Update %s, value: %d", self._name, self.current_value)