Skip to content

Commit

Permalink
Added binary sensors (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
frimtec authored Jul 4, 2021
1 parent ee52f44 commit df27da2
Show file tree
Hide file tree
Showing 6 changed files with 184 additions and 104 deletions.
21 changes: 11 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,20 +65,21 @@ Name | Values | Description
`state` | `on` or `off` | Switch state.
`switch_progress` | `on`, `off` or `error` | Whether a swich change is in progress or not or `error` if the last switch operation was faulty.

### Sensor
### Sensor & Binary sensor
The componet offers various sensors:

Entity | Description
---- | -----------
switch.compal.wifi.modem.last.poll | Timestamp of last status poll
switch.compal.wifi.modem.status | Modem status
switch.compal.wifi.modem.model | Modem model
switch.compal.wifi.modem.hardware Version | Hardware version
switch.compal.wifi.modem.software Version | Software version
switch.compal.wifi.modem.operator | Modem operator
switch.compal.wifi.modem.uptime | Modem uptime
switch.compal.wifi.modem.telephone.line.1 | State of telephone line 1
switch.compal.wifi.modem.telephone.line.2 | State of telephone line 2
binary_sensor.compal_wifi_modem_guest_wifi | Guest Wifi state
binary_sensor.compal_wifi_modem_internet_connectivity | Modem internet connectivity state
binary_sensor.compal_wifi_modem_telephone_line_1 | Telephone line 1 state
binary_sensor.compal_wifi_modem_telephone_line_2 | Telephone line 2 state
sensor.compal_wifi_modem_last_poll | Timestamp of last status poll
sensor.compal_wifi_modem_model | Modem model
sensor.compal_wifi_modem_hardware_version | Hardware version
sensor.compal_wifi_modem_software_version | Software version
sensor.compal_wifi_modem_operator | Modem operator
sensor.compal_wifi_modem_uptime | Modem uptime

## Services
The componet offers the following services:
Expand Down
4 changes: 1 addition & 3 deletions custom_components/compal_wifi/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,6 @@ def modem_reboot_blocking():
except:
return False
finally:
compal_config.last_update = compal_config.last_update - timedelta(
seconds=compal_config.polling_interval
)
compal_config.semaphore.release()

threading.Thread(
Expand All @@ -79,6 +76,7 @@ def setup(hass, config):
hass.data[DOMAIN] = compal_config

hass.helpers.discovery.load_platform("sensor", DOMAIN, {}, config)
hass.helpers.discovery.load_platform("binary_sensor", DOMAIN, {}, config)
hass.helpers.discovery.load_platform("switch", DOMAIN, {}, config)

def handle_reboot(call):
Expand Down
149 changes: 149 additions & 0 deletions custom_components/compal_wifi/binary_sensor.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
"""Platform for binary_sensor integration."""

from homeassistant.components.binary_sensor import BinarySensorEntity
from homeassistant.components.binary_sensor import DEVICE_CLASS_CONNECTIVITY

from . import DOMAIN


def setup_platform(hass, config, add_entities, discovery_info=None):
"""Set up the sensor platform."""
# We only want this platform to be set up via discovery.
if discovery_info is None:
return
compal_config = hass.data[DOMAIN]
add_entities(
[
GuestWifiBinarySensor(compal_config),
ModemConnectivityBinarySensor(compal_config),
TelephoneLineBinarySensor(
compal_config,
0,
),
TelephoneLineBinarySensor(
compal_config,
1,
),
]
)


class GuestWifiBinarySensor(BinarySensorEntity):
"""representation of a Demo binary sensor."""

def __init__(self, compal_config):
"""Initialize the demo sensor."""
self._compal_config = compal_config

@property
def name(self):
"""Return the name of the binary sensor."""
return "Compal Wifi Modem Guest Wifi"

@property
def is_on(self):
"""Return true if the binary sensor is on."""
guest_wifis = self._compal_config.current_modem_state["wifi_guest"]
for quest_wifi in guest_wifis:
if quest_wifi["enabled"]:
return True
return False

@property
def icon(self):
"""Return the icon to use for the valve."""
if self.is_on:
return "mdi:wifi"
return "mdi:wifi-off"


class ModemConnectivityBinarySensor(BinarySensorEntity):
"""representation of a Demo binary sensor."""

def __init__(self, compal_config):
"""Initialize the demo sensor."""
self._compal_config = compal_config

@property
def name(self):
"""Return the name of the binary sensor."""
return "Compal Wifi Modem Internet Connectivity"

@property
def is_on(self):
"""Return true if the binary sensor is on."""
return self._compal_config.current_modem_state["modem"]["status"] == "online"

@property
def device_class(self):
return DEVICE_CLASS_CONNECTIVITY

@property
def device_state_attributes(self):
"""Return device specific state attributes."""
return {"state": self._compal_config.current_modem_state["modem"]["status"]}


class TelephoneLineBinarySensor(BinarySensorEntity):
"""Representation of a sensor."""

def __init__(self, compal_config, line_index):
"""Initialize the sensor."""
self._compal_config = compal_config
self._line_index = line_index
self._state = self.get_state()
self._on_hook = self.get_on_hook()

def get_state(self):
return self._compal_config.current_modem_state["telephone_line"][
self._line_index
]["mta_state"]

def get_on_hook(self):
return self._compal_config.current_modem_state["telephone_line"][
self._line_index
]["on_hook"]

@property
def name(self):
"""Return the name of the sensor."""
return (
"Compal Wifi Modem Telephone Line "
+ self._compal_config.current_modem_state["telephone_line"][
self._line_index
]["line_number"]
)

@property
def is_on(self):
return self._state == "ready"

def update(self):
"""Fetch new state data for the sensor.
This is the only method that should fetch new data for Home Assistant.
"""
self._state = self.get_state()
self._on_hook = self.get_on_hook()

@property
def icon(self):
"""Return the icon to use for the valve."""
return (
"mdi:phone-off-outline"
if self._state != "ready"
else "mdi:phone-hangup"
if self._on_hook
else "mdi:phone-in-talk"
)

@property
def device_state_attributes(self):
"""Return device specific state attributes."""
return {
"state": self._state,
"on_hook": self._on_hook,
}

@property
def device_class(self):
return DEVICE_CLASS_CONNECTIVITY
73 changes: 0 additions & 73 deletions custom_components/compal_wifi/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,6 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
PollingSensor(
compal_config,
),
ModemSensor(
"Compal Wifi Modem Status",
compal_config,
lambda modem: modem["status"],
"mdi:checkbox-marked-circle",
),
ModemSensor(
"Compal Wifi Modem Model",
compal_config,
Expand All @@ -51,14 +45,6 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
lambda modem: modem["uptime"].split(":", 1)[0],
"mdi:timer",
),
TelephoneLineSensor(
compal_config,
0,
),
TelephoneLineSensor(
compal_config,
1,
),
]
)

Expand Down Expand Up @@ -131,62 +117,3 @@ def update(self):
@property
def icon(self):
return self._icon


class TelephoneLineSensor(Entity):
"""Representation of a sensor."""

def __init__(self, compal_config, line_index):
"""Initialize the sensor."""
self._compal_config = compal_config
self._line_index = line_index
self._state = self.get_state()
self._on_hook = self.get_on_hook()

def get_state(self):
return self._compal_config.current_modem_state["telephone_line"][
self._line_index
]["mta_state"]

def get_on_hook(self):
return self._compal_config.current_modem_state["telephone_line"][
self._line_index
]["on_hook"]

@property
def name(self):
"""Return the name of the sensor."""
return (
"Compal Wifi Modem Telephone Line "
+ self._compal_config.current_modem_state["telephone_line"][
self._line_index
]["line_number"]
)

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

def update(self):
"""Fetch new state data for the sensor.
This is the only method that should fetch new data for Home Assistant.
"""
self._state = self.get_state()
self._on_hook = self.get_on_hook()

@property
def icon(self):
"""Return the icon to use for the valve."""
return (
"mdi:phone-off-outline"
if self._state != "ready"
else "mdi:phone-hangup"
if self._on_hook
else "mdi:phone-in-talk"
)

@property
def device_state_attributes(self):
"""Return device specific state attributes."""
return {"on_hook": self._on_hook}
41 changes: 23 additions & 18 deletions custom_components/compal_wifi/switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,35 +66,40 @@ def internal_state(self) -> bool:


def switch_wifi(wifi_switch: WifiSwitch, state, band):
compal_config = wifi_switch.config()
wifi_switch.set_processing_state("on")

def switch_wifi_blocking(
semaphore, _host, _password, _state, _band, _guest, _pause
):
semaphore.acquire()
def switch_wifi_blocking():
compal_config.semaphore.acquire()
enable_guest = False
if _state == Switch.ON:
enable_guest = _guest
if state == Switch.ON:
enable_guest = compal_config.guest
try:
Commands.switch(_host, _password, _state, _band, enable_guest, _pause)
Commands.switch(
compal_config.host,
compal_config.password,
state,
band,
enable_guest,
compal_config.pause,
)
wifi_switch.set_processing_state("off")
except:
wifi_switch.set_processing_state("error")
finally:
semaphore.release()
try:
compal_config.current_modem_state = Commands.status(
compal_config.host, compal_config.password
)
compal_config.last_update = datetime.now()
compal_config.update_state = "ok"
except:
compal_config.update_state = "error"
finally:
compal_config.semaphore.release()

config = wifi_switch.config()
threading.Thread(
target=switch_wifi_blocking,
args=(
config.semaphore,
config.host,
config.password,
state,
band,
config.guest,
config.pause,
),
).start()


Expand Down
Binary file modified images/compal-wifi.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit df27da2

Please sign in to comment.