Skip to content

Commit

Permalink
Add: cover - reverse option
Browse files Browse the repository at this point in the history
It is a flag to reverse the direction to open the curtain.
The default value is `false`, open to left and close to right.
  • Loading branch information
mugifly committed May 9, 2019
1 parent 14c53cb commit 112c47f
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 15 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ cover:
api_key: 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
mac: 'xx:xx:xx:xx:xx:xx'
name: 'Curtain of Living'
reverse: false
```

* `platform` (Required) - It must be `mornin`.
Expand All @@ -72,6 +73,8 @@ cover:

* `name` (Optional) - Name of your mornin+. In typically it should be name of curtain.

* `reverse` (Optional) - Flag to reverse the direction to open the curtain. The default value is `false`, open to left and close to right.

### 4. Restart Home Assistant

After restarting, your mornin'+ will be appeared as Cover on your Home Assistant.
Expand Down
40 changes: 25 additions & 15 deletions custom_components/mornin/cover.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,6 @@
# Import the logger for debugging
import logging

# Define the validation of configuration
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
vol.Required(CONF_API_KEY): cv.string,
vol.Required(CONF_MAC): cv.string,
vol.Optional(CONF_NAME): cv.string
})

# Initialize the logger
_LOGGER = logging.getLogger(__name__)

# Define constants
BLE_CONNECT_TIMEOUT_SEC = 15
BLE_CONNECT_MAX_RETRY_COUNT = 5
Expand All @@ -40,6 +30,18 @@
CONTROL_SERVICE_CONTROL_CLOSE_VALUE = b'\x00\x01'
CONTROL_SERVICE_CONTROL_STOP_VALUE = b'\x00\x02'
CONTROL_SERVICE_CONTROL_WAIT_SEC = 5
CONF_REVERSE = 'reverse'

# Define the validation of configuration
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
vol.Required(CONF_API_KEY): cv.string,
vol.Required(CONF_MAC): cv.string,
vol.Optional(CONF_NAME): cv.string,
vol.Optional(CONF_REVERSE, default = False): cv.boolean
})

# Initialize the logger
_LOGGER = logging.getLogger(__name__)

def setup_platform(hass, config, add_devices, discovery_info=None):
"""Setup platform."""
Expand All @@ -48,12 +50,13 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
mac_address = config[CONF_MAC]

name = config.get(CONF_NAME)
reverse = config.get(CONF_REVERSE)

add_devices([MorninCoverDevice(auth_key, mac_address, name)])
add_devices([MorninCoverDevice(auth_key, mac_address, name, reverse)])

class MorninCoverDevice(CoverDevice):

def __init__(self, auth_key: str, mac_address: str, name: str) -> None:
def __init__(self, auth_key: str, mac_address: str, name: str, reverse: bool) -> None:
"""Initialzing device...."""

# Import dependencies
Expand All @@ -66,6 +69,7 @@ def __init__(self, auth_key: str, mac_address: str, name: str) -> None:
self._auth_key = auth_key
self._mac_address = mac_address.upper()
self._name = name if name != None else mac_address
self._reverse = reverse
self._mornin_device = None
self._state = None

Expand Down Expand Up @@ -145,7 +149,10 @@ def open_cover(self, **kwargs) -> None:

# Control device
_LOGGER.info('Moving the mornin to open the curtain %s...', self._mac_address)
self._mornin_device.char_write(CONTROL_SERVICE_CONTROL_UUID, CONTROL_SERVICE_CONTROL_OPEN_VALUE, True)
if (self._reverse):
self._mornin_device.char_write(CONTROL_SERVICE_CONTROL_UUID, CONTROL_SERVICE_CONTROL_CLOSE_VALUE, True)
else:
self._mornin_device.char_write(CONTROL_SERVICE_CONTROL_UUID, CONTROL_SERVICE_CONTROL_OPEN_VALUE, True)
self._sleep(CONTROL_SERVICE_CONTROL_WAIT_SEC)

# Change the state on Home Assistant
Expand All @@ -164,7 +171,10 @@ def close_cover(self, **kwargs) -> None:

# Control the device
_LOGGER.info('Moving the mornin to close the curtain %s...', self._mac_address)
self._mornin_device.char_write(CONTROL_SERVICE_CONTROL_UUID, CONTROL_SERVICE_CONTROL_CLOSE_VALUE, True)
if (self._reverse):
self._mornin_device.char_write(CONTROL_SERVICE_CONTROL_UUID, CONTROL_SERVICE_CONTROL_OPEN_VALUE, True)
else:
self._mornin_device.char_write(CONTROL_SERVICE_CONTROL_UUID, CONTROL_SERVICE_CONTROL_CLOSE_VALUE, True)
self._sleep(CONTROL_SERVICE_CONTROL_WAIT_SEC)

# Change the state on Home Assistant
Expand Down Expand Up @@ -253,7 +263,7 @@ def _get_encrypted_main_token(self) -> str:

_LOGGER.debug('Receiving status from mornin %s...', self._mac_address)
app_service_status = self._mornin_device.char_read(APP_SERVICE_STATUS_UUID, BLE_READ_TIMEOUT_SEC)
_LOGGER.debug('Status received from mornin %s: ', self._mac_address, app_service_status)
_LOGGER.debug('Status received from mornin %s: %s', self._mac_address, app_service_status)

_LOGGER.debug('Generating seed for %s... %s', self._mac_address)
seeds = self._get_seeds_by_app_service_status(app_service_status)
Expand Down

0 comments on commit 112c47f

Please sign in to comment.