Skip to content

Commit

Permalink
Merge pull request #77 from Minims/dev
Browse files Browse the repository at this point in the history
Improvements
  • Loading branch information
Minims authored Jan 8, 2023
2 parents 5c5ac84 + 55ad325 commit ea06af6
Show file tree
Hide file tree
Showing 7 changed files with 262 additions and 47 deletions.
52 changes: 26 additions & 26 deletions somfyProtect2Mqtt/business/mqtt.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

from homeassistant.ha_discovery import ALARM_STATUS
from paho.mqtt import client
from somfy_protect.api import SomfyProtectApi
from somfy_protect.api import SomfyProtectApi, ACTION_LIST

LOGGER = logging.getLogger(__name__)

Expand Down Expand Up @@ -108,33 +108,33 @@ def consume_mqtt_message(
LOGGER.info(f"Stop the Siren On Site ID {site_id}")
api.stop_alarm(site_id=site_id)

# Manage Camera Shutter
elif msg.topic.split("/")[3] == "shutter_state":
# Manage Actions
elif text_payload in ACTION_LIST:
site_id = msg.topic.split("/")[1]
device_id = msg.topic.split("/")[2]
if text_payload == "closed":
text_payload = "shutter_close"
if text_payload == "opened":
text_payload = "shutter_open"
LOGGER.info(
f"Message received for Site ID: {site_id}, Device ID: {device_id}, Action: {text_payload}"
)
# Update Camera Shutter via API
action_device = api.action_device(
site_id=site_id,
device_id=device_id,
action=text_payload,
)
LOGGER.debug(action_device)
# Read updated device
sleep(2)
update_device(
api=api,
mqtt_client=mqtt_client,
mqtt_config=mqtt_config,
site_id=site_id,
device_id=device_id,
)
if device_id:
LOGGER.info(
f"Message received for Site ID: {site_id}, Device ID: {device_id}, Action: {text_payload}"
)
action_device = api.action_device(
site_id=site_id,
device_id=device_id,
action=text_payload,
)
LOGGER.debug(action_device)
# Read updated device
sleep(1)
update_device(
api=api,
mqtt_client=mqtt_client,
mqtt_config=mqtt_config,
site_id=site_id,
device_id=device_id,
)
else:
LOGGER.info(
f"Message received for Site ID: {site_id}, Action: {text_payload}"
)

# Manage Manual Snapshot
elif msg.topic.split("/")[3] == "snapshot":
Expand Down
2 changes: 1 addition & 1 deletion somfyProtect2Mqtt/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ version: "3"
services:
somfyprotect2mqtt:
build: .
image: somfyprotect2mqtt:v0.2.5
image: somfyprotect2mqtt:v0.2.6
volumes:
- ./config:/config

111 changes: 104 additions & 7 deletions somfyProtect2Mqtt/homeassistant/ha_discovery.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,21 @@
"rlink_quality_percent": {
"type": "sensor",
"config": {
"device_class": "signal_strength",
"unit_of_measurement": "%",
},
},
"sensitivity": {
"type": "number",
"config": {"min": 0, "max": 100},
},
"ambient_light_threshold": {
"type": "number",
"config": {"min": 0, "max": 100},
},
"lighting_duration": {
"type": "number",
"config": {"min": 0, "max": 900},
},
"sensitivity_IntelliTag": {
"type": "number",
"config": {"min": 1, "max": 9},
Expand Down Expand Up @@ -87,6 +94,18 @@
"options": ["FHD", "HD", "SD"],
},
},
"smart_alarm_duration": {
"type": "select",
"config": {
"options": [30, 60, 90, 120],
},
},
"lighting_trigger": {
"type": "select",
"config": {
"options": [30, 60, 90, 120],
},
},
"power_mode": {
"type": "sensor",
"config": {},
Expand Down Expand Up @@ -119,14 +138,12 @@
"wifi_level_percent": {
"type": "sensor",
"config": {
"device_class": "signal_strength",
"unit_of_measurement": "%",
},
},
"lora_quality_percent": {
"type": "sensor",
"config": {
"device_class": "signal_strength",
"unit_of_measurement": "%",
},
},
Expand Down Expand Up @@ -238,14 +255,21 @@
"type": "sensor",
"config": {},
},
"video_backend": {
"type": "sensor",
"config": {},
},
"gsm_antenna_in_use": {
"type": "sensor",
"config": {},
},
"night_mode": {
"type": "sensor",
"config": {},
},
"mfa_quality_percent": {
"type": "sensor",
"config": {
"device_class": "signal_strength",
"unit_of_measurement": "%",
},
},
Expand All @@ -271,11 +295,36 @@
"pl_off": "False",
},
},
"push_to_talk_available": {
"type": "binary_sensor",
"config": {
"pl_on": "True",
"pl_off": "False",
},
},
"homekit_capable": {
"type": "binary_sensor",
"config": {
"pl_on": "True",
"pl_off": "False",
},
},
"lighting_state": {
"type": "switch",
"config": {
"state_on": "True",
"state_off": "False",
"pl_on": "light_on",
"pl_off": "light_off",
},
},
"shutter_state": {
"type": "switch",
"config": {
"pl_on": "opened",
"pl_off": "closed",
"state_on": "opened",
"state_off": "closed",
"pl_on": "shutter_open",
"pl_off": "shutter_close",
},
},
"detection_enabled": {
Expand All @@ -299,13 +348,41 @@
"pl_off": "False",
},
},
"lighting_wired": {
"type": "switch",
"config": {
"pl_on": "True",
"pl_off": "False",
},
},
"siren_disabled": {
"type": "switch",
"config": {
"pl_on": "True",
"pl_off": "False",
},
},
"human_detect_enabled": {
"type": "switch",
"config": {
"pl_on": "True",
"pl_off": "False",
},
},
"siren_on_camera_detection_disabled": {
"type": "switch",
"config": {
"pl_on": "True",
"pl_off": "False",
},
},
"auto_rotate_enabled": {
"type": "switch",
"config": {
"pl_on": "True",
"pl_off": "False",
},
},
"sound_recording_enabled": {
"type": "switch",
"config": {
Expand All @@ -327,6 +404,13 @@
"pl_off": "False",
},
},
"code_required_to_arm": {
"type": "switch",
"config": {
"pl_on": "True",
"pl_off": "False",
},
},
"auto_protect_enabled": {
"type": "switch",
"config": {
Expand Down Expand Up @@ -369,6 +453,13 @@
"pl_off": "False",
},
},
"is_full_gsm": {
"type": "binary_sensor",
"config": {
"pl_on": "True",
"pl_off": "False",
},
},
"sp_smoke_detector_error_chamber": {
"type": "binary_sensor",
"config": {
Expand Down Expand Up @@ -507,12 +598,18 @@ def ha_discovery_devices(
device_config = {}
device_type = DEVICE_CAPABILITIES.get(sensor_name).get("type")

update_available = device.update_available
if update_available is False:
update_available = "(Up to Date)"
else:
update_available = f"(New Version Available: {update_available})"

device_info = {
"identifiers": [device.id],
"manufacturer": "Somfy",
"model": device.device_definition.get("label"),
"name": device.label,
"sw_version": device.version,
"sw_version": f"{device.version} {update_available}",
}

command_topic = f"{mqtt_config.get('topic_prefix', 'somfyProtect2mqtt')}/{site_id}/{device.id}/{sensor_name}/command"
Expand Down
18 changes: 13 additions & 5 deletions somfyProtect2Mqtt/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from somfy_protect.api import SomfyProtectApi
from somfy_protect.websocket import SomfyProtectWebsocket

VERSION = "0.2.5"
VERSION = "0.2.6"


def somfy_protect_loop(somfy_protect_2_mqtt):
Expand All @@ -39,8 +39,12 @@ def somfy_protect_wss_loop(somfy_protect_websocket):

# Read Arguments
PARSER = argparse.ArgumentParser()
PARSER.add_argument("--verbose", "-v", action="store_true", help="verbose mode")
PARSER.add_argument("--configuration", "-c", type=str, help="config file path")
PARSER.add_argument(
"--verbose", "-v", action="store_true", help="verbose mode"
)
PARSER.add_argument(
"--configuration", "-c", type=str, help="config file path"
)
ARGS = PARSER.parse_args()
DEBUG = ARGS.verbose
CONFIG_FILE = ARGS.configuration
Expand All @@ -55,10 +59,14 @@ def somfy_protect_wss_loop(somfy_protect_websocket):
SSO = init_sso(config=CONFIG)
API = SomfyProtectApi(sso=SSO)
MQTT_CLIENT = init_mqtt(config=CONFIG, api=API)
WSS = SomfyProtectWebsocket(sso=SSO, debug=DEBUG, config=CONFIG, mqtt_client=MQTT_CLIENT, api=API)
WSS = SomfyProtectWebsocket(
sso=SSO, debug=DEBUG, config=CONFIG, mqtt_client=MQTT_CLIENT, api=API
)

try:
SOMFY_PROTECT = SomfyProtect2Mqtt(api=API, mqtt_client=MQTT_CLIENT, config=CONFIG)
SOMFY_PROTECT = SomfyProtect2Mqtt(
api=API, mqtt_client=MQTT_CLIENT, config=CONFIG
)

except SomfyProtectInitError as exp:
LOGGER.error(f"Unable to init: {exp}")
Expand Down
2 changes: 1 addition & 1 deletion somfyProtect2Mqtt/mqtt/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ def shutdown(self):
self.client.disconnect()


def init_mqtt(config: dict, api: SomfyProtectApi) -> None:
def init_mqtt(config: dict, api: SomfyProtectApi) -> MQTTClient:
"""Init MQTT
Args:
Expand Down
Loading

0 comments on commit ea06af6

Please sign in to comment.